-
Notifications
You must be signed in to change notification settings - Fork 1
/
magefile.go
216 lines (190 loc) · 4.72 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
//go:build mage
package main
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"regexp"
"strings"
"syscall"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"golang.org/x/crypto/ssh/terminal"
)
const (
Go = "go"
)
// Build all mobile targets.
func Build() {
fmt.Println("Building...")
IOS()
Android()
}
// Clean deletes any build artifacts.
func Clean() {
fmt.Println("Cleaning...")
os.RemoveAll("bin")
}
// Test runs unit tests without coverage.
// The mage `-v` option will trigger a verbose output of the test
func Test() error {
return runTests()
}
func runTests(extraTestArgs ...string) error {
args := []string{"test"}
if mg.Verbose() {
args = append(args, "-v")
}
args = append(args, "-race", "-tags=jwx_es256k")
args = append(args, extraTestArgs...)
args = append(args, "./...")
testEnv := map[string]string{
"CGO_ENABLED": "1",
"GO111MODULE": "on",
}
writer := ColorizeTestStdout()
fmt.Printf("%+v", args)
_, err := sh.Exec(testEnv, writer, os.Stderr, Go, args...)
return err
}
func ColorizeTestOutput(w io.Writer) io.Writer {
writer := NewRegexpWriter(w, `PASS.*`, "\033[32m$0\033[0m")
return NewRegexpWriter(writer, `FAIL.*`, "\033[31m$0\033[0m")
}
func ColorizeTestStdout() io.Writer {
if terminal.IsTerminal(syscall.Stdout) {
return ColorizeTestOutput(os.Stdout)
}
return os.Stdout
}
type regexpWriter struct {
inner io.Writer
re *regexp.Regexp
repl []byte
}
func NewRegexpWriter(inner io.Writer, re string, repl string) io.Writer {
return ®expWriter{inner, regexp.MustCompile(re), []byte(repl)}
}
func (w *regexpWriter) Write(p []byte) (int, error) {
r := w.re.ReplaceAll(p, w.repl)
n, err := w.inner.Write(r)
if n > len(r) {
n = len(r)
}
return n, err
}
func runGo(cmd string, args ...string) error {
return sh.Run(findOnPathOrGoPath("go"), append([]string{"run", cmd}, args...)...)
}
// InstallIfNotPresent installs a go based tool (if not already installed)
func installIfNotPresent(execName, goPackage string) error {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
return err
}
pathOfExec := findOnPathOrGoPath(execName)
if len(pathOfExec) == 0 {
cmd := exec.Command(Go, "get", "-u", goPackage)
cmd.Dir = usr.HomeDir
if err := cmd.Start(); err != nil {
return err
}
return cmd.Wait()
}
return nil
}
func findOnPathOrGoPath(execName string) string {
if p := findOnPath(execName); p != "" {
return p
}
p := filepath.Join(goPath(), "bin", execName)
if _, err := os.Stat(p); err == nil {
return p
}
fmt.Printf("Could not find %s on PATH or in GOPATH/bin\n", execName)
return ""
}
func findOnPath(execName string) string {
pathEnv := os.Getenv("PATH")
pathDirectories := strings.Split(pathEnv, string(os.PathListSeparator))
for _, pathDirectory := range pathDirectories {
possible := filepath.Join(pathDirectory, execName)
stat, err := os.Stat(possible)
if err == nil || os.IsExist(err) {
if (stat.Mode() & 0111) != 0 {
return possible
}
}
}
return ""
}
func goPath() string {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
return ""
}
goPath, goPathSet := os.LookupEnv("GOPATH")
if !goPathSet {
goPath = filepath.Join(usr.HomeDir, Go)
}
return goPath
}
// CBT runs clean; build; test
func CBT() error {
Clean()
Build()
if err := Test(); err != nil {
return err
}
return nil
}
// CITest runs unit tests with coverage as a part of CI.
// The mage `-v` option will trigger a verbose output of the test
func CITest() error {
return runCITests()
}
func runCITests(extraTestArgs ...string) error {
args := []string{"test"}
if mg.Verbose() {
args = append(args, "-v")
}
args = append(args, "-tags=jwx_es256k")
args = append(args, "-covermode=atomic")
args = append(args, "-coverprofile=coverage.out")
args = append(args, extraTestArgs...)
args = append(args, "./...")
testEnv := map[string]string{
"CGO_ENABLED": "1",
"GO111MODULE": "on",
}
writer := ColorizeTestStdout()
fmt.Printf("%+v", args)
_, err := sh.Exec(testEnv, writer, os.Stderr, Go, args...)
return err
}
func installGoMobileIfNotPresent() error {
return installIfNotPresent("gomobile", "golang.org/x/mobile/cmd/gomobile@latest")
}
// Generates the iOS packages
// Note: this command also installs "gomobile" if not present
func IOS() {
installGoMobileIfNotPresent()
fmt.Println("Building iOS...")
bindIOs := sh.RunCmd("gomobile", "bind", "-target", "ios")
bindIOs("mobile")
}
// Generates the Android packages
// Note: this command also installs "gomobile" if not present
func Android() {
installGoMobileIfNotPresent()
apiLevel := "23"
fmt.Println("Building Android - Api Level: " + apiLevel + "...")
bindAndroid := sh.RunCmd("gomobile", "bind", "-target", "android", "-androidapi", "23")
bindAndroid("./mobile")
}