-
Notifications
You must be signed in to change notification settings - Fork 10
/
runner.go
78 lines (63 loc) · 1.44 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
// Package manager provides manager for running watch and exec loop
package runner
import (
"github.com/shanzi/wu/command"
"log"
"strings"
"time"
)
type Runner interface {
Path() string
Patterns() []string
Command() command.Command
Start()
Exit()
}
type runner struct {
path string
patterns []string
command command.Command
abort chan struct{}
}
func New(path string, patterns []string, command command.Command) Runner {
return &runner{
path: path,
patterns: patterns,
command: command,
}
}
func (r *runner) Path() string {
return r.path
}
func (r *runner) Patterns() []string {
return r.patterns
}
func (r *runner) Command() command.Command {
return r.command
}
func (r *runner) Start() {
r.abort = make(chan struct{})
changed, err := watch(r.path, r.abort)
if err != nil {
log.Fatal("Failed to initialize watcher:", err)
}
matched := match(changed, r.patterns)
log.Println("Start watching...")
// Run the command once at initially
r.command.Start(200 * time.Millisecond)
for fp := range matched {
files := gather(fp, matched, 500*time.Millisecond)
// Terminate previous running command
r.command.Terminate(2 * time.Second)
log.Println("File changed:", strings.Join(files, ", "))
// Run new command
r.command.Start(200 * time.Millisecond)
}
}
func (r *runner) Exit() {
log.Println()
log.Println("Shutting down...")
r.abort <- struct{}{}
close(r.abort)
r.command.Terminate(2 * time.Second)
}