-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
115 lines (95 loc) · 2.58 KB
/
magefile.go
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
// +build mage
// jsont: A high performance Go library to transform JSON data using simple and easily understandable instructions
// written in JSON.
package main
import (
"errors"
"fmt"
"os"
"runtime"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// Default target to run when none is specified
var Default = Build
var Aliases = map[string]interface{}{
"deps": InstallDeps,
}
var modulePath = "github.com/lucku/jsont/cmd"
var name = "jsont"
var ldflags = `-s -w -X "main.timestamp=$TIMESTAMP" -X "main.commitHash=$COMMIT_HASH" -X "main.version=$VERSION"`
// Builds the application
func Build() error {
mg.Deps(InstallDeps)
fmt.Println("Building...")
fileName := name
if runtime.GOOS == "windows" {
fileName += ".exe"
}
return sh.RunWith(envParams(), "go", "build", "-ldflags", ldflags, "-o", name, modulePath)
}
//Installs the application
func Install() error {
fmt.Println("Installing...")
return sh.RunWith(envParams(), "go", "install", "-ldflags", ldflags, modulePath)
}
// Installs all application's dependencies
func InstallDeps() error {
fmt.Println("Installing Dependencies...")
return sh.RunV("go", "mod", "download")
}
// Performs all tests on the application
func Test() error {
fmt.Println("Running tests...")
return sh.RunV("go", "test", "-race", "-v", "./...")
}
// Cleans up all build artifacts in the project root dir
func Clean() {
fmt.Println("Cleaning...")
os.RemoveAll(name)
}
// Releases the application using goreleaser
func Release() (err error) {
fmt.Println("Releasing...")
if os.Getenv("TAG") == "" {
return errors.New("TAG environment variable is required")
}
if err := sh.RunV("git", "tag", "-a", "$TAG"); 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")
}
// tag returns the git tag for the current branch or "" if none.
func tag() string {
tag, _ := sh.Output("git", "describe", "--tags")
return tag
}
// hash returns the git hash for the current repo or "" if none.
func hash() string {
hash, _ := sh.Output("git", "rev-parse", "HEAD")
return hash
}
// envVars returns key/values pairs of environment parameters for the build process
func envParams() map[string]string {
tag := tag()
if tag == "" {
tag = "dev"
}
timestamp := time.Now().Format(time.RFC3339)
return map[string]string{
"MODULE": modulePath,
"TIMESTAMP": timestamp,
"VERSION": tag,
"COMMIT_HASH": hash(),
}
}