-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.go
559 lines (510 loc) · 14.6 KB
/
app.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package main
import (
"archive/zip"
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"time"
"github.com/mijia/gobuildweb/assets"
"github.com/mijia/gobuildweb/loggers"
"os/signal"
"strings"
"sync"
)
type TaskType int
var APP_SHELL_JS_TASK_INIT_ENTRY_KEY = "*****gb-init*****"
const (
// The Order is important
kTaskBuildImages TaskType = iota
kTaskBuildStyles
kTaskClearJavaScripts
kTaskBuildJavaScripts
kTaskGenAssetsMapping
kTaskBinaryTest
kTaskBuildBinary
kTaskBinaryRestart
)
type AppShellTask struct {
taskType TaskType
module string
}
type AppShell struct {
binName string
args []string
isProduction bool
taskChan chan AppShellTask
curError error
command *exec.Cmd
buildGuard *sync.Mutex
buildCmd *exec.Cmd
}
func (app *AppShell) Run() error {
app.isProduction = false
buildImageDone := make(chan bool)
buildJavascriptDone := make(chan bool)
buildBinaryDone := make(chan bool)
buildStyleDone := make(chan bool)
go func() {
err := app.buildImages("")
if app.curError == nil && err != nil {
app.curError = err
}
err = app.genAssetsMapping()
if app.curError == nil && err != nil {
app.curError = err
}
buildImageDone <- true
}()
<- buildImageDone
go func() {
app.clearJavaScriptsAssets()
err := app.buildJavaScripts(APP_SHELL_JS_TASK_INIT_ENTRY_KEY)
if app.curError == nil && err != nil {
app.curError = err
}
buildJavascriptDone <- true
}()
go func() {
err := app.buildBinary("")
if app.curError == nil && err != nil {
app.curError = err
}
buildBinaryDone <- true
}()
go func() {
err := app.buildStyles("")
if app.curError == nil && err != nil {
app.curError = err
}
buildStyleDone <- true
}()
<- buildStyleDone
<- buildJavascriptDone
<- buildBinaryDone
go app.startRunner()
if app.curError != nil {
loggers.Error(app.curError.Error())
loggers.Warn("You have errors with current assets and binary, please fix that ...")
} else {
app.executeTask(
AppShellTask{kTaskGenAssetsMapping, ""},
AppShellTask{kTaskBinaryRestart, ""},
)
}
return nil
}
func (app *AppShell)interruptProcess(){
//capture Interrupt signal
if(app.isProduction) {
return
}
interruptChan := make(chan os.Signal, 1)
signal.Notify(interruptChan, os.Interrupt)
go func(){
for sig := range interruptChan {
loggers.Info("Receive Interrupt Signal(%v), kill the app!", sig)
app.kill()
loggers.Info("Leaving gobuildweb, bye!")
os.Exit(0)
}
}()
}
func (app *AppShell) Dist() error {
app.isProduction = true
fmt.Println()
loggers.Info("Creating distribution package for %v-%v",
rootConfig.Package.Name, rootConfig.Package.Version)
var err error
if err = app.buildImages(""); err != nil {
loggers.Error("Error when building images, %v", err)
} else if err = app.genAssetsMapping(); err != nil {
loggers.Error("Error when generating assets mapping source code, %v", err)
} else if err = app.buildStyles(""); err != nil {
loggers.Error("Error when building stylesheets, %v", err)
} else if err = app.clearJavaScriptsAssets(); err != nil {
loggers.Error("Error when clear javascripts, %v", err)
} else if err = app.buildJavaScripts(APP_SHELL_JS_TASK_INIT_ENTRY_KEY); err != nil {
loggers.Error("Error when building javascripts, %v", err)
} else if err = app.genAssetsMapping(); err != nil {
loggers.Error("Error when generating assets mapping source code, %v", err)
} else if err = app.binaryTest(""); err != nil {
loggers.Error("You have failed test cases, %v", err)
} else if err = app.distExtraCommand(); err != nil {
loggers.Error("Error when running the distribution extra command, %v", err)
} else if err == nil {
goOs, goArch := runtime.GOOS, runtime.GOARCH
targets := append(rootConfig.Distribution.CrossTargets, [2]string{goOs, goArch})
visited := make(map[string]struct{})
for _, target := range targets {
buildTarget := fmt.Sprintf("%s_%s", target[0], target[1])
if _, ok := visited[buildTarget]; ok {
continue
}
visited[buildTarget] = struct{}{}
if err = app.buildBinary(target[:]...); err != nil {
loggers.Error("Error when building binary for %v, %v", target, err)
}
}
}
if err == nil {
err = app.buildPackage()
}
return err
}
func (app *AppShell) distExtraCommand() error {
extraCmd := rootConfig.Distribution.ExtraCmd
if extraCmd == nil || len(extraCmd) == 0 {
return nil
}
cmd := exec.Command(extraCmd[0], extraCmd[1:]...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
loggers.Error("Error when running distribution extra command, %v, %s", extraCmd, err)
return err
}
loggers.Succ("Run extra command succ: %v", cmd.Args)
return nil
}
func (app *AppShell) buildPackage() error {
name, version := rootConfig.Package.Name, rootConfig.Package.Version
pkgName := fmt.Sprintf("%s-%s", name, version)
srcFolders := append([]string{"public"}, rootConfig.Distribution.PackExtras...)
goOs, goArch := runtime.GOOS, runtime.GOARCH
targets := append(rootConfig.Distribution.CrossTargets, [2]string{goOs, goArch})
visited := make(map[string]struct{})
for _, target := range targets {
buildTarget := fmt.Sprintf("%s_%s", target[0], target[1])
if _, ok := visited[buildTarget]; ok {
continue
}
visited[buildTarget] = struct{}{}
srcFolders = append(srcFolders, app.binaryName(name, version, target[0], target[1]))
}
if zipFile, err := os.Create(pkgName + ".zip"); err != nil {
return fmt.Errorf("Cannot create the zip file[%q], %v", pkgName, err)
} else {
defer zipFile.Close()
zw := zip.NewWriter(zipFile)
defer zw.Close()
for _, srcFolder := range srcFolders {
err := filepath.Walk(srcFolder, func(fn string, info os.FileInfo, err error) error {
if err == nil && !info.IsDir() {
zipSrcName := path.Join(pkgName, fn)
fileHeader, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
fileHeader.Name = zipSrcName
zipSrcFile, err := zw.CreateHeader(fileHeader)
if err != nil {
return err
}
srcFile, err := os.Open(fn)
if err != nil {
return err
}
io.Copy(zipSrcFile, srcFile)
srcFile.Close()
loggers.Debug("Archiving %s", zipSrcName)
}
return nil
})
if err != nil {
return fmt.Errorf("Cannot walk the files when creating the zip file, %v", err)
}
}
}
loggers.Succ("Finish packing the deploy package in %s.zip", pkgName)
return nil
}
func (app *AppShell) startRunner() {
for task := range app.taskChan {
switch task.taskType {
case kTaskBuildImages:
app.curError = app.buildImages(task.module)
case kTaskBuildStyles:
app.curError = app.buildStyles(task.module)
case kTaskClearJavaScripts:
app.curError = app.clearJavaScriptsAssets()
case kTaskBuildJavaScripts:
app.curError = app.buildJavaScripts(task.module)
case kTaskGenAssetsMapping:
app.curError = app.genAssetsMapping()
case kTaskBinaryTest:
app.curError = app.binaryTest(task.module)
case kTaskBuildBinary:
app.curError = app.buildBinary()
case kTaskBinaryRestart:
loggers.Info("Binary Restart!")
if app.curError == nil {
if err := app.kill(); err != nil {
loggers.Error("App cannot be killed, maybe you should restart the gobuildweb: %v", err)
} else {
if err := app.start(); err != nil {
loggers.Error("App cannot be started, maybe you should restart the gobuildweb: %v", err)
}
}
} else {
loggers.Warn("You have errors with current assets and binary, please fix that ...")
}
fmt.Println()
loggers.Info("Waiting for the file changes ...")
}
}
}
func (app *AppShell) executeTask(tasks ...AppShellTask) {
for _, task := range tasks {
app.taskChan <- task
}
//app.taskChan <- AppShellTask{kTaskBinaryRestart, ""}
}
func (app *AppShell) kill() error {
if app.command != nil && (app.command.ProcessState == nil || !app.command.ProcessState.Exited()) {
if runtime.GOOS == "windows" {
if err := app.command.Process.Kill(); err != nil {
return err
}
} else if err := app.command.Process.Signal(os.Interrupt); err != nil {
return err
}
rootConfig.RLock()
isGraceful := rootConfig.Package.IsGraceful
rootConfig.RUnlock()
if !isGraceful {
// Wait for our process to die before we return or hard kill after 3 sec
// when this is not a graceful server
select {
case <-time.After(3 * time.Second):
if err := app.command.Process.Kill(); err != nil {
loggers.Warn("failed to kill the app: %v", err)
}
}
}
app.command = nil
}
return nil
}
func (app *AppShell) start() error {
app.command = exec.Command("./"+app.binName, app.args...)
app.command.Stdout = os.Stdout
app.command.Stderr = os.Stderr
app.command.Env = mergeEnv(nil)
if err := app.command.Start(); err != nil {
return err
}
loggers.Succ("App is starting, %v", app.command.Args)
fmt.Println()
go app.command.Wait()
time.Sleep(500 * time.Millisecond)
return nil
}
func (app *AppShell) clearJavaScriptsAssets() error {
rootConfig.RLock()
entries := append(rootConfig.Assets.VendorSets, rootConfig.Assets.Entries...)
rootConfig.RUnlock()
entryMap := make(map[string]string)
for _, entry := range entries {
entryMap[entry.Name] = ""
}
assets.ClearJavaScriptsDir(entryMap)
return nil
}
func (app *AppShell) buildAssetsTraverse(functor func(entry string) error) error {
rootConfig.RLock()
vendors := rootConfig.Assets.VendorSets
entries := rootConfig.Assets.Entries
rootConfig.RUnlock()
for _, vendor := range vendors {
if err := functor(vendor.Name); err != nil {
return err
}
}
for _, entry := range entries {
if err := functor(entry.Name); err != nil {
return err
}
}
return nil
}
func (app *AppShell) buildImages(entry string) error {
rootConfig.RLock()
if rootConfig.Assets == nil {
rootConfig.RUnlock()
return nil
}
rootConfig.RUnlock()
if entry == "" {
if err := assets.ResetDir("public/images", true); err != nil {
return err
}
return app.buildAssetsTraverse(app.buildImages)
}
rootConfig.RLock()
defer rootConfig.RUnlock()
return assets.ImageLibrary(*rootConfig.Assets, entry).Build(app.isProduction)
}
func (app *AppShell) buildStyles(entry string) error {
rootConfig.RLock()
if rootConfig.Assets == nil {
rootConfig.RUnlock()
return nil
}
rootConfig.RUnlock()
if entry == "" {
if err := assets.ResetDir("public/stylesheets", true); err != nil {
return err
}
return app.buildAssetsTraverse(app.buildStyles)
}
rootConfig.RLock()
defer rootConfig.RUnlock()
return assets.StyleSheet(*rootConfig.Assets, entry).Build(app.isProduction)
}
func (app *AppShell) buildJavaScripts(entry string) error {
rootConfig.RLock()
if rootConfig.Assets == nil {
rootConfig.RUnlock()
return nil
}
rootConfig.RUnlock()
if entry == APP_SHELL_JS_TASK_INIT_ENTRY_KEY {
if err := assets.CheckMkdir("public/javascripts"); err != nil {
return err
}
return app.buildAssetsTraverse(app.buildJavaScripts)
}
if entry == "" {
if err := assets.ResetDir("public/javascripts", true); err != nil {
return err
}
if err := app.genAssetsMapping(); err != nil {
return err
}
return app.buildAssetsTraverse(app.buildJavaScripts)
}
rootConfig.RLock()
defer rootConfig.RUnlock()
return assets.JavaScript(*rootConfig.Assets, entry).Build(app.isProduction)
}
func (app *AppShell) genAssetsMapping() (err error) {
rootConfig.RLock()
defer rootConfig.RUnlock()
if rootConfig.Assets == nil {
return nil
}
return assets.Mappings(*rootConfig.Assets).Build(app.isProduction)
}
func (app *AppShell) binaryTest(module string) error {
return nil // close the test first, will reconsider this
if module == "" {
module = "./..."
}
builder := rootConfig.Package.Builder
cmdName := "go"
flags := make([]string, 0)
if builder != "" {
cmdName = builder
flags = append(flags, "go")
}
flags = append(flags, "test", "-v", module)
testCmd := exec.Command(cmdName, flags...)
testCmd.Stderr = os.Stderr
testCmd.Stdout = os.Stdout
testCmd.Env = mergeEnv(nil)
if err := testCmd.Run(); err != nil {
loggers.Error("Error when testing go modules[%s], %v", module, err)
return err
}
loggers.Succ("Module[%s] Test passed: %v", module, testCmd.Args)
return nil
}
func (app *AppShell) binaryName(name, version, goOs, goArch string) string {
binName := fmt.Sprintf("%s-%s.%s.%s", name, version, goOs, goArch)
if goOs == "windows" {
binName += ".exe"
}
return binName
}
func (app *AppShell) buildBinary(params ...string) error {
goOs, goArch := runtime.GOOS, runtime.GOARCH
if len(params) == 2 && (goOs != params[0] || goArch != params[1]) {
goOs, goArch = params[0], params[1]
}
rootConfig.RLock()
builder := rootConfig.Package.Builder
binName := app.binaryName(rootConfig.Package.Name, rootConfig.Package.Version, goOs, goArch)
var buildOpts []string
if app.isProduction {
buildOpts = make([]string, len(rootConfig.Distribution.BuildOpts))
copy(buildOpts, rootConfig.Distribution.BuildOpts)
} else {
buildOpts = make([]string, len(rootConfig.Package.BuildOpts))
copy(buildOpts, rootConfig.Package.BuildOpts)
}
rootConfig.RUnlock()
cmdName := "go"
flags := make([]string, 0, 3+len(buildOpts))
if builder != "" {
flags = append(flags, "go")
cmdName = builder
}
flags = append(flags, "build")
flags = append(flags, buildOpts...)
flags = append(flags, []string{"-o", binName}...)
buildCmd := exec.Command(cmdName, flags...)
buildCmd.Stderr = os.Stderr
buildCmd.Stdout = os.Stdout
buildCmd.Env = mergeEnv(map[string]string{
"GOOS": goOs,
"GOARCH": goArch,
})
loggers.Debug("Running build: %v", buildCmd.Args)
start := time.Now()
app.buildGuard.Lock()
app.buildCmd = buildCmd
app.buildGuard.Unlock()
if err := app.buildCmd.Run(); err != nil {
if strings.Contains(err.Error(), "interrupt") {
loggers.Info("File changed while building binary, rebuild will start!")
<- app.taskChan
return nil
} else {
loggers.Error("Building failed: %v", err)
return err
}
}
app.binName = binName
duration := float64(time.Since(start).Nanoseconds()) / 1e6
loggers.Succ("Got binary built %s, takes=%.3fms", binName, duration)
return nil
}
func NewAppShell(args []string) *AppShell {
app := &AppShell{
args: args,
taskChan: make(chan AppShellTask,2),
buildGuard: &sync.Mutex{},
}
//app.interruptProcess()
return app
}
func (app *AppShell) stopBuildBinary(params ...string) error {
app.buildGuard.Lock()
defer app.buildGuard.Unlock()
if app.buildCmd!= nil && (app.buildCmd.ProcessState == nil || !app.buildCmd.ProcessState.Exited()) {
if runtime.GOOS == "windows" {
if err := app.buildCmd.Process.Kill(); err != nil {
loggers.Error(err.Error())
return err
}
} else if err := app.buildCmd.Process.Signal(os.Interrupt); err != nil {
loggers.Error(err.Error())
return err
}
}
return nil
}