Debian Codegraph @

runner.go

raw symbols SCIP
1 package godo
2
3 import (
4 "fmt"
5 "os"
6 "os/signal"
7 "strings"
8 "syscall"
9 "time"
10
11 "github.com/mgutz/minimist"
12 "gopkg.in/godo.v2/util"
13 "gopkg.in/godo.v2/watcher"
14 )
15
16 // Message are sent on the Events channel
17 type Message struct {
18 Event string
19 Data string
20 }
21
22 const defaultWatchDelay = 1200 * time.Millisecond
23
24 var watching bool
25 var help bool
26 var verbose bool
27 var version bool
28 var deprecatedWarnings bool
29
30 // DebounceMs is the default time (1500 ms) to debounce task events in watch mode.
31 var Debounce time.Duration
32 var runnerWaitGroup = &WaitGroupN{}
33 var waitExit bool
34 var argm minimist.ArgMap
35 var wd string
36 var watchDelay = defaultWatchDelay
37
38 // SetWatchDelay sets the time duration between watches.
39 func SetWatchDelay(delay time.Duration) {
40 if delay == 0 {
41 delay = defaultWatchDelay
42 }
43 watchDelay = delay
44 watcher.SetWatchDelay(watchDelay)
45 }
46
47 // GetWatchDelay gets the watch delay
48 func GetWatchDelay() time.Duration {
49 return watchDelay
50 }
51
52 func init() {
53 // WatchDelay is the time to poll the file system
54 SetWatchDelay(watchDelay)
55 Debounce = 2000 * time.Millisecond
56 var err error
57 wd, err = os.Getwd()
58 if err != nil {
59 panic(err)
60 }
61
62 }
63
64 // Usage prints a usage screen with task descriptions.
65 func Usage(tasks string) {
66 // go's flag package prints ugly screen
67 format := `godo %s - do task(s)
68
69 Usage: godo [flags] [task...]
70 -D Print deprecated warnings
71 --dump Dump debug info about the project
72 -h, --help This screen
73 -i, --install Install Godofile dependencies
74 --rebuild Rebuild Godofile
75 -v --verbose Log verbosely
76 -V, --version Print version
77 -w, --watch Watch task and dependencies`
78
79 if tasks == "" {
80 fmt.Printf(format, Version)
81 } else {
82 format += "\n\n%s"
83 fmt.Printf(format, Version, tasks)
84 }
85 }
86
87 // Godo runs a project of tasks.
88 func Godo(tasksFunc func(*Project)) {
89 godo(tasksFunc, nil)
90 }
91
92 func godo(tasksFn func(*Project), argv []string) {
93 godoExit(tasksFn, argv, os.Exit)
94 }
95
96 // used for testing to switch out exitFn
97 func godoExit(tasksFunc func(*Project), argv []string, exitFn func(int)) {
98 if argv == nil {
99 argm = minimist.Parse()
100 } else {
101 argm = minimist.ParseArgv(argv)
102 }
103
104 dump := argm.AsBool("dump")
105 help = argm.AsBool("help", "h", "?")
106 verbose = argm.AsBool("verbose", "v")
107 version = argm.AsBool("version", "V")
108 watching = argm.AsBool("watch", "w")
109 deprecatedWarnings = argm.AsBool("D")
110 contextArgm := minimist.ParseArgv(argm.Unparsed())
111
112 project := NewProject(tasksFunc, exitFn, contextArgm)
113
114 if help {
115 Usage(project.usage())
116 exitFn(0)
117 }
118
119 if version {
120 fmt.Printf("godo %s\n", Version)
121 exitFn(0)
122 }
123
124 if dump {
125 project.dump(os.Stdout, "", " ")
126 exitFn(0)
127 }
128
129 // env vars are any nonflag key=value pair
130 addToOSEnviron(argm.NonFlags())
131
132 // Run each task including their dependencies.
133 args := []string{}
134 for _, s := range argm.NonFlags() {
135 // skip env vars
136 if !strings.Contains(s, "=") {
137 args = append(args, s)
138 }
139 }
140
141 if len(args) == 0 {
142 if project.Tasks["default"] != nil {
143 args = append(args, "default")
144 } else {
145 Usage(project.usage())
146 exitFn(0)
147 }
148 }
149
150 for _, name := range args {
151 err := project.Run(name)
152 if err != nil {
153 util.Error("ERR", "%s\n", err.Error())
154 exitFn(1)
155 }
156 }
157
158 if watching {
159 if project.Watch(args, true) {
160 runnerWaitGroup.Add(1)
161 waitExit = true
162 } else {
163 fmt.Println("Nothing to watch. Use Task#Src() to specify watch patterns")
164 exitFn(0)
165 }
166 }
167
168 if waitExit {
169 // Ctrl+C handler
170 csig := make(chan os.Signal, 1)
171 signal.Notify(csig, syscall.SIGQUIT)
172 go func() {
173 for sig := range csig {
174 fmt.Println("SIG caught")
175 if sig == syscall.SIGQUIT {
176 fmt.Println("SIG caught B")
177 project.Exit(0)
178 break
179 }
180 }
181 }()
182
183 runnerWaitGroup.Wait()
184 }
185 exitFn(0)
186 }
187
188 // MustNotError checks if error is not nil. If it is not nil it will panic.
189 func mustNotError(err error) {
190 if err != nil {
191 panic(err)
192 }
193 }