-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
324 lines (287 loc) · 6.63 KB
/
watcher.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
package watcher
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
)
type Watcher struct {
watcher *fsnotify.Watcher
Label string
Extensions []string
IncludePatterns []*regexp.Regexp
IgnorePatterns []*regexp.Regexp
IncludeDirs []string
IgnoreDirs []string
BuildCommand string
RunCommand *string
RebuildDelay time.Duration
ChangeDetected bool
runCmd *exec.Cmd
stop chan struct{}
debug bool
}
func compilePatterns(patterns []string) []*regexp.Regexp {
compiledPatterns := make([]*regexp.Regexp, len(patterns))
for i, pattern := range patterns {
compiledPatterns[i] = regexp.MustCompile(pattern)
}
return compiledPatterns
}
func NewWatcher(label string, extensions []string, includeDirs []string, ignoreDirs []string, includePatterns []string, ignorePatterns []string, buildCommand string, runCommand *string, rebuildDelay time.Duration, debug bool) (*Watcher, error) {
// Minimum rebuild delay is 500ms
if rebuildDelay < 500 {
rebuildDelay = time.Duration(500)
}
// Default values for optional parameters
if label == "" {
addedField := false
separator := ", "
var sb strings.Builder
sb.WriteString("[")
if extensions != nil {
// if addedField {
// sb.WriteString(separator)
// }
fmt.Fprintf(&sb, "E:%v", extensions)
addedField = true
}
if includeDirs != nil {
if addedField {
sb.WriteString(separator)
}
fmt.Fprintf(&sb, "D:%v", includeDirs)
addedField = true
}
if includePatterns != nil {
if addedField {
sb.WriteString(separator)
}
fmt.Fprintf(&sb, "P:%v", includePatterns)
// addedField = true
}
sb.WriteString("]")
label = sb.String()
}
if includeDirs == nil {
includeDirs = []string{"."}
}
if ignoreDirs == nil {
ignoreDirs = []string{}
}
if includePatterns == nil {
includePatterns = []string{}
}
if ignorePatterns == nil {
ignorePatterns = []string{}
}
if debug {
var printRunCmd string
if runCommand == nil {
printRunCmd = ""
} else {
printRunCmd = *runCommand
}
log.Printf(`Creating watcher
Label: %v
Include:
- Extensions %v
- Dirs %v
- Patterns %v
Ignore:
- Dirs %v
- Patterns %v
Commands:
- Build [%v]
- Run [%v]
`,
label,
extensions, includeDirs, includePatterns,
ignoreDirs, ignorePatterns,
buildCommand, printRunCmd)
}
fsWatcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
w := &Watcher{
watcher: fsWatcher,
Label: label,
Extensions: extensions,
IncludeDirs: includeDirs,
IgnoreDirs: ignoreDirs,
IncludePatterns: compilePatterns(includePatterns),
IgnorePatterns: compilePatterns(ignorePatterns),
BuildCommand: buildCommand,
RunCommand: runCommand,
RebuildDelay: rebuildDelay,
debug: debug,
}
for _, dir := range w.IncludeDirs {
if err := w.addDirectoryRecursively(dir); err != nil {
return nil, err
}
}
w.processFileChange()
return w, nil
}
func (w *Watcher) addDirectoryRecursively(path string) error {
// Check if the directory should be ignored
for _, ignoreDir := range w.IgnoreDirs {
if ignoreDir == path {
return nil
}
}
// Check if the path matches any ignore patterns
for _, pattern := range w.IgnorePatterns {
if pattern.MatchString(path) {
return nil
}
}
// Add path, and recursively check subdirectories
if err := w.watcher.Add(path); err != nil {
return err
}
entries, err := os.ReadDir(path)
if err != nil {
return err
}
for _, entry := range entries {
fullPath := filepath.Join(path, entry.Name())
if entry.IsDir() {
if err := w.addDirectoryRecursively(fullPath); err != nil {
return err
}
}
}
return nil
}
func (w *Watcher) isValidFile(path string) bool {
// Ignore directories
info, err := os.Stat(path)
if err != nil || info.IsDir() {
return false
}
// Check if the path matches any ignore patterns
for _, pattern := range w.IgnorePatterns {
if pattern.MatchString(path) {
return false
}
}
// Check if the file matches any extensions
ext := filepath.Ext(path)
for _, e := range w.Extensions {
if ext == e {
return true
}
}
// Check if the file matches any include patterns
for _, pattern := range w.IncludePatterns {
if pattern.MatchString(path) {
return true
}
}
return false
}
func (w *Watcher) executeBuildCommand(command string) error {
if w.debug {
log.Printf("%v - BUILD: %v", w.Label, command)
}
cmd := exec.Command("bash", "-c", command)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func (w *Watcher) executeRunCommand(command string) error {
if w.debug {
log.Printf("%v - RUN: %v", w.Label, command)
}
cmd := exec.Command("bash", "-c", command)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
w.runCmd = cmd
err := w.runCmd.Start()
if err != nil {
return err
}
go func() {
err := w.runCmd.Wait()
if err != nil {
exitError, ok := err.(*exec.ExitError)
if w.debug && ok && exitError.ProcessState.Sys().(syscall.WaitStatus).Signal() == syscall.SIGKILL {
log.Printf("%v - KILLED: %v", w.Label, command)
return
} else {
log.Printf("%v - Run command failed: %v", w.Label, err)
os.Exit(1)
}
}
}()
return nil
}
func (w *Watcher) processFileChange() {
if err := w.executeBuildCommand(w.BuildCommand); err != nil {
log.Printf("%v - Build command failed: %v", w.Label, err)
}
if w.RunCommand != nil {
if w.runCmd != nil {
if w.runCmd.ProcessState == nil || !w.runCmd.ProcessState.Exited() {
if err := w.runCmd.Process.Kill(); err != nil {
log.Printf("%v - Error killing previous run command: %v", w.Label, err)
os.Exit(1)
}
}
w.runCmd = nil
}
if err := w.executeRunCommand(*w.RunCommand); err != nil {
log.Printf("%v - Run command failed to start: %v", w.Label, err)
}
}
}
func debounce(interval time.Duration, fn func()) func() {
var (
timer *time.Timer
mutex sync.Mutex
)
return func() {
mutex.Lock()
defer mutex.Unlock()
if timer != nil {
timer.Stop()
}
timer = time.AfterFunc(interval, fn)
}
}
func (w *Watcher) Start() {
debouncedRebuild := debounce(w.RebuildDelay*time.Millisecond, w.processFileChange)
defer w.watcher.Close()
for {
select {
case event, ok := <-w.watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
if w.isValidFile(event.Name) {
debouncedRebuild()
}
}
case err, ok := <-w.watcher.Errors:
if !ok {
return
}
log.Printf("Watcher error: %v", err)
os.Exit(1)
}
}
}
func (w *Watcher) Stop() {
close(w.stop)
}