-
-
Notifications
You must be signed in to change notification settings - Fork 619
/
watch.go
196 lines (169 loc) · 4.12 KB
/
watch.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
package task
import (
"context"
"fmt"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/radovskyb/watcher"
"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/fingerprint"
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/taskfile/ast"
)
const defaultWatchInterval = 5 * time.Second
// watchTasks start watching the given tasks
func (e *Executor) watchTasks(calls ...*ast.Call) error {
tasks := make([]string, len(calls))
for i, c := range calls {
tasks[i] = c.Task
}
e.Logger.Errf(logger.Green, "task: Started watching for tasks: %s\n", strings.Join(tasks, ", "))
ctx, cancel := context.WithCancel(context.Background())
for _, c := range calls {
c := c
go func() {
if err := e.RunTask(ctx, c); err != nil && !isContextError(err) {
e.Logger.Errf(logger.Red, "%v\n", err)
}
}()
}
var watchInterval time.Duration
switch {
case e.Interval != 0:
watchInterval = e.Interval
case e.Taskfile.Interval != 0:
watchInterval = e.Taskfile.Interval
default:
watchInterval = defaultWatchInterval
}
e.Logger.VerboseOutf(logger.Green, "task: Watching for changes every %v\n", watchInterval)
w := watcher.New()
defer w.Close()
w.SetMaxEvents(1)
closeOnInterrupt(w)
go func() {
for {
select {
case event := <-w.Event:
e.Logger.VerboseErrf(logger.Magenta, "task: received watch event: %v\n", event)
cancel()
ctx, cancel = context.WithCancel(context.Background())
e.Compiler.ResetCache()
for _, c := range calls {
c := c
go func() {
if err := e.RunTask(ctx, c); err != nil && !isContextError(err) {
e.Logger.Errf(logger.Red, "%v\n", err)
}
}()
}
case err := <-w.Error:
switch err {
case watcher.ErrWatchedFileDeleted:
default:
e.Logger.Errf(logger.Red, "%v\n", err)
}
case <-w.Closed:
cancel()
return
}
}
}()
go func() {
// re-register every 5 seconds because we can have new files, but this process is expensive to run
for {
if err := e.registerWatchedFiles(w, calls...); err != nil {
e.Logger.Errf(logger.Red, "%v\n", err)
}
time.Sleep(watchInterval)
}
}()
return w.Start(watchInterval)
}
func isContextError(err error) bool {
if taskRunErr, ok := err.(*errors.TaskRunError); ok {
err = taskRunErr.Err
}
return err == context.Canceled || err == context.DeadlineExceeded
}
func closeOnInterrupt(w *watcher.Watcher) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
go func() {
<-ch
w.Close()
}()
}
func (e *Executor) registerWatchedFiles(w *watcher.Watcher, calls ...*ast.Call) error {
watchedFiles := w.WatchedFiles()
var registerTaskFiles func(*ast.Call) error
registerTaskFiles = func(c *ast.Call) error {
task, err := e.CompiledTask(c)
if err != nil {
return err
}
for _, d := range task.Deps {
if err := registerTaskFiles(&ast.Call{Task: d.Task, Vars: d.Vars}); err != nil {
return err
}
}
for _, c := range task.Cmds {
if c.Task != "" {
if err := registerTaskFiles(&ast.Call{Task: c.Task, Vars: c.Vars}); err != nil {
return err
}
}
}
globs, err := fingerprint.Globs(task.Dir, task.Sources)
if err != nil {
return err
}
for _, s := range globs {
files, err := fingerprint.Glob(task.Dir, s)
if err != nil {
return fmt.Errorf("task: %s: %w", s, err)
}
for _, f := range files {
absFile, err := filepath.Abs(f)
if err != nil {
return err
}
if ShouldIgnoreFile(absFile) {
continue
}
if _, ok := watchedFiles[absFile]; ok {
continue
}
if err := w.Add(absFile); err != nil {
return err
}
e.Logger.VerboseOutf(logger.Green, "task: watching new file: %v\n", absFile)
}
}
return nil
}
for _, c := range calls {
if err := registerTaskFiles(c); err != nil {
return err
}
}
return nil
}
func ShouldIgnoreFile(path string) bool {
ignorePaths := []string{
"/.task",
"/.git",
"/.hg",
"/node_modules",
}
for _, p := range ignorePaths {
if strings.Contains(path, fmt.Sprintf("%s/", p)) || strings.HasSuffix(path, p) {
return true
}
}
return false
}