2017-02-27 14:48:50 +02:00
|
|
|
package task
|
2017-02-27 01:43:50 +02:00
|
|
|
|
|
|
|
import (
|
2017-04-13 01:32:56 +02:00
|
|
|
"context"
|
2019-06-16 03:37:20 +02:00
|
|
|
"errors"
|
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"
|
2019-06-16 02:12:54 +02:00
|
|
|
"sync"
|
2017-07-08 18:33:55 +02:00
|
|
|
"sync/atomic"
|
2017-02-27 01:43:50 +02:00
|
|
|
|
2020-08-16 20:48:19 +02:00
|
|
|
"github.com/go-task/task/v3/internal/compiler"
|
|
|
|
compilerv2 "github.com/go-task/task/v3/internal/compiler/v2"
|
|
|
|
compilerv3 "github.com/go-task/task/v3/internal/compiler/v3"
|
|
|
|
"github.com/go-task/task/v3/internal/execext"
|
|
|
|
"github.com/go-task/task/v3/internal/logger"
|
|
|
|
"github.com/go-task/task/v3/internal/output"
|
|
|
|
"github.com/go-task/task/v3/internal/summary"
|
2020-08-19 10:59:58 +02:00
|
|
|
"github.com/go-task/task/v3/taskfile"
|
|
|
|
"github.com/go-task/task/v3/taskfile/read"
|
2017-03-12 22:18:59 +02:00
|
|
|
|
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-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 {
|
2018-02-17 18:22:18 +02:00
|
|
|
Taskfile *taskfile.Taskfile
|
2019-07-21 15:54:09 +02:00
|
|
|
|
2020-06-12 20:09:53 +02:00
|
|
|
Dir string
|
|
|
|
Entrypoint string
|
|
|
|
Force bool
|
|
|
|
Watch bool
|
|
|
|
Verbose bool
|
|
|
|
Silent bool
|
|
|
|
Dry bool
|
|
|
|
Summary bool
|
|
|
|
Parallel bool
|
|
|
|
Color bool
|
|
|
|
Concurrency int
|
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
|
|
|
|
|
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
|
2018-02-17 20:12:41 +02:00
|
|
|
|
2020-03-29 21:54:59 +02:00
|
|
|
taskvars *taskfile.Vars
|
2017-07-06 02:03:59 +02:00
|
|
|
|
2020-06-12 20:09:53 +02:00
|
|
|
concurrencySemaphore chan struct{}
|
|
|
|
taskCallCount map[string]*int32
|
|
|
|
mkdirMutexMap map[string]*sync.Mutex
|
2021-07-28 22:39:00 +02:00
|
|
|
executionHashes map[string]context.Context
|
|
|
|
executionHashesMutex sync.Mutex
|
2017-06-04 21:02:04 +02:00
|
|
|
}
|
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 {
|
2018-02-18 14:50:39 +02: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 16:33:09 +02:00
|
|
|
if e.Summary {
|
2020-01-20 15:38:18 +02:00
|
|
|
for i, c := range calls {
|
2021-01-12 17:03:04 +02:00
|
|
|
compiledTask, err := e.FastCompiledTask(c)
|
2020-01-20 15:38:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
summary.PrintSpaceBetweenSummaries(e.Logger, i)
|
|
|
|
summary.PrintTask(e.Logger, compiledTask)
|
|
|
|
}
|
2019-02-24 10:24:57 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-18 14:50:39 +02:00
|
|
|
if e.Watch {
|
|
|
|
return e.watchTasks(calls...)
|
|
|
|
}
|
|
|
|
|
2019-11-13 22:50:04 +02:00
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
2018-02-18 14:50:39 +02:00
|
|
|
for _, c := range calls {
|
2019-11-13 22:50:04 +02:00
|
|
|
c := c
|
|
|
|
if e.Parallel {
|
|
|
|
g.Go(func() error { return e.RunTask(ctx, c) })
|
|
|
|
} else {
|
|
|
|
if err := e.RunTask(ctx, c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-18 14:50:39 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-13 22:50:04 +02:00
|
|
|
return g.Wait()
|
2018-02-18 14:50:39 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 19:39:40 +02:00
|
|
|
// Setup setups Executor's internal state
|
|
|
|
func (e *Executor) Setup() error {
|
2019-07-21 15:54:09 +02:00
|
|
|
if e.Entrypoint == "" {
|
|
|
|
e.Entrypoint = "Taskfile.yml"
|
|
|
|
}
|
|
|
|
|
2018-07-22 21:05:47 +02:00
|
|
|
var err error
|
2019-07-21 15:54:09 +02:00
|
|
|
e.Taskfile, err = read.Taskfile(e.Dir, e.Entrypoint)
|
2018-07-22 21:05:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-17 20:34:32 +02:00
|
|
|
|
2020-05-17 20:42:27 +02:00
|
|
|
v, err := e.Taskfile.ParsedVersion()
|
2018-07-22 21:05:47 +02:00
|
|
|
if err != nil {
|
2018-03-11 19:39:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-17 20:34:32 +02:00
|
|
|
if v < 3.0 {
|
|
|
|
e.taskvars, err = read.Taskvars(e.Dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2018-02-18 14:50:39 +02:00
|
|
|
e.Logger = &logger.Logger{
|
|
|
|
Stdout: e.Stdout,
|
|
|
|
Stderr: e.Stderr,
|
|
|
|
Verbose: e.Verbose,
|
2019-07-07 19:13:53 +02:00
|
|
|
Color: e.Color,
|
2018-02-17 20:12:41 +02:00
|
|
|
}
|
2019-06-16 03:37:20 +02:00
|
|
|
|
2019-08-18 17:37:21 +02:00
|
|
|
if v < 2 {
|
2019-08-19 20:53:35 +02:00
|
|
|
return fmt.Errorf(`task: Taskfile versions prior to v2 are not supported anymore`)
|
2019-08-18 17:37:21 +02:00
|
|
|
}
|
|
|
|
|
2019-06-16 03:37:20 +02:00
|
|
|
// consider as equal to the greater version if round
|
|
|
|
if v == 2.0 {
|
|
|
|
v = 2.6
|
|
|
|
}
|
2021-08-01 01:29:59 +02:00
|
|
|
if v == 3.0 {
|
|
|
|
v = 3.7
|
|
|
|
}
|
2019-06-16 03:37:20 +02:00
|
|
|
|
2021-07-28 21:50:29 +02:00
|
|
|
if v > 3.7 {
|
|
|
|
return fmt.Errorf(`task: Taskfile versions greater than v3.7 not implemented in the version of Task`)
|
2019-07-07 19:18:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Color available only on v3
|
|
|
|
if v < 3 {
|
|
|
|
e.Logger.Color = false
|
2019-06-16 03:37:20 +02:00
|
|
|
}
|
|
|
|
|
2020-05-16 20:45:41 +02:00
|
|
|
if v < 3 {
|
|
|
|
e.Compiler = &compilerv2.CompilerV2{
|
|
|
|
Dir: e.Dir,
|
|
|
|
Taskvars: e.taskvars,
|
|
|
|
TaskfileVars: e.Taskfile.Vars,
|
|
|
|
Expansions: e.Taskfile.Expansions,
|
|
|
|
Logger: e.Logger,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
e.Compiler = &compilerv3.CompilerV3{
|
|
|
|
Dir: e.Dir,
|
2021-01-12 16:09:46 +02:00
|
|
|
TaskfileEnv: e.Taskfile.Env,
|
2020-05-16 20:45:41 +02:00
|
|
|
TaskfileVars: e.Taskfile.Vars,
|
|
|
|
Logger: e.Logger,
|
|
|
|
}
|
2018-04-15 16:11:07 +02:00
|
|
|
}
|
2018-04-15 19:35:29 +02:00
|
|
|
|
2021-06-05 20:54:10 +02:00
|
|
|
if v >= 3.0 {
|
|
|
|
env, err := read.Dotenv(e.Compiler, e.Taskfile, e.Dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = env.Range(func(key string, value taskfile.Var) error {
|
|
|
|
if _, ok := e.Taskfile.Env.Mapping[key]; !ok {
|
|
|
|
e.Taskfile.Env.Set(key, value)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 03:37:20 +02:00
|
|
|
if v < 2.1 && e.Taskfile.Output != "" {
|
2018-04-15 16:11:07 +02:00
|
|
|
return fmt.Errorf(`task: Taskfile option "output" is only available starting on Taskfile version v2.1`)
|
2018-02-17 20:12:41 +02:00
|
|
|
}
|
2021-01-01 23:27:50 +02:00
|
|
|
if v < 2.2 && e.Taskfile.Includes.Len() > 0 {
|
2018-10-13 23:25:40 +02:00
|
|
|
return fmt.Errorf(`task: Including Taskfiles is only available starting on Taskfile version v2.2`)
|
|
|
|
}
|
2020-05-17 20:28:25 +02:00
|
|
|
if v >= 3.0 && e.Taskfile.Expansions > 2 {
|
|
|
|
return fmt.Errorf(`task: The "expansions" setting is not available anymore on v3.0`)
|
|
|
|
}
|
2019-06-16 03:37:20 +02:00
|
|
|
|
2019-02-05 08:42:57 +02:00
|
|
|
if e.OutputStyle != "" {
|
|
|
|
e.Taskfile.Output = e.OutputStyle
|
|
|
|
}
|
2018-04-15 19:35:29 +02:00
|
|
|
switch e.Taskfile.Output {
|
|
|
|
case "", "interleaved":
|
|
|
|
e.Output = output.Interleaved{}
|
|
|
|
case "group":
|
|
|
|
e.Output = output.Group{}
|
2018-04-22 20:41:53 +02:00
|
|
|
case "prefixed":
|
|
|
|
e.Output = output.Prefixed{}
|
2018-04-15 19:35:29 +02:00
|
|
|
default:
|
|
|
|
return fmt.Errorf(`task: output option "%s" not recognized`, e.Taskfile.Output)
|
|
|
|
}
|
2017-07-01 20:05:51 +02:00
|
|
|
|
2019-09-09 03:51:56 +02:00
|
|
|
if e.Taskfile.Method == "" {
|
|
|
|
if v >= 3 {
|
|
|
|
e.Taskfile.Method = "checksum"
|
|
|
|
} else {
|
|
|
|
e.Taskfile.Method = "timestamp"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 03:37:20 +02:00
|
|
|
if v <= 2.1 {
|
|
|
|
err := errors.New(`task: Taskfile option "ignore_error" is only available starting on Taskfile version v2.1`)
|
2018-08-19 20:46:07 +02:00
|
|
|
|
|
|
|
for _, task := range e.Taskfile.Tasks {
|
|
|
|
if task.IgnoreError {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, cmd := range task.Cmds {
|
|
|
|
if cmd.IgnoreError {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 03:37:20 +02:00
|
|
|
if v < 2.6 {
|
|
|
|
for _, task := range e.Taskfile.Tasks {
|
|
|
|
if len(task.Preconditions) > 0 {
|
|
|
|
return errors.New(`task: Task option "preconditions" is only available starting on Taskfile version v2.6`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 16:24:06 +02:00
|
|
|
if v < 3 {
|
2021-01-01 23:27:50 +02:00
|
|
|
err := e.Taskfile.Includes.Range(func(_ string, taskfile taskfile.IncludedTaskfile) error {
|
2020-02-15 16:24:06 +02:00
|
|
|
if taskfile.AdvancedImport {
|
|
|
|
return errors.New(`task: Import with additional parameters is only available starting on Taskfile version v3`)
|
|
|
|
}
|
2021-01-01 23:27:50 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-02-15 16:24:06 +02:00
|
|
|
}
|
2021-07-28 21:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if v < 3.7 {
|
|
|
|
if e.Taskfile.Run != "" {
|
|
|
|
return errors.New(`task: Setting the "run" type is only available starting on Taskfile version v3.7`)
|
|
|
|
}
|
|
|
|
|
2020-08-26 19:10:27 +02:00
|
|
|
for _, task := range e.Taskfile.Tasks {
|
|
|
|
if task.Run != "" {
|
2021-07-28 21:50:29 +02:00
|
|
|
return errors.New(`task: Setting the "run" type is only available starting on Taskfile version v3.7`)
|
2020-08-26 19:10:27 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-15 16:24:06 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 21:50:29 +02:00
|
|
|
if e.Taskfile.Run == "" {
|
|
|
|
e.Taskfile.Run = "always"
|
|
|
|
}
|
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
e.executionHashes = make(map[string]context.Context)
|
2020-08-17 21:25:17 +02:00
|
|
|
|
2017-12-29 22:27:32 +02:00
|
|
|
e.taskCallCount = make(map[string]*int32, len(e.Taskfile.Tasks))
|
2019-06-16 02:12:54 +02:00
|
|
|
e.mkdirMutexMap = make(map[string]*sync.Mutex, len(e.Taskfile.Tasks))
|
2017-12-29 22:27:32 +02:00
|
|
|
for k := range e.Taskfile.Tasks {
|
2017-07-16 21:09:55 +02:00
|
|
|
e.taskCallCount[k] = new(int32)
|
2019-06-16 02:12:54 +02:00
|
|
|
e.mkdirMutexMap[k] = &sync.Mutex{}
|
2017-07-16 21:09:55 +02:00
|
|
|
}
|
2020-06-12 20:09:53 +02:00
|
|
|
|
|
|
|
if e.Concurrency > 0 {
|
|
|
|
e.concurrencySemaphore = make(chan struct{}, e.Concurrency)
|
|
|
|
}
|
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
|
2018-02-17 18: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 21:09:55 +02: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 21:00:17 +02:00
|
|
|
}
|
|
|
|
|
2020-06-12 20:09:53 +02:00
|
|
|
release := e.acquireConcurrencyLimit()
|
|
|
|
defer release()
|
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
return e.startExecution(ctx, t, func(ctx context.Context) error {
|
|
|
|
if err := e.runDeps(ctx, t); err != nil {
|
2017-05-01 00:32:28 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-05-17 22:13:47 +02:00
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
if !e.Force {
|
|
|
|
preCondMet, err := e.areTaskPreconditionsMet(ctx, t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-17 22:13:47 +02:00
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
upToDate, err := e.isTaskUpToDate(ctx, t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if upToDate && preCondMet {
|
|
|
|
if !e.Silent {
|
|
|
|
e.Logger.Errf(logger.Magenta, `task: Task "%s" is up to date`, t.Name())
|
|
|
|
}
|
|
|
|
return nil
|
2017-09-30 19:56:35 +02:00
|
|
|
}
|
2017-05-01 00:32:28 +02:00
|
|
|
}
|
2017-03-05 11:15:49 +02:00
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
if err := e.mkdir(t); err != nil {
|
|
|
|
e.Logger.Errf(logger.Red, "task: cannot make directory %q: %v", t.Dir, err)
|
|
|
|
}
|
2019-06-04 18:58:22 +02:00
|
|
|
|
2021-07-28 22:39:00 +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(logger.Yellow, "task: error cleaning status on error: %v", err2)
|
|
|
|
}
|
2018-08-05 17:53:42 +02:00
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
if execext.IsExitError(err) && t.IgnoreError {
|
|
|
|
e.Logger.VerboseErrf(logger.Yellow, "task: task error ignored: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2018-08-05 17:53:42 +02:00
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
return &taskRunError{t.Task, err}
|
|
|
|
}
|
2017-02-27 01:43:50 +02:00
|
|
|
}
|
2021-07-28 22:39:00 +02:00
|
|
|
return nil
|
|
|
|
})
|
2017-02-27 01:43:50 +02:00
|
|
|
}
|
2017-02-27 15:07:54 +02:00
|
|
|
|
2019-06-16 02:12:54 +02:00
|
|
|
func (e *Executor) mkdir(t *taskfile.Task) error {
|
|
|
|
if t.Dir == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex := e.mkdirMutexMap[t.Task]
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
|
|
|
|
if _, err := os.Stat(t.Dir); os.IsNotExist(err) {
|
|
|
|
if err := os.MkdirAll(t.Dir, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
func (e *Executor) runDeps(ctx context.Context, t *taskfile.Task) error {
|
2017-04-13 01:53:41 +02:00
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
2017-03-16 01:19:29 +02:00
|
|
|
|
2020-06-12 20:09:53 +02:00
|
|
|
reacquire := e.releaseConcurrencyLimit()
|
|
|
|
defer reacquire()
|
|
|
|
|
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 {
|
2019-05-17 22:13:47 +02:00
|
|
|
err := e.RunTask(ctx, taskfile.Call{Task: d.Task, Vars: d.Vars})
|
|
|
|
if err != nil {
|
2019-05-28 22:02:59 +02:00
|
|
|
return err
|
2019-05-17 22:13:47 +02:00
|
|
|
}
|
|
|
|
return nil
|
2017-04-13 01:39:52 +02:00
|
|
|
})
|
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
|
|
|
}
|
|
|
|
|
2018-02-17 18:22:18 +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]
|
2017-06-04 21:02:04 +02:00
|
|
|
|
2018-06-24 15:29:46 +02:00
|
|
|
switch {
|
|
|
|
case cmd.Task != "":
|
2020-06-12 20:09:53 +02:00
|
|
|
reacquire := e.releaseConcurrencyLimit()
|
|
|
|
defer reacquire()
|
|
|
|
|
2019-05-17 22:13:47 +02:00
|
|
|
err := e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars})
|
|
|
|
if err != nil {
|
2019-05-28 22:02:59 +02:00
|
|
|
return err
|
2019-05-17 22:13:47 +02:00
|
|
|
}
|
|
|
|
return nil
|
2018-06-24 15:29:46 +02:00
|
|
|
case cmd.Cmd != "":
|
2019-12-08 02:44:09 +02:00
|
|
|
if e.Verbose || (!cmd.Silent && !t.Silent && !e.Taskfile.Silent && !e.Silent) {
|
2020-11-05 22:27:42 +02:00
|
|
|
e.Logger.Errf(logger.Green, "task: [%s] %s", t.Name(), cmd.Cmd)
|
2018-06-24 15:29:46 +02:00
|
|
|
}
|
2017-03-25 20:26:42 +02:00
|
|
|
|
2018-08-05 16:28:02 +02:00
|
|
|
if e.Dry {
|
2018-07-29 01:38:52 +02:00
|
|
|
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)
|
2019-04-21 21:55:47 +02:00
|
|
|
defer func() {
|
|
|
|
if _, ok := stdOut.(*os.File); !ok {
|
|
|
|
if closer, ok := stdOut.(io.Closer); ok {
|
|
|
|
closer.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := stdErr.(*os.File); !ok {
|
|
|
|
if closer, ok := stdErr.(io.Closer); ok {
|
|
|
|
closer.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2018-06-24 15:29:46 +02:00
|
|
|
|
2018-09-01 16:02:23 +02:00
|
|
|
err := execext.RunCommand(ctx, &execext.RunCommandOptions{
|
2018-08-05 17:40:11 +02:00
|
|
|
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 {
|
2020-11-05 22:27:42 +02:00
|
|
|
e.Logger.VerboseErrf(logger.Yellow, "task: [%s] command error ignored: %v", t.Name(), err)
|
2018-08-05 17:40:11 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2018-06-24 15:29:46 +02:00
|
|
|
default:
|
|
|
|
return nil
|
2017-09-16 19:05:07 +02:00
|
|
|
}
|
2017-02-27 15:07:54 +02:00
|
|
|
}
|
2017-04-22 20:46:29 +02:00
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
func getEnviron(t *taskfile.Task) []string {
|
2017-04-22 20:46:29 +02:00
|
|
|
if t.Env == nil {
|
2017-07-16 21:09:55 +02:00
|
|
|
return nil
|
2017-04-22 20:46:29 +02:00
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:40 +02:00
|
|
|
environ := os.Environ()
|
2021-01-12 16:32:49 +02:00
|
|
|
|
2019-08-25 22:16:59 +02:00
|
|
|
for k, v := range t.Env.ToCacheMap() {
|
2021-01-12 16:32:49 +02:00
|
|
|
str, isString := v.(string)
|
|
|
|
if !isString {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, alreadySet := os.LookupEnv(k); alreadySet {
|
|
|
|
continue
|
2019-09-14 22:54:41 +02:00
|
|
|
}
|
2021-01-12 16:32:49 +02:00
|
|
|
|
|
|
|
environ = append(environ, fmt.Sprintf("%s=%s", k, str))
|
2017-04-22 20:46:29 +02:00
|
|
|
}
|
2021-01-12 16:32:49 +02:00
|
|
|
|
2019-01-02 16:05:40 +02:00
|
|
|
return environ
|
2017-04-22 20:46:29 +02:00
|
|
|
}
|
2020-08-17 21:25:17 +02:00
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
func (e *Executor) startExecution(ctx context.Context, t *taskfile.Task, execute func(ctx context.Context) error) error {
|
2020-08-17 21:25:17 +02:00
|
|
|
h, err := e.GetHash(t)
|
|
|
|
if err != nil {
|
2021-07-28 22:39:00 +02:00
|
|
|
return err
|
2020-08-17 21:25:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if h == "" {
|
2021-07-28 22:39:00 +02:00
|
|
|
return execute(ctx)
|
2020-08-17 21:25:17 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
e.executionHashesMutex.Lock()
|
|
|
|
otherExecutionCtx, ok := e.executionHashes[h]
|
2021-08-01 01:29:59 +02:00
|
|
|
|
2020-08-17 21:25:17 +02:00
|
|
|
if ok {
|
2021-08-01 01:29:59 +02:00
|
|
|
e.executionHashesMutex.Unlock()
|
|
|
|
e.Logger.VerboseErrf(logger.Magenta, "task: skipping execution of task: %s", h)
|
2021-07-28 22:39:00 +02:00
|
|
|
<-otherExecutionCtx.Done()
|
|
|
|
return nil
|
2020-08-17 21:25:17 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
2021-08-01 01:29:59 +02:00
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
e.executionHashes[h] = ctx
|
2021-08-01 01:29:59 +02:00
|
|
|
e.executionHashesMutex.Unlock()
|
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
return execute(ctx)
|
2020-08-17 21:25:17 +02:00
|
|
|
}
|