-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpipeline.go
266 lines (238 loc) · 5.65 KB
/
pipeline.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
// Copyright 2015 Davin Hills. All rights reserved.
// MIT license. License details can be found in the LICENSE file.
package goauto
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"time"
)
// NOTE check out -gcflags=-m
// want things on the stack so not GCed
const (
batchTick = 300 * time.Millisecond // batching Duration in ms
)
// Flags to WatchRecursive to include or ignore hidden directories
const (
IgnoreHidden = true
IncludeHidden = false
)
// Flags for verbose output
const (
Verbose = true
Silent = false
)
// A Pipeline watches one or more directories for changes
type Pipeline struct {
Name string
Watches []string
Wout, Werr io.Writer
Workflows []Workflower
Verbose bool
OSX bool
watcher Watcher
recDirs map[string]bool
events <-chan ESlice
}
// NewPipeline returns a basic Pipeline with a dir to watch, output and error writers and a workflow
func NewPipeline(name string, verbose bool) *Pipeline {
p := Pipeline{Name: name, Wout: os.Stdout, Werr: os.Stderr, Verbose: verbose}
return &p
}
// Watch adds a GOPATH relative or absolute path to watch
// rejects invalid paths and ignores duplicates
func (p *Pipeline) Watch(watchDir string) (d string, err error) {
d, err = AbsPath(watchDir)
if err != nil {
if p.Verbose {
fmt.Fprintf(p.Werr, "> %v", err)
}
return
}
// Make sure we are not already watching it
for _, w := range p.Watches {
if w == d {
return
}
}
if p.Verbose && p.OSX {
fmt.Fprintf(p.Wout, "OSX watches are always recursive and do not skip directories. Adding %v recursivly\n", d)
}
p.Watches = append(p.Watches, d)
if p.watcher != nil {
err = p.watcher.Add(d)
}
return
}
// WatchRecursive adds a GOPATH relative or absolute path to watch recursivly
func (p *Pipeline) WatchRecursive(watchDir string, ignoreHidden bool) error {
if p.OSX {
_, err := p.Watch(watchDir)
return err
}
d, err := AbsPath(watchDir)
if err != nil {
return err
}
if p.recDirs == nil {
p.recDirs = make(map[string]bool)
}
p.recDirs[d] = ignoreHidden
err = filepath.Walk(d, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() {
if IsHidden(info.Name()) && ignoreHidden {
return filepath.SkipDir
}
_, err = p.Watch(path)
}
return nil
})
return nil
}
// Add adds one or more Workflows to the pipeline
func (p *Pipeline) Add(ws ...Workflower) {
for _, w := range ws {
p.Workflows = append(p.Workflows, w)
}
}
// Start begins watching for changes to files in the Watches directories
// Detected file changes will be compared with workflow regexp and if match will run the workflow tasks
func (p *Pipeline) Start() {
if p.watcher == nil {
if p.OSX {
p.watcher = NewWatchOSX()
} else {
p.watcher = NewWatchFS()
}
if p.Verbose {
p.watcher.SetVerbose(p.Wout)
}
}
if p.Wout == nil {
p.Wout = os.Stdout
}
if p.Werr == nil {
p.Werr = os.Stderr
}
if p.Name == "" {
p.Name = "<UNNAMED>"
}
if len(p.Watches) < 1 {
fmt.Fprintln(p.Werr, "Pipeline", p.Name, "is not watching anything")
}
if len(p.Workflows) < 1 {
fmt.Fprintln(p.Werr, "Pipeline", p.Name, "has no Workflows")
}
// setup the com channels
qdc := p.queryRecDir()
qwc := p.queryWorkflow()
var err error
p.events, err = p.watcher.Start(batchTick, p.Watches)
if err != nil {
fmt.Fprintln(p.Werr, err)
return
}
// block
p.distributeEvents(qdc, qwc)
}
// distributeEvents sends batched events to a list of write channels
// when finished it closes the write channels
func (p *Pipeline) distributeEvents(cs ...chan<- *Event) {
defer func() {
for _, c := range cs {
close(c)
}
}()
for {
select {
case d := <-p.events:
if d == nil || len(d) < 1 {
return
}
for _, e := range d {
for _, c := range cs {
c <- e
}
}
}
}
}
// queryWorkflow checks for file match for each workflow and if matches executes the workflow tasks
// returns a write channel that the caller should close
func (p *Pipeline) queryWorkflow() chan<- *Event {
in := make(chan *Event)
go func() {
for {
select {
case e := <-in:
if e == nil {
return
}
for _, wf := range p.Workflows {
if wf.Match(e.Path, e.Op) {
wf.Run(&TaskInfo{Src: e.Path, Tout: p.Wout, Terr: p.Werr, Verbose: p.Verbose})
}
}
}
}
}()
return in
}
// matchNewRec checks if an event is adding or renaming a directory in a recursive watch
// reruns WatchRecursive if it is
func (p *Pipeline) matchNewRec(e Event) {
dirOps := Create | Rename
fi, err := os.Stat(e.Path)
if err == nil && fi.IsDir() && dirOps&e.Op == e.Op {
h := IsHidden(e.Path)
for dir, iHidden := range p.recDirs {
if h && iHidden {
continue
}
if _, err := filepath.Rel(dir, e.Path); err == nil {
if err := p.WatchRecursive(dir, iHidden); err != nil {
fmt.Fprint(p.Wout, err)
} else if p.Verbose {
fmt.Fprintf(p.Wout, "> Detected new watch %v\n", e.Path)
}
break
}
}
}
}
// queryRecDir checks if an event is adding or renaming a directory in a recursive watch
// returns a write channel that the caller should close
func (p *Pipeline) queryRecDir() chan<- *Event {
in := make(chan *Event, 10) // bursts of events often come in, try not to slow the workflows down
go func() {
for {
select {
case e := <-in:
if e == nil {
return
}
p.matchNewRec(*e)
}
}
}()
return in
}
// Stop will discontinue watching for file changes
func (p *Pipeline) Stop() (err error) {
if p.watcher == nil {
return errors.New("Pipeline was not started or has not completed")
}
err = p.watcher.Stop()
if err != nil {
fmt.Fprintln(p.Wout, err)
}
if p.Verbose {
fmt.Fprintln(p.Wout, "> Pipeline stopped")
}
return
}