349 lines
7.6 KiB
Go
349 lines
7.6 KiB
Go
// Vikunja is a to-do list application to facilitate your life.
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
// +build mage
|
|
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"fmt"
|
|
"github.com/magefile/mage/mg"
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
PACKAGE = `code.vikunja.io/api`
|
|
DIST = `dist`
|
|
)
|
|
|
|
var (
|
|
Goflags = []string{
|
|
"-v",
|
|
}
|
|
Executable = "vikunja"
|
|
Ldflags = ""
|
|
Tags = ""
|
|
VersionNumber = "dev"
|
|
Version = "master" // This holds the built version, master by default, when building from a tag or release branch, their name
|
|
BinLocation = ""
|
|
PkgVersion = "master"
|
|
ApiPackages = []string{}
|
|
RootPath = ""
|
|
GoFiles = []string{}
|
|
|
|
// Aliases are mage aliases of targets
|
|
Aliases = map[string]interface{}{
|
|
"integration-test": IntegrationTest,
|
|
"fmt-check": FmtCheck,
|
|
"do-the-swag": DoTheSwag,
|
|
"got-swag": GotSwag,
|
|
}
|
|
)
|
|
|
|
func setVersion() {
|
|
versionCmd := exec.Command("git", "describe", "--tags", "--always", "--abbrev=10")
|
|
version, err := versionCmd.Output()
|
|
if err != nil {
|
|
fmt.Printf("Error getting version: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
VersionNumber = strings.Trim(string(version), "\n")
|
|
VersionNumber = strings.Replace(VersionNumber, "-", "+", 1)
|
|
VersionNumber = strings.Replace(VersionNumber, "-g", "-", 1)
|
|
|
|
if os.Getenv("DRONE_TAG") != "" {
|
|
Version = os.Getenv("DRONE_TAG")
|
|
} else if os.Getenv("DRONE_BRANCH") != "" {
|
|
Version = strings.Replace(os.Getenv("DRONE_BRANCH"), "release/v", "", 1)
|
|
}
|
|
}
|
|
|
|
func setBinLocation() {
|
|
if os.Getenv("DRONE_WORKSPACE") != "" {
|
|
BinLocation = DIST + `/binaries/` + Executable + `-` + Version + `-linux-amd64`
|
|
} else {
|
|
BinLocation = Executable
|
|
}
|
|
}
|
|
|
|
func setPkgVersion() {
|
|
if Version == "master" {
|
|
PkgVersion = VersionNumber
|
|
}
|
|
}
|
|
|
|
func setExecutable() {
|
|
if runtime.GOOS == "windows" {
|
|
Executable += ".exe"
|
|
}
|
|
}
|
|
|
|
func setApiPackages() {
|
|
cmd := exec.Command("go", "list", "all")
|
|
pkgs, err := cmd.Output()
|
|
if err != nil {
|
|
fmt.Printf("Error getting packages: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
for _, p := range strings.Split(string(pkgs), "\n") {
|
|
if strings.Contains(p, "code.vikunja.io/api") && !strings.Contains(p, "code.vikunja.io/api/pkg/integrations") {
|
|
ApiPackages = append(ApiPackages, p)
|
|
}
|
|
}
|
|
}
|
|
|
|
func setRootPath() {
|
|
pwd, err := os.Getwd()
|
|
if err != nil {
|
|
fmt.Printf("Error getting pwd: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
if err := os.Setenv("VIKUNJA_SERVICE_ROOTPATH", pwd); err != nil {
|
|
fmt.Printf("Error setting root path: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
RootPath = pwd
|
|
}
|
|
|
|
func setGoFiles() {
|
|
// GOFILES := $(shell find . -name "*.go" -type f ! -path "*/bindata.go")
|
|
cmd := exec.Command("find", ".", "-name", "*.go", "-type", "f", "!", "-path", "*/bindata.go")
|
|
files, err := cmd.Output()
|
|
if err != nil {
|
|
fmt.Printf("Error getting go files: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
for _, f := range strings.Split(string(files), "\n") {
|
|
GoFiles = append(GoFiles, RootPath+strings.TrimLeft(f, "."))
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
Tags = os.Getenv("TAGS")
|
|
setVersion()
|
|
setExecutable()
|
|
setBinLocation()
|
|
setPkgVersion()
|
|
setApiPackages()
|
|
setRootPath()
|
|
setGoFiles()
|
|
Ldflags = `-X "` + PACKAGE + `/pkg/version.VersionNumber=` + VersionNumber + `" -X "main.Tags=` + Tags + `"`
|
|
}
|
|
|
|
func runAndStreamOutput(cmd string, args ...string) {
|
|
c := exec.Command(cmd, args...)
|
|
|
|
c.Env = os.Environ()
|
|
c.Dir = RootPath
|
|
|
|
fmt.Printf("%s\n\n", c.String())
|
|
|
|
stdout, _ := c.StdoutPipe()
|
|
errbuf := bytes.Buffer{}
|
|
c.Stderr = &errbuf
|
|
c.Start()
|
|
|
|
reader := bufio.NewReader(stdout)
|
|
line, err := reader.ReadString('\n')
|
|
for err == nil {
|
|
fmt.Print(line)
|
|
line, err = reader.ReadString('\n')
|
|
}
|
|
|
|
if err := c.Wait(); err != nil {
|
|
fmt.Printf(errbuf.String())
|
|
fmt.Printf("Error: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
// Will check if the tool exists and if not install it from the provided import path
|
|
// If any errors occur, it will exit with a status code of 1.
|
|
func checkAndInstallGoTool(tool, importPath string) {
|
|
if err := exec.Command(tool).Run(); err != nil {
|
|
fmt.Printf("%s not installed, installing %s...\n", tool, importPath)
|
|
if err := exec.Command("go", "install", Goflags[0], importPath).Run(); err != nil {
|
|
fmt.Printf("Error installing %s\n", tool)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("Installed.")
|
|
}
|
|
}
|
|
|
|
// Cleans all build, executable and bindata files.
|
|
func Clean() error {
|
|
if err := exec.Command("go", "clean", "./...").Run(); err != nil {
|
|
return err
|
|
}
|
|
if err := os.Remove(Executable); err != nil && !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
if err := os.RemoveAll(DIST); err != nil && !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
if err := os.RemoveAll(BinLocation); err != nil && !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Runs all tests except integration tests
|
|
func Test() {
|
|
// We run everything sequentially and not in parallel to prevent issues with real test databases
|
|
args := append([]string{"test", Goflags[0], "-p", "1"}, ApiPackages...)
|
|
runAndStreamOutput("go", args...)
|
|
}
|
|
|
|
// Runs the tests and builds the coverage html file from coverage output
|
|
func TestCoverage() {
|
|
mg.Deps(Test)
|
|
runAndStreamOutput("go", "tool", "cover", "-html=cover.out", "-o", "cover.html")
|
|
}
|
|
|
|
// Runs the integration tests
|
|
func IntegrationTest() {
|
|
// We run everything sequentially and not in parallel to prevent issues with real test databases
|
|
runAndStreamOutput("go", "test", Goflags[0], "-p", "1", PACKAGE+"/pkg/integrations")
|
|
}
|
|
|
|
// Formats the code using go fmt
|
|
func Fmt() {
|
|
args := append([]string{"-s", "-w"}, GoFiles...)
|
|
runAndStreamOutput("gofmt", args...)
|
|
}
|
|
|
|
// Checks if the code is properly formatted with go fmt
|
|
func FmtCheck() error {
|
|
args := append([]string{"-s", "-d"}, GoFiles...)
|
|
c := exec.Command("gofmt", args...)
|
|
out, err := c.Output()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(out) > 0 {
|
|
fmt.Println("Code is not properly gofmt'ed.")
|
|
fmt.Println("Please run 'mage fmt' and commit the result:")
|
|
fmt.Print(string(out))
|
|
os.Exit(1)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func Lint() {
|
|
checkAndInstallGoTool("golint", "golang.org/x/lint/golint")
|
|
args := append([]string{"-set_exit_status"}, ApiPackages...)
|
|
runAndStreamOutput("golint", args...)
|
|
}
|
|
|
|
func DoTheSwag() {
|
|
checkAndInstallGoTool("swag", "github.com/swaggo/swag/cmd/swag")
|
|
runAndStreamOutput("swag", "init", "-g", "./pkg/routes/routes.go", "--parseDependency", "-d", RootPath, "-o", RootPath+"/pkg/swagger")
|
|
}
|
|
|
|
func GotSwag() {
|
|
|
|
}
|
|
|
|
func MisspellCheck() {
|
|
|
|
}
|
|
|
|
func IneffassignCheck() {
|
|
|
|
}
|
|
|
|
func GocycloCheck() {
|
|
|
|
}
|
|
|
|
func StaticCheck() {
|
|
|
|
}
|
|
|
|
func GoSecCheck() {
|
|
|
|
}
|
|
|
|
func GoconstCheck() {
|
|
|
|
}
|
|
|
|
// Generates static content into the final binary
|
|
func Generate() {
|
|
runAndStreamOutput("go", "generate", PACKAGE+"/pkg/static")
|
|
}
|
|
|
|
// Builds a vikunja binary, ready to run.
|
|
func Build() {
|
|
mg.Deps(Generate)
|
|
runAndStreamOutput("go", "build", Goflags[0], "-tags", Tags, "-ldflags", "-s -w "+Ldflags, "-o", Executable)
|
|
}
|
|
|
|
func CompressBuild() {
|
|
|
|
}
|
|
|
|
func Release() {
|
|
|
|
}
|
|
|
|
func ReleaseDirs() {
|
|
|
|
}
|
|
|
|
func ReleaseWindows() {
|
|
|
|
}
|
|
|
|
func ReleaseLinux() {
|
|
|
|
}
|
|
|
|
func ReleaseDarwin() {
|
|
|
|
}
|
|
|
|
func ReleaseCompress() {
|
|
|
|
}
|
|
|
|
func ReleaseCopy() {
|
|
|
|
}
|
|
|
|
func ReleaseOsPackage() {
|
|
|
|
}
|
|
|
|
func ReleaseZip() {
|
|
|
|
}
|
|
|
|
func BuildDeb() {
|
|
|
|
}
|
|
|
|
func Reprepro() {
|
|
|
|
}
|