-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
231 lines (207 loc) · 5.4 KB
/
build.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
package main
import (
"context"
"crypto/sha1"
"encoding/hex"
"flag"
"io"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"sync"
"time"
"github.com/pkg/errors"
"github.com/thijzert/go-resemble"
)
type job func(ctx context.Context) error
func main() {
if _, err := os.Stat("qm9k/assets"); err != nil {
log.Fatalf("Error: cannot find quizmeister9000 assets directory. (error: %s)\nAre you running this from the repository root?", err)
}
devBuild := false
quickBuild := false
watch := false
run := false
flag.BoolVar(&devBuild, "development", false, "Create a development build")
flag.BoolVar(&quickBuild, "quick", false, "Create a development build")
flag.BoolVar(&watch, "watch", false, "Watch source tree for changes")
flag.BoolVar(&run, "run", false, "Run quizmeister9000 upon successful compilation")
flag.Parse()
if devBuild && quickBuild {
log.Printf("")
log.Printf("You requested a quick build. This will assume")
log.Printf(" you have a version of `gulp watch` running")
log.Printf(" in a separate process.")
log.Printf("")
}
var theJob job
if run {
theJob = func(ctx context.Context) error {
err := compile(ctx, devBuild, quickBuild)
if err != nil {
return err
}
runArgs := append([]string{"./quizmeister9000"}, flag.Args()...)
return passthru(ctx, runArgs...)
}
} else {
theJob = func(ctx context.Context) error {
return compile(ctx, devBuild, quickBuild)
}
}
if watch {
theJob = watchSourceTree([]string{"."}, []string{"*.go"}, theJob)
}
err := theJob(context.Background())
if err != nil {
log.Fatal(err)
}
}
func compile(ctx context.Context, devBuild, quickBuild bool) error {
// Check for local gulp
fi, err := os.Stat("node_modules/.bin/gulp")
if (!devBuild && !quickBuild) || err != nil || fi.Mode()&0x1 != 1 {
err = passthru(ctx, "npm", "install")
if err != nil {
return errors.WithMessage(err, "error installing local gulp")
}
}
// Compile static assets
production := "--production"
if devBuild {
production = "--development"
}
if !devBuild || !quickBuild {
err = passthru(ctx, "node_modules/.bin/gulp", "compile", production)
if err != nil {
return errors.WithMessage(err, "error compiling assets")
}
}
// Embed static assets
if err := os.Chdir("qm9k/assets"); err != nil {
return errors.Errorf("Error: cannot find quizmeister9000 assets directory. (error: %s)\nAre you *sure* you're running this from the repository root?", err)
}
var emb resemble.Resemble
emb.OutputFile = "../assets.go"
emb.PackageName = "qm9k"
emb.Debug = devBuild
emb.AssetPaths = []string{
".",
}
if err := emb.Run(); err != nil {
return errors.WithMessage(err, "error running 'resemble'")
}
os.Chdir("../..")
// Build main executable
gofiles, err := filepath.Glob("cmd/quizmeister9000/*.go")
if err != nil || gofiles == nil {
return errors.WithMessage(err, "error: cannot find any go files to compile.")
}
compileArgs := append([]string{
"go", "build", "-o", "quizmeister9000",
}, gofiles...)
err = passthru(ctx, compileArgs...)
if err != nil {
return errors.WithMessage(err, "compilation failed")
}
if devBuild && !quickBuild {
log.Printf("")
log.Printf("Development build finished. For best results,")
log.Printf(" run `node_modules/.bin/gulp watch` in a")
log.Printf(" separate process.")
log.Printf("")
} else {
log.Printf("Compilation finished.")
}
return nil
}
func passthru(ctx context.Context, argv ...string) error {
c := exec.CommandContext(ctx, argv[0], argv[1:]...)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Stdin = os.Stdin
return c.Run()
}
func watchSourceTree(paths []string, fileFilter []string, childJob job) job {
return func(ctx context.Context) error {
var mu sync.Mutex
for {
lastHash := sourceTreeHash(paths, fileFilter)
current := lastHash
cctx, cancel := context.WithCancel(ctx)
go func() {
mu.Lock()
err := childJob(cctx)
if err != nil {
log.Printf("child process: %s", err)
}
mu.Unlock()
}()
for lastHash == current {
time.Sleep(250 * time.Millisecond)
current = sourceTreeHash(paths, fileFilter)
}
log.Printf("Source change detected - rebuilding")
cancel()
}
}
}
func sourceTreeHash(paths []string, fileFilter []string) string {
h := sha1.New()
for _, d := range paths {
h.Write(directoryHash(0, d, fileFilter))
}
return hex.EncodeToString(h.Sum(nil))
}
func directoryHash(level int, filePath string, fileFilter []string) []byte {
h := sha1.New()
h.Write([]byte(filePath))
fi, err := os.Stat(filePath)
if err != nil {
return h.Sum(nil)
}
if fi.IsDir() {
base := filepath.Base(filePath)
if level > 0 {
if base == ".git" || base == ".." || base == "node_modules" {
return []byte{}
}
}
// recurse
var names []string
f, err := os.Open(filePath)
if err == nil {
names, err = f.Readdirnames(-1)
}
if err == nil {
for _, name := range names {
if name == "" || name[0] == '.' {
continue
}
h.Write(directoryHash(level+1, path.Join(filePath, name), fileFilter))
}
}
} else {
if fileFilter != nil {
found := false
for _, pattern := range fileFilter {
if ok, _ := filepath.Match(pattern, filePath); ok {
found = true
} else if ok, _ := filepath.Match(pattern, filepath.Base(filePath)); ok {
found = true
}
}
if !found {
return []byte{}
}
}
f, err := os.Open(filePath)
if err == nil {
io.Copy(h, f)
f.Close()
}
}
return h.Sum(nil)
}