-
Notifications
You must be signed in to change notification settings - Fork 31
/
task.go
283 lines (244 loc) · 6.77 KB
/
task.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
package godo
import (
"fmt"
"io"
"path/filepath"
"strings"
"sync"
"time"
"github.com/mgutz/minimist"
"github.com/mgutz/str"
"gopkg.in/godo.v2/glob"
"gopkg.in/godo.v2/util"
"gopkg.in/godo.v2/watcher"
)
// TaskFunction is the signature of the function used to define a type.
// type TaskFunc func(string, ...interface{}) *Task
// type UseFunc func(string, interface{})
// A Task is an operation performed on a user's project directory.
type Task struct {
Name string
description string
Handler Handler
dependencies Series
argm minimist.ArgMap
// Watches are watches files. On change the task is rerun. For example `**/*.less`
// Usually Watches and Sources are the same.
// WatchFiles []*FileAsset
// WatchGlobs []string
// WatchRegexps []*RegexpInfo
// computed based on dependencies
EffectiveWatchRegexps []*glob.RegexpInfo
EffectiveWatchGlobs []string
// Complete indicates whether this task has already ran. This flag is
// ignored in watch mode.
Complete bool
debounce time.Duration
RunOnce bool
SrcFiles []*glob.FileAsset
SrcGlobs []string
SrcRegexps []*glob.RegexpInfo
DestFiles []*glob.FileAsset
DestGlobs []string
DestRegexps []*glob.RegexpInfo
// used when a file event is received between debounce intervals, the file event
// will queue itself and set this flag and force debounce to run it
// when time has elapsed
sync.Mutex
ignoreEvents bool
}
// NewTask creates a new Task.
func NewTask(name string, argm minimist.ArgMap) *Task {
runOnce := false
if strings.HasSuffix(name, "?") {
runOnce = true
name = str.ChompRight(name, "?")
}
return &Task{Name: name, RunOnce: runOnce, dependencies: Series{}, argm: argm}
}
// Expands glob patterns.
func (task *Task) expandGlobs() {
// runs once lazily
if len(task.SrcFiles) > 0 {
return
}
files, regexps, err := glob.Glob(task.SrcGlobs)
if err != nil {
util.Error(task.Name, "%v", err)
return
}
task.SrcRegexps = regexps
task.SrcFiles = files
if len(task.DestGlobs) > 0 {
files, regexps, err := glob.Glob(task.DestGlobs)
if err != nil {
util.Error(task.Name, "%v", err)
return
}
task.DestRegexps = regexps
task.DestFiles = files
}
}
// Run runs all the dependencies of this task and when they have completed,
// runs this task.
func (task *Task) Run() error {
if !watching && task.Complete {
util.Debug(task.Name, "Already ran\n")
return nil
}
return task.RunWithEvent(task.Name, nil)
}
// isWatchedFile determines if a FileEvent's file is a watched file
func (task *Task) isWatchedFile(path string) bool {
filename, err := filepath.Rel(wd, path)
if err != nil {
return false
}
filename = filepath.ToSlash(filename)
//util.Debug("task", "checking for match %s\n", filename)
matched := false
for _, info := range task.EffectiveWatchRegexps {
if info.Negate {
if matched {
matched = !info.MatchString(filename)
//util.Debug("task", "negated match? %s %s\n", filename, matched)
continue
}
} else if info.MatchString(filename) {
matched = true
//util.Debug("task", "matched %s %s\n", filename, matched)
continue
}
}
return matched
}
// RunWithEvent runs this task when triggered from a watch.
// *e* FileEvent contains information about the file/directory which changed
// in watch mode.
func (task *Task) RunWithEvent(logName string, e *watcher.FileEvent) (err error) {
if task.RunOnce && task.Complete {
util.Debug(task.Name, "Already ran\n")
return nil
}
task.expandGlobs()
if !task.shouldRun(e) {
util.Info(logName, "up-to-date 0ms\n")
return nil
}
start := time.Now()
if len(task.SrcGlobs) > 0 && len(task.SrcFiles) == 0 {
util.Error("task", "\""+task.Name+"\" '%v' did not match any files\n", task.SrcGlobs)
}
// Run this task only if the file matches watch Regexps
rebuilt := ""
if e != nil {
rebuilt = "rebuilt "
if !task.isWatchedFile(e.Path) && len(task.SrcGlobs) > 0 {
return nil
}
if verbose {
util.Debug(logName, "%s\n", e.String())
}
}
log := true
if task.Handler != nil {
context := Context{Task: task, Args: task.argm, FileEvent: e}
defer func() {
if p := recover(); p != nil {
sp, ok := p.(*softPanic)
if !ok {
panic(p)
}
err = fmt.Errorf("%q: %s", logName, sp)
}
}()
task.Handler.Handle(&context)
if context.Error != nil {
return fmt.Errorf("%q: %s", logName, context.Error.Error())
}
} else if len(task.dependencies) > 0 {
// no need to log if just dependency
log = false
} else {
util.Info(task.Name, "Ignored. Task does not have a handler or dependencies.\n")
return nil
}
if log {
if rebuilt != "" {
util.InfoColorful(logName, "%s%vms\n", rebuilt, time.Since(start).Nanoseconds()/1e6)
} else {
util.Info(logName, "%s%vms\n", rebuilt, time.Since(start).Nanoseconds()/1e6)
}
}
task.Complete = true
return nil
}
// DependencyNames gets the flattened dependency names.
func (task *Task) DependencyNames() []string {
if len(task.dependencies) == 0 {
return nil
}
deps := []string{}
for _, dep := range task.dependencies {
switch d := dep.(type) {
default:
panic("dependencies can only be Serial or Parallel")
case Series:
deps = append(deps, d.names()...)
case Parallel:
deps = append(deps, d.names()...)
case S:
deps = append(deps, Series(d).names()...)
case P:
deps = append(deps, Parallel(d).names()...)
}
}
return deps
}
func (task *Task) dump(buf io.Writer, indent string) {
fmt.Fprintln(buf, indent, task.Name)
fmt.Fprintln(buf, indent+indent, "EffectiveWatchGlobs", task.EffectiveWatchGlobs)
fmt.Fprintln(buf, indent+indent, "SrcFiles", task.SrcFiles)
fmt.Fprintln(buf, indent+indent, "SrcGlobs", task.SrcGlobs)
}
func (task *Task) shouldRun(e *watcher.FileEvent) bool {
if e == nil || len(task.SrcFiles) == 0 {
return true
} else if !task.isWatchedFile(e.Path) {
// fmt.Printf("received a file so it should return immediately\n")
return false
}
// lazily expand globs
task.expandGlobs()
if len(task.SrcFiles) == 0 || len(task.DestFiles) == 0 {
// fmt.Printf("no source files %s %#v\n", task.Name, task.SrcFiles)
// fmt.Printf("no source files %s %#v\n", task.Name, task.DestFiles)
return true
}
// TODO figure out intelligent way to cache this instead of stating
// each time
for _, src := range task.SrcFiles {
// refresh stat
src.Stat()
for _, dest := range task.DestFiles {
// refresh stat
dest.Stat()
if filepath.Base(src.Path) == "foo.txt" {
fmt.Printf("src %s %#v\n", src.Path, src.ModTime().UnixNano())
fmt.Printf("dest %s %#v\n", dest.Path, dest.ModTime().UnixNano())
}
if src.ModTime().After(dest.ModTime()) {
return true
}
}
}
fmt.Printf("FileEvent ignored %#v\n", e)
return false
}
func (task *Task) debounceValue() time.Duration {
if task.debounce == 0 {
// use default Debounce
return Debounce
}
return task.debounce
}