forked from Tonkpils/snag
/
builder.go
192 lines (164 loc) · 3.27 KB
/
builder.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/Tonkpils/snag/vow"
"github.com/shiena/ansicolor"
fsn "gopkg.in/fsnotify.v1"
)
var mtimes = map[string]time.Time{}
var clearBuffer = func() {
fmt.Print("\033c")
}
type Bob struct {
w *fsn.Watcher
mtx sync.RWMutex
curVow *vow.Vow
done chan struct{}
watching map[string]struct{}
watchDir string
cmds [][]string
ignoredItems []string
verbose bool
}
func NewBuilder(c config) (*Bob, error) {
w, err := fsn.NewWatcher()
if err != nil {
return nil, err
}
cmds := make([][]string, len(c.Script))
for i, s := range c.Script {
cmds[i] = strings.Split(s, " ")
}
return &Bob{
w: w,
done: make(chan struct{}),
watching: map[string]struct{}{},
cmds: cmds,
ignoredItems: c.IgnoredItems,
verbose: c.Verbose,
}, nil
}
func (b *Bob) Close() error {
close(b.done)
return b.w.Close()
}
func (b *Bob) Watch(path string) error {
b.watchDir = path
b.watch(path)
b.execute()
for {
select {
case ev := <-b.w.Events:
var queueBuild bool
switch {
case isCreate(ev.Op):
queueBuild = b.watch(ev.Name)
case isDelete(ev.Op):
if _, ok := b.watching[ev.Name]; ok {
b.w.Remove(ev.Name)
delete(b.watching, ev.Name)
}
queueBuild = true
case isModify(ev.Op):
queueBuild = true
}
if queueBuild {
b.maybeQueue(ev.Name)
}
case err := <-b.w.Errors:
log.Println("error:", err)
case <-b.done:
return nil
}
}
}
func (b *Bob) maybeQueue(path string) {
if b.isExcluded(path) {
return
}
stat, err := os.Stat(path)
if err == nil {
mtime := stat.ModTime()
lasttime := mtimes[path]
if !mtime.Equal(lasttime) {
mtimes[path] = mtime
b.execute()
}
} else {
delete(mtimes, path)
b.execute()
}
}
func (b *Bob) stopCurVow() {
if b.curVow != nil {
b.mtx.Lock()
b.curVow.Stop()
b.mtx.Unlock()
}
}
func (b *Bob) execute() {
b.stopCurVow()
clearBuffer()
b.mtx.Lock()
// setup the first command
firstCmd := b.cmds[0]
b.curVow = vow.To(firstCmd[0], firstCmd[1:]...)
// setup the remaining commands
for i := 1; i < len(b.cmds); i++ {
cmd := b.cmds[i]
b.curVow = b.curVow.Then(cmd[0], cmd[1:]...)
}
b.curVow.Verbose = b.verbose
go b.curVow.Exec(ansicolor.NewAnsiColorWriter(os.Stdout))
b.mtx.Unlock()
}
func (b *Bob) watch(path string) bool {
var shouldBuild bool
if _, ok := b.watching[path]; ok {
return false
}
filepath.Walk(path, func(p string, fi os.FileInfo, err error) error {
if fi == nil {
return filepath.SkipDir
}
if fi.IsDir() {
if b.isExcluded(p) {
return filepath.SkipDir
}
if err := b.w.Add(p); err != nil {
return err
}
b.watching[p] = struct{}{}
} else {
shouldBuild = true
}
return nil
})
return shouldBuild
}
func (b *Bob) isExcluded(path string) bool {
// get the relative path
path = strings.TrimPrefix(path, b.watchDir+string(filepath.Separator))
for _, p := range b.ignoredItems {
if p == path {
return true
}
}
return false
}
func isCreate(op fsn.Op) bool {
return op&fsn.Create == fsn.Create
}
func isDelete(op fsn.Op) bool {
return op&fsn.Remove == fsn.Remove
}
func isModify(op fsn.Op) bool {
return op&fsn.Write == fsn.Write ||
op&fsn.Rename == fsn.Rename
}