forked from githubnemo/CompileDaemon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
daemon.go
431 lines (348 loc) · 10.9 KB
/
daemon.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
/*
CompileDaemon is a very simple compile daemon for Go.
CompileDaemon watches your .go files in a directory and invokes `go build`
if a file changes.
Examples
In its simplest form, the defaults will do. With the current working directory set
to the source directory you can simply…
$ CompileDaemon
… and it will recompile your code whenever you save a source file.
If you want it to also run your program each time it builds you might add…
$ CompileDaemon -command="./MyProgram -my-options"
… and it will also keep a copy of your program running. Killing the old one and
starting a new one each time you build.
You may find that you need to exclude some directories and files from
monitoring, such as a .git repository or emacs temporary files…
$ CompileDaemon -exclude-dir=.git -exclude=".#*"
If you want to monitor files other than .go and .c files you might…
$ CompileDaemon -include=Makefile -include="*.less" -include="*.tmpl"
Options
There are command line options.
FILE SELECTION
-directory=XXX – which directory to monitor for changes
-recursive=XXX – look into subdirectories
-exclude-dir=XXX – exclude directories matching glob pattern XXX
-exlude=XXX – exclude files whose basename matches glob pattern XXX
-include=XXX – include files whose basename matches glob pattern XXX
-pattern=XXX – include files whose path matches regexp XXX
MISC
-color - enable colorized output
-log-prefix - Enable/disable stdout/stderr labelling for the child process
-graceful-kill - On supported platforms, send the child process a SIGTERM to
allow it to exit gracefully if possible.
ACTIONS
-build=CCC – Execute CCC to rebuild when a file changes
-command=CCC – Run command CCC after a successful build, stops previous command first
*/
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
"strings"
"syscall"
"time"
"github.com/fatih/color"
"gopkg.in/fsnotify.v1"
)
// Milliseconds to wait for the next job to begin after a file change
const WorkDelay = 900
// Default pattern to match files which trigger a build
const FilePattern = `(.+\.go|.+\.c)$`
type globList []string
func (g *globList) String() string {
return fmt.Sprint(*g)
}
func (g *globList) Set(value string) error {
*g = append(*g, value)
return nil
}
func (g *globList) Matches(value string) bool {
for _, v := range *g {
if match, err := filepath.Match(v, value); err != nil {
log.Fatalf("Bad pattern \"%s\": %s", v, err.Error())
} else if match {
return true
}
}
return false
}
var (
flag_directory = flag.String("directory", ".", "Directory to watch for changes")
flag_pattern = flag.String("pattern", FilePattern, "Pattern of watched files")
flag_command = flag.String("command", "", "Command to run and restart after build")
flag_command_stop = flag.Bool("command-stop", false, "Stop command before building")
flag_recursive = flag.Bool("recursive", true, "Watch all dirs. recursively")
flag_build = flag.String("build", "go build", "Command to rebuild after changes")
flag_build_dir = flag.String("build-dir", "", "Directory to run build command in. Defaults to directory")
flag_color = flag.Bool("color", false, "Colorize output for CompileDaemon status messages")
flag_logprefix = flag.Bool("log-prefix", true, "Print log timestamps and subprocess stderr/stdout output")
flag_gracefulkill = flag.Bool("graceful-kill", false, "Gracefully attempt to kill the child process by sending a SIGTERM first")
// initialized in main() due to custom type.
flag_excludedDirs globList
flag_excludedFiles globList
flag_includedFiles globList
)
func okColor(format string, args ...interface{}) string {
if *flag_color {
return color.GreenString(format, args...)
} else {
return fmt.Sprintf(format, args...)
}
}
func failColor(format string, args ...interface{}) string {
if *flag_color {
return color.RedString(format, args...)
} else {
return fmt.Sprintf(format, args...)
}
}
// Run `go build` and print the output if something's gone wrong.
func build() bool {
log.Println(okColor("Running build command!"))
args := strings.Split(*flag_build, " ")
if len(args) == 0 {
// If the user has specified and empty then we are done.
return true
}
cmd := exec.Command(args[0], args[1:]...)
if *flag_build_dir != "" {
cmd.Dir = *flag_build_dir
} else {
cmd.Dir = *flag_directory
}
output, err := cmd.CombinedOutput()
if err == nil {
log.Println(okColor("Build ok."))
} else {
log.Println(failColor("Error while building:\n"), failColor(string(output)))
}
return err == nil
}
func matchesPattern(pattern *regexp.Regexp, file string) bool {
return pattern.MatchString(file)
}
// Accept build jobs and start building when there are no jobs rushing in.
// The inrush protection is WorkDelay milliseconds long, in this period
// every incoming job will reset the timer.
func builder(jobs <-chan string, buildStarted chan<- struct{}, buildDone chan<- bool) {
createThreshold := func() <-chan time.Time {
return time.After(time.Duration(WorkDelay * time.Millisecond))
}
threshold := createThreshold()
for {
select {
case <-jobs:
threshold = createThreshold()
case <-threshold:
buildStarted <- struct{}{}
buildDone <- build()
}
}
}
func logger(pipeChan <-chan io.ReadCloser) {
dumper := func(pipe io.ReadCloser, prefix string) {
reader := bufio.NewReader(pipe)
readloop:
for {
line, err := reader.ReadString('\n')
if err != nil {
break readloop
}
if *flag_logprefix {
log.Print(prefix, " ", line)
} else {
log.Print(line)
}
}
}
for {
pipe := <-pipeChan
go dumper(pipe, "stdout:")
pipe = <-pipeChan
go dumper(pipe, "stderr:")
}
}
// Start the supplied command and return stdout and stderr pipes for logging.
func startCommand(command string) (cmd *exec.Cmd, stdout io.ReadCloser, stderr io.ReadCloser, err error) {
args := strings.Split(command, " ")
cmd = exec.Command(args[0], args[1:]...)
if stdout, err = cmd.StdoutPipe(); err != nil {
err = fmt.Errorf("can't get stdout pipe for command: %s", err)
return
}
if stderr, err = cmd.StderrPipe(); err != nil {
err = fmt.Errorf("can't get stderr pipe for command: %s", err)
return
}
if err = cmd.Start(); err != nil {
err = fmt.Errorf("can't start command: %s", err)
return
}
return
}
// Run the command in the given string and restart it after
// a message was received on the buildDone channel.
func runner(command string, buildStarted <-chan struct{}, buildSuccess <-chan bool) {
var currentProcess *os.Process
pipeChan := make(chan io.ReadCloser)
go logger(pipeChan)
go func() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, fatalSignals...)
<-sigChan
log.Println(okColor("Received signal, terminating cleanly."))
if currentProcess != nil {
killProcess(currentProcess)
}
os.Exit(0)
}()
for {
<-buildStarted
if !*flag_command_stop {
if !<-buildSuccess {
continue
}
}
if currentProcess != nil {
killProcess(currentProcess)
}
if *flag_command_stop {
log.Println(okColor("Command stopped. Waiting for build to complete."))
<-buildSuccess
}
log.Println(okColor("Restarting the given command."))
cmd, stdoutPipe, stderrPipe, err := startCommand(command)
if err != nil {
log.Fatal(failColor("Could not start command:", err))
}
pipeChan <- stdoutPipe
pipeChan <- stderrPipe
currentProcess = cmd.Process
}
}
func killProcess(process *os.Process) {
if *flag_gracefulkill {
killProcessGracefully(process)
} else {
killProcessHard(process)
}
}
func killProcessHard(process *os.Process) {
log.Println(okColor("Hard stopping the current process.."))
if err := process.Kill(); err != nil {
log.Fatal(failColor("Could not kill child process. Aborting due to danger of infinite forks."))
}
if _, err := process.Wait(); err != nil {
log.Fatal(failColor("Could not wait for child process. Aborting due to danger of infinite forks."))
}
}
func killProcessGracefully(process *os.Process) {
done := make(chan error, 1)
go func() {
log.Println(okColor("Gracefully stopping the current process.."))
if err := terminateGracefully(process); err != nil {
done <- err
return
}
_, err := process.Wait()
done <- err
}()
select {
case <-time.After(3 * time.Second):
log.Println(failColor("Could not gracefully stop the current process, proceeding to hard stop."))
killProcessHard(process)
<-done
case err := <-done:
if err != nil {
log.Fatal(failColor("Could not kill child process. Aborting due to danger of infinite forks."))
}
}
}
func flusher(buildStarted <-chan struct{}, buildSuccess <-chan bool) {
for {
<-buildStarted
<-buildSuccess
}
}
func main() {
flag.Var(&flag_excludedDirs, "exclude-dir", " Don't watch directories matching this name")
flag.Var(&flag_excludedFiles, "exclude", " Don't watch files matching this name")
flag.Var(&flag_includedFiles, "include", " Watch files matching this name")
flag.Parse()
if !*flag_logprefix {
log.SetFlags(0)
}
if *flag_directory == "" {
fmt.Fprintf(os.Stderr, "-directory=... is required.\n")
os.Exit(1)
}
if *flag_gracefulkill && !gracefulTerminationPossible() {
log.Fatal("Graceful termination is not supported on your platform.")
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
if *flag_recursive == true {
err = filepath.Walk(*flag_directory, func(path string, info os.FileInfo, err error) error {
if err == nil && info.IsDir() {
if flag_excludedDirs.Matches(info.Name()) {
return filepath.SkipDir
} else {
return watcher.Add(path)
}
}
return err
})
if err != nil {
log.Fatal("filepath.Walk():", err)
}
if err := watcher.Add(*flag_directory); err != nil {
log.Fatal("watcher.Add():", err)
}
} else {
if err := watcher.Add(*flag_directory); err != nil {
log.Fatal("watcher.Add():", err)
}
}
pattern := regexp.MustCompile(*flag_pattern)
jobs := make(chan string)
buildSuccess := make(chan bool)
buildStarted := make(chan struct{})
go builder(jobs, buildStarted, buildSuccess)
if *flag_command != "" {
go runner(*flag_command, buildStarted, buildSuccess)
} else {
go flusher(buildStarted, buildSuccess)
}
for {
select {
case ev := <-watcher.Events:
if ev.Op&fsnotify.Remove == fsnotify.Remove || ev.Op&fsnotify.Write == fsnotify.Write || ev.Op&fsnotify.Create == fsnotify.Create || ev.Op&fsnotify.Chmod == fsnotify.Chmod {
base := filepath.Base(ev.Name)
if flag_includedFiles.Matches(base) || matchesPattern(pattern, ev.Name) {
if !flag_excludedFiles.Matches(base) {
jobs <- ev.Name
}
}
}
case err := <-watcher.Errors:
if v, ok := err.(*os.SyscallError); ok {
if v.Err == syscall.EINTR {
continue
}
log.Fatal("watcher.Error: SyscallError:", v)
}
log.Fatal("watcher.Error:", err)
}
}
}