1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
package main
import ( "errors" "fmt" "os" "os/exec" "time"
"github.com/magefile/mage/mg" "github.com/magefile/mage/sh" )
func init() { os.Setenv("MATTEL_REPO", "go-chilopod") os.Setenv("TAG", "v0.1.2") }
func Build_v1() error { mg.Deps(InstallDeps) fmt.Println("Building...") cmd := exec.Command("go", "build", "-o", "MyApp", ".") return cmd.Run() }
func Build() error { mg.Deps(Dep) return sh.RunV("go", "build", "-o", "$MATTEL_REPO", "-ldflags="+ldflags(), "github.com/riboseyim/$MATTEL_REPO") }
func Dep() error { fmt.Println("Installing Deps...") cmd := exec.Command("dep", "ensure") return cmd.Run() }
func Install() error { mg.Deps(Build) fmt.Println("Installing...") return os.Rename("./MyApp", "/usr/bin/MyApp") }
func InstallDeps() error { fmt.Println("Installing Deps...") cmd := exec.Command("go", "get", "github.com/stretchr/piglatin") return cmd.Run() }
func Clean() { fmt.Println("Cleaning...") os.RemoveAll("MyApp") }
func Tools() error {
update := envBool("UPDATE")
retool := "github.com/twitchtv/retool"
args := []string{"get", retool} if update { args = []string{"get", "-u", retool} }
if err := sh.Run("go", args...); err != nil { return err }
return sh.Run("retool", "sync") }
func retool(cmd string, args ...string) error { return sh.Run("retool", append([]string{"do", cmd}, args...)...) }
func Release() (err error) { if os.Getenv("TAG") == "" { return errors.New("TAG environment variable is required") } if err := sh.RunV("git", "tag", "-a", "$TAG", "-m", "add tag $TAG by mage tools"); err != nil { return err } if err := sh.RunV("git", "push", "origin", "$TAG"); err != nil { return err }
defer func() { if err != nil { sh.RunV("git", "tag", "--delete", "$TAG") sh.RunV("git", "push", "--delete", "origin", "$TAG") } }() return sh.RunV("goreleaser", "release", "--skip-publish", "--rm-dist") }
func ldflags() string { timestamp := time.Now().Format(time.RFC3339) hash := hash() tag := tag() if tag == "" { tag = "dev" }
return fmt.Sprintf(`-X "github.com/riboseyim/project/proj.timestamp=%s" `+ `-X "github.com/riboseyim/project/proj.commitHash=%s" `+ `-X "github.com/riboseyim/project/proj.gitTag=%s"`, timestamp, hash, tag) }
func tag() string { s, _ := sh.Output("git", "describe", "--tags") return s }
func hash() string { hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD") return hash }
func envBool(key string) bool { value := os.Getenv(key) if value != "" { return true } else { return false } }
|