2017-02-27 14:48:50 +02:00
|
|
|
package task
|
2017-02-27 01:43:50 +02:00
|
|
|
|
|
|
|
import (
|
2017-04-22 20:46:29 +02:00
|
|
|
"bytes"
|
2017-04-13 01:32:56 +02:00
|
|
|
"context"
|
2017-03-06 14:48:56 +02:00
|
|
|
"fmt"
|
2017-07-01 20:05:51 +02:00
|
|
|
"io"
|
2017-02-27 01:43:50 +02:00
|
|
|
"os"
|
2017-06-16 16:24:01 +02:00
|
|
|
"path/filepath"
|
2017-03-07 09:01:20 +02:00
|
|
|
"strings"
|
2017-07-06 02:03:59 +02:00
|
|
|
"sync"
|
2017-07-08 18:33:55 +02:00
|
|
|
"sync/atomic"
|
2017-02-27 01:43:50 +02:00
|
|
|
|
2017-03-12 22:18:59 +02:00
|
|
|
"github.com/go-task/task/execext"
|
|
|
|
|
2017-04-13 01:39:52 +02:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-02-27 01:43:50 +02:00
|
|
|
)
|
|
|
|
|
2017-06-04 21:02:04 +02:00
|
|
|
const (
|
2017-02-28 14:50:40 +02:00
|
|
|
// TaskFilePath is the default Taskfile
|
2017-02-28 18:13:48 +02:00
|
|
|
TaskFilePath = "Taskfile"
|
2017-07-08 18:33:55 +02:00
|
|
|
// MaximumTaskCall is the max number of times a task can be called.
|
|
|
|
// This exists to prevent infinite loops on cyclic dependencies
|
|
|
|
MaximumTaskCall = 100
|
2017-06-04 21:02:04 +02:00
|
|
|
)
|
2017-02-27 02:18:53 +02:00
|
|
|
|
2017-06-04 21:02:04 +02:00
|
|
|
// Executor executes a Taskfile
|
|
|
|
type Executor struct {
|
2017-07-06 01:55:50 +02:00
|
|
|
Tasks Tasks
|
|
|
|
Dir string
|
|
|
|
Force bool
|
|
|
|
Watch bool
|
|
|
|
Verbose bool
|
2017-03-02 01:21:36 +02:00
|
|
|
|
2017-07-01 20:05:51 +02:00
|
|
|
Stdin io.Reader
|
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
|
|
|
|
2017-07-06 01:07:24 +02:00
|
|
|
taskvars Vars
|
2017-06-04 21:02:04 +02:00
|
|
|
watchingFiles map[string]struct{}
|
2017-07-06 02:03:59 +02:00
|
|
|
|
|
|
|
dynamicCache Vars
|
|
|
|
muDynamicCache sync.Mutex
|
2017-06-04 21:02:04 +02:00
|
|
|
}
|
2017-02-27 01:43:50 +02:00
|
|
|
|
2017-07-06 01:07:24 +02:00
|
|
|
// Vars is a string[string] variables map
|
|
|
|
type Vars map[string]string
|
|
|
|
|
2017-06-04 21:41:38 +02:00
|
|
|
// Tasks representas a group of tasks
|
|
|
|
type Tasks map[string]*Task
|
|
|
|
|
2017-02-28 14:50:40 +02:00
|
|
|
// Task represents a task
|
2017-02-27 01:43:50 +02:00
|
|
|
type Task struct {
|
2017-07-02 20:30:50 +02:00
|
|
|
Cmds []*Cmd
|
|
|
|
Deps []*Dep
|
2017-03-06 23:25:50 +02:00
|
|
|
Desc string
|
2017-02-27 22:03:25 +02:00
|
|
|
Sources []string
|
|
|
|
Generates []string
|
2017-05-17 19:37:11 +02:00
|
|
|
Status []string
|
2017-03-01 15:33:10 +02:00
|
|
|
Dir string
|
2017-07-06 01:07:24 +02:00
|
|
|
Vars Vars
|
2017-03-02 21:19:25 +02:00
|
|
|
Set string
|
2017-07-06 01:07:24 +02:00
|
|
|
Env Vars
|
2017-07-08 18:33:55 +02:00
|
|
|
|
|
|
|
callCount int32
|
2017-02-27 01:43:50 +02:00
|
|
|
}
|
|
|
|
|
2017-02-28 14:50:40 +02:00
|
|
|
// Run runs Task
|
2017-06-04 21:02:04 +02:00
|
|
|
func (e *Executor) Run(args ...string) error {
|
2017-07-01 20:05:51 +02:00
|
|
|
if e.Stdin == nil {
|
|
|
|
e.Stdin = os.Stdin
|
|
|
|
}
|
|
|
|
if e.Stdout == nil {
|
|
|
|
e.Stdout = os.Stdout
|
|
|
|
}
|
|
|
|
if e.Stderr == nil {
|
|
|
|
e.Stderr = os.Stderr
|
|
|
|
}
|
|
|
|
|
2017-07-06 02:03:59 +02:00
|
|
|
if e.dynamicCache == nil {
|
|
|
|
e.dynamicCache = make(Vars, 10)
|
|
|
|
}
|
|
|
|
|
2017-03-19 20:18:18 +02:00
|
|
|
// check if given tasks exist
|
|
|
|
for _, a := range args {
|
2017-06-04 21:02:04 +02:00
|
|
|
if _, ok := e.Tasks[a]; !ok {
|
|
|
|
// FIXME: move to the main package
|
|
|
|
e.printExistingTasksHelp()
|
|
|
|
return &taskNotFoundError{taskName: a}
|
2017-03-19 20:18:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-04 21:02:04 +02:00
|
|
|
if e.Watch {
|
|
|
|
if err := e.watchTasks(args...); err != nil {
|
|
|
|
return err
|
2017-04-01 21:04:52 +02:00
|
|
|
}
|
2017-06-04 21:02:04 +02:00
|
|
|
return nil
|
2017-04-01 21:04:52 +02:00
|
|
|
}
|
|
|
|
|
2017-02-27 15:13:06 +02:00
|
|
|
for _, a := range args {
|
2017-07-08 19:34:17 +02:00
|
|
|
if err := e.RunTask(context.Background(), Call{Task: a}); err != nil {
|
2017-06-04 21:02:04 +02:00
|
|
|
return err
|
2017-02-27 15:13:06 +02:00
|
|
|
}
|
2017-02-27 01:43:50 +02:00
|
|
|
}
|
2017-06-04 21:02:04 +02:00
|
|
|
return nil
|
2017-02-27 02:18:53 +02:00
|
|
|
}
|
2017-02-27 01:43:50 +02:00
|
|
|
|
2017-02-28 14:50:40 +02:00
|
|
|
// RunTask runs a task by its name
|
2017-07-08 19:34:17 +02:00
|
|
|
func (e *Executor) RunTask(ctx context.Context, call Call) error {
|
|
|
|
t, ok := e.Tasks[call.Task]
|
2017-02-27 01:43:50 +02:00
|
|
|
if !ok {
|
2017-07-08 19:34:17 +02:00
|
|
|
return &taskNotFoundError{call.Task}
|
2017-02-27 01:43:50 +02:00
|
|
|
}
|
|
|
|
|
2017-07-08 18:33:55 +02:00
|
|
|
if atomic.AddInt32(&t.callCount, 1) >= MaximumTaskCall {
|
2017-07-08 19:34:17 +02:00
|
|
|
return &MaximumTaskCallExceededError{task: call.Task}
|
2017-07-08 18:33:55 +02:00
|
|
|
}
|
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
if err := e.runDeps(ctx, call); err != nil {
|
2017-03-16 01:19:29 +02:00
|
|
|
return err
|
2017-02-27 01:43:50 +02:00
|
|
|
}
|
2017-03-05 11:15:49 +02:00
|
|
|
|
2017-06-04 21:02:04 +02:00
|
|
|
if !e.Force {
|
2017-07-08 19:34:17 +02:00
|
|
|
upToDate, err := e.isTaskUpToDate(ctx, call)
|
2017-05-01 00:32:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if upToDate {
|
2017-07-08 19:34:17 +02:00
|
|
|
e.printfln(`task: Task "%s" is up to date`, call.Task)
|
2017-05-01 00:32:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-05 11:15:49 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 20:52:58 +02:00
|
|
|
for i := range t.Cmds {
|
2017-07-08 19:34:17 +02:00
|
|
|
if err := e.runCommand(ctx, call, i); err != nil {
|
|
|
|
return &taskRunError{call.Task, err}
|
2017-02-27 01:43:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-27 15:07:54 +02:00
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
func (e *Executor) runDeps(ctx context.Context, call Call) error {
|
2017-04-13 01:53:41 +02:00
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
2017-07-08 19:34:17 +02:00
|
|
|
t := e.Tasks[call.Task]
|
2017-03-16 01:19:29 +02:00
|
|
|
|
|
|
|
for _, d := range t.Deps {
|
2017-07-02 20:30:50 +02:00
|
|
|
d := d
|
2017-03-16 01:19:29 +02:00
|
|
|
|
2017-04-13 01:39:52 +02:00
|
|
|
g.Go(func() error {
|
2017-07-08 19:34:17 +02:00
|
|
|
dep, err := e.ReplaceVariables(d.Task, call)
|
2017-03-16 01:19:29 +02:00
|
|
|
if err != nil {
|
2017-04-13 01:39:52 +02:00
|
|
|
return err
|
2017-03-16 01:19:29 +02:00
|
|
|
}
|
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
if err = e.RunTask(ctx, Call{Task: dep, Vars: d.Vars}); err != nil {
|
2017-04-13 01:39:52 +02:00
|
|
|
return err
|
2017-03-16 01:19:29 +02:00
|
|
|
}
|
2017-04-13 01:39:52 +02:00
|
|
|
return nil
|
|
|
|
})
|
2017-03-16 01:19:29 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 00:13:21 +02:00
|
|
|
if err := g.Wait(); err != nil {
|
2017-03-16 01:19:29 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-04-13 01:39:52 +02:00
|
|
|
return nil
|
2017-03-16 01:19:29 +02:00
|
|
|
}
|
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
func (e *Executor) isTaskUpToDate(ctx context.Context, call Call) (bool, error) {
|
|
|
|
t := e.Tasks[call.Task]
|
2017-06-16 16:24:01 +02:00
|
|
|
|
2017-05-17 19:37:11 +02:00
|
|
|
if len(t.Status) > 0 {
|
2017-07-08 19:34:17 +02:00
|
|
|
return e.isUpToDateStatus(ctx, call)
|
2017-06-04 21:45:34 +02:00
|
|
|
}
|
2017-07-08 19:34:17 +02:00
|
|
|
return e.isUpToDateTimestamp(ctx, call)
|
2017-06-04 21:45:34 +02:00
|
|
|
}
|
2017-05-17 19:37:11 +02:00
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
func (e *Executor) isUpToDateStatus(ctx context.Context, call Call) (bool, error) {
|
|
|
|
t := e.Tasks[call.Task]
|
2017-06-16 16:24:01 +02:00
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
environ, err := e.getEnviron(call)
|
2017-06-16 16:24:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2017-07-08 19:34:17 +02:00
|
|
|
dir, err := e.getTaskDir(call)
|
2017-07-02 20:30:50 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2017-07-08 19:34:17 +02:00
|
|
|
status, err := e.ReplaceSliceVariables(t.Status, call)
|
2017-06-04 21:45:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2017-07-02 20:30:50 +02:00
|
|
|
for _, s := range status {
|
2017-06-04 21:45:34 +02:00
|
|
|
err = execext.RunCommand(&execext.RunCommandOptions{
|
|
|
|
Context: ctx,
|
|
|
|
Command: s,
|
2017-06-16 16:24:01 +02:00
|
|
|
Dir: dir,
|
2017-06-04 21:45:34 +02:00
|
|
|
Env: environ,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
2017-05-17 19:37:11 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-04 21:45:34 +02:00
|
|
|
return true, nil
|
|
|
|
}
|
2017-05-17 19:37:11 +02:00
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
func (e *Executor) isUpToDateTimestamp(ctx context.Context, call Call) (bool, error) {
|
|
|
|
t := e.Tasks[call.Task]
|
2017-06-16 16:24:01 +02:00
|
|
|
|
2017-02-27 22:03:25 +02:00
|
|
|
if len(t.Sources) == 0 || len(t.Generates) == 0 {
|
2017-05-01 00:32:28 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
dir, err := e.getTaskDir(call)
|
2017-05-01 00:32:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2017-06-16 16:24:01 +02:00
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
sources, err := e.ReplaceSliceVariables(t.Sources, call)
|
2017-06-16 16:24:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2017-07-08 19:34:17 +02:00
|
|
|
generates, err := e.ReplaceSliceVariables(t.Generates, call)
|
2017-05-01 00:32:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2017-02-27 21:44:08 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 16:24:01 +02:00
|
|
|
sourcesMaxTime, err := getPatternsMaxTime(dir, sources)
|
2017-02-27 21:44:08 +02:00
|
|
|
if err != nil || sourcesMaxTime.IsZero() {
|
2017-05-01 00:32:28 +02:00
|
|
|
return false, nil
|
2017-02-27 21:44:08 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 16:24:01 +02:00
|
|
|
generatesMinTime, err := getPatternsMinTime(dir, generates)
|
2017-02-27 21:44:08 +02:00
|
|
|
if err != nil || generatesMinTime.IsZero() {
|
2017-05-01 00:32:28 +02:00
|
|
|
return false, nil
|
2017-02-27 21:44:08 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 00:32:28 +02:00
|
|
|
return generatesMinTime.After(sourcesMaxTime), nil
|
2017-02-27 21:44:08 +02:00
|
|
|
}
|
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
func (e *Executor) runCommand(ctx context.Context, call Call, i int) error {
|
|
|
|
t := e.Tasks[call.Task]
|
2017-07-02 20:30:50 +02:00
|
|
|
cmd := t.Cmds[i]
|
2017-06-04 21:02:04 +02:00
|
|
|
|
2017-07-02 20:30:50 +02:00
|
|
|
if cmd.Cmd == "" {
|
2017-07-06 18:40:21 +02:00
|
|
|
cmdVars := make(Vars, len(cmd.Vars))
|
|
|
|
for k, v := range cmd.Vars {
|
2017-07-08 19:34:17 +02:00
|
|
|
v, err := e.ReplaceVariables(v, call)
|
2017-07-06 18:40:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmdVars[k] = v
|
|
|
|
}
|
2017-07-08 19:34:17 +02:00
|
|
|
return e.RunTask(ctx, Call{Task: cmd.Task, Vars: cmdVars})
|
2017-03-05 21:49:44 +02:00
|
|
|
}
|
2017-03-25 20:26:42 +02:00
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
c, err := e.ReplaceVariables(cmd.Cmd, call)
|
2017-07-02 20:30:50 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-03-25 20:26:42 +02:00
|
|
|
}
|
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
dir, err := e.getTaskDir(call)
|
2017-03-05 21:49:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-22 20:46:29 +02:00
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
envs, err := e.getEnviron(call)
|
2017-04-22 20:46:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-03-01 14:35:00 +02:00
|
|
|
}
|
2017-04-22 20:46:29 +02:00
|
|
|
opts := &execext.RunCommandOptions{
|
|
|
|
Context: ctx,
|
|
|
|
Command: c,
|
|
|
|
Dir: dir,
|
|
|
|
Env: envs,
|
2017-07-01 20:05:51 +02:00
|
|
|
Stdin: e.Stdin,
|
|
|
|
Stderr: e.Stderr,
|
2017-03-06 14:48:56 +02:00
|
|
|
}
|
2017-04-22 20:46:29 +02:00
|
|
|
|
2017-07-03 14:59:06 +02:00
|
|
|
e.println(c)
|
2017-04-22 20:46:29 +02:00
|
|
|
if t.Set == "" {
|
2017-07-01 20:05:51 +02:00
|
|
|
opts.Stdout = e.Stdout
|
2017-04-22 20:46:29 +02:00
|
|
|
if err = execext.RunCommand(opts); err != nil {
|
2017-03-05 20:52:58 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-04-22 20:46:29 +02:00
|
|
|
} else {
|
2017-07-08 19:57:12 +02:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
opts.Stdout = &stdout
|
2017-04-22 20:46:29 +02:00
|
|
|
if err = execext.RunCommand(opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-08 19:57:12 +02:00
|
|
|
os.Setenv(t.Set, strings.TrimSpace(stdout.String()))
|
2017-02-27 15:07:54 +02:00
|
|
|
}
|
2017-03-05 20:52:58 +02:00
|
|
|
return nil
|
2017-02-27 15:07:54 +02:00
|
|
|
}
|
2017-04-22 20:46:29 +02:00
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
func (e *Executor) getTaskDir(call Call) (string, error) {
|
|
|
|
t := e.Tasks[call.Task]
|
2017-06-16 16:24:01 +02:00
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
taskDir, err := e.ReplaceVariables(t.Dir, call)
|
2017-06-16 16:24:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-07-08 20:01:45 +02:00
|
|
|
return filepath.Join(e.Dir, taskDir), nil
|
2017-06-16 16:24:01 +02:00
|
|
|
}
|
|
|
|
|
2017-07-08 19:34:17 +02:00
|
|
|
func (e *Executor) getEnviron(call Call) ([]string, error) {
|
|
|
|
t := e.Tasks[call.Task]
|
2017-06-16 16:24:01 +02:00
|
|
|
|
2017-04-22 20:46:29 +02:00
|
|
|
if t.Env == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
envs := os.Environ()
|
|
|
|
|
|
|
|
for k, v := range t.Env {
|
2017-07-08 19:34:17 +02:00
|
|
|
env, err := e.ReplaceVariables(fmt.Sprintf("%s=%s", k, v), call)
|
2017-04-22 20:46:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-01 00:13:21 +02:00
|
|
|
envs = append(envs, env)
|
2017-04-22 20:46:29 +02:00
|
|
|
}
|
|
|
|
return envs, nil
|
|
|
|
}
|