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"
|
2021-01-04 02:08:16 +02:00
|
|
|
"path/filepath"
|
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"
|
|
|
|
|
2020-08-16 20:48:19 +02:00
|
|
|
"github.com/go-task/task/v3/internal/logger"
|
2020-08-19 10:59:58 +02:00
|
|
|
"github.com/go-task/task/v3/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
|
|
|
)
|
|
|
|
|
2021-01-05 15:48:04 +02:00
|
|
|
const watchInterval = 5 * time.Second
|
2017-08-05 18:35:10 +02:00
|
|
|
|
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-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())
|
2021-01-05 16:19:34 +02:00
|
|
|
|
|
|
|
e.Compiler.ResetCache()
|
|
|
|
|
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:
|
|
|
|
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() {
|
2021-01-05 15:48:04 +02:00
|
|
|
// re-register every 5 seconds because we can have new files, but this process is expensive to run
|
2017-08-05 16:50:39 +02:00
|
|
|
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
|
|
|
}
|
2021-01-05 15:48:04 +02:00
|
|
|
time.Sleep(watchInterval)
|
2017-04-01 21:04:52 +02:00
|
|
|
}
|
2017-08-05 16:50:39 +02:00
|
|
|
}()
|
|
|
|
|
2021-01-05 15:48:04 +02:00
|
|
|
return w.Start(watchInterval)
|
2017-04-01 21:04:52 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 02:59:00 +02:00
|
|
|
func isContextError(err error) bool {
|
2020-04-29 04:56:02 +02:00
|
|
|
if taskRunErr, ok := err.(*taskRunError); ok {
|
|
|
|
err = taskRunErr.err
|
|
|
|
}
|
|
|
|
|
2018-09-17 02:59:00 +02:00
|
|
|
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 {
|
2021-01-05 15:48:04 +02:00
|
|
|
watchedFiles := w.WatchedFiles()
|
2017-04-01 21:04:52 +02:00
|
|
|
|
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 {
|
2021-01-04 02:08:16 +02:00
|
|
|
absFile, err := filepath.Abs(f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-05 15:48:04 +02:00
|
|
|
if shouldIgnoreFile(absFile) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := watchedFiles[absFile]; ok {
|
2017-08-05 16:50:39 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-01-04 02:08:16 +02:00
|
|
|
if err := w.Add(absFile); err != nil {
|
2017-04-01 21:04:52 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-01-04 02:08:16 +02:00
|
|
|
e.Logger.VerboseOutf(logger.Green, "task: watching new file: %v", absFile)
|
2017-04-01 21:04:52 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|
2021-01-05 15:48:04 +02:00
|
|
|
|
|
|
|
func shouldIgnoreFile(path string) bool {
|
|
|
|
return strings.Contains(path, "/.git") || strings.Contains(path, "/.task") || strings.Contains(path, "/node_modules")
|
|
|
|
}
|