2017-04-01 21:04:52 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
2017-04-13 01:53:41 +02:00
|
|
|
"context"
|
2017-04-01 21:04:52 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
"github.com/mattn/go-zglob"
|
|
|
|
)
|
|
|
|
|
2017-06-04 21:02:04 +02:00
|
|
|
// watchTasks start watching the given tasks
|
|
|
|
func (e *Executor) watchTasks(args ...string) error {
|
2017-07-01 20:05:51 +02:00
|
|
|
e.printfln("task: Started watching for tasks: %s", strings.Join(args, ", "))
|
2017-04-01 21:04:52 +02:00
|
|
|
|
|
|
|
// run tasks on init
|
|
|
|
for _, a := range args {
|
2017-07-02 20:30:50 +02:00
|
|
|
if err := e.RunTask(context.Background(), a, nil); err != nil {
|
2017-07-01 20:05:51 +02:00
|
|
|
e.println(err)
|
2017-04-01 21:04:52 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2017-06-04 21:02:04 +02:00
|
|
|
if err := e.registerWatchedFiles(watcher, args); err != nil {
|
2017-07-01 20:05:51 +02:00
|
|
|
e.printfln("Error watching files: %v", err)
|
2017-04-01 21:04:52 +02:00
|
|
|
}
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-watcher.Events:
|
|
|
|
for _, a := range args {
|
2017-07-02 20:30:50 +02:00
|
|
|
if err := e.RunTask(context.Background(), a, nil); err != nil {
|
2017-07-01 20:05:51 +02:00
|
|
|
e.println(err)
|
2017-04-01 21:04:52 +02:00
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case err := <-watcher.Errors:
|
2017-07-01 20:05:51 +02:00
|
|
|
e.println(err)
|
2017-04-01 21:04:52 +02:00
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-04 21:02:04 +02:00
|
|
|
func (e *Executor) registerWatchedFiles(w *fsnotify.Watcher, args []string) error {
|
|
|
|
oldWatchingFiles := e.watchingFiles
|
|
|
|
e.watchingFiles = make(map[string]struct{}, len(oldWatchingFiles))
|
2017-04-01 21:04:52 +02:00
|
|
|
|
|
|
|
for k := range oldWatchingFiles {
|
|
|
|
if err := w.Remove(k); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, a := range args {
|
2017-06-04 21:02:04 +02:00
|
|
|
task, ok := e.Tasks[a]
|
2017-04-01 21:04:52 +02:00
|
|
|
if !ok {
|
|
|
|
return &taskNotFoundError{a}
|
|
|
|
}
|
2017-07-02 20:30:50 +02:00
|
|
|
deps := make([]string, len(task.Deps))
|
|
|
|
for i, d := range task.Deps {
|
|
|
|
deps[i] = d.Task
|
|
|
|
}
|
|
|
|
if err := e.registerWatchedFiles(w, deps); err != nil {
|
2017-04-01 21:04:52 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, s := range task.Sources {
|
|
|
|
files, err := zglob.Glob(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
if err := w.Add(f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-04 21:02:04 +02:00
|
|
|
e.watchingFiles[f] = struct{}{}
|
2017-04-01 21:04:52 +02:00
|
|
|
|
|
|
|
// run if is new file
|
|
|
|
if oldWatchingFiles != nil {
|
|
|
|
if _, ok := oldWatchingFiles[f]; !ok {
|
|
|
|
w.Events <- fsnotify.Event{Name: f, Op: fsnotify.Create}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|