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"
|
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"
|
2023-01-07 02:39:57 +02:00
|
|
|
"runtime"
|
2023-08-29 23:04:01 +02:00
|
|
|
"slices"
|
2019-06-16 02:12:54 +02:00
|
|
|
"sync"
|
2017-07-08 18:33:55 +02:00
|
|
|
"sync/atomic"
|
2022-12-31 18:48:49 +02:00
|
|
|
"time"
|
2017-02-27 01:43:50 +02:00
|
|
|
|
2024-08-15 03:53:14 +02:00
|
|
|
"mvdan.cc/sh/v3/interp"
|
|
|
|
|
2023-04-15 22:22:25 +02:00
|
|
|
"github.com/go-task/task/v3/errors"
|
2020-08-16 20:48:19 +02:00
|
|
|
"github.com/go-task/task/v3/internal/compiler"
|
2023-03-10 20:27:30 +02:00
|
|
|
"github.com/go-task/task/v3/internal/env"
|
2020-08-16 20:48:19 +02:00
|
|
|
"github.com/go-task/task/v3/internal/execext"
|
2023-03-10 20:27:30 +02:00
|
|
|
"github.com/go-task/task/v3/internal/fingerprint"
|
2020-08-16 20:48:19 +02:00
|
|
|
"github.com/go-task/task/v3/internal/logger"
|
|
|
|
"github.com/go-task/task/v3/internal/output"
|
2023-01-14 21:41:56 +02:00
|
|
|
"github.com/go-task/task/v3/internal/slicesext"
|
2023-04-06 13:07:57 +02:00
|
|
|
"github.com/go-task/task/v3/internal/sort"
|
2020-08-16 20:48:19 +02:00
|
|
|
"github.com/go-task/task/v3/internal/summary"
|
2022-01-14 02:11:47 +02:00
|
|
|
"github.com/go-task/task/v3/internal/templater"
|
2023-12-29 22:32:03 +02:00
|
|
|
"github.com/go-task/task/v3/taskfile/ast"
|
2017-03-12 22:18:59 +02:00
|
|
|
|
2022-10-02 17:49:38 +02:00
|
|
|
"github.com/sajari/fuzzy"
|
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
|
2023-10-07 23:28:49 +02:00
|
|
|
MaximumTaskCall = 1000
|
2017-06-04 21:02:04 +02:00
|
|
|
)
|
2017-02-27 02:18:53 +02:00
|
|
|
|
2024-06-28 18:01:11 +02:00
|
|
|
type TempDir struct {
|
|
|
|
Remote string
|
|
|
|
Fingerprint string
|
|
|
|
}
|
|
|
|
|
2017-06-04 21:02:04 +02:00
|
|
|
// Executor executes a Taskfile
|
|
|
|
type Executor struct {
|
2023-12-29 22:32:03 +02:00
|
|
|
Taskfile *ast.Taskfile
|
2019-07-21 15:54:09 +02:00
|
|
|
|
2020-06-12 20:09:53 +02:00
|
|
|
Dir string
|
|
|
|
Entrypoint string
|
2024-06-28 18:01:11 +02:00
|
|
|
TempDir TempDir
|
2020-06-12 20:09:53 +02:00
|
|
|
Force bool
|
2023-06-18 03:32:18 +02:00
|
|
|
ForceAll bool
|
2023-09-12 23:42:54 +02:00
|
|
|
Insecure bool
|
|
|
|
Download bool
|
|
|
|
Offline bool
|
2023-11-17 22:51:10 +02:00
|
|
|
Timeout time.Duration
|
2020-06-12 20:09:53 +02:00
|
|
|
Watch bool
|
|
|
|
Verbose bool
|
|
|
|
Silent bool
|
2023-06-04 03:33:00 +02:00
|
|
|
AssumeYes bool
|
2023-10-07 23:55:43 +02:00
|
|
|
AssumeTerm bool // Used for testing
|
2020-06-12 20:09:53 +02:00
|
|
|
Dry bool
|
|
|
|
Summary bool
|
|
|
|
Parallel bool
|
|
|
|
Color bool
|
|
|
|
Concurrency int
|
2022-12-31 18:48:49 +02:00
|
|
|
Interval time.Duration
|
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
|
|
|
|
|
2023-08-26 23:06:50 +02:00
|
|
|
Logger *logger.Logger
|
2023-12-29 22:26:02 +02:00
|
|
|
Compiler *compiler.Compiler
|
2023-08-26 23:06:50 +02:00
|
|
|
Output output.Output
|
2023-12-29 22:32:03 +02:00
|
|
|
OutputStyle ast.Output
|
2023-08-26 23:06:50 +02:00
|
|
|
TaskSorter sort.TaskSorter
|
|
|
|
UserWorkingDir string
|
2018-02-17 20:12:41 +02:00
|
|
|
|
2022-10-02 17:49:38 +02:00
|
|
|
fuzzyModel *fuzzy.Model
|
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
|
2024-01-26 16:34:18 +02:00
|
|
|
func (e *Executor) Run(ctx context.Context, calls ...*ast.Call) error {
|
2018-02-18 14:50:39 +02:00
|
|
|
// check if given tasks exist
|
2022-10-02 00:39:44 +02:00
|
|
|
for _, call := range calls {
|
|
|
|
task, err := e.GetTask(call)
|
|
|
|
if err != nil {
|
2023-04-15 22:22:25 +02:00
|
|
|
if _, ok := err.(*errors.TaskNotFoundError); ok {
|
2022-12-17 15:35:30 +02:00
|
|
|
if _, err := e.ListTasks(ListOptions{ListOnlyTasksWithDescriptions: true}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-10-02 00:39:44 +02:00
|
|
|
return err
|
2018-02-18 14:50:39 +02:00
|
|
|
}
|
2022-10-15 01:08:00 +02:00
|
|
|
|
2022-10-02 00:39:44 +02:00
|
|
|
if task.Internal {
|
2023-04-15 22:22:25 +02:00
|
|
|
if _, ok := err.(*errors.TaskNotFoundError); ok {
|
2022-12-17 15:35:30 +02:00
|
|
|
if _, err := e.ListTasks(ListOptions{ListOnlyTasksWithDescriptions: true}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-04-15 22:22:25 +02:00
|
|
|
return &errors.TaskInternalError{TaskName: call.Task}
|
2022-07-22 04:15:35 +02:00
|
|
|
}
|
2018-02-18 14:50:39 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-07 23:06:43 +02:00
|
|
|
regularCalls, watchCalls, err := e.splitRegularAndWatchCalls(calls...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-02-18 14:50:39 +02:00
|
|
|
}
|
|
|
|
|
2019-11-13 22:50:04 +02:00
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
2023-10-07 23:06:43 +02:00
|
|
|
for _, c := range regularCalls {
|
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
|
|
|
}
|
|
|
|
}
|
2023-10-07 23:06:43 +02:00
|
|
|
if err := g.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(watchCalls) > 0 {
|
|
|
|
return e.watchTasks(watchCalls...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-26 16:34:18 +02:00
|
|
|
func (e *Executor) splitRegularAndWatchCalls(calls ...*ast.Call) (regularCalls []*ast.Call, watchCalls []*ast.Call, err error) {
|
2023-10-07 23:06:43 +02:00
|
|
|
for _, c := range calls {
|
|
|
|
t, err := e.GetTask(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Watch || t.Watch {
|
|
|
|
watchCalls = append(watchCalls, c)
|
|
|
|
} else {
|
|
|
|
regularCalls = append(regularCalls, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2018-02-18 14:50:39 +02:00
|
|
|
}
|
|
|
|
|
2017-02-28 14:50:40 +02:00
|
|
|
// RunTask runs a task by its name
|
2024-01-26 16:34:18 +02:00
|
|
|
func (e *Executor) RunTask(ctx context.Context, call *ast.Call) error {
|
2023-10-22 02:42:26 +02:00
|
|
|
t, err := e.FastCompiledTask(call)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !shouldRunOnCurrentPlatform(t.Platforms) {
|
|
|
|
e.Logger.VerboseOutf(logger.Yellow, `task: %q not for current platform - ignored\n`, call.Task)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err = e.CompiledTask(call)
|
2017-07-16 21:09:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-02 00:39:44 +02:00
|
|
|
if !e.Watch && atomic.AddInt32(e.taskCallCount[t.Task], 1) >= MaximumTaskCall {
|
2023-10-07 23:28:49 +02:00
|
|
|
return &errors.TaskCalledTooManyTimesError{
|
|
|
|
TaskName: t.Task,
|
|
|
|
MaximumTaskCall: MaximumTaskCall,
|
|
|
|
}
|
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 {
|
2023-04-27 02:20:06 +02:00
|
|
|
e.Logger.VerboseErrf(logger.Magenta, "task: %q started\n", call.Task)
|
2021-07-28 22:39:00 +02:00
|
|
|
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
|
|
|
|
2024-01-11 02:32:49 +02:00
|
|
|
skipFingerprinting := e.ForceAll || (!call.Indirect && e.Force)
|
2023-06-18 03:32:18 +02:00
|
|
|
if !skipFingerprinting {
|
2021-12-04 17:37:52 +02:00
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-14 15:37:05 +02:00
|
|
|
if err := e.areTaskRequiredVarsSet(t, call); err != nil {
|
2023-06-30 03:13:41 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
preCondMet, err := e.areTaskPreconditionsMet(ctx, t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-17 22:13:47 +02:00
|
|
|
|
2023-03-10 20:27:30 +02:00
|
|
|
// Get the fingerprinting method to use
|
|
|
|
method := e.Taskfile.Method
|
|
|
|
if t.Method != "" {
|
|
|
|
method = t.Method
|
|
|
|
}
|
|
|
|
|
|
|
|
upToDate, err := fingerprint.IsTaskUpToDate(ctx, t,
|
|
|
|
fingerprint.WithMethod(method),
|
2024-06-28 18:01:11 +02:00
|
|
|
fingerprint.WithTempDir(e.TempDir.Fingerprint),
|
2023-03-10 20:27:30 +02:00
|
|
|
fingerprint.WithDry(e.Dry),
|
|
|
|
fingerprint.WithLogger(e.Logger),
|
|
|
|
)
|
2021-07-28 22:39:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if upToDate && preCondMet {
|
2023-04-27 08:23:45 +02:00
|
|
|
if e.Verbose || (!call.Silent && !t.Silent && !e.Taskfile.Silent && !e.Silent) {
|
2023-04-27 02:20:06 +02:00
|
|
|
e.Logger.Errf(logger.Magenta, "task: Task %q is up to date\n", t.Name())
|
2021-07-28 22:39:00 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2024-10-29 15:37:03 +02:00
|
|
|
for _, p := range t.Prompt {
|
|
|
|
if p != "" && !e.Dry {
|
|
|
|
if err := e.Logger.Prompt(logger.Yellow, p, "n", "y", "yes"); errors.Is(err, logger.ErrNoTerminal) {
|
|
|
|
return &errors.TaskCancelledNoTerminalError{TaskName: call.Task}
|
|
|
|
} else if errors.Is(err, logger.ErrPromptCancelled) {
|
|
|
|
return &errors.TaskCancelledByUserError{TaskName: call.Task}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-13 15:37:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
if err := e.mkdir(t); err != nil {
|
2023-04-27 02:20:06 +02:00
|
|
|
e.Logger.Errf(logger.Red, "task: cannot make directory %q: %v\n", t.Dir, err)
|
2021-07-28 22:39:00 +02:00
|
|
|
}
|
2019-06-04 18:58:22 +02:00
|
|
|
|
2024-08-15 03:53:14 +02:00
|
|
|
var deferredExitCode uint8
|
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
for i := range t.Cmds {
|
2021-12-15 07:03:37 +02:00
|
|
|
if t.Cmds[i].Defer {
|
2024-08-15 03:53:14 +02:00
|
|
|
defer e.runDeferred(t, call, i, &deferredExitCode)
|
2021-12-15 07:03:37 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-28 22:39:00 +02:00
|
|
|
if err := e.runCommand(ctx, t, call, i); err != nil {
|
|
|
|
if err2 := e.statusOnError(t); err2 != nil {
|
2023-04-27 02:20:06 +02:00
|
|
|
e.Logger.VerboseErrf(logger.Yellow, "task: error cleaning status on error: %v\n", err2)
|
2021-07-28 22:39:00 +02:00
|
|
|
}
|
2018-08-05 17:53:42 +02:00
|
|
|
|
2024-08-15 03:53:14 +02:00
|
|
|
exitCode, isExitError := interp.IsExitStatus(err)
|
|
|
|
if isExitError {
|
|
|
|
if t.IgnoreError {
|
|
|
|
e.Logger.VerboseErrf(logger.Yellow, "task: task error ignored: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
deferredExitCode = exitCode
|
2021-07-28 22:39:00 +02:00
|
|
|
}
|
2018-08-05 17:53:42 +02:00
|
|
|
|
2024-01-11 02:32:49 +02:00
|
|
|
if call.Indirect {
|
2023-07-20 01:08:22 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-15 22:22:25 +02:00
|
|
|
return &errors.TaskRunError{TaskName: t.Task, Err: err}
|
2021-07-28 22:39:00 +02:00
|
|
|
}
|
2017-02-27 01:43:50 +02:00
|
|
|
}
|
2023-04-27 02:20:06 +02:00
|
|
|
e.Logger.VerboseErrf(logger.Magenta, "task: %q finished\n", call.Task)
|
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
|
|
|
|
2023-12-29 22:32:03 +02:00
|
|
|
func (e *Executor) mkdir(t *ast.Task) error {
|
2019-06-16 02:12:54 +02:00
|
|
|
if t.Dir == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex := e.mkdirMutexMap[t.Task]
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
|
|
|
|
if _, err := os.Stat(t.Dir); os.IsNotExist(err) {
|
2022-08-17 19:37:58 +02:00
|
|
|
if err := os.MkdirAll(t.Dir, 0o755); err != nil {
|
2019-06-16 02:12:54 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-29 22:32:03 +02:00
|
|
|
func (e *Executor) runDeps(ctx context.Context, t *ast.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-04-13 01:39:52 +02:00
|
|
|
g.Go(func() error {
|
2024-01-26 16:34:18 +02:00
|
|
|
err := e.RunTask(ctx, &ast.Call{Task: d.Task, Vars: d.Vars, Silent: d.Silent, Indirect: true})
|
2019-05-17 22:13:47 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-08-15 03:53:14 +02:00
|
|
|
func (e *Executor) runDeferred(t *ast.Task, call *ast.Call, i int, deferredExitCode *uint8) {
|
2022-01-04 21:56:13 +02:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2021-12-15 07:03:37 +02:00
|
|
|
defer cancel()
|
2022-01-04 21:56:13 +02:00
|
|
|
|
2024-09-19 03:17:53 +02:00
|
|
|
origTask, err := e.GetTask(call)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-15 03:53:14 +02:00
|
|
|
cmd := t.Cmds[i]
|
2024-09-19 14:22:39 +02:00
|
|
|
vars, _ := e.Compiler.GetVariables(origTask, call)
|
2024-08-15 03:53:14 +02:00
|
|
|
cache := &templater.Cache{Vars: vars}
|
|
|
|
extra := map[string]any{}
|
|
|
|
|
|
|
|
if deferredExitCode != nil && *deferredExitCode > 0 {
|
|
|
|
extra["EXIT_CODE"] = fmt.Sprintf("%d", *deferredExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Cmd = templater.ReplaceWithExtra(cmd.Cmd, cache, extra)
|
|
|
|
|
2021-12-15 07:03:37 +02:00
|
|
|
if err := e.runCommand(ctx, t, call, i); err != nil {
|
2023-04-27 02:20:06 +02:00
|
|
|
e.Logger.VerboseErrf(logger.Yellow, "task: ignored error in deferred cmd: %s\n", err.Error())
|
2021-12-15 07:03:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 16:34:18 +02:00
|
|
|
func (e *Executor) runCommand(ctx context.Context, t *ast.Task, call *ast.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()
|
|
|
|
|
2024-01-26 16:34:18 +02:00
|
|
|
err := e.RunTask(ctx, &ast.Call{Task: cmd.Task, Vars: cmd.Vars, Silent: cmd.Silent, Indirect: true})
|
2019-05-17 22:13:47 +02:00
|
|
|
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 != "":
|
2023-01-07 02:39:57 +02:00
|
|
|
if !shouldRunOnCurrentPlatform(cmd.Platforms) {
|
2023-04-27 02:20:06 +02:00
|
|
|
e.Logger.VerboseOutf(logger.Yellow, "task: [%s] %s not for current platform - ignored\n", t.Name(), cmd.Cmd)
|
2023-01-07 02:38:35 +02:00
|
|
|
return nil
|
|
|
|
}
|
2023-01-07 02:39:57 +02:00
|
|
|
|
2023-04-27 08:23:45 +02:00
|
|
|
if e.Verbose || (!call.Silent && !cmd.Silent && !t.Silent && !e.Taskfile.Silent && !e.Silent) {
|
2023-04-27 02:20:06 +02:00
|
|
|
e.Logger.Errf(logger.Green, "task: [%s] %s\n", 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
|
|
|
|
}
|
|
|
|
|
2021-09-27 02:55:31 +02:00
|
|
|
outputWrapper := e.Output
|
2021-09-01 18:28:38 +02:00
|
|
|
if t.Interactive {
|
2021-09-27 02:55:31 +02:00
|
|
|
outputWrapper = output.Interleaved{}
|
2021-09-01 18:28:38 +02:00
|
|
|
}
|
2022-01-14 02:11:47 +02:00
|
|
|
vars, err := e.Compiler.FastGetVariables(t, call)
|
2024-03-10 19:11:07 +02:00
|
|
|
outputTemplater := &templater.Cache{Vars: vars}
|
2022-01-14 02:11:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("task: failed to get variables: %w", err)
|
|
|
|
}
|
2022-07-06 15:43:32 +02:00
|
|
|
stdOut, stdErr, close := outputWrapper.WrapWriter(e.Stdout, e.Stderr, t.Prefix, outputTemplater)
|
2018-06-24 15:29:46 +02:00
|
|
|
|
2022-01-14 02:11:47 +02:00
|
|
|
err = execext.RunCommand(ctx, &execext.RunCommandOptions{
|
2023-01-14 21:41:56 +02:00
|
|
|
Command: cmd.Cmd,
|
|
|
|
Dir: t.Dir,
|
2023-03-10 20:27:30 +02:00
|
|
|
Env: env.Get(t),
|
2023-01-14 21:41:56 +02:00
|
|
|
PosixOpts: slicesext.UniqueJoin(e.Taskfile.Set, t.Set, cmd.Set),
|
|
|
|
BashOpts: slicesext.UniqueJoin(e.Taskfile.Shopt, t.Shopt, cmd.Shopt),
|
|
|
|
Stdin: e.Stdin,
|
|
|
|
Stdout: stdOut,
|
|
|
|
Stderr: stdErr,
|
2018-06-24 15:29:46 +02:00
|
|
|
})
|
2023-03-09 03:37:04 +02:00
|
|
|
if closeErr := close(err); closeErr != nil {
|
2023-04-27 02:20:06 +02:00
|
|
|
e.Logger.Errf(logger.Red, "task: unable to close writer: %v\n", closeErr)
|
2023-03-09 03:37:04 +02:00
|
|
|
}
|
2024-08-15 03:53:14 +02:00
|
|
|
if _, isExitError := interp.IsExitStatus(err); isExitError && cmd.IgnoreError {
|
2023-04-27 02:20:06 +02:00
|
|
|
e.Logger.VerboseErrf(logger.Yellow, "task: [%s] command error ignored: %v\n", 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
|
|
|
|
2023-12-29 22:32:03 +02:00
|
|
|
func (e *Executor) startExecution(ctx context.Context, t *ast.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()
|
2021-08-01 01:29:59 +02:00
|
|
|
|
2023-03-02 02:53:38 +02:00
|
|
|
if otherExecutionCtx, ok := e.executionHashes[h]; ok {
|
2021-08-01 01:29:59 +02:00
|
|
|
e.executionHashesMutex.Unlock()
|
2023-04-27 02:20:06 +02:00
|
|
|
e.Logger.VerboseErrf(logger.Magenta, "task: skipping execution of task: %s\n", h)
|
2023-03-02 02:53:38 +02:00
|
|
|
|
|
|
|
// Release our execution slot to avoid blocking other tasks while we wait
|
|
|
|
reacquire := e.releaseConcurrencyLimit()
|
|
|
|
defer reacquire()
|
|
|
|
|
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
|
|
|
}
|
2022-10-02 00:39:44 +02:00
|
|
|
|
|
|
|
// GetTask will return the task with the name matching the given call from the taskfile.
|
|
|
|
// If no task is found, it will search for tasks with a matching alias.
|
|
|
|
// If multiple tasks contain the same alias or no matches are found an error is returned.
|
2024-01-26 16:34:18 +02:00
|
|
|
func (e *Executor) GetTask(call *ast.Call) (*ast.Task, error) {
|
2022-10-02 00:39:44 +02:00
|
|
|
// Search for a matching task
|
2024-02-22 22:52:05 +02:00
|
|
|
matchingTasks := e.Taskfile.Tasks.FindMatchingTasks(call)
|
|
|
|
switch len(matchingTasks) {
|
|
|
|
case 0: // Carry on
|
|
|
|
case 1:
|
|
|
|
if call.Vars == nil {
|
|
|
|
call.Vars = &ast.Vars{}
|
|
|
|
}
|
|
|
|
call.Vars.Set("MATCH", ast.Var{Value: matchingTasks[0].Wildcards})
|
|
|
|
return matchingTasks[0].Task, nil
|
|
|
|
default:
|
|
|
|
taskNames := make([]string, len(matchingTasks))
|
|
|
|
for i, matchingTask := range matchingTasks {
|
|
|
|
taskNames[i] = matchingTask.Task.Task
|
|
|
|
}
|
|
|
|
return nil, &errors.TaskNameConflictError{
|
|
|
|
Call: call.Task,
|
|
|
|
TaskNames: taskNames,
|
|
|
|
}
|
2022-10-02 00:39:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If didn't find one, search for a task with a matching alias
|
2024-02-22 22:52:05 +02:00
|
|
|
var matchingTask *ast.Task
|
2022-10-02 00:39:44 +02:00
|
|
|
var aliasedTasks []string
|
2023-04-06 13:07:57 +02:00
|
|
|
for _, task := range e.Taskfile.Tasks.Values() {
|
2022-10-02 00:39:44 +02:00
|
|
|
if slices.Contains(task.Aliases, call.Task) {
|
|
|
|
aliasedTasks = append(aliasedTasks, task.Task)
|
|
|
|
matchingTask = task
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we found multiple tasks
|
|
|
|
if len(aliasedTasks) > 1 {
|
2023-04-15 22:22:25 +02:00
|
|
|
return nil, &errors.TaskNameConflictError{
|
2024-02-22 22:52:05 +02:00
|
|
|
Call: call.Task,
|
2023-04-15 22:22:25 +02:00
|
|
|
TaskNames: aliasedTasks,
|
2022-10-02 00:39:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we found no tasks
|
|
|
|
if len(aliasedTasks) == 0 {
|
2022-10-15 01:08:00 +02:00
|
|
|
didYouMean := ""
|
|
|
|
if e.fuzzyModel != nil {
|
|
|
|
didYouMean = e.fuzzyModel.SpellCheck(call.Task)
|
|
|
|
}
|
2023-04-15 22:22:25 +02:00
|
|
|
return nil, &errors.TaskNotFoundError{
|
|
|
|
TaskName: call.Task,
|
|
|
|
DidYouMean: didYouMean,
|
2022-10-02 00:39:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchingTask, nil
|
|
|
|
}
|
2022-11-02 16:38:26 +02:00
|
|
|
|
2023-12-29 22:32:03 +02:00
|
|
|
type FilterFunc func(task *ast.Task) bool
|
2022-11-02 16:38:26 +02:00
|
|
|
|
2023-12-29 22:32:03 +02:00
|
|
|
func (e *Executor) GetTaskList(filters ...FilterFunc) ([]*ast.Task, error) {
|
|
|
|
tasks := make([]*ast.Task, 0, e.Taskfile.Tasks.Len())
|
2022-11-02 16:38:26 +02:00
|
|
|
|
2023-01-14 21:45:52 +02:00
|
|
|
// Create an error group to wait for each task to be compiled
|
|
|
|
var g errgroup.Group
|
|
|
|
|
2023-04-06 13:07:57 +02:00
|
|
|
// Filter tasks based on the given filter functions
|
|
|
|
for _, task := range e.Taskfile.Tasks.Values() {
|
|
|
|
var shouldFilter bool
|
|
|
|
for _, filter := range filters {
|
|
|
|
if filter(task) {
|
|
|
|
shouldFilter = true
|
2023-01-14 21:45:52 +02:00
|
|
|
}
|
2023-04-06 13:07:57 +02:00
|
|
|
}
|
|
|
|
if !shouldFilter {
|
|
|
|
tasks = append(tasks, task)
|
|
|
|
}
|
|
|
|
}
|
2023-01-14 21:45:52 +02:00
|
|
|
|
2023-04-06 13:07:57 +02:00
|
|
|
// Compile the list of tasks
|
|
|
|
for i := range tasks {
|
|
|
|
g.Go(func() error {
|
2024-08-14 15:37:05 +02:00
|
|
|
compiledTask, err := e.FastCompiledTask(&ast.Call{Task: tasks[i].Task})
|
2023-12-02 04:26:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-01-14 21:45:52 +02:00
|
|
|
}
|
2024-08-14 15:37:05 +02:00
|
|
|
tasks[i] = compiledTask
|
2023-01-14 21:45:52 +02:00
|
|
|
return nil
|
|
|
|
})
|
2022-11-02 16:38:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-14 21:45:52 +02:00
|
|
|
// Wait for all the go routines to finish
|
|
|
|
if err := g.Wait(); err != nil {
|
|
|
|
return nil, err
|
2022-11-02 16:38:26 +02:00
|
|
|
}
|
|
|
|
|
2023-04-06 13:07:57 +02:00
|
|
|
// Sort the tasks
|
|
|
|
if e.TaskSorter == nil {
|
|
|
|
e.TaskSorter = &sort.AlphaNumericWithRootTasksFirst{}
|
|
|
|
}
|
|
|
|
e.TaskSorter.Sort(tasks)
|
2022-11-02 16:38:26 +02:00
|
|
|
|
2023-01-14 21:45:52 +02:00
|
|
|
return tasks, nil
|
2022-11-02 16:38:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FilterOutNoDesc removes all tasks that do not contain a description.
|
2023-12-29 22:32:03 +02:00
|
|
|
func FilterOutNoDesc(task *ast.Task) bool {
|
2023-01-14 21:45:52 +02:00
|
|
|
return task.Desc == ""
|
2022-11-02 16:38:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FilterOutInternal removes all tasks that are marked as internal.
|
2023-12-29 22:32:03 +02:00
|
|
|
func FilterOutInternal(task *ast.Task) bool {
|
2023-01-14 21:45:52 +02:00
|
|
|
return task.Internal
|
2022-11-02 16:38:26 +02:00
|
|
|
}
|
2023-01-07 02:38:35 +02:00
|
|
|
|
2023-12-29 22:32:03 +02:00
|
|
|
func shouldRunOnCurrentPlatform(platforms []*ast.Platform) bool {
|
2023-01-07 02:38:35 +02:00
|
|
|
if len(platforms) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2023-01-07 02:39:57 +02:00
|
|
|
for _, p := range platforms {
|
|
|
|
if (p.OS == "" || p.OS == runtime.GOOS) && (p.Arch == "" || p.Arch == runtime.GOARCH) {
|
2023-01-07 02:38:35 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|