-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.go
412 lines (366 loc) · 10.3 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
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
package main
import (
"context"
"crypto/sha1"
"encoding/hex"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"github.com/thijzert/go-resemble"
esb "github.com/evanw/esbuild/pkg/api"
)
type job func(ctx context.Context) error
type compileConfig struct {
Development bool
Quick bool
GOOS string
GOARCH string
PackageVersion string
}
func main() {
if _, err := os.Stat("pkg/web/assets"); err != nil {
log.Fatalf("Error: cannot find speeldoos assets directory. (error: %s)\nAre you running this from the repository root?", err)
}
var conf compileConfig
watch := false
run := false
flag.BoolVar(&conf.Development, "development", false, "Create a development build")
flag.BoolVar(&conf.Quick, "quick", false, "Create a development build")
flag.StringVar(&conf.GOARCH, "GOARCH", "", "Cross-compile for architecture")
flag.StringVar(&conf.GOOS, "GOOS", "", "Cross-compile for operating system")
flag.StringVar(&conf.PackageVersion, "version", "", "Override embedded version number")
flag.BoolVar(&watch, "watch", false, "Watch source tree for changes")
flag.BoolVar(&run, "run", false, "Run speeldoos upon successful compilation")
flag.Parse()
if conf.Development && conf.Quick {
log.Printf("")
log.Printf("You requested a quick build. This will not re-build static assets.")
log.Printf("")
}
var theJob job
if run {
theJob = func(ctx context.Context) error {
err := compile(ctx, conf)
if err != nil {
return err
}
runArgs := append([]string{"./speeldoos"}, flag.Args()...)
return passthru(ctx, runArgs...)
}
} else {
theJob = func(ctx context.Context) error {
return compile(ctx, conf)
}
}
if watch {
theJob = watchSourceTree([]string{"."}, []string{"*.go"}, theJob)
}
err := theJob(context.Background())
if err != nil {
log.Fatal(err)
}
}
func compileJavascript(target esb.Target, outDir string, entryPoints ...string) error {
err := os.MkdirAll(outDir, 0755)
if err != nil {
return fmt.Errorf("cannot create output directory: %w", err)
}
result := esb.Build(esb.BuildOptions{
EntryPoints: entryPoints,
Target: target,
Outdir: outDir,
Write: true,
})
var rv error
for _, err := range result.Warnings {
log.Printf("Warning: %s in %s on line %d", err.Text, err.Location.File, err.Location.Line)
}
for _, err := range result.Errors {
log.Printf("Error: %s in %s on line %d", err.Text, err.Location.File, err.Location.Line)
rv = fmt.Errorf("%s in %s on line %d", err.Text, err.Location.File, err.Location.Line)
}
return rv
}
func inlineSvg(w io.Writer, file string, fill string) error {
buf, err := os.ReadFile(path.Join("pkg/web/assets/src/svg", file))
if err != nil {
return fmt.Errorf("error reading svg file: %w", err)
}
rv := string(buf)
rv = strings.TrimSpace(rv)
for len(rv) > 5 && (rv[:2] == "<?" || rv[:4] == "<!--") {
search := "-->"
if rv[:2] == "<?" {
search = "?>"
}
idx := strings.Index(rv, search)
if idx <= 0 {
break
}
rv = strings.TrimSpace(rv[idx+len(search):])
}
if fill != "" {
// Hack the fill colour in there.
// (If it's stupid and works, it wasn't stupid.)
idx := 1 + strings.Index(rv[1:], "<")
if idx != -1 {
idx1 := idx + strings.Index(rv[idx:], ">")
idx2 := idx + strings.Index(rv[idx:], " ")
if idx2 > -1 && idx2 < idx1 {
idx1 = idx2
}
rv = rv[:idx1] + " fill=\"" + fill + "\"" + rv[idx1:]
}
}
rv = strings.ReplaceAll(rv, "%", "%25")
rv = strings.ReplaceAll(rv, "#", "%23")
rv = strings.ReplaceAll(rv, "<", "%3C")
rv = strings.ReplaceAll(rv, ">", "%3E")
rv = strings.ReplaceAll(rv, "?", "%3F")
rv = strings.ReplaceAll(rv, "\"", "'")
rv = strings.ReplaceAll(rv, "\n", " ")
_, err = fmt.Fprintf(w, "url(\"data:image/svg+xml,%s\")", rv)
return err
}
func compileSCSS(ctx context.Context, conf compileConfig, file string) error {
scssStyle := "compressed"
scssMap := "--omit-map-comment"
if conf.Development {
scssStyle = "nested"
scssMap = "--sourcemap"
}
if len(file) < 6 || file[0:1] == "_" || file[len(file)-5:] != ".scss" {
return nil
}
sourceFile := path.Join("pkg/web/assets/src/scss", file)
targetFile := path.Join("pkg/web/assets/dist/css", file[:len(file)-5]+".css")
err := os.MkdirAll(path.Dir(targetFile), 0755)
if err != nil {
return fmt.Errorf("cannot create CSS output directory: %w", err)
}
err = passthru(ctx, "sassc", "--style", scssStyle, scssMap, sourceFile, targetFile)
if err != nil {
return errors.WithMessage(err, fmt.Sprintf("error compiling assets: error compiling '%s'", file))
}
// Inline SVG images
if true {
buf, err := os.ReadFile(targetFile)
if err != nil {
return fmt.Errorf("error reading css output: %w", err)
}
f, err := os.Create(targetFile)
if err != nil {
return fmt.Errorf("error reopening css output: %w", err)
}
defer f.Close()
re := regexp.MustCompile("svg-load\\(\"([^\"]+)\"(,\\s*fill=([^\\)]+))?\\)")
lastIndex := 0
matches := re.FindAllSubmatchIndex(buf, -1)
for _, m := range matches {
_, err = f.Write(buf[lastIndex:m[0]])
if err != nil {
return fmt.Errorf("error writing css chunk: %w", err)
}
err = inlineSvg(f, string(buf[m[2]:m[3]]), string(buf[m[6]:m[7]]))
if err != nil {
return fmt.Errorf("error writing inlined svg image: %w", err)
}
lastIndex = m[1]
}
_, err = f.Write(buf[lastIndex:])
if err != nil {
return fmt.Errorf("error writing css chunk: %w", err)
}
}
return nil
}
func compile(ctx context.Context, conf compileConfig) error {
// Compile static assets: Javascript
err := compileJavascript(esb.ESNext, "pkg/web/assets/dist/js", "pkg/web/assets/src/js/**/*.js")
if err != nil {
return fmt.Errorf("error compiling javascript: %w", err)
}
err = compileJavascript(esb.ES2015, "pkg/web/assets/dist/ancient-js", "pkg/web/assets/src/js/speeldoos.js")
if err != nil {
return fmt.Errorf("error compiling ancient javascript: %w", err)
}
// Compile static assets: SCSS
if !conf.Development || !conf.Quick {
err := compileSCSS(ctx, conf, "speeldoos.scss")
if err != nil {
return err
}
d, err := os.Open("pkg/web/assets/src/scss/pages")
if err != nil {
return err
}
pages, _ := d.Readdirnames(-1)
for _, page := range pages {
err := compileSCSS(ctx, conf, path.Join("pages", page))
if err != nil {
return err
}
}
}
// Embed static assets
if err := os.Chdir("pkg/web/assets"); err != nil {
return errors.Errorf("Error: cannot find speeldoos assets directory. (error: %s)\nAre you *sure* you're running this from the repository root?", err)
}
var emb resemble.Resemble
emb.OutputFile = "../../../internal/web-plumbing/assets.go"
emb.PackageName = "plumbing"
emb.Debug = conf.Development
emb.AssetPaths = []string{
".",
}
if err := emb.Run(); err != nil {
os.Chdir("../../..")
return errors.WithMessage(err, "error running 'resemble'")
}
os.Chdir("../../..")
if conf.PackageVersion == "" {
gitDescCmd := exec.CommandContext(ctx, "git", "describe")
gitDescribe, err := gitDescCmd.Output()
if err != nil || len(gitDescribe) == 0 {
conf.PackageVersion = "default version"
} else {
conf.PackageVersion = string(gitDescribe)
}
}
// Build main executable
execOutput := "speeldoos"
if runtime.GOOS == "windows" || conf.GOOS == "windows" {
execOutput = "speeldoos.exe"
}
gofiles, err := filepath.Glob("cmd/speeldoos/*.go")
if err != nil || gofiles == nil {
return errors.WithMessage(err, "error: cannot find any go files to compile.")
}
compileArgs := append([]string{
"build",
"-ldflags", "-X github.com/thijzert/speeldoos/pkg.PackageVersion=" + conf.PackageVersion,
"-o", execOutput,
}, gofiles...)
compileCmd := exec.CommandContext(ctx, "go", compileArgs...)
compileCmd.Env = append(compileCmd.Env, os.Environ()...)
if conf.GOOS != "" {
compileCmd.Env = append(compileCmd.Env, "GOOS="+conf.GOOS)
}
if conf.GOARCH != "" {
compileCmd.Env = append(compileCmd.Env, "GOARCH="+conf.GOARCH)
}
err = passthruCmd(compileCmd)
if err != nil {
return errors.WithMessage(err, "compilation failed")
}
if conf.Development && !conf.Quick {
log.Printf("")
log.Printf("Development build finished.")
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:]...)
return passthruCmd(c)
}
func passthruCmd(c *exec.Cmd) error {
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)
}