// 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 . // +build mage package main import ( "bufio" "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{} ) 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) } } func init() { Tags = os.Getenv("TAGS") setVersion() setExecutable() setBinLocation() setPkgVersion() setApiPackages() setRootPath() 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() fmt.Printf("%s\n\n", c.String()) stdout, _ := c.StdoutPipe() 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("Error: %s\n", err) os.Exit(1) } } // Clean 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 } 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...) } func TestCoverage() { mg.Deps(Test) runAndStreamOutput("go", "tool", "cover", "-html=cover.out", "-o", "cover.html") } 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") } func Lint() { } func Fmt() { } func FmtCheck() { } // Generates static content into the final binary func Generate() error { return exec.Command("go", "generate", PACKAGE+"/pkg/static").Run() } // Builds a vikunja binary, ready to run. func Build() error { mg.Deps(Generate) cmd := exec.Command("go", "build", Goflags[0], "-tags", Tags, "-ldflags", "-s -w "+Ldflags, "-o", Executable) fmt.Println(cmd.String()) out, err := cmd.CombinedOutput() fmt.Print(string(out)) return err } func CompressBuild() { } func Release() { } func ReleaseDirs() { } func ReleaseWindows() { } func ReleaseLinux() { } func ReleaseDarwin() { } func ReleaseCompress() { } func ReleaseCopy() { } func ReleaseOsPackage() { } func ReleaseZip() { } func BuildDeb() { } func Reprepro() { } func GotSwag() { } func DoTheSwag() { } func MisspellCheck() { } func IneffassignCheck() { } func GocycloCheck() { } func StaticCheck() { } func GoSecCheck() { } func GoconstCheck() { }