-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagefile.go
338 lines (279 loc) · 8.7 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
332
333
334
335
336
337
338
//go:build mage
package main
import (
"fmt"
"io/fs"
"os"
"path"
"regexp"
"strconv"
"strings"
"github.com/hmerritt/reactenv/version"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
type Build mg.Namespace
type Npm mg.Namespace
var Aliases = map[string]interface{}{
"build": Build.Release,
}
// ----------------------------------------------------------------------------
// Npm packages - run (yarn) commands within each npm directory
// ----------------------------------------------------------------------------
func (Npm) Webpack(target string) error {
log := NewLogger()
currentDir, _ := os.Getwd()
cliDir := path.Join(currentDir, "npm", "plugin-webpack")
if err := RunStream([]string{"yarn", target}, cliDir, true); err != nil {
return log.Error(err)
}
log.End()
return nil
}
// ----------------------------------------------------------------------------
// Test
// ----------------------------------------------------------------------------
// Runs Go tests
func Test() error {
log := NewLogger()
defer log.End()
return RunSync([][]string{
{"gotestsum", "--format", "pkgname", "--", "--cover", "./..."},
})
}
func Bench() error {
log := NewLogger()
defer log.End()
return RunSync([][]string{
{"gotestsum", "--format", "pkgname", "--", "--cover", "-bench", ".", "-benchmem", "./..."},
})
}
// ----------------------------------------------------------------------------
// Build
// ----------------------------------------------------------------------------
func (Build) Clean() error {
log := NewLogger()
defer log.End()
log.Info("cleaning bin directory")
if err := os.RemoveAll("bin"); err != nil {
return log.Error(err)
}
return fs.WalkDir(os.DirFS("npm"), ".", func(filePath string, d fs.DirEntry, err error) error {
if d.IsDir() ||
filePath == "reactenv/bin/reactenv" ||
!strings.HasPrefix(filePath, "reactenv") ||
!strings.HasPrefix(d.Name(), "reactenv") {
return nil
}
return os.Remove(path.Join("npm", filePath))
})
}
func (Build) Debug() error {
log := NewLogger()
defer log.End()
log.Info("compiling debug binary")
return RunSync([][]string{
{"go", "build", "-ldflags", "-s -w", "."},
})
}
func (Build) Release() error {
mg.Deps(Build.Clean, BumpVersion)
log := NewLogger()
defer log.End()
log.Info("compiling release binary")
return RunSync([][]string{
{"gox",
"-osarch",
"darwin/amd64 darwin/arm64 freebsd/amd64 freebsd/arm linux/amd64 linux/arm64 netbsd/amd64 netbsd/arm openbsd/amd64 windows/amd64",
"-gocmd",
"go",
"-ldflags",
LdFlagString(),
"-tags",
"reactenv",
"-output",
"bin/{{.OS}}_{{.Arch}}/reactenv",
"."},
})
}
// ----------------------------------------------------------------------------
// Release
// ----------------------------------------------------------------------------
func Release() error {
log := NewLogger()
defer log.End()
releaseVersion := GetEnv("RELEASE_VERSION", version.Version)
log.Info("release version: ", releaseVersion)
releaseArchs := []string{
"darwin_amd64",
"darwin_arm64",
"freebsd_amd64",
"freebsd_arm",
"linux_amd64",
"linux_arm64",
"netbsd_amd64",
"netbsd_arm",
"openbsd_amd64",
"windows_amd64"}
releaseArchsNpmDirectories := []string{
"reactenv-darwin-x64",
"reactenv-darwin-arm64",
"",
"",
"reactenv-linux-x64",
"reactenv-linux-arm64",
"",
"",
"",
"reactenv-win32-x64"}
// Zip
for archIndex, arch := range releaseArchs {
log.Info(fmt.Sprintf("%13s", arch))
archLog := fmt.Sprintf("%13s |", arch)
binDirPath := fmt.Sprintf("bin/%s", arch)
binFilePath := ""
// Search each binary path for the release binary,
// ensure binary file exists and check it's file size.
log.Debug(archLog, "checking release binary")
binPathfiles, err := os.ReadDir(binDirPath)
if err != nil {
return log.Error(archLog, "error reading directory:", err)
}
for _, file := range binPathfiles {
if file.IsDir() {
continue
}
if strings.Contains(file.Name(), "reactenv") {
fileInfo, err := file.Info()
if err != nil {
return log.Error(archLog, "error getting file info:", err)
}
if fileInfo.Size() < 1000000 {
return log.Error(archLog, "release binary is too small:", err)
}
binFilePath = fmt.Sprintf("%s/%s", binDirPath, file.Name())
break
}
}
if binFilePath == "" {
return log.Error(archLog, "failed to find release binary", arch)
}
log.Debug(archLog, "zip for release")
zipFileName := fmt.Sprintf("reactenv_%s_%s.zip", releaseVersion, arch)
zipFilePath := fmt.Sprintf("bin/%s", zipFileName)
err = ZipFiles(zipFilePath, binFilePath)
if err != nil {
return log.Error(archLog, "failed to zip binary", err)
}
// Copy binary into npm directory
if releaseArchsNpmDirectories[archIndex] != "" {
log.Debug(archLog, "copy binary into npm directory")
CopyFile(binFilePath, path.Join("npm", releaseArchsNpmDirectories[archIndex], path.Base(binFilePath)))
}
}
return nil
}
// ----------------------------------------------------------------------------
// Housekeeping
// ----------------------------------------------------------------------------
// Bootstraps required packages (installs required linux/macOS packages if needed)
func Bootstrap() error {
log := NewLogger()
defer log.End()
// Install mage bootstrap (the recommended, as seen in https://magefile.org)
if !ExecExists("mage") && ExecExists("git") {
log.Info("installing mage")
tmpDir := "__tmp_mage"
if err := sh.Run("git", "clone", "https://github.com/magefile/mage", tmpDir); err != nil {
return log.Error("error: installing mage: ", err)
}
if err := os.Chdir(tmpDir); err != nil {
return log.Error("error: installing mage: ", err)
}
if err := sh.Run("go", "run", "bootstrap.go"); err != nil {
return log.Error("error: installing mage: ", err)
}
if err := os.Chdir("../"); err != nil {
return log.Error("error: installing mage: ", err)
}
os.RemoveAll(tmpDir)
}
// Install Go dependencies
log.Info("installing go dependencies")
return RunSync([][]string{
{"go", "mod", "vendor"},
{"go", "mod", "tidy"},
{"go", "generate", "-tags", "tools", "tools/tools.go"},
})
}
// Update all Go dependencies
func UpdateDeps() error {
log := NewLogger()
defer log.End()
return RunSync([][]string{
{"go", "get", "-u", "all"},
{"go", "mod", "vendor"},
{"go", "mod", "tidy"},
})
}
// Bumps (syncs) patch version to the commit count (see `version/version_base.go`)
func BumpVersion() error {
log := NewLogger()
defer log.End()
// Get the total commit count
commitCountString, err := sh.Output("git", "rev-list", "--count", "HEAD")
if err != nil {
return log.Error("failed to get commit count:", err)
}
commitCount, err := strconv.Atoi(commitCountString)
if err != nil {
return log.Error("failed to parse commit count:", commitCountString, err)
}
filesWithVersion := []string{
"version/version_base.go",
"npm/reactenv/package.json",
"npm/reactenv-darwin-arm64/package.json",
"npm/reactenv-darwin-x64/package.json",
"npm/reactenv-linux-arm64/package.json",
"npm/reactenv-linux-x64/package.json",
"npm/reactenv-win32-x64/package.json",
}
for index, versionFile := range filesWithVersion {
versionFileContent, err := os.ReadFile(versionFile)
if err != nil {
return log.Error("failed open version file:", err)
}
// Change regex based on file type
var versionMatchRegex, versionReplaceRegex, versionReplaceSprintf string
switch path.Ext(versionFile) {
case ".go":
versionMatchRegex = `= "(\d+).(\d+).(\d+)"`
versionReplaceRegex = `= "\d+.\d+.\d+"`
versionReplaceSprintf = `= "%s.%s.%d"`
case ".json":
versionMatchRegex = `"version": "(\d+).(\d+).(\d+)"`
versionReplaceRegex = `"version": "\d+.\d+.\d+"`
versionReplaceSprintf = `"version": "%s.%s.%d"`
}
// Extract current patch version
versionMatch := regexp.MustCompile(versionMatchRegex).FindStringSubmatch(string(versionFileContent))
if len(versionMatch) < 4 {
return log.Error("failed to parse version:", versionFile, versionMatch)
}
majorVersion := versionMatch[1]
minorVersion := versionMatch[2]
patchVersionCurrent := versionMatch[3]
patchVersion := commitCount
versionCurrent := fmt.Sprintf("%s.%s.%s", majorVersion, minorVersion, patchVersionCurrent)
versionNew := fmt.Sprintf("%s.%s.%d", majorVersion, minorVersion, patchVersion)
if index == 0 {
log.Info("bumping version", versionCurrent, "->", versionNew)
}
// Update version file
versionFileContent = regexp.MustCompile(versionReplaceRegex).ReplaceAll(versionFileContent, []byte(fmt.Sprintf(versionReplaceSprintf, majorVersion, minorVersion, patchVersion)))
if err := os.WriteFile(versionFile, versionFileContent, 0644); err != nil {
return log.Error("failed to write version file:", err)
}
}
return nil
}