-
Notifications
You must be signed in to change notification settings - Fork 10
/
magefile.go
164 lines (130 loc) · 3.26 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//go:build mage
// Run "mage help" for prerequisites
package main
import (
"fmt"
"regexp"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"golang.org/x/tools/go/packages"
)
const (
ldflags = "-ldflags=-extldflags -static"
debugGCflags = "-gcflags='all=-N -l'"
)
type (
Test mg.Namespace
Build mg.Namespace
Lint mg.Namespace
IntTest mg.Namespace
)
var Aliases = map[string]interface{}{
"test": Test.Unit,
}
// Show dependencies, useful tips.
func Help() {
h := `Dependencies:
- golang: brew install golang
- golangci-lint: brew install golangci-lint
- go run mage.go tools
Add 'export MAGEFILE_ENABLE_COLOR=1' to env for colors
`
fmt.Println(h)
}
// Run unit tests.
func (Test) Unit() error {
return test()
}
// Run tests and create coverage profile.
func (Test) Coverage() error {
pkgs, err := getPackages()
if err != nil {
return err
}
err = test("-coverprofile=coverage", "-covermode=atomic", "-coverpkg", strings.Join(pkgs, ","))
if err != nil {
return err
}
return sh.RunV("bash", "-c", "go tool cover -func=coverage | tail -n1")
}
// Show test coverage in html (opens browser).
func (Test) CoverHTML() error {
mg.Deps(Test.Coverage)
return sh.RunV("go", "tool", "cover", "-html=coverage")
}
// Save test coverage as cobertura (coverage.xml).
func (Test) CoverXML() error {
mg.Deps(Test.Coverage)
return sh.RunV("bash", "-c", "gocover-cobertura < coverage > coverage.xml")
}
// Run golangci-lint with --fix.
func (Lint) GoFix() error {
return sh.RunV("golangci-lint", "run", "--timeout", "5m", "--fix")
}
// Run golangci-lint and output as code-climate: gl-code-quality-report.json.
func (Lint) Go() error {
return sh.RunV("bash", "-c", "golangci-lint run --timeout 5m --out-format code-climate > gl-code-quality-report.json")
}
// Remove all build, coverage and linting artifacts.
func Clean() error {
return sh.RunV(
"bash", "-c", "rm -f "+
"coverage "+
"coverage.xml "+
"gl-code-quality-report.json ",
)
}
// Update go dependencies.
func Update() error {
err := sh.RunV("go", "get", "-u", "-t", "./...")
if err != nil {
return err
}
return sh.RunV("go", "mod", "tidy", "-compat=1.19")
}
// Generate
func Generate() error {
return sh.RunV("go", "generate", "./...")
}
// Check if auto-generated code is up-2-date. Ignores comments starting with //..
func DiffGen() error {
mg.Deps(Generate)
return sh.RunV("git", "diff", "-G", "^[^/]", "--exit-code")
}
// Install all tools needed to work with this project.
func Tools() error {
tools := []string{
"github.com/boumenot/gocover-cobertura",
"github.com/magefile/mage/mage",
"go.uber.org/mock/mockgen",
"mvdan.cc/gofumpt",
}
for _, tool := range tools {
err := sh.RunV("go", "install", tool)
if err != nil {
return err
}
}
return nil
}
func getPackages() ([]string, error) {
pkgs, err := packages.Load(nil, "./...")
if err != nil {
return []string{}, err
}
var pkgPaths []string
prx := regexp.MustCompile("mage|mocks")
for _, pkg := range pkgs {
matched := prx.MatchString(pkg.PkgPath)
if !matched {
pkgPaths = append(pkgPaths, pkg.PkgPath)
}
}
return pkgPaths, nil
}
func test(args ...string) error {
a := []string{"test", "./...", "-test.short", "-race"}
a = append(a, args...)
return sh.RunV("go", a...)
}