-
Notifications
You must be signed in to change notification settings - Fork 31
/
project.go
616 lines (540 loc) · 14.5 KB
/
project.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package godo
import (
"fmt"
"io"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/mgutz/minimist"
"gopkg.in/godo.v2/glob"
"gopkg.in/godo.v2/util"
"gopkg.in/godo.v2/watcher"
)
// softPanic is used to check for errors within a task handler.
type softPanic struct {
// msg is the original error that caused the panic
err error
}
func (sp *softPanic) Error() string {
return sp.err.Error()
}
// Halt is a soft panic and stops a task.
func Halt(v interface{}) {
if v == nil {
panic("No reason provided")
} else if err, ok := v.(error); ok {
panic(&softPanic{err})
}
panic(&softPanic{fmt.Errorf("%v", v)})
}
// M is generic string to interface alias
type M map[string]interface{}
// Project is a container for tasks.
type Project struct {
sync.Mutex
Tasks map[string]*Task
Namespace map[string]*Project
lastRun map[string]time.Time
exitFn func(code int)
ns string
contextArgm minimist.ArgMap
cwatchTasks map[chan bool]bool
parent *Project
}
// NewProject creates am empty project ready for tasks.
func NewProject(tasksFunc func(*Project), exitFn func(code int), argm minimist.ArgMap) *Project {
project := &Project{Tasks: map[string]*Task{}, lastRun: map[string]time.Time{}}
project.Namespace = map[string]*Project{}
project.Namespace[""] = project
project.ns = "root"
project.exitFn = exitFn
project.contextArgm = argm
project.Define(tasksFunc)
project.cwatchTasks = map[chan bool]bool{}
return project
}
// reset resets project state
func (project *Project) reset() {
for _, task := range project.Tasks {
task.Complete = false
}
project.lastRun = map[string]time.Time{}
}
func (project *Project) mustTask(name string) (*Project, *Task, string) {
if name == "" {
panic("Cannot get task for empty string")
}
proj := project
// use root
if strings.HasPrefix(name, "/") {
name = name[1:]
for true {
if proj.parent != nil {
proj = proj.parent
} else {
break
}
}
} else {
proj = project
}
taskName := "default"
parts := strings.Split(name, ":")
if len(parts) == 1 {
taskName = parts[0]
} else {
namespace := ""
for i := 0; i < len(parts)-1; i++ {
if namespace != "" {
namespace += ":"
}
ns := parts[i]
namespace += ns
proj = proj.Namespace[ns]
if proj == nil {
util.Panic("ERR", "Could not find project having namespace \"%s\"\n", namespace)
}
}
taskName = parts[len(parts)-1]
}
task := proj.Tasks[taskName]
if task == nil {
util.Panic("ERR", `"%s" task is not defined`+"\n", name)
}
return proj, task, taskName
}
func (project *Project) debounce(task *Task) bool {
if task.Name == "" {
panic("task name should not be empty")
}
debounce := task.debounce
if debounce == 0 {
debounce = Debounce
}
now := time.Now()
project.Lock()
defer project.Unlock()
oldRun := project.lastRun[task.Name]
if oldRun.IsZero() {
project.lastRun[task.Name] = now
return false
}
if oldRun.Add(debounce).After(now) {
project.lastRun[task.Name] = now
return true
}
return false
}
// Run runs a task by name.
func (project *Project) Run(name string) error {
return project.run(name, name, nil)
}
// RunWithEvent runs a task by name and adds FileEvent e to the context.
func (project *Project) runWithEvent(name string, logName string, e *watcher.FileEvent) error {
return project.run(name, logName, e)
}
func (project *Project) runTask(depName string, parentName string, e *watcher.FileEvent) error {
proj, _, taskName := project.mustTask(depName)
if proj == nil {
return fmt.Errorf("Project was not loaded for \"%s\" task", parentName)
}
return proj.runWithEvent(taskName, parentName+">"+depName, e)
}
func (project *Project) runParallel(steps []interface{}, parentName string, e *watcher.FileEvent) error {
var funcs = []func() error{}
for _, step := range steps {
switch t := step.(type) {
default:
panic(parentName + ": Parallel flow can only have types: (string | Series | Parallel)")
case string:
funcs = append(funcs, func() error {
return project.runTask(t, parentName, e)
})
case S:
funcs = append(funcs, func() error {
return project.runSeries(t, parentName, e)
})
case Series:
funcs = append(funcs, func() error {
return project.runSeries(t, parentName, e)
})
case P:
funcs = append(funcs, func() error {
return project.runParallel(t, parentName, e)
})
case Parallel:
funcs = append(funcs, func() error {
return project.runParallel(t, parentName, e)
})
}
}
err := GoThrottle(3, funcs...)
return err
}
func (project *Project) runSeries(steps []interface{}, parentName string, e *watcher.FileEvent) error {
var err error
for _, step := range steps {
switch t := step.(type) {
default:
panic(parentName + ": Series can only have types: (string | Series | Parallel)")
case string:
err = project.runTask(t, parentName, e)
case S:
err = project.runSeries(t, parentName, e)
case Series:
err = project.runSeries(t, parentName, e)
case P:
err = project.runParallel(t, parentName, e)
case Parallel:
err = project.runParallel(t, parentName, e)
}
if err != nil {
return err
}
}
return nil
}
// run runs the project, executing any tasks named on the command line.
func (project *Project) run(name string, logName string, e *watcher.FileEvent) error {
proj, task, _ := project.mustTask(name)
if !task.shouldRun(e) {
return nil
}
// debounce needs to be separate from shouldRun, so we can enqueue
// a file event that arrives between debounce intervals
if proj.debounce(task) {
if task.shouldRun(e) {
task.Lock()
if !task.ignoreEvents {
task.ignoreEvents = true
// fmt.Printf("DBG: ENQUEUE fileevent in between debounce\n")
time.AfterFunc(task.debounceValue(), func() {
// fmt.Printf("DBG: Running ENQUEUED\n")
task.Lock()
task.ignoreEvents = false
task.Unlock()
project.run(name, logName, e)
})
}
task.Unlock()
}
return nil
}
// run dependencies first
err := proj.runSeries(task.dependencies, name, e)
if err != nil {
return err
}
// then run the task itself
return task.RunWithEvent(logName, e)
}
// usage returns a string for usage screen
func (project *Project) usage() string {
tasks := "Tasks:\n"
names := []string{}
m := map[string]*Task{}
for ns, proj := range project.Namespace {
if ns != "" {
ns += ":"
}
for _, task := range proj.Tasks {
names = append(names, ns+task.Name)
m[ns+task.Name] = task
}
}
sort.Strings(names)
longest := 0
for _, name := range names {
l := len(name)
if l > longest {
longest = l
}
}
for _, name := range names {
task := m[name]
description := task.description
if description == "" {
if len(task.dependencies) > 0 {
description = fmt.Sprintf("Runs %v %s", task.DependencyNames(), name)
} else {
description = "Runs " + name
}
}
tasks += fmt.Sprintf(" %-"+strconv.Itoa(longest)+"s %s\n", name, description)
}
return tasks
}
// Use uses another project's task within a namespace.
func (project *Project) Use(namespace string, tasksFunc func(*Project)) {
namespace = strings.Trim(namespace, ":")
proj := NewProject(tasksFunc, project.exitFn, project.contextArgm)
proj.ns = project.ns + ":" + namespace
project.Namespace[namespace] = proj
proj.parent = project
}
// Task adds a task to the project with dependencies and handler.
func (project *Project) Task(name string, dependencies Dependency, handler func(*Context)) *Task {
task := NewTask(name, project.contextArgm)
if handler == nil && dependencies == nil {
util.Panic("godo", "Task %s requires a dependency or handler\n", name)
}
if handler != nil {
task.Handler = HandlerFunc(handler)
}
if dependencies != nil {
task.dependencies = append(task.dependencies, dependencies)
}
project.Tasks[task.Name] = task
return task
}
// Task1 adds a simple task to the project.
func (project *Project) Task1(name string, handler func(*Context)) *Task {
task := NewTask(name, project.contextArgm)
if handler == nil {
util.Panic("godo", "Task %s requires a dependency or handler\n", name)
}
task.Handler = HandlerFunc(handler)
project.Tasks[task.Name] = task
return task
}
// TaskD adds a task which runs other dependencies with no handler.
func (project *Project) TaskD(name string, dependencies Dependency) *Task {
task := NewTask(name, project.contextArgm)
if dependencies == nil {
util.Panic("godo", "Task %s requires a dependency or handler\n", name)
}
task.dependencies = append(task.dependencies, dependencies)
project.Tasks[task.Name] = task
return task
}
func (project *Project) watchTask(task *Task, root string, logName string, handler func(e *watcher.FileEvent)) {
ignorePathFn := func(p string) bool {
return watcher.DefaultIgnorePathFn(p) || !task.isWatchedFile(p)
}
const bufferSize = 2048
watchr, err := watcher.NewWatcher(bufferSize)
if err != nil {
util.Panic("project", "%v\n", err)
}
watchr.IgnorePathFn = ignorePathFn
watchr.ErrorHandler = func(err error) {
util.Error("project", "Watcher error %v\n", err)
}
watchr.WatchRecursive(root)
// this function will block forever, Ctrl+C to quit app
abs, err := filepath.Abs(root)
if err != nil {
fmt.Println("Could not get absolute path", err)
return
}
util.Info(logName, "watching %s\n", abs)
// not sure why this need to be unbuffered, but it was blocking
// on cquit <- true
cquit := make(chan bool, 1)
project.Lock()
project.cwatchTasks[cquit] = true
project.Unlock()
watchr.Start()
forloop:
for {
select {
case event := <-watchr.Event:
if event.Path != "" {
util.InfoColorful("godo", "%s changed\n", event.Path)
}
handler(event)
case <-cquit:
watchr.Stop()
break forloop
}
}
}
// Define defines tasks
func (project *Project) Define(fn func(*Project)) {
fn(project)
}
func calculateWatchPaths(patterns []string) []string {
//fmt.Println("DBG:calculateWatchPaths patterns", patterns)
paths := map[string]bool{}
for _, pat := range patterns {
if pat == "" {
continue
}
path := glob.PatternRoot(pat)
abs, err := filepath.Abs(path)
if err != nil {
fmt.Println("Error calculating watch paths", err)
}
paths[abs] = true
}
var keys []string
for key := range paths {
keys = append(keys, key)
}
sort.Strings(keys)
//fmt.Println("DBG:calculateWatchPaths keys", keys)
// skip any directories that overlap each other, eg test/sub should be
// ignored if test/ is in paths
var skip = map[string]bool{}
for i, dir := range keys {
dirSlash := dir + "/"
for _, dirj := range keys[i+1:] {
if strings.HasPrefix(dirj, dirSlash) {
skip[dirj] = true
}
}
}
var keep = []string{}
for _, dir := range keys {
if skip[dir] {
continue
}
rel, err := filepath.Rel(wd, dir)
if err != nil {
fmt.Println("Error calculating relative path", err)
continue
}
keep = append(keep, rel)
}
//fmt.Println("DBG:calculateWatchPaths keep", keep)
return keep
}
// gatherWatchInfo updates globs and regexps for the task based on its dependencies
func (project *Project) gatherWatchInfo(task *Task) (globs []string, regexps []*glob.RegexpInfo) {
globs = task.SrcGlobs
regexps = task.SrcRegexps
if len(task.dependencies) > 0 {
names := task.DependencyNames()
proj := project
for _, depname := range names {
var task *Task
proj, task, _ = project.mustTask(depname)
tglobs, tregexps := proj.gatherWatchInfo(task)
task.EffectiveWatchRegexps = tregexps
globs = append(globs, tglobs...)
regexps = append(regexps, tregexps...)
}
}
task.EffectiveWatchRegexps = regexps
task.EffectiveWatchGlobs = globs
return
}
// Watch watches the Files of a task and reruns the task on a watch event. Any
// direct dependency is also watched. Returns true if watching.
//
//
// TODO:
// 1. Only the parent task watches, but it gathers wath info from all dependencies.
//
// 2. Anything without src files always run when a dependency is triggered by a glob match.
//
// build [generate{*.go} compile] => go file changes => build, generate and compile
//
// 3. Tasks with src only run if it matches a src
//
// build [generate{*.go} css{*.scss} compile] => go file changes => build, generate and compile
// css does not need to run since no SCSS files ran
//
// X depends on [A:txt, B] => txt changes A runs, X runs without deps
// X:txt on [A, B] => txt changes A, B, X runs
//
func (project *Project) Watch(names []string, isParent bool) bool {
// fixes a bug where the first debounce prevents the task from running because
// all tasks are run once before Watch() is called
project.reset()
funcs := []func(){}
taskClosure := func(project *Project, task *Task, taskname string, logName string) func() {
paths := calculateWatchPaths(task.EffectiveWatchGlobs)
return func() {
if len(paths) == 0 {
return
}
for _, pth := range paths {
go func(path string) {
project.watchTask(task, path, logName, func(e *watcher.FileEvent) {
err := project.run(taskname, taskname, e)
if err != nil {
util.Error("ERR", "%s\n", err.Error())
}
})
}(pth)
}
}
}
for _, taskname := range names {
proj, task, _ := project.mustTask(taskname)
// updates effectiveWatchGlobs
proj.gatherWatchInfo(task)
if len(task.EffectiveWatchGlobs) > 0 {
funcs = append(funcs, taskClosure(project, task, taskname, taskname))
}
}
if len(funcs) > 0 {
<-all(funcs)
return true
}
return false
}
// Dumps information about the project to the console
func (project *Project) dump(buf io.Writer, prefix string, indent string) {
fmt.Fprintln(buf, "")
fmt.Fprintln(buf, prefix, project.ns, " =>")
fmt.Fprintln(buf, indent, "Tasks:")
for _, task := range project.Tasks {
task.dump(buf, indent+indent)
}
for key, proj := range project.Namespace {
if key == "" {
continue
}
proj.dump(buf, prefix, indent)
}
}
func (project *Project) quit(isParent bool) {
for ns, proj := range project.Namespace {
if ns != "" {
proj.quit(false)
}
}
// kill all watchTasks
for cquit := range project.cwatchTasks {
cquit <- true
}
if isParent {
runnerWaitGroup.Stop()
for _, process := range Processes {
if process != nil {
process.Kill()
}
}
}
//fmt.Printf("DBG: QUITTED\n")
}
// Exit quits the project.
func (project *Project) Exit(code int) {
project.quit(true)
}
// all runs the functions in fns concurrently.
func all(fns []func()) (done <-chan bool) {
var wg sync.WaitGroup
wg.Add(len(fns))
ch := make(chan bool, 1)
for _, fn := range fns {
go func(f func()) {
f()
wg.Done()
}(fn)
}
go func() {
wg.Wait()
doneSig(ch, true)
}()
return ch
}
func doneSig(ch chan bool, val bool) {
ch <- val
close(ch)
}