-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
magefile.go
157 lines (137 loc) · 3.2 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
//go:build mage
// +build mage
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"time"
"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
)
var (
// Default target to run when none is specified
// If not set, running mage will list available targets
Default = Build
app string = "tar-split"
Stdout = ourStdout
Stderr = ourStderr
golangcilintVersion = "v1.61.0"
cleanFiles = []string{}
)
// Run all-the-things
func All() error {
mg.Deps(Vet)
mg.Deps(Test)
mg.Deps(Build)
mg.Deps(Lint)
return nil
}
// A build step that requires additional params, or platform specific steps for example
func Build() error {
mg.Deps(InstallDeps)
fmt.Println("Building...")
cmd := exec.Command("go", "build", "-v", "-o", app, "./cmd/tar-split")
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
}
// Vet the codes
func Vet() error {
fmt.Println("go vet...")
cmd := exec.Command("go", "vet", "./...")
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
}
// Run the Linters
func Lint() error {
mg.Deps(InstallToolsLint)
fmt.Println("Linting...")
cmd := exec.Command("golangci-lint", "run")
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
}
// Run the tests available
func Test() error {
fmt.Println("Testing...")
cmd := exec.Command("go", "test", "-cover", "-v", "-bench", "'.'", "-benchmem", "./...")
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
}
// A custom install step if you need your bin someplace other than go/bin
func Install() error {
mg.Deps(Build)
fmt.Println("Installing...")
return os.Rename(app, "/usr/local/bin/"+app)
}
func init() {
cleanFiles = append(cleanFiles, ".install.deps") // sloppy
}
// Manage your deps, or running package managers.
func InstallDeps() error {
const fpath = ".install.deps"
success := false
defer func() {
if success {
fd, err := os.Create(fpath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
fd.Close()
}
}()
if IsFresh(fpath, time.Now()) {
return nil
}
mg.Deps(Tidy)
fmt.Println("Installing Deps...")
cmd := exec.Command("go", "get", "./...")
cmd.Stdout = Stdout
cmd.Stderr = Stderr
err := cmd.Run()
if err != nil {
return err
}
success = true
return nil
}
// Tools used during build/dev/test
func InstallTools() error {
mg.Deps(InstallToolsLint)
return nil
}
func InstallToolsLint() error {
fmt.Println("Installing Deps...")
cmd := exec.Command("go", "install", "github.com/golangci/golangci-lint/cmd/golangci-lint@"+golangcilintVersion)
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
}
// Tidy go modules
func Tidy() error {
fmt.Println("Tidy up...")
cmd := exec.Command("go", "mod", "tidy")
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
}
// Clean up after yourself
func Clean() {
fmt.Println("Cleaning...")
os.RemoveAll(app)
for _, fpath := range cleanFiles {
os.RemoveAll(fpath)
}
}
// IsFresh checks if `fpath` exists (therefore `false`, it is not fresh) or if
// `fpath` is _newer_ than `t` (true, as in it's freshly built)
func IsFresh(fpath string, t time.Time) bool {
fi, err := os.Stat(fpath)
if err != nil && errors.Is(err, os.ErrNotExist) {
return false
}
return fi.ModTime().Before(t)
}