-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup_watcher.go
477 lines (410 loc) · 12.5 KB
/
setup_watcher.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
package dev
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/bmatcuk/doublestar/v4"
"github.com/fsnotify/fsnotify"
"github.com/sjc5/kiruna/internal/buildtime"
"github.com/sjc5/kiruna/internal/common"
"github.com/sjc5/kiruna/internal/runtime"
"github.com/sjc5/kiruna/internal/util"
)
var naiveIgnoreDirPatterns = [4]string{"**/.git", "**/node_modules", "dist/bin", "dist/kiruna"}
var ignoredDirPatterns = []string{}
var ignoredFilePatterns = []string{}
var defaultWatchedFiles = []common.WatchedFile{}
func mustSetupWatcher(manager *ClientManager, config *common.Config) {
defer mustKillAppDev()
cleanRootDir := config.GetCleanRootDir()
for _, p := range naiveIgnoreDirPatterns {
ignoredDirPatterns = append(ignoredDirPatterns, filepath.Join(cleanRootDir, p))
}
for _, p := range config.DevConfig.IgnorePatterns.Dirs {
ignoredDirPatterns = append(ignoredDirPatterns, filepath.Join(cleanRootDir, p))
}
for _, p := range config.DevConfig.IgnorePatterns.Files {
ignoredFilePatterns = append(ignoredFilePatterns, filepath.Join(cleanRootDir, p))
}
// Loop through all WatchedFiles...
for i, wfc := range config.DevConfig.WatchedFiles {
// and make each WatchedFile's Pattern relative to cleanRootDir...
config.DevConfig.WatchedFiles[i].Pattern = filepath.Join(cleanRootDir, wfc.Pattern)
// then loop through such WatchedFile's OnChangeCallbacks...
for j, oc := range wfc.OnChangeCallbacks {
// and make each such OnChangeCallback's ExcludedPatterns also relative to cleanRootDir
for k, p := range oc.ExcludedPatterns {
config.DevConfig.WatchedFiles[i].OnChangeCallbacks[j].ExcludedPatterns[k] = filepath.Join(cleanRootDir, p)
}
}
}
defaultWatchedFiles = append(defaultWatchedFiles, common.WatchedFile{
Pattern: filepath.Join(cleanRootDir, "static/{public,private}/**/*"),
})
watcher, err := fsnotify.NewWatcher()
if err != nil {
errMsg := fmt.Sprintf("error: failed to create watcher: %v", err)
util.Log.Error(errMsg)
panic(errMsg)
}
defer watcher.Close()
err = addDirs(watcher, config.GetCleanRootDir())
if err != nil {
errMsg := fmt.Sprintf("error: failed to add directories to watcher: %v", err)
util.Log.Error(errMsg)
panic(errMsg)
}
done := make(chan bool)
go func() {
mustKillAppDev()
err := buildtime.CompileBinary(config)
if err != nil {
util.Log.Errorf("error: failed to build app: %v", err)
}
mustStartAppDev(config)
mustHandleWatcherEmissions(config, manager, watcher)
}()
<-done
}
func mustHandleWatcherEmissions(config *common.Config, manager *ClientManager, watcher *fsnotify.Watcher) {
for {
select {
case evt := <-watcher.Events:
time.Sleep(10 * time.Millisecond) // let the file system settle
fileInfo, _ := os.Stat(evt.Name) // no need to check error, because we want to process either way
if fileInfo != nil && fileInfo.IsDir() {
if evt.Has(fsnotify.Create) || evt.Has(fsnotify.Rename) {
if err := addDirs(watcher, evt.Name); err != nil {
util.Log.Errorf("error: failed to add directory to watcher: %v", err)
continue
}
}
continue
}
evtDetails := getEvtDetails(config, evt)
if evtDetails.isIgnored {
continue
}
err := mustHandleFileChange(config, manager, evt, evtDetails)
if err != nil {
util.Log.Errorf("error: failed to handle file change: %v", err)
}
case err := <-watcher.Errors:
util.Log.Errorf("watcher error: %v", err)
}
}
}
type sortedOnChangeCallbacks struct {
stratPre []common.OnChange
stratConcurrent []common.OnChange
stratPost []common.OnChange
stratConcurrentNoWait []common.OnChange
exists bool
}
func sortOnChangeCallbacks(onChanges []common.OnChange) sortedOnChangeCallbacks {
stratPre := []common.OnChange{}
stratConcurrent := []common.OnChange{}
stratPost := []common.OnChange{}
stratConcurrentNoWait := []common.OnChange{}
exists := false
if len(onChanges) == 0 {
return sortedOnChangeCallbacks{}
} else {
exists = true
}
for _, o := range onChanges {
switch o.Strategy {
case common.OnChangeStrategyPre, "":
stratPre = append(stratPre, o)
case common.OnChangeStrategyConcurrent:
stratConcurrent = append(stratConcurrent, o)
case common.OnChangeStrategyPost:
stratPost = append(stratPost, o)
case common.OnChangeStrategyConcurrentNoWait:
stratConcurrentNoWait = append(stratConcurrentNoWait, o)
}
}
return sortedOnChangeCallbacks{
stratPre: stratPre,
stratConcurrent: stratConcurrent,
stratPost: stratPost,
stratConcurrentNoWait: stratConcurrentNoWait,
exists: exists,
}
}
func getIsIgnored(path string, ignoredPatterns *[]string) bool {
for _, pattern := range *ignoredPatterns {
if getIsMatch(pattern, path) {
return true
}
}
return false
}
func runConcurrentOnChangeCallbacks(onChanges *[]common.OnChange, evtName string, shouldWait bool) {
if len(*onChanges) > 0 {
wg := sync.WaitGroup{}
wg.Add(len(*onChanges))
for _, o := range *onChanges {
if getIsIgnored(evtName, &o.ExcludedPatterns) {
wg.Done()
continue
}
go func(o common.OnChange) {
defer wg.Done()
err := o.Func(evtName)
if err != nil {
util.Log.Errorf("error running extension callback: %v", err)
}
}(o)
}
if shouldWait {
wg.Wait()
}
}
}
func simpleRunOnChangeCallbacks(onChanges *[]common.OnChange, evtName string) {
for _, o := range *onChanges {
if getIsIgnored(evtName, &o.ExcludedPatterns) {
continue
}
err := o.Func(evtName)
if err != nil {
util.Log.Errorf("error running extension callback: %v", err)
}
}
}
func mustKillAndRestart(config *common.Config) {
util.Log.Infof("killing and restarting app")
mustKillAppDev()
mustStartAppDev(config)
}
// This is different than inside of handleGoFileChange, because here we
// assume we need to re-run other build steps too, not just recompile Go.
// Also, we don't necessarily recompile Go here (we only necessarily) run
// the other build steps. We only recompile Go if wfc.RecompileBinary is true.
func runOtherFileBuild(config *common.Config, wfc *common.WatchedFile) error {
err := buildtime.Build(config, wfc.RecompileBinary, true)
if err != nil {
msg := fmt.Sprintf("error: failed to build app: %v", err)
util.Log.Error(msg)
return errors.New(msg)
}
return nil
}
func mustReloadBroadcast(config *common.Config, manager *ClientManager, rfp RefreshFilePayload) {
if waitForAppReadiness(config) {
manager.broadcast <- rfp
return
}
errMsg := fmt.Sprintf("error: app never became ready: %v", rfp.ChangeType)
util.Log.Error(errMsg)
panic(errMsg)
}
func addDirs(watcher *fsnotify.Watcher, path string) error {
return filepath.Walk(path, func(walkedPath string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("error walking path: %v", err)
}
if info.IsDir() {
if getIsIgnored(walkedPath, &ignoredDirPatterns) {
return filepath.SkipDir
}
err := watcher.Add(walkedPath)
if err != nil {
return fmt.Errorf("error adding directory to watcher: %v", err)
}
}
return nil
})
}
type EvtDetails struct {
isIgnored bool
isGo bool
isOther bool
isCriticalCSS bool
isNormalCSS bool
isKirunaCSS bool
matchingWatchedFile *common.WatchedFile
}
var cachedMatchResults = map[string]bool{}
func getIsMatch(pattern string, path string) bool {
combined := pattern + path
if hit, isCached := cachedMatchResults[combined]; isCached {
return hit
}
normalizedPath := filepath.ToSlash(path)
matches, err := doublestar.Match(pattern, normalizedPath)
if err != nil {
util.Log.Errorf("error: failed to match file: %v", err)
return false
}
cachedMatchResults[combined] = matches // cache the result
return matches
}
func getEvtDetails(config *common.Config, evt fsnotify.Event) EvtDetails {
isCssSimple := filepath.Ext(evt.Name) == ".css"
isCriticalCSS := isCssSimple && getIsCssEvtType(config, evt, ChangeTypeCriticalCSS)
isNormalCSS := isCssSimple && getIsCssEvtType(config, evt, ChangeTypeNormalCSS)
isKirunaCSS := isCriticalCSS || isNormalCSS
var matchingWatchedFile *common.WatchedFile
for _, wfc := range config.DevConfig.WatchedFiles {
isMatch := getIsMatch(wfc.Pattern, evt.Name)
if isMatch {
matchingWatchedFile = &wfc
break
}
}
if matchingWatchedFile == nil {
for _, wfc := range defaultWatchedFiles {
isMatch := getIsMatch(wfc.Pattern, evt.Name)
if isMatch {
matchingWatchedFile = &wfc
break
}
}
}
isGo := filepath.Ext(evt.Name) == ".go"
if isGo && matchingWatchedFile != nil && matchingWatchedFile.TreatAsNonGo {
isGo = false
}
isOther := !isGo && !isKirunaCSS
isIgnored := getIsIgnored(evt.Name, &ignoredFilePatterns)
if isOther && matchingWatchedFile == nil {
isIgnored = true
}
return EvtDetails{
isOther: isOther,
isKirunaCSS: isKirunaCSS,
isGo: isGo,
isIgnored: isIgnored,
isCriticalCSS: isCriticalCSS,
isNormalCSS: isNormalCSS,
matchingWatchedFile: matchingWatchedFile,
}
}
func getIsCssEvtType(config *common.Config, evt fsnotify.Event, cssType ChangeType) bool {
return strings.HasPrefix(evt.Name, filepath.Join(config.GetCleanRootDir(), "styles/"+string(cssType)))
}
func getIsEmptyFile(evt fsnotify.Event) bool {
file, err := os.Open(evt.Name)
if err != nil {
util.Log.Errorf("error: failed to open file: %v", err)
return false
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
util.Log.Errorf("error: failed to get file stats: %v", err)
return false
}
return stat.Size() == 0
}
func getIsNonEmptyCHMODOnly(evt fsnotify.Event) bool {
isSolelyCHMOD := !evt.Has(fsnotify.Write) && !evt.Has(fsnotify.Create) && !evt.Has(fsnotify.Remove) && !evt.Has(fsnotify.Rename)
return isSolelyCHMOD && !getIsEmptyFile(evt)
}
func callback(config *common.Config, wfc *common.WatchedFile, evtDetails EvtDetails) error {
if evtDetails.isGo {
return buildtime.CompileBinary(config)
}
if evtDetails.isKirunaCSS {
if wfc.RecompileBinary || wfc.RestartApp {
return runOtherFileBuild(config, wfc)
}
cssType := ChangeTypeNormalCSS
if evtDetails.isCriticalCSS {
cssType = ChangeTypeCriticalCSS
}
return buildtime.ProcessCSS(config, string(cssType))
}
return runOtherFileBuild(config, wfc)
}
func mustHandleFileChange(
config *common.Config,
manager *ClientManager,
evt fsnotify.Event,
evtDetails EvtDetails,
) error {
if getIsNonEmptyCHMODOnly(evt) {
return nil
}
wfc := evtDetails.matchingWatchedFile
if wfc == nil {
wfc = &common.WatchedFile{}
}
if !config.DevConfig.ServerOnly && !wfc.SkipRebuildingNotification && !evtDetails.isKirunaCSS {
manager.broadcast <- RefreshFilePayload{
ChangeType: ChangeTypeRebuilding,
}
}
util.Log.Infof("modified: %s", evt.Name)
if evtDetails.isGo || wfc.RecompileBinary {
util.Log.Infof("recompiling binary")
}
sortedOnChanges := sortOnChangeCallbacks(wfc.OnChangeCallbacks)
var buildErr error
if sortedOnChanges.exists {
go func() {
runConcurrentOnChangeCallbacks(&sortedOnChanges.stratConcurrentNoWait, evt.Name, false)
}()
simpleRunOnChangeCallbacks(&sortedOnChanges.stratPre, evt.Name)
if wfc.RunOnChangeOnly {
util.Log.Infof("ran applicable onChange callbacks")
return nil
}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
buildErr = callback(config, wfc, evtDetails)
}()
runConcurrentOnChangeCallbacks(&sortedOnChanges.stratConcurrent, evt.Name, true)
wg.Wait()
} else {
buildErr = callback(config, wfc, evtDetails)
}
if buildErr != nil {
util.Log.Errorf("error: failed to build: %v", buildErr)
return buildErr
}
simpleRunOnChangeCallbacks(&sortedOnChanges.stratPost, evt.Name)
needsHardReloadEvenIfNonGo := wfc.RecompileBinary || wfc.RestartApp
if evtDetails.isGo || needsHardReloadEvenIfNonGo {
mustKillAndRestart(config)
}
if config.DevConfig.ServerOnly {
return nil
}
if !evtDetails.isKirunaCSS || needsHardReloadEvenIfNonGo {
util.Log.Infof("hard reloading browser")
mustReloadBroadcast(
config,
manager,
RefreshFilePayload{
ChangeType: ChangeTypeOther,
},
)
return nil
}
// At this point, we know it's a CSS file
cssType := ChangeTypeNormalCSS
if evtDetails.isCriticalCSS {
cssType = ChangeTypeCriticalCSS
}
util.Log.Infof("hot reloading browser")
mustReloadBroadcast(
config,
manager,
RefreshFilePayload{
ChangeType: cssType,
// These must be called AFTER ProcessCSS
CriticalCSS: runtime.GetCriticalCSS(config),
NormalCSSURL: runtime.GetStyleSheetURL(config),
},
)
return nil
}