2017-02-27 09:48:50 -03:00
|
|
|
package task
|
2017-02-26 20:43:50 -03:00
|
|
|
|
|
|
|
import (
|
2017-04-12 20:32:56 -03:00
|
|
|
"context"
|
2017-03-06 13:48:56 +01:00
|
|
|
"fmt"
|
2019-02-24 16:25:27 +01:00
|
|
|
"github.com/go-task/task/v2/internal/summary"
|
2017-07-01 15:05:51 -03:00
|
|
|
"io"
|
2017-02-26 20:43:50 -03:00
|
|
|
"os"
|
2017-07-08 13:33:55 -03:00
|
|
|
"sync/atomic"
|
2017-02-26 20:43:50 -03:00
|
|
|
|
2018-11-04 21:23:35 -02:00
|
|
|
"github.com/go-task/task/v2/internal/compiler"
|
|
|
|
compilerv1 "github.com/go-task/task/v2/internal/compiler/v1"
|
|
|
|
compilerv2 "github.com/go-task/task/v2/internal/compiler/v2"
|
|
|
|
"github.com/go-task/task/v2/internal/execext"
|
|
|
|
"github.com/go-task/task/v2/internal/logger"
|
|
|
|
"github.com/go-task/task/v2/internal/output"
|
|
|
|
"github.com/go-task/task/v2/internal/taskfile"
|
|
|
|
"github.com/go-task/task/v2/internal/taskfile/read"
|
|
|
|
"github.com/go-task/task/v2/internal/taskfile/version"
|
2017-03-12 17:18:59 -03:00
|
|
|
|
2018-03-03 18:54:42 -03:00
|
|
|
"github.com/Masterminds/semver"
|
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-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 {
|
2018-02-17 14:22:18 -02:00
|
|
|
Taskfile *taskfile.Taskfile
|
2017-12-29 18:27:32 -02:00
|
|
|
Dir string
|
|
|
|
Force bool
|
|
|
|
Watch bool
|
|
|
|
Verbose bool
|
|
|
|
Silent bool
|
2018-08-05 11:28:02 -03:00
|
|
|
Dry bool
|
2019-02-24 15:33:09 +01:00
|
|
|
Summary 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
|
|
|
|
|
2019-02-09 10:15:38 -02:00
|
|
|
Logger *logger.Logger
|
|
|
|
Compiler compiler.Compiler
|
|
|
|
Output output.Output
|
2019-02-05 15:42:57 +09:00
|
|
|
OutputStyle string
|
2018-02-17 16:12:41 -02:00
|
|
|
|
2018-02-17 14:22:18 -02:00
|
|
|
taskvars taskfile.Vars
|
2017-07-05 21:03:59 -03:00
|
|
|
|
2017-07-16 16:09:55 -03:00
|
|
|
taskCallCount map[string]*int32
|
2017-06-04 16:02:04 -03:00
|
|
|
}
|
2017-02-26 20:43:50 -03:00
|
|
|
|
2017-02-28 09:50:40 -03:00
|
|
|
// Run runs Task
|
2019-02-09 10:16:13 -02:00
|
|
|
func (e *Executor) Run(ctx context.Context, calls ...taskfile.Call) error {
|
2018-02-18 09:50:39 -03:00
|
|
|
// check if given tasks exist
|
|
|
|
for _, c := range calls {
|
|
|
|
if _, ok := e.Taskfile.Tasks[c.Task]; !ok {
|
|
|
|
// FIXME: move to the main package
|
|
|
|
e.PrintTasksHelp()
|
|
|
|
return &taskNotFoundError{taskName: c.Task}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-24 15:33:09 +01:00
|
|
|
if e.Summary {
|
2019-02-24 16:17:47 +01:00
|
|
|
firstTask := calls[0].Task
|
|
|
|
e.printTaskSummary(firstTask)
|
2019-02-24 09:24:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-18 09:50:39 -03:00
|
|
|
if e.Watch {
|
|
|
|
return e.watchTasks(calls...)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range calls {
|
2019-02-09 10:16:13 -02:00
|
|
|
if err := e.RunTask(ctx, c); err != nil {
|
2018-02-18 09:50:39 -03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-24 15:38:18 +01:00
|
|
|
func (e *Executor) printTaskSummary(task string) {
|
2019-02-24 14:37:14 +01:00
|
|
|
t := e.Taskfile.Tasks[task]
|
2019-02-24 16:25:27 +01:00
|
|
|
if t.Summary == "" {
|
2019-02-24 15:54:11 +01:00
|
|
|
e.Logger.Errf("task: There is no summary for task: %s", task)
|
2019-02-24 11:31:25 +01:00
|
|
|
return
|
|
|
|
}
|
2019-02-24 14:01:53 +01:00
|
|
|
|
2019-02-24 16:25:27 +01:00
|
|
|
summary.Print(e.Logger, t)
|
2019-02-24 09:27:26 +01:00
|
|
|
}
|
|
|
|
|
2018-03-11 14:39:40 -03:00
|
|
|
// Setup setups Executor's internal state
|
|
|
|
func (e *Executor) Setup() error {
|
2018-07-22 16:05:47 -03:00
|
|
|
var err error
|
|
|
|
e.Taskfile, err = read.Taskfile(e.Dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
e.taskvars, err = read.Taskvars(e.Dir)
|
|
|
|
if err != nil {
|
2018-03-11 14:39:40 -03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-15 11:11:07 -03:00
|
|
|
v, err := semver.NewConstraint(e.Taskfile.Version)
|
2018-03-03 18:54:42 -03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(`task: could not parse taskfile version "%s": %v`, e.Taskfile.Version, err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2018-02-18 09:50:39 -03:00
|
|
|
e.Logger = &logger.Logger{
|
|
|
|
Stdout: e.Stdout,
|
|
|
|
Stderr: e.Stderr,
|
|
|
|
Verbose: e.Verbose,
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
2018-03-03 18:54:42 -03:00
|
|
|
switch {
|
|
|
|
case version.IsV1(v):
|
2018-02-17 16:12:41 -02:00
|
|
|
e.Compiler = &compilerv1.CompilerV1{
|
|
|
|
Dir: e.Dir,
|
|
|
|
Vars: e.taskvars,
|
|
|
|
Logger: e.Logger,
|
|
|
|
}
|
2018-10-13 18:25:40 -03:00
|
|
|
case version.IsV2(v), version.IsV21(v), version.IsV22(v):
|
2018-02-18 09:50:39 -03:00
|
|
|
e.Compiler = &compilerv2.CompilerV2{
|
2018-03-04 15:39:14 -03:00
|
|
|
Dir: e.Dir,
|
|
|
|
Taskvars: e.taskvars,
|
|
|
|
TaskfileVars: e.Taskfile.Vars,
|
2018-03-04 15:50:03 -03:00
|
|
|
Expansions: e.Taskfile.Expansions,
|
2018-03-04 15:39:14 -03:00
|
|
|
Logger: e.Logger,
|
2018-02-18 09:50:39 -03:00
|
|
|
}
|
2018-10-13 18:25:40 -03:00
|
|
|
case version.IsV23(v):
|
|
|
|
return fmt.Errorf(`task: Taskfile versions greater than v2.3 not implemented in the version of Task`)
|
2018-04-15 11:11:07 -03:00
|
|
|
}
|
2018-04-15 14:35:29 -03:00
|
|
|
|
2018-04-15 11:11:07 -03:00
|
|
|
if !version.IsV21(v) && e.Taskfile.Output != "" {
|
|
|
|
return fmt.Errorf(`task: Taskfile option "output" is only available starting on Taskfile version v2.1`)
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
2018-10-13 18:25:40 -03:00
|
|
|
if !version.IsV22(v) && len(e.Taskfile.Includes) > 0 {
|
|
|
|
return fmt.Errorf(`task: Including Taskfiles is only available starting on Taskfile version v2.2`)
|
|
|
|
}
|
2019-02-05 15:42:57 +09:00
|
|
|
if e.OutputStyle != "" {
|
|
|
|
e.Taskfile.Output = e.OutputStyle
|
|
|
|
}
|
2018-04-15 14:35:29 -03:00
|
|
|
switch e.Taskfile.Output {
|
|
|
|
case "", "interleaved":
|
|
|
|
e.Output = output.Interleaved{}
|
|
|
|
case "group":
|
|
|
|
e.Output = output.Group{}
|
2018-04-22 15:41:53 -03:00
|
|
|
case "prefixed":
|
|
|
|
e.Output = output.Prefixed{}
|
2018-04-15 14:35:29 -03:00
|
|
|
default:
|
|
|
|
return fmt.Errorf(`task: output option "%s" not recognized`, e.Taskfile.Output)
|
|
|
|
}
|
2017-07-01 15:05:51 -03:00
|
|
|
|
2018-08-19 15:46:07 -03:00
|
|
|
if !version.IsV21(v) {
|
|
|
|
err := fmt.Errorf(`task: Taskfile option "ignore_error" is only available starting on Taskfile version v2.1`)
|
|
|
|
|
|
|
|
for _, task := range e.Taskfile.Tasks {
|
|
|
|
if task.IgnoreError {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, cmd := range task.Cmds {
|
|
|
|
if cmd.IgnoreError {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-29 18:27:32 -02:00
|
|
|
e.taskCallCount = make(map[string]*int32, len(e.Taskfile.Tasks))
|
|
|
|
for k := range e.Taskfile.Tasks {
|
2017-07-16 16:09:55 -03:00
|
|
|
e.taskCallCount[k] = new(int32)
|
|
|
|
}
|
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
|
2018-02-17 14:22:18 -02:00
|
|
|
func (e *Executor) RunTask(ctx context.Context, call taskfile.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-06-04 16:02:04 -03:00
|
|
|
if !e.Force {
|
2019-02-09 10:41:19 -02:00
|
|
|
upToDate, err := e.isTaskUpToDate(ctx, t)
|
2017-04-30 19:32:28 -03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if upToDate {
|
2017-09-30 14:56:35 -03:00
|
|
|
if !e.Silent {
|
2018-02-17 16:12:41 -02:00
|
|
|
e.Logger.Errf(`task: Task "%s" is up to date`, t.Task)
|
2017-09-30 14:56:35 -03:00
|
|
|
}
|
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 {
|
2019-02-09 10:41:19 -02:00
|
|
|
if err2 := e.statusOnError(t); err2 != nil {
|
2018-02-17 16:12:41 -02:00
|
|
|
e.Logger.VerboseErrf("task: error cleaning status on error: %v", err2)
|
2017-09-16 11:44:13 -03:00
|
|
|
}
|
2018-08-05 12:53:42 -03:00
|
|
|
|
2018-09-01 11:02:23 -03:00
|
|
|
if execext.IsExitError(err) && t.IgnoreError {
|
2018-08-05 12:53:42 -03:00
|
|
|
e.Logger.VerboseErrf("task: task error ignored: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-09-16 11:44:13 -03:00
|
|
|
return &taskRunError{t.Task, err}
|
2017-02-26 20:43:50 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-27 10:07:54 -03:00
|
|
|
|
2018-02-17 14:22:18 -02:00
|
|
|
func (e *Executor) runDeps(ctx context.Context, t *taskfile.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 {
|
2018-08-01 10:44:53 +02:00
|
|
|
return e.RunTask(ctx, taskfile.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
|
|
|
}
|
|
|
|
|
2018-02-17 14:22:18 -02:00
|
|
|
func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfile.Call, i int) error {
|
2017-07-02 15:30:50 -03:00
|
|
|
cmd := t.Cmds[i]
|
2017-06-04 16:02:04 -03:00
|
|
|
|
2018-06-24 10:29:46 -03:00
|
|
|
switch {
|
|
|
|
case cmd.Task != "":
|
2018-08-01 10:44:53 +02:00
|
|
|
return e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars})
|
2018-06-24 10:29:46 -03:00
|
|
|
case cmd.Cmd != "":
|
|
|
|
if e.Verbose || (!cmd.Silent && !t.Silent && !e.Silent) {
|
|
|
|
e.Logger.Errf(cmd.Cmd)
|
|
|
|
}
|
2017-03-25 15:26:42 -03:00
|
|
|
|
2018-08-05 11:28:02 -03:00
|
|
|
if e.Dry {
|
2018-07-29 00:38:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-24 10:29:46 -03:00
|
|
|
stdOut := e.Output.WrapWriter(e.Stdout, t.Prefix)
|
|
|
|
stdErr := e.Output.WrapWriter(e.Stderr, t.Prefix)
|
|
|
|
defer stdOut.Close()
|
|
|
|
defer stdErr.Close()
|
|
|
|
|
2018-09-01 11:02:23 -03:00
|
|
|
err := execext.RunCommand(ctx, &execext.RunCommandOptions{
|
2018-08-05 12:40:11 -03:00
|
|
|
Command: cmd.Cmd,
|
|
|
|
Dir: t.Dir,
|
|
|
|
Env: getEnviron(t),
|
|
|
|
Stdin: e.Stdin,
|
|
|
|
Stdout: stdOut,
|
|
|
|
Stderr: stdErr,
|
2018-06-24 10:29:46 -03:00
|
|
|
})
|
2018-09-01 11:02:23 -03:00
|
|
|
if execext.IsExitError(err) && cmd.IgnoreError {
|
2018-08-05 12:40:11 -03:00
|
|
|
e.Logger.VerboseErrf("task: command error ignored: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2018-06-24 10:29:46 -03:00
|
|
|
default:
|
|
|
|
return nil
|
2017-09-16 14:05:07 -03:00
|
|
|
}
|
2017-02-27 10:07:54 -03:00
|
|
|
}
|
2017-04-22 15:46:29 -03:00
|
|
|
|
2018-02-17 14:22:18 -02:00
|
|
|
func getEnviron(t *taskfile.Task) []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
|
|
|
}
|
|
|
|
|
2019-01-02 12:05:40 -02:00
|
|
|
environ := os.Environ()
|
2018-02-17 14:22:18 -02:00
|
|
|
for k, v := range t.Env.ToStringMap() {
|
2019-01-02 12:05:40 -02:00
|
|
|
environ = append(environ, fmt.Sprintf("%s=%s", k, v))
|
2017-04-22 15:46:29 -03:00
|
|
|
}
|
2019-01-02 12:05:40 -02:00
|
|
|
return environ
|
2017-04-22 15:46:29 -03:00
|
|
|
}
|