2017-02-27 09:48:50 -03:00
|
|
|
package task
|
2017-02-26 20:43:50 -03:00
|
|
|
|
|
|
|
import (
|
2017-04-22 15:46:29 -03:00
|
|
|
"bytes"
|
2017-04-12 20:32:56 -03:00
|
|
|
"context"
|
2017-03-06 13:48:56 +01:00
|
|
|
"fmt"
|
2017-07-01 15:05:51 -03:00
|
|
|
"io"
|
2017-02-26 20:43:50 -03:00
|
|
|
"os"
|
2017-03-07 08:01:20 +01:00
|
|
|
"strings"
|
2017-07-05 21:03:59 -03:00
|
|
|
"sync"
|
2017-07-08 13:33:55 -03:00
|
|
|
"sync/atomic"
|
2017-02-26 20:43:50 -03:00
|
|
|
|
2017-03-12 17:18:59 -03:00
|
|
|
"github.com/go-task/task/execext"
|
|
|
|
|
2017-04-12 20:39:52 -03:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-02-26 20:43:50 -03:00
|
|
|
)
|
|
|
|
|
2017-06-04 16:02:04 -03:00
|
|
|
const (
|
2017-02-28 09:50:40 -03:00
|
|
|
// TaskFilePath is the default Taskfile
|
2017-02-28 08:13:48 -08:00
|
|
|
TaskFilePath = "Taskfile"
|
2017-07-08 13:33:55 -03: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 16:02:04 -03:00
|
|
|
)
|
2017-02-26 21:18:53 -03:00
|
|
|
|
2017-06-04 16:02:04 -03:00
|
|
|
// Executor executes a Taskfile
|
|
|
|
type Executor struct {
|
2017-07-05 20:55:50 -03:00
|
|
|
Tasks Tasks
|
|
|
|
Dir string
|
|
|
|
Force bool
|
|
|
|
Watch bool
|
|
|
|
Verbose bool
|
2017-07-19 20:20:24 -03:00
|
|
|
Silent bool
|
2017-03-01 20:21:36 -03:00
|
|
|
|
2017-07-01 15:05:51 -03:00
|
|
|
Stdin io.Reader
|
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
|
|
|
|
2017-08-05 11:50:39 -03:00
|
|
|
taskvars Vars
|
2017-07-05 21:03:59 -03:00
|
|
|
|
2017-07-16 16:09:55 -03:00
|
|
|
taskCallCount map[string]*int32
|
|
|
|
|
2017-07-15 15:28:59 -03:00
|
|
|
dynamicCache map[string]string
|
2017-07-05 21:03:59 -03:00
|
|
|
muDynamicCache sync.Mutex
|
2017-06-04 16:02:04 -03:00
|
|
|
}
|
2017-02-26 20:43:50 -03:00
|
|
|
|
2017-06-04 16:41:38 -03:00
|
|
|
// Tasks representas a group of tasks
|
|
|
|
type Tasks map[string]*Task
|
|
|
|
|
2017-02-28 09:50:40 -03:00
|
|
|
// Task represents a task
|
2017-02-26 20:43:50 -03:00
|
|
|
type Task struct {
|
2017-07-02 15:30:50 -03:00
|
|
|
Cmds []*Cmd
|
|
|
|
Deps []*Dep
|
2017-03-06 22:25:50 +01:00
|
|
|
Desc string
|
2017-02-27 17:03:25 -03:00
|
|
|
Sources []string
|
|
|
|
Generates []string
|
2017-05-17 14:37:11 -03:00
|
|
|
Status []string
|
2017-03-01 14:33:10 +01:00
|
|
|
Dir string
|
2017-07-05 20:07:24 -03:00
|
|
|
Vars Vars
|
2017-03-02 20:19:25 +01:00
|
|
|
Set string
|
2017-07-05 20:07:24 -03:00
|
|
|
Env Vars
|
2017-07-19 20:20:24 -03:00
|
|
|
Silent bool
|
2017-02-26 20:43:50 -03:00
|
|
|
}
|
|
|
|
|
2017-02-28 09:50:40 -03:00
|
|
|
// Run runs Task
|
2017-06-04 16:02:04 -03:00
|
|
|
func (e *Executor) Run(args ...string) error {
|
2017-07-01 15:05:51 -03: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-16 16:09:55 -03:00
|
|
|
e.taskCallCount = make(map[string]*int32, len(e.Tasks))
|
|
|
|
for k := range e.Tasks {
|
|
|
|
e.taskCallCount[k] = new(int32)
|
|
|
|
}
|
|
|
|
|
2017-07-05 21:03:59 -03:00
|
|
|
if e.dynamicCache == nil {
|
2017-07-15 15:28:59 -03:00
|
|
|
e.dynamicCache = make(map[string]string, 10)
|
2017-07-05 21:03:59 -03:00
|
|
|
}
|
|
|
|
|
2017-03-19 15:18:18 -03:00
|
|
|
// check if given tasks exist
|
|
|
|
for _, a := range args {
|
2017-06-04 16:02:04 -03:00
|
|
|
if _, ok := e.Tasks[a]; !ok {
|
|
|
|
// FIXME: move to the main package
|
2017-07-15 14:09:27 -03:00
|
|
|
e.PrintTasksHelp()
|
2017-06-04 16:02:04 -03:00
|
|
|
return &taskNotFoundError{taskName: a}
|
2017-03-19 15:18:18 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-04 16:02:04 -03:00
|
|
|
if e.Watch {
|
|
|
|
if err := e.watchTasks(args...); err != nil {
|
|
|
|
return err
|
2017-04-01 16:04:52 -03:00
|
|
|
}
|
2017-06-04 16:02:04 -03:00
|
|
|
return nil
|
2017-04-01 16:04:52 -03:00
|
|
|
}
|
|
|
|
|
2017-02-27 10:13:06 -03:00
|
|
|
for _, a := range args {
|
2017-07-20 09:05:37 +02:00
|
|
|
if err := e.RunTask(context.Background(), Call{Task: a, Vars: nil}); err != nil {
|
2017-06-04 16:02:04 -03:00
|
|
|
return err
|
2017-02-27 10:13:06 -03:00
|
|
|
}
|
2017-02-26 20:43:50 -03:00
|
|
|
}
|
2017-06-04 16:02:04 -03:00
|
|
|
return nil
|
2017-02-26 21:18:53 -03:00
|
|
|
}
|
2017-02-26 20:43:50 -03:00
|
|
|
|
2017-02-28 09:50:40 -03:00
|
|
|
// RunTask runs a task by its name
|
2017-07-08 14:34:17 -03:00
|
|
|
func (e *Executor) RunTask(ctx context.Context, call Call) error {
|
2017-08-16 13:04:58 +02:00
|
|
|
t, err := e.CompiledTask(call)
|
2017-07-16 16:09:55 -03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-16 13:04:58 +02:00
|
|
|
if !e.Watch && atomic.AddInt32(e.taskCallCount[call.Task], 1) >= MaximumTaskCall {
|
|
|
|
return &MaximumTaskCallExceededError{task: call.Task}
|
2017-07-08 16:00:17 -03:00
|
|
|
}
|
|
|
|
|
2017-07-16 16:09:55 -03:00
|
|
|
if err := e.runDeps(ctx, t); err != nil {
|
2017-03-15 20:19:29 -03:00
|
|
|
return err
|
2017-02-26 20:43:50 -03:00
|
|
|
}
|
2017-03-05 10:15:49 +01:00
|
|
|
|
2017-09-03 12:48:06 +02:00
|
|
|
// FIXME: doing again, since a var may have been overridden using the
|
|
|
|
// `set:` attribute of a dependency. Remove this when `set` (that is
|
2017-08-16 13:04:58 +02:00
|
|
|
// deprecated) be removed.
|
|
|
|
t, err = e.CompiledTask(call)
|
2017-07-08 16:00:17 -03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-04 16:02:04 -03:00
|
|
|
if !e.Force {
|
2017-07-30 19:45:01 -03:00
|
|
|
upToDate, err := t.isUpToDate(ctx)
|
2017-04-30 19:32:28 -03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if upToDate {
|
2017-07-08 14:34:17 -03:00
|
|
|
e.printfln(`task: Task "%s" is up to date`, call.Task)
|
2017-04-30 19:32:28 -03:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-05 10:15:49 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:52:58 -03:00
|
|
|
for i := range t.Cmds {
|
2017-07-16 16:09:55 -03:00
|
|
|
if err := e.runCommand(ctx, t, call, i); err != nil {
|
2017-07-08 14:34:17 -03:00
|
|
|
return &taskRunError{call.Task, err}
|
2017-02-26 20:43:50 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-27 10:07:54 -03:00
|
|
|
|
2017-07-16 16:09:55 -03:00
|
|
|
func (e *Executor) runDeps(ctx context.Context, t *Task) error {
|
2017-04-12 20:53:41 -03:00
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
2017-03-15 20:19:29 -03:00
|
|
|
|
|
|
|
for _, d := range t.Deps {
|
2017-07-02 15:30:50 -03:00
|
|
|
d := d
|
2017-03-15 20:19:29 -03:00
|
|
|
|
2017-04-12 20:39:52 -03:00
|
|
|
g.Go(func() error {
|
2017-07-16 16:09:55 -03:00
|
|
|
return e.RunTask(ctx, Call{Task: d.Task, Vars: d.Vars})
|
2017-04-12 20:39:52 -03:00
|
|
|
})
|
2017-03-15 20:19:29 -03:00
|
|
|
}
|
|
|
|
|
2017-07-08 15:08:44 -03:00
|
|
|
return g.Wait()
|
2017-03-15 20:19:29 -03:00
|
|
|
}
|
|
|
|
|
2017-07-16 16:09:55 -03:00
|
|
|
func (e *Executor) runCommand(ctx context.Context, t *Task, call Call, i int) error {
|
2017-07-02 15:30:50 -03:00
|
|
|
cmd := t.Cmds[i]
|
2017-06-04 16:02:04 -03:00
|
|
|
|
2017-07-02 15:30:50 -03:00
|
|
|
if cmd.Cmd == "" {
|
2017-07-16 16:09:55 -03:00
|
|
|
return e.RunTask(ctx, Call{Task: cmd.Task, Vars: cmd.Vars})
|
2017-03-25 15:26:42 -03:00
|
|
|
}
|
|
|
|
|
2017-04-22 15:46:29 -03:00
|
|
|
opts := &execext.RunCommandOptions{
|
|
|
|
Context: ctx,
|
2017-07-16 16:09:55 -03:00
|
|
|
Command: cmd.Cmd,
|
2017-07-30 19:45:01 -03:00
|
|
|
Dir: t.Dir,
|
|
|
|
Env: t.getEnviron(),
|
2017-07-01 15:05:51 -03:00
|
|
|
Stdin: e.Stdin,
|
|
|
|
Stderr: e.Stderr,
|
2017-03-06 13:48:56 +01:00
|
|
|
}
|
2017-04-22 15:46:29 -03:00
|
|
|
|
2017-07-30 19:21:06 -03:00
|
|
|
if e.Verbose || (!cmd.Silent && !t.Silent && !e.Silent) {
|
2017-07-19 20:20:24 -03:00
|
|
|
e.println(cmd.Cmd)
|
|
|
|
}
|
2017-07-08 15:08:44 -03:00
|
|
|
if t.Set != "" {
|
2017-07-08 14:57:12 -03:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
opts.Stdout = &stdout
|
2017-07-16 16:09:55 -03:00
|
|
|
if err := execext.RunCommand(opts); err != nil {
|
2017-04-22 15:46:29 -03:00
|
|
|
return err
|
|
|
|
}
|
2017-07-08 15:08:44 -03:00
|
|
|
return os.Setenv(t.Set, strings.TrimSpace(stdout.String()))
|
2017-02-27 10:07:54 -03:00
|
|
|
}
|
2017-07-08 15:08:44 -03:00
|
|
|
|
|
|
|
opts.Stdout = e.Stdout
|
|
|
|
return execext.RunCommand(opts)
|
2017-02-27 10:07:54 -03:00
|
|
|
}
|
2017-04-22 15:46:29 -03:00
|
|
|
|
2017-07-30 19:45:01 -03:00
|
|
|
func (t *Task) getEnviron() []string {
|
2017-04-22 15:46:29 -03:00
|
|
|
if t.Env == nil {
|
2017-07-16 16:09:55 -03:00
|
|
|
return nil
|
2017-04-22 15:46:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
envs := os.Environ()
|
2017-08-16 13:04:58 +02:00
|
|
|
for k, v := range t.Env.toStringMap() {
|
2017-07-16 16:09:55 -03:00
|
|
|
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
|
2017-04-22 15:46:29 -03:00
|
|
|
}
|
2017-07-16 16:09:55 -03:00
|
|
|
return envs
|
2017-04-22 15:46:29 -03:00
|
|
|
}
|