forked from go-godo/godo
/
runner.go
130 lines (108 loc) · 2.54 KB
/
runner.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
package godo
import (
"fmt"
"os"
"sync"
"github.com/mgutz/minimist"
"gopkg.in/godo.v1/util"
)
var watching bool
var help bool
var verbose bool
var version bool
var deprecatedWarnings bool
// DebounceMs is the default time (1500 ms) to debounce task events in watch mode.
var DebounceMs int64
var waitgroup sync.WaitGroup
var waitExit bool
var argm minimist.ArgMap
var contextArgm minimist.ArgMap
func init() {
DebounceMs = 2000
}
// Usage prints a usage screen with task descriptions.
func Usage(tasks string) {
// go's flag package prints ugly screen
format := `godo %s - do task(s)
Usage: godo [flags] [task...]
-D Print deprecated warnings
-h, --help This screen
--rebuild Rebuild Godofile
-v --verbose Log verbosely
-V, --version Print version
-w, --watch Watch task and dependencies`
if tasks == "" {
fmt.Printf(format, Version)
} else {
format += "\n\n%s"
fmt.Printf(format, Version, tasks)
}
}
// Godo runs a project of tasks.
func Godo(tasksFunc func(*Project)) {
godo(tasksFunc, nil)
}
func godo(tasksFunc func(*Project), argv []string) {
if argv == nil {
argm = minimist.Parse()
} else {
argm = minimist.ParseArgv(argv)
}
help = argm.ZeroBool("help", "h", "?")
verbose = argm.ZeroBool("verbose", "v")
version = argm.ZeroBool("version", "V")
watching = argm.ZeroBool("watch", "w")
deprecatedWarnings = argm.ZeroBool("D")
contextArgm = minimist.ParseArgv(argm.Unparsed())
project := NewProject(tasksFunc)
if help {
Usage(project.usage())
os.Exit(0)
}
if version {
fmt.Printf("godo %s\n", Version)
os.Exit(0)
}
// Run each task including their dependencies.
args := []string{}
for _, v := range argm.Leftover() {
args = append(args, fmt.Sprintf("%v", v))
}
if len(args) == 0 {
if project.Tasks["default"] != nil {
args = append(args, "default")
} else {
Usage(project.usage())
os.Exit(0)
}
}
// quick fix to make cascading watch work on default task
if len(args) == 1 && args[0] == "default" {
args = project.Tasks["default"].Dependencies
}
for _, name := range args {
err := project.Run(name)
if err != nil {
util.Error("ERR", "%s\n", err.Error())
os.Exit(1)
}
}
if watching {
if project.Watch(args, true) {
waitgroup.Add(1)
waitExit = true
} else {
fmt.Println("Nothing to watch. Use W{} or Watch{} to specify glob patterns")
os.Exit(0)
}
}
if waitExit {
waitgroup.Wait()
}
}
// MustNotError checks if error is not nil. If it is not nil it will panic.
func mustNotError(err error) {
if err != nil {
panic(err)
}
}