forked from neilotoole/sq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagefile.go
More file actions
434 lines (356 loc) · 9.81 KB
/
magefile.go
File metadata and controls
434 lines (356 loc) · 9.81 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// +build mage
// Magefile for building/test sq. This magefile was originally copied from
// the Hugo magefile, and may contain functionality that can be ditched.
package main
// See https://magefile.org
// This file originally derived from the Hugo magefile, see https://github.com/gohugoio/hugo
import (
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const (
packageName = "github.com/neilotoole/sq"
)
var ldflags = "-X $PACKAGE/cli/buildinfo.Version=$BUILD_VERSION -X $PACKAGE/cli/buildinfo.Timestamp=$BUILD_TIMESTAMP -X $PACKAGE/cli/buildinfo.Commit=$BUILD_COMMIT"
// allow user to override go executable by running as GO=xxx mage ... on unix-like systems
var gocmd = "go"
func init() {
if exe := os.Getenv("GO"); exe != "" {
gocmd = exe
}
// We want to use Go 1.11 modules even if the source lives inside GOPATH.
// The default is "auto".
os.Setenv("GO111MODULE", "on")
}
var Default = Install
func ldflagsEnv() map[string]string {
hash := gitHeadCommit(true)
return map[string]string{
"PACKAGE": packageName,
"BUILD_COMMIT": hash,
"BUILD_TIMESTAMP": time.Now().Format("2006-01-02T15:04:05Z0700"),
"BUILD_VERSION": generateBuildVersion(),
"BUILD_BRANCH": gitCurrentBranch(),
}
}
// Clean cleans the dist dir, build dirs and sq binaries.
// We use `which` to iteratively find any binaries on the path
// named "sq" and delete them.
func Clean() error {
logIf(rmAll("./dist/*"))
logIf(rmAll("./grammar/build"))
// Delete the sq binary that "go build" might produce
logIf(sh.Rm("./sq"))
for {
// Find any other sq binaries
sqPath, err := sh.Output("which", "sq")
if err != nil || sqPath == "" {
break
}
// Make sure it's actually our sq binary. We'll do this
// by checking the version output.
version, err := sh.Output(sqPath, "version")
if err != nil || !strings.HasPrefix(version, "sq v") {
break
}
logIf(sh.Rm(sqPath))
}
return nil
}
// Build builds sq.
func Build() error {
mg.Deps(Vet)
return sh.RunWith(ldflagsEnv(), gocmd, "build", "-ldflags", ldflags, "-tags", buildTags(), packageName)
}
// BuildRace builds sq with race detector enabled
func BuildRace() error {
mg.Deps(Vet)
return sh.RunWith(ldflagsEnv(), gocmd, "build", "-race", "-ldflags", ldflags, "-tags", buildTags(), packageName)
}
// Install installs the sq binary.
func Install() error {
mg.Deps(Vet)
return sh.RunWith(ldflagsEnv(), gocmd, "install", "-ldflags", ldflags, "-tags", buildTags(), packageName)
}
// testGoFlags returns -test.short if isCI returns false.
func testGoFlags() string {
if isCI() {
return ""
}
return "-test.short"
}
// Test runs go test.
func Test() error {
mg.Deps(Vet)
env := map[string]string{"GOFLAGS": testGoFlags()}
return runCmd(env, gocmd, "test", "./...", "-tags", buildTags())
}
// TestAll runs go test including the "heavy" build tag.
func TestAll() error {
mg.Deps(Vet)
env := map[string]string{"GOFLAGS": testGoFlags()}
return runCmd(env, gocmd, "test", "./...", "-tags", "heavy")
}
// Lint runs the golangci-lint linters.
// The golangci-lint binary must be installed:
//
// $ brew install golangci-lint
//
// See .golangci.yml for configuration.
func Lint() error {
return sh.RunV("golangci-lint", "run", "./...")
}
// Vet runs go vet.
func Vet() error {
if err := sh.Run(gocmd, "vet", "./..."); err != nil {
fmt.Println("WARNING: issue reported by go vet:", err)
// return fmt.Errorf("error running go vet: %v", err)
}
return nil
}
// GenerateParser generates SLQ parser Go files from the
// antlr grammar. Note that the antlr generator tool is java-based.
func GenerateParser() error {
// https://www.antlr.org/download/antlr-4.8-complete.jar
err := ensureJava()
if err != nil {
log.Println("java is required by antlr to generate the Go parser files, but java may not be available:", err)
return err
}
jarPath, err := ensureAntlrJar()
if err != nil {
return err
}
genDir, err := execAntlrGenerate(jarPath)
if err != nil {
return err
}
log.Println("Antlr generated files to:", genDir)
err = updateParserFiles(genDir)
if err != nil {
return err
}
// Finally clean up the generated files
return rmAll(filepath.Join(genDir, "*"))
}
// updateParserFiles updates/copies files from genDir to
// the version-controlled slq dir.
func updateParserFiles(genDir string) error {
log.Println("Building generated parser files:", genDir)
err := sh.RunV("go", "build", "-v", genDir)
if err != nil {
return err
}
log.Println("Deleting previous generated files from libsq/ast/internal/slq")
err = rmAll("libsq/ast/internal/slq/*.go", "libsq/ast/internal/slq/*.token", "libsq/ast/internal/slq/*.interp")
if err != nil {
return err
}
log.Println("Copying generated files to libsq/ast/internal/slq")
err = copyFilesToDir("libsq/ast/internal/slq", filepath.Join(genDir, "*"))
if err != nil {
return err
}
return nil
}
// execAntlrGenerate executes the antlr tool from jarPath,
// generates Go parser files into genDir.
func execAntlrGenerate(jarPath string) (genDir string, err error) {
log.Println("Using antlr jar:", jarPath)
// The antlr tool is finicky about paths, so we're going
// to bypass this problem by using absolute paths.
grammarFile := filepath.Join("grammar", "SLQ.g4")
grammarFile, err = filepath.Abs(grammarFile)
if err != nil {
return "", err
}
genDir = filepath.Join("grammar", "build", "slq")
genDir, err = filepath.Abs(genDir)
if err != nil {
return "", err
}
// Make sure the output dir exists.
err = os.MkdirAll(genDir, 0700)
if err != nil {
return "", err
}
// Delete any existing files in output dir.
err = rmAll(filepath.Join(genDir, "*"))
if err != nil {
return "", err
}
err = sh.Run("java",
"-cp", jarPath, "org.antlr.v4.Tool",
"-listener", "-visitor",
"-package", "slq",
"-Dlanguage=Go",
"-o", genDir,
grammarFile,
)
if err != nil {
return "", err
}
return genDir, nil
}
// ensureJava ensures that java is available.
func ensureJava() error {
err := sh.Run("java", "-version")
if err != nil {
log.Println("Didn't find java executable")
return err
}
log.Println("Found java executable")
return nil
}
// ensureAntlrJar ensures that we have the antlr jar
// available, and returns the absolute path to the jar.
// The antlr jar can be found in grammar/build.
func ensureAntlrJar() (jarPath string, err error) {
const (
baseDownloadURL = "https://www.antlr.org/download/"
jarName = "antlr-4.7.2-complete.jar"
)
// Make sure our grammar/build dir exists
err = os.MkdirAll(filepath.Join("grammar", "build"), 0700)
if err != nil {
return "", err
}
path := filepath.Join("grammar", "build", jarName)
path, err = filepath.Abs(path)
panicIf(err)
_, err = os.Stat(path)
if err == nil {
log.Println("Found antlr jar:", path)
return path, nil
}
log.Println("Did not find antlr jar at:", path)
// Create the file
f, err := os.Create(path)
if err != nil {
return "", err
}
defer f.Close()
downloadURL := baseDownloadURL + jarName
log.Println("Downloading jar from:", downloadURL)
// Get the data
resp, err := http.Get(downloadURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
// Write the body to file
_, err = io.Copy(f, resp.Body)
if err != nil {
return "", err
}
log.Printf("Downloaded %d bytes to %s", resp.ContentLength, path)
return path, nil
}
func runCmd(env map[string]string, cmd string, args ...string) error {
if mg.Verbose() {
return sh.RunWith(env, cmd, args...)
}
output, err := sh.OutputWith(env, cmd, args...)
if err != nil {
fmt.Fprint(os.Stderr, output)
}
return err
}
func isGoLatest() bool {
return strings.Contains(runtime.Version(), "1.14")
}
func isCI() bool {
return os.Getenv("CI") != ""
}
func buildTags() string {
if envtags := os.Getenv("BUILD_TAGS"); envtags != "" {
return envtags
}
return "none"
}
func panicIf(err error) {
if err != nil {
panic(err)
}
}
func logIf(err error) {
if err != nil {
log.Println(err)
}
}
// rmAll deletes all files or dirs matching a pattern.
func rmAll(patterns ...string) error {
var allMatches []string
for _, pattern := range patterns {
matches, err := filepath.Glob(pattern)
if err != nil {
return err
}
allMatches = append(allMatches, matches...)
}
for _, match := range allMatches {
log.Printf("rmAll: %s", match)
err := sh.Rm(match)
if err != nil {
return err
}
}
return nil
}
// copyFilesToDir copies all files matching a pattern to dstDir.
// Because of flattening that occurs, if multiple files have the
// same name in dstDir, the later file will be be the last one
// copied and thus take effect. Directories are skipped.
func copyFilesToDir(dstDir string, patterns ...string) error {
var allMatches []string
for _, pattern := range patterns {
matches, err := filepath.Glob(pattern)
if err != nil {
return err
}
for _, fname := range matches {
fi, err := os.Stat(fname)
if err != nil {
log.Printf("copyFilesToDir: skipping %s because: %v", fname, err)
continue
}
if fi.IsDir() {
log.Printf("copyFilesToDir: skipping %s because it's a dir", fname)
continue
}
allMatches = append(allMatches, fname)
}
}
for _, match := range allMatches {
_, name := filepath.Split(match)
dstFile := filepath.Join(dstDir, name)
log.Printf("copyFilesToDir: %s --> %s\n", match, dstFile)
err := sh.Copy(dstFile, match)
if err != nil {
return err
}
}
return nil
}
// CheckDocker verifies that docker is running by executing echo
// on alpine.
func CheckDocker() error {
return execDocker("run", "-it", "alpine", "echo", "docker is working")
}
// execDocker executes a docker command with args, returning
// any error. Example:
//
// execDocker("run", "-it", "alpine", "echo", "hello world")
func execDocker(cmd string, args ...string) error {
args = append([]string{cmd}, args...)
return sh.RunV("docker", args...)
}