-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
130 lines (103 loc) · 2.86 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//go:build mage
// +build mage
package main
import (
"context"
"fmt"
"os"
"os/exec"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// Default is the default build target.
var Default = Build
// All cleans output, builds, tests, and lints.
func All(ctx context.Context) error {
type target func(context.Context) error
targets := []target{
Clean,
Build,
Test,
Lint,
LintFix,
}
for _, t := range targets {
if err := t(ctx); err != nil {
return err
}
}
return nil
}
// Build builds the Mint-CLI
func Build(ctx context.Context) error {
args := []string{"./cmd/mint"}
ldflags, err := getLdflags()
if err != nil {
return err
}
args = append([]string{"-ldflags", ldflags}, args...)
if cgo_enabled := os.Getenv("CGO_ENABLED"); cgo_enabled == "0" {
args = append([]string{"-a"}, args...)
}
return sh.RunV("go", append([]string{"build"}, args...)...)
}
// Clean removes any generated artifacts from the repository.
func Clean(ctx context.Context) error {
return sh.Rm("./mint")
}
// Lint runs the linter & performs static-analysis checks.
func Lint(ctx context.Context) error {
return sh.RunV("golangci-lint", "run", "./...")
}
// Applies lint checks and fixes any issues.
func LintFix(ctx context.Context) error {
if err := sh.RunV("golangci-lint", "run", "--fix", "./..."); err != nil {
return err
}
if err := sh.RunV("go", "mod", "tidy"); err != nil {
return err
}
return nil
}
func UnitTest(ctx context.Context) error {
// `ginkgo ./...` or `go test ./...` work out of the box
// but `ginkgo ./...` includes ~ confusing empty test output for integration tests
// so `mage test` explicitly doesn't call ginkgo against the `/test/` directory
return (makeTestTask("./internal/...", "./cmd/..."))(ctx)
}
func IntegrationTest(ctx context.Context) error {
mg.Deps(Build)
return (makeTestTask("./test/..."))(ctx)
}
func Test(ctx context.Context) error {
mg.Deps(UnitTest)
mg.Deps(IntegrationTest)
return nil
}
func makeTestTask(args ...string) func(ctx context.Context) error {
return func(ctx context.Context) error {
ldflags, err := getLdflags()
if err != nil {
return err
}
args = append([]string{"-ldflags", ldflags}, args...)
if report := os.Getenv("REPORT"); report != "" {
return sh.RunV("ginkgo", append([]string{"-p", "--junit-report=report.xml"}, args...)...)
}
cmd := exec.Command("command", "-v", "ginkgo")
if err := cmd.Run(); err != nil {
return sh.RunV("go", append([]string{"test"}, args...)...)
}
return sh.RunV("ginkgo", append([]string{"-p"}, args...)...)
}
}
func getLdflags() (string, error) {
if ldflags := os.Getenv("LDFLAGS"); ldflags != "" {
return ldflags, nil
}
sha, err := exec.Command("git", "rev-parse", "HEAD").Output()
if err != nil {
return "", err
}
return fmt.Sprintf("-X github.com/rwx-research/mint-cli/cmd/mint/config.Version=git-%v", string(sha)), nil
}