-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
331 lines (288 loc) · 7.5 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//go:build mage
// +build mage
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const (
packageName = "github.com/peaceiris/tss"
)
var ldflags = `-X $PACKAGE/cmd.buildVersion=$VERSION -X $PACKAGE/cmd.buildCommit=$COMMIT_HASH -X $PACKAGE/cmd.buildDate=$BUILD_DATE`
// allow user to override go executable by running as GOEXE=xxx make ... on unix-like systems
var goexe = "go"
func init() {
if exe := os.Getenv("GOEXE"); exe != "" {
goexe = exe
}
// We want to use Go 1.11 modules even if the source lives inside GOPATH.
// The default is "auto".
os.Setenv("GO111MODULE", "on")
}
func runWith(env map[string]string, cmd string, inArgs ...interface{}) error {
s := argsToStrings(inArgs...)
return sh.RunWith(env, cmd, s...)
}
// Install development tools
func Setup() error {
if err := sh.Run(goexe, "install", "golang.org/x/lint/golint"); err != nil {
return err
}
return nil
}
// Build binary
func Build() error {
return runWith(flagEnv(), goexe, "build", "-ldflags", ldflags, buildFlags(), packageName)
}
// Build binary with race detector enabled
func BuildRace() error {
return runWith(flagEnv(), goexe, "build", "-race", "-ldflags", ldflags, buildFlags(), packageName)
}
// Install binary
func Install() error {
return runWith(flagEnv(), goexe, "install", "-ldflags", ldflags, buildFlags(), packageName)
}
// Uninstall binary
func Uninstall() error {
return sh.Run(goexe, "clean", "-i", packageName)
}
func flagEnv() map[string]string {
hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
version, _ := sh.Output("git", "describe", "--tags")
return map[string]string{
"VERSION": version,
"PACKAGE": packageName,
"COMMIT_HASH": hash,
"BUILD_DATE": time.Now().Format("2006-01-02T15:04:05Z0700"),
}
}
// Run tests and linters
func Check() {
mg.Deps(Fmt, Vet)
// don't run two tests in parallel, they saturate the CPUs anyway, and running two
// causes memory issues in CI.
mg.Deps(TestRace)
}
func testGoFlags() string {
if isCI() {
return ""
}
return "-test.short"
}
// Run tests
func Test() error {
env := map[string]string{"GOFLAGS": testGoFlags()}
return runCmd(env, goexe, "test", "-shuffle", "on", "-coverpkg", "./...", "-covermode", "atomic", "-coverprofile", "coverage.txt", "./...", buildFlags())
}
// Run tests with race detector
func TestRace() error {
env := map[string]string{"GOFLAGS": testGoFlags()}
return runCmd(env, goexe, "test", "-shuffle", "on", "-race", "-coverpkg", "./...", "-covermode", "atomic", "-coverprofile", "coverage.txt", "./...", buildFlags())
}
// Run gofmt -w
func Fmtw() error {
if !isGoLatest() {
return nil
}
pkgs, err := tssPackages()
if err != nil {
return err
}
for _, pkg := range pkgs {
files, err := filepath.Glob(filepath.Join(pkg, "*.go"))
if err != nil {
return nil
}
for _, f := range files {
_, err := sh.Output("gofmt", "-w", f)
if err != nil {
fmt.Printf("ERROR: running gofmt on %q: %v\n", f, err)
}
}
}
return nil
}
// Run gofmt linter
func Fmt() error {
if !isGoLatest() {
return nil
}
pkgs, err := tssPackages()
if err != nil {
return err
}
failed := false
first := true
for _, pkg := range pkgs {
files, err := filepath.Glob(filepath.Join(pkg, "*.go"))
if err != nil {
return nil
}
for _, f := range files {
// gofmt doesn't exit with non-zero when it finds unformatted code
// so we have to explicitly look for output, and if we find any, we
// should fail this target.
s, err := sh.Output("gofmt", "-l", f)
if err != nil {
fmt.Printf("ERROR: running gofmt on %q: %v\n", f, err)
failed = true
}
if s != "" {
if first {
fmt.Println("The following files are not gofmt'ed:")
first = false
}
failed = true
fmt.Println(f)
}
}
}
if failed {
return errors.New("improperly formatted go files")
}
return nil
}
var (
pkgPrefixLen = len("github.com/peaceiris/tss")
pkgs []string
pkgsInit sync.Once
)
func tssPackages() ([]string, error) {
var err error
pkgsInit.Do(func() {
var s string
s, err = sh.Output(goexe, "list", "./...")
if err != nil {
return
}
pkgs = strings.Split(s, "\n")
for i := range pkgs {
pkgs[i] = "." + pkgs[i][pkgPrefixLen:]
}
})
return pkgs, err
}
// Run golint linter
func Lint() error {
pkgs, err := tssPackages()
if err != nil {
return err
}
failed := false
for _, pkg := range pkgs {
// We don't actually want to fail this target if we find golint errors,
// so we don't pass -set_exit_status, but we still print out any failures.
if _, err := sh.Exec(nil, os.Stderr, nil, "golint", pkg); err != nil {
fmt.Printf("ERROR: running go lint on %q: %v\n", pkg, err)
failed = true
}
}
if failed {
return errors.New("errors running golint")
}
return nil
}
// Run go vet linter
func Vet() error {
if err := sh.Run(goexe, "vet", "./..."); err != nil {
return fmt.Errorf("error running go vet: %v", err)
}
return nil
}
func runCmd(env map[string]string, cmd string, args ...interface{}) error {
if mg.Verbose() {
return runWith(env, cmd, args...)
}
output, err := sh.OutputWith(env, cmd, argsToStrings(args...)...)
if err != nil {
fmt.Fprint(os.Stderr, output)
}
return err
}
func isGoLatest() bool {
return strings.Contains(runtime.Version(), "1.17")
}
func isCI() bool {
return os.Getenv("CI") != ""
}
func buildFlags() []string {
if runtime.GOOS == "windows" {
return []string{"-buildmode", "exe"}
}
return nil
}
func argsToStrings(v ...interface{}) []string {
var args []string
for _, arg := range v {
switch v := arg.(type) {
case string:
if v != "" {
args = append(args, v)
}
case []string:
if v != nil {
args = append(args, v...)
}
default:
panic("invalid type")
}
}
return args
}
func flagEnvNpm() map[string]string {
return map[string]string{
"npm_config_yes": "true",
}
}
func BumpVersion(releaseType string) error {
fmt.Printf("\nnpm_config_yes=true npx standard-version --release-as %s --preset eslint\n", releaseType)
out, err := sh.OutputWith(flagEnvNpm(), "npx", "standard-version", "--release-as", releaseType, "--preset", "eslint")
fmt.Println(out)
if err != nil {
return err
}
if err := sh.Run("git", "push", "origin", "main"); err != nil {
return err
}
if err := sh.Run("git", "push", "origin", "--tags"); err != nil {
return err
}
return nil
}
// Run npm_config_yes=true npx standard-version --release-as patch --preset eslint
func BumpPatchVersion() error {
return BumpVersion("patch")
}
// Run npm_config_yes=true npx standard-version --release-as minor --preset eslint
func BumpMinorVersion() error {
return BumpVersion("minor")
}
// Run npm_config_yes=true npx standard-version --release-as patch --preset eslint --dry-run
func BumpVersionTest() error {
fmt.Printf("\nnpx standard-version --release-as patch --preset eslint --dry-run\n")
out, err := sh.OutputWith(flagEnvNpm(), "npx", "standard-version", "--release-as", "patch", "--preset", "eslint", "--dry-run")
fmt.Println(out)
return err
}
// Run goreleaser check
func GoreleaserCheck() error {
fmt.Printf("\ngoreleaser check\n")
return sh.Run("goreleaser", "check")
}
// Run goreleaser --snapshot --skip-publish --rm-dist
func GoreleaserTest() error {
fmt.Printf("\ngoreleaser --snapshot --skip-publish --rm-dist\n")
return sh.Run("goreleaser", "--snapshot", "--skip-publish", "--rm-dist")
}
// Run GoreleaserCheck, GoreleaserTest, BumpVersionTest
func ReleaseTest() {
mg.SerialDeps(GoreleaserCheck, GoreleaserTest, BumpVersionTest)
}