1
0
mirror of https://github.com/go-task/task.git synced 2024-12-04 10:24:45 +02:00
task/watch.go

168 lines
3.5 KiB
Go
Raw Normal View History

2017-04-01 21:04:52 +02:00
package task
import (
"context"
"os"
"os/signal"
"path/filepath"
2017-04-01 21:04:52 +02:00
"strings"
"syscall"
2017-04-01 21:04:52 +02:00
"time"
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/taskfile"
2017-04-01 21:04:52 +02:00
"github.com/mattn/go-zglob"
"github.com/radovskyb/watcher"
2017-04-01 21:04:52 +02:00
)
var watchIgnoredDirs = []string{
".git",
"node_modules",
}
// watchTasks start watching the given tasks
func (e *Executor) watchTasks(calls ...taskfile.Call) error {
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
ctx, cancel := context.WithCancel(context.Background())
for _, c := range calls {
c := c
go func() {
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-04-01 21:04:52 +02:00
}
w := watcher.New()
defer w.Close()
w.SetMaxEvents(1)
if err := w.Ignore(watchIgnoredDirs...); err != nil {
cancel()
return err
}
2017-04-01 21:04:52 +02:00
closeOnInterrupt(w)
2017-04-01 21:04:52 +02:00
go func() {
for {
select {
case event := <-w.Event:
2019-05-26 23:36:39 +02:00
e.Logger.VerboseErrf(logger.Magenta, "task: received watch event: %v", event)
cancel()
ctx, cancel = context.WithCancel(context.Background())
for _, c := range calls {
c := c
go func() {
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)
}
}()
}
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)
}
case <-w.Closed:
cancel()
return
2017-04-01 21:04:52 +02:00
}
}
}()
go func() {
2021-01-04 06:26:09 +02:00
// re-register every 20 seconds because we can have new files, but this process is expensive to run
for {
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-04 06:26:09 +02:00
time.Sleep(time.Second * 20)
2017-04-01 21:04:52 +02:00
}
}()
return w.Start(time.Second)
2017-04-01 21:04:52 +02:00
}
func isContextError(err error) bool {
if taskRunErr, ok := err.(*taskRunError); ok {
err = taskRunErr.err
}
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()
}()
}
func (e *Executor) registerWatchedFiles(w *watcher.Watcher, calls ...taskfile.Call) error {
oldWatchedFiles := make(map[string]struct{})
for f := range w.WatchedFiles() {
oldWatchedFiles[f] = struct{}{}
}
2017-04-01 21:04:52 +02:00
var registerTaskFiles func(taskfile.Call) error
registerTaskFiles = func(c taskfile.Call) error {
task, err := e.CompiledTask(c)
if err != nil {
return err
2017-07-02 20:30:50 +02:00
}
for _, d := range task.Deps {
if err := registerTaskFiles(taskfile.Call{Task: d.Task, Vars: d.Vars}); err != nil {
return err
}
2017-04-01 21:04:52 +02:00
}
for _, c := range task.Cmds {
if c.Task != "" {
if err := registerTaskFiles(taskfile.Call{Task: c.Task, Vars: c.Vars}); err != nil {
return err
}
}
}
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 {
absFile, err := filepath.Abs(f)
if err != nil {
return err
}
if _, ok := oldWatchedFiles[absFile]; ok {
continue
}
if err := w.Add(absFile); err != nil {
2017-04-01 21:04:52 +02:00
return err
}
e.Logger.VerboseOutf(logger.Green, "task: watching new file: %v", absFile)
2017-04-01 21:04:52 +02:00
}
}
return nil
}
for _, c := range calls {
if err := registerTaskFiles(c); err != nil {
return err
}
2017-04-01 21:04:52 +02:00
}
return nil
}