1
0
mirror of https://github.com/go-task/task.git synced 2024-12-14 10:52:43 +02:00
task/task.go

322 lines
7.0 KiB
Go
Raw Normal View History

2017-02-27 14:48:50 +02:00
package task
2017-02-27 01:43:50 +02:00
import (
"context"
"fmt"
"io"
2017-02-27 01:43:50 +02:00
"os"
2019-02-24 12:31:25 +02:00
"strings"
"sync/atomic"
2017-02-27 01:43:50 +02:00
2018-11-05 01: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 22:18:59 +02:00
2018-03-03 23:54:42 +02:00
"github.com/Masterminds/semver"
"golang.org/x/sync/errgroup"
2017-02-27 01:43:50 +02:00
)
const (
// MaximumTaskCall is the max number of times a task can be called.
// This exists to prevent infinite loops on cyclic dependencies
MaximumTaskCall = 100
)
2017-02-27 02:18:53 +02:00
// Executor executes a Taskfile
type Executor struct {
Taskfile *taskfile.Taskfile
Dir string
Force bool
Watch bool
Verbose bool
Silent bool
2018-08-05 16:28:02 +02:00
Dry bool
2019-02-24 09:51:20 +02:00
Details bool
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
2019-02-09 14:15:38 +02:00
Logger *logger.Logger
Compiler compiler.Compiler
Output output.Output
2019-02-05 08:42:57 +02:00
OutputStyle string
taskvars taskfile.Vars
2017-07-06 02:03:59 +02:00
taskCallCount map[string]*int32
}
2017-02-27 01:43:50 +02:00
2017-02-28 14:50:40 +02:00
// Run runs Task
2019-02-09 14:16:13 +02:00
func (e *Executor) Run(ctx context.Context, calls ...taskfile.Call) error {
// 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 10:24:57 +02:00
if e.Details {
2019-02-24 15:01:53 +02:00
e.printTaskDetails(calls[0].Task)
2019-02-24 10:24:57 +02:00
return nil
}
if e.Watch {
return e.watchTasks(calls...)
}
for _, c := range calls {
2019-02-09 14:16:13 +02:00
if err := e.RunTask(ctx, c); err != nil {
return err
}
}
return nil
}
2019-02-24 15:01:53 +02:00
func (e *Executor) printTaskDetails(task string) {
2019-02-24 15:37:14 +02:00
t := e.Taskfile.Tasks[task]
s := t.Details
2019-02-24 12:31:25 +02:00
if s == "" {
2019-02-24 10:27:26 +02:00
e.Logger.Errf("task: There is no detailed description for task: %s", task)
2019-02-24 12:31:25 +02:00
return
}
2019-02-24 15:01:53 +02:00
e.Logger.Outf("task: " + task)
e.Logger.Outf("")
2019-02-24 15:08:27 +02:00
Logger := e.Logger
displayTaskDetailedDescription(s, Logger)
2019-02-24 15:53:39 +02:00
e.Logger.Outf("")
e.Logger.Outf("dependencies:")
for _, d := range t.Deps {
e.Logger.Outf(" - %s", d.Task)
}
2019-02-24 15:08:27 +02:00
e.Logger.Outf("")
e.Logger.Outf("commands:")
2019-02-24 15:37:14 +02:00
for _, c := range t.Cmds {
e.Logger.Outf(" - %s", c.Cmd)
}
2019-02-24 15:08:27 +02:00
}
func displayTaskDetailedDescription(description string, Logger *logger.Logger) {
lines := strings.Split(description, "\n")
for i, line := range lines {
notLastLine := i+1 < len(lines)
if notLastLine || line != "" {
2019-02-24 15:08:27 +02:00
Logger.Outf(line)
2019-02-24 12:31:25 +02:00
}
2019-02-24 10:27:26 +02:00
}
}
2018-03-11 19:39:40 +02:00
// Setup setups Executor's internal state
func (e *Executor) Setup() error {
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 19:39:40 +02:00
return err
}
v, err := semver.NewConstraint(e.Taskfile.Version)
2018-03-03 23:54:42 +02:00
if err != nil {
return fmt.Errorf(`task: could not parse taskfile version "%s": %v`, e.Taskfile.Version, err)
}
if e.Stdin == nil {
e.Stdin = os.Stdin
}
if e.Stdout == nil {
e.Stdout = os.Stdout
}
if e.Stderr == nil {
e.Stderr = os.Stderr
}
e.Logger = &logger.Logger{
Stdout: e.Stdout,
Stderr: e.Stderr,
Verbose: e.Verbose,
}
2018-03-03 23:54:42 +02:00
switch {
case version.IsV1(v):
e.Compiler = &compilerv1.CompilerV1{
Dir: e.Dir,
Vars: e.taskvars,
Logger: e.Logger,
}
case version.IsV2(v), version.IsV21(v), version.IsV22(v):
e.Compiler = &compilerv2.CompilerV2{
Dir: e.Dir,
Taskvars: e.taskvars,
TaskfileVars: e.Taskfile.Vars,
Expansions: e.Taskfile.Expansions,
Logger: e.Logger,
}
case version.IsV23(v):
return fmt.Errorf(`task: Taskfile versions greater than v2.3 not implemented in the version of Task`)
}
if !version.IsV21(v) && e.Taskfile.Output != "" {
return fmt.Errorf(`task: Taskfile option "output" is only available starting on Taskfile version v2.1`)
}
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 08:42:57 +02:00
if e.OutputStyle != "" {
e.Taskfile.Output = e.OutputStyle
}
switch e.Taskfile.Output {
case "", "interleaved":
e.Output = output.Interleaved{}
case "group":
e.Output = output.Group{}
case "prefixed":
e.Output = output.Prefixed{}
default:
return fmt.Errorf(`task: output option "%s" not recognized`, e.Taskfile.Output)
}
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
}
}
}
}
e.taskCallCount = make(map[string]*int32, len(e.Taskfile.Tasks))
for k := range e.Taskfile.Tasks {
e.taskCallCount[k] = new(int32)
}
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
func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {
t, err := e.CompiledTask(call)
if err != nil {
return err
}
if !e.Watch && atomic.AddInt32(e.taskCallCount[call.Task], 1) >= MaximumTaskCall {
return &MaximumTaskCallExceededError{task: call.Task}
}
if err := e.runDeps(ctx, t); 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
if !e.Force {
upToDate, err := e.isTaskUpToDate(ctx, t)
if err != nil {
return err
}
if upToDate {
if !e.Silent {
e.Logger.Errf(`task: Task "%s" is up to date`, t.Task)
}
return nil
}
2017-03-05 11:15:49 +02:00
}
for i := range t.Cmds {
if err := e.runCommand(ctx, t, call, i); err != nil {
if err2 := e.statusOnError(t); err2 != nil {
e.Logger.VerboseErrf("task: error cleaning status on error: %v", err2)
}
2018-08-05 17:53:42 +02:00
2018-09-01 16:02:23 +02:00
if execext.IsExitError(err) && t.IgnoreError {
2018-08-05 17:53:42 +02:00
e.Logger.VerboseErrf("task: task error ignored: %v", err)
continue
}
return &taskRunError{t.Task, err}
2017-02-27 01:43:50 +02:00
}
}
return nil
}
func (e *Executor) runDeps(ctx context.Context, t *taskfile.Task) error {
g, ctx := errgroup.WithContext(ctx)
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
g.Go(func() error {
return e.RunTask(ctx, taskfile.Call{Task: d.Task, Vars: d.Vars})
})
2017-03-16 01:19:29 +02:00
}
2017-07-08 20:08:44 +02:00
return g.Wait()
2017-03-16 01:19:29 +02:00
}
func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfile.Call, i int) error {
2017-07-02 20:30:50 +02:00
cmd := t.Cmds[i]
2018-06-24 15:29:46 +02:00
switch {
case cmd.Task != "":
return e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars})
2018-06-24 15:29:46 +02:00
case cmd.Cmd != "":
if e.Verbose || (!cmd.Silent && !t.Silent && !e.Silent) {
e.Logger.Errf(cmd.Cmd)
}
2017-03-25 20:26:42 +02:00
2018-08-05 16:28:02 +02:00
if e.Dry {
return nil
}
2018-06-24 15:29:46 +02: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 16:02:23 +02:00
err := execext.RunCommand(ctx, &execext.RunCommandOptions{
Command: cmd.Cmd,
Dir: t.Dir,
Env: getEnviron(t),
Stdin: e.Stdin,
Stdout: stdOut,
Stderr: stdErr,
2018-06-24 15:29:46 +02:00
})
2018-09-01 16:02:23 +02:00
if execext.IsExitError(err) && cmd.IgnoreError {
e.Logger.VerboseErrf("task: command error ignored: %v", err)
return nil
}
return err
2018-06-24 15:29:46 +02:00
default:
return nil
2017-09-16 19:05:07 +02:00
}
}
func getEnviron(t *taskfile.Task) []string {
if t.Env == nil {
return nil
}
environ := os.Environ()
for k, v := range t.Env.ToStringMap() {
environ = append(environ, fmt.Sprintf("%s=%s", k, v))
}
return environ
}