2017-04-01 21:04:52 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
2017-04-13 01:53:41 +02:00
|
|
|
"context"
|
2018-09-17 02:59:00 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2017-04-01 21:04:52 +02:00
|
|
|
"strings"
|
2018-09-17 02:59:00 +02:00
|
|
|
"syscall"
|
2017-04-01 21:04:52 +02:00
|
|
|
"time"
|
|
|
|
|
2019-05-26 23:36:39 +02:00
|
|
|
"github.com/go-task/task/v2/internal/logger"
|
2018-11-05 01:23:35 +02:00
|
|
|
"github.com/go-task/task/v2/internal/taskfile"
|
2017-04-01 21:04:52 +02:00
|
|
|
"github.com/mattn/go-zglob"
|
2017-08-05 16:50:39 +02:00
|
|
|
"github.com/radovskyb/watcher"
|
2017-04-01 21:04:52 +02:00
|
|
|
)
|
|
|
|
|
2017-08-05 18:35:10 +02:00
|
|
|
var watchIgnoredDirs = []string{
|
|
|
|
".git",
|
|
|
|
"node_modules",
|
|
|
|
}
|
|
|
|
|
2017-06-04 21:02:04 +02:00
|
|
|
// watchTasks start watching the given tasks
|
2018-02-17 18:22:18 +02:00
|
|
|
func (e *Executor) watchTasks(calls ...taskfile.Call) error {
|
2017-09-07 18:57:06 +02:00
|
|
|
tasks := make([]string, len(calls))
|
|
|
|
for i, c := range calls {
|
|
|
|
tasks[i] = c.Task
|
|
|
|
}
|
2019-05-26 23:36:39 +02:00
|
|
|
e.Logger.Errf(logger.Green, "task: Started watching for tasks: %s", strings.Join(tasks, ", "))
|
2017-04-01 21:04:52 +02:00
|
|
|
|
2017-08-04 17:48:15 +02:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-09-07 18:57:06 +02:00
|
|
|
for _, c := range calls {
|
|
|
|
c := c
|
2017-08-04 17:48:15 +02:00
|
|
|
go func() {
|
2017-09-07 18:57:06 +02:00
|
|
|
if err := e.RunTask(ctx, c); err != nil && !isContextError(err) {
|
2019-05-26 23:36:39 +02:00
|
|
|
e.Logger.Errf(logger.Red, "%v", err)
|
2017-08-04 17:48:15 +02:00
|
|
|
}
|
|
|
|
}()
|
2017-04-01 21:04:52 +02:00
|
|
|
}
|
|
|
|
|
2017-08-05 16:50:39 +02:00
|
|
|
w := watcher.New()
|
|
|
|
defer w.Close()
|
|
|
|
w.SetMaxEvents(1)
|
2017-08-05 18:35:10 +02:00
|
|
|
if err := w.Ignore(watchIgnoredDirs...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-01 21:04:52 +02:00
|
|
|
|
2018-09-17 02:59:00 +02:00
|
|
|
closeOnInterrupt(w)
|
|
|
|
|
2017-04-01 21:04:52 +02:00
|
|
|
go func() {
|
|
|
|
for {
|
2017-08-05 16:50:39 +02:00
|
|
|
select {
|
|
|
|
case event := <-w.Event:
|
2019-05-26 23:36:39 +02:00
|
|
|
e.Logger.VerboseErrf(logger.Magenta, "task: received watch event: %v", event)
|
2017-08-05 16:50:39 +02:00
|
|
|
|
|
|
|
cancel()
|
|
|
|
ctx, cancel = context.WithCancel(context.Background())
|
2017-09-07 18:57:06 +02:00
|
|
|
for _, c := range calls {
|
|
|
|
c := c
|
2017-08-05 16:50:39 +02:00
|
|
|
go func() {
|
2017-09-07 18:57:06 +02:00
|
|
|
if err := e.RunTask(ctx, c); err != nil && !isContextError(err) {
|
2019-05-26 23:36:39 +02:00
|
|
|
e.Logger.Errf(logger.Red, "%v", err)
|
2017-08-05 16:50:39 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
case err := <-w.Error:
|
|
|
|
switch err {
|
|
|
|
case watcher.ErrWatchedFileDeleted:
|
|
|
|
go func() {
|
|
|
|
w.TriggerEvent(watcher.Remove, nil)
|
|
|
|
}()
|
|
|
|
default:
|
2019-05-26 23:36:39 +02:00
|
|
|
e.Logger.Errf(logger.Red, "%v", err)
|
2017-08-05 16:50:39 +02:00
|
|
|
}
|
|
|
|
case <-w.Closed:
|
2018-09-17 02:59:00 +02:00
|
|
|
cancel()
|
2017-08-05 16:50:39 +02:00
|
|
|
return
|
2017-04-01 21:04:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-08-05 16:50:39 +02:00
|
|
|
go func() {
|
|
|
|
// re-register each second because we can have new files
|
|
|
|
for {
|
2017-09-30 20:19:58 +02:00
|
|
|
if err := e.registerWatchedFiles(w, calls...); err != nil {
|
2019-05-26 23:36:39 +02:00
|
|
|
e.Logger.Errf(logger.Red, "%v", err)
|
2017-04-01 21:04:52 +02:00
|
|
|
}
|
2017-08-05 16:50:39 +02:00
|
|
|
time.Sleep(time.Second)
|
2017-04-01 21:04:52 +02:00
|
|
|
}
|
2017-08-05 16:50:39 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
return w.Start(time.Second)
|
2017-04-01 21:04:52 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 02:59:00 +02:00
|
|
|
func isContextError(err error) bool {
|
|
|
|
return err == context.Canceled || err == context.DeadlineExceeded
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeOnInterrupt(w *watcher.Watcher) {
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, os.Interrupt, os.Kill, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
<-ch
|
|
|
|
w.Close()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
func (e *Executor) registerWatchedFiles(w *watcher.Watcher, calls ...taskfile.Call) error {
|
2017-08-05 16:50:39 +02:00
|
|
|
oldWatchedFiles := make(map[string]struct{})
|
|
|
|
for f := range w.WatchedFiles() {
|
|
|
|
oldWatchedFiles[f] = struct{}{}
|
|
|
|
}
|
2017-04-01 21:04:52 +02:00
|
|
|
|
2017-08-05 16:50:39 +02:00
|
|
|
for f := range oldWatchedFiles {
|
|
|
|
if err := w.Remove(f); err != nil {
|
2017-04-01 21:04:52 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
var registerTaskFiles func(taskfile.Call) error
|
|
|
|
registerTaskFiles = func(c taskfile.Call) error {
|
2017-09-30 20:19:58 +02:00
|
|
|
task, err := e.CompiledTask(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-07-02 20:30:50 +02:00
|
|
|
}
|
2017-08-05 16:50:39 +02:00
|
|
|
|
|
|
|
for _, d := range task.Deps {
|
2018-02-17 18:22:18 +02:00
|
|
|
if err := registerTaskFiles(taskfile.Call{Task: d.Task, Vars: d.Vars}); err != nil {
|
2017-08-05 16:50:39 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-04-01 21:04:52 +02:00
|
|
|
}
|
2017-09-30 20:19:58 +02:00
|
|
|
for _, c := range task.Cmds {
|
|
|
|
if c.Task != "" {
|
2018-02-17 18:22:18 +02:00
|
|
|
if err := registerTaskFiles(taskfile.Call{Task: c.Task, Vars: c.Vars}); err != nil {
|
2017-09-30 20:19:58 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-05 16:50:39 +02:00
|
|
|
|
2017-04-01 21:04:52 +02:00
|
|
|
for _, s := range task.Sources {
|
|
|
|
files, err := zglob.Glob(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
2017-08-05 16:50:39 +02:00
|
|
|
if _, ok := oldWatchedFiles[f]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2017-04-01 21:04:52 +02:00
|
|
|
if err := w.Add(f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-05 16:50:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-30 20:19:58 +02:00
|
|
|
for _, c := range calls {
|
|
|
|
if err := registerTaskFiles(c); err != nil {
|
2017-08-05 16:50:39 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-04-01 21:04:52 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|