1
0
mirror of https://github.com/go-task/task.git synced 2024-12-04 10:24:45 +02:00
task/task.go

503 lines
12 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 (
"bufio"
"context"
"fmt"
"io"
2017-02-27 01:43:50 +02:00
"os"
"runtime"
"strings"
2019-06-16 02:12:54 +02:00
"sync"
"sync/atomic"
2022-12-31 18:48:49 +02:00
"time"
2017-02-27 01:43:50 +02:00
2023-04-15 22:22:25 +02:00
"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/compiler"
"github.com/go-task/task/v3/internal/env"
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/fingerprint"
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/internal/output"
"github.com/go-task/task/v3/internal/slicesext"
"github.com/go-task/task/v3/internal/sort"
"github.com/go-task/task/v3/internal/summary"
"github.com/go-task/task/v3/internal/templater"
"github.com/go-task/task/v3/internal/term"
"github.com/go-task/task/v3/taskfile"
2017-03-12 22:18:59 +02:00
"github.com/sajari/fuzzy"
"golang.org/x/exp/slices"
"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
func shouldPromptContinue(input string) bool {
input = strings.ToLower(strings.TrimSpace(input))
return slices.Contains([]string{"y", "yes"}, input)
}
// Executor executes a Taskfile
type Executor struct {
Taskfile *taskfile.Taskfile
2020-06-12 20:09:53 +02:00
Dir string
TempDir string
2020-06-12 20:09:53 +02:00
Entrypoint string
Force bool
ForceAll bool
2020-06-12 20:09:53 +02:00
Watch bool
Verbose bool
Silent bool
AssumeYes bool
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
AssumesTerm 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
2022-02-20 00:31:27 +02:00
OutputStyle taskfile.Output
TaskSorter sort.TaskSorter
taskvars *taskfile.Vars
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-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 _, 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
}
}
return err
}
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}
}
}
2019-02-24 16:33:09 +02:00
if e.Summary {
for i, c := range calls {
compiledTask, err := e.FastCompiledTask(c)
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
}
if e.Watch {
return e.watchTasks(calls...)
}
g, ctx := errgroup.WithContext(ctx)
for _, c := range calls {
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
}
}
}
return g.Wait()
}
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[t.Task], 1) >= MaximumTaskCall {
2023-04-15 22:22:25 +02:00
return &errors.TaskCalledTooManyTimesError{TaskName: t.Task}
}
2020-06-12 20:09:53 +02:00
release := e.acquireConcurrencyLimit()
defer release()
if t.Prompt != "" && !e.AssumeYes {
if !e.AssumesTerm && !term.IsTerminal() {
return &errors.TaskCancelledNoTerminalError{TaskName: call.Task}
}
2023-06-27 11:53:06 +02:00
e.Logger.Outf(logger.Yellow, "task: %q [y/N]: ", t.Prompt)
reader := bufio.NewReader(e.Stdin)
userInput, err := reader.ReadString('\n')
if err != nil {
return err
}
userInput = strings.ToLower(strings.TrimSpace(userInput))
if !shouldPromptContinue(userInput) {
return &errors.TaskCancelledByUserError{TaskName: call.Task}
}
}
2021-07-28 22:39:00 +02:00
return e.startExecution(ctx, t, func(ctx context.Context) error {
if !shouldRunOnCurrentPlatform(t.Platforms) {
e.Logger.VerboseOutf(logger.Yellow, `task: %q not for current platform - ignored\n`, call.Task)
return nil
}
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 {
return err
}
2019-05-17 22:13:47 +02:00
skipFingerprinting := e.ForceAll || (call.Direct && e.Force)
if !skipFingerprinting {
if err := ctx.Err(); err != nil {
return err
}
if err := e.areTaskRequiredVarsSet(ctx, t, call); err != nil {
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
// 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),
fingerprint.WithTempDir(e.TempDir),
fingerprint.WithDry(e.Dry),
fingerprint.WithLogger(e.Logger),
)
2021-07-28 22:39:00 +02:00
if err != nil {
return err
}
if upToDate && preCondMet {
if e.Verbose || (!call.Silent && !t.Silent && !e.Taskfile.Silent && !e.Silent) {
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-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\n", t.Dir, err)
2021-07-28 22:39:00 +02:00
}
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 {
defer e.runDeferred(t, call, i)
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 {
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
2021-07-28 22:39:00 +02:00
if execext.IsExitError(err) && t.IgnoreError {
e.Logger.VerboseErrf(logger.Yellow, "task: task error ignored: %v\n", err)
2021-07-28 22:39:00 +02:00
continue
}
2018-08-05 17:53:42 +02:00
2023-07-20 01:08:22 +02:00
if !call.Direct {
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
}
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
}
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) {
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
}
func (e *Executor) runDeps(ctx context.Context, t *taskfile.Task) error {
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
g.Go(func() error {
err := e.RunTask(ctx, taskfile.Call{Task: d.Task, Vars: d.Vars, Silent: d.Silent})
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-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
}
2021-12-15 07:03:37 +02:00
func (e *Executor) runDeferred(t *taskfile.Task, call taskfile.Call, i int) {
ctx, cancel := context.WithCancel(context.Background())
2021-12-15 07:03:37 +02:00
defer cancel()
2021-12-15 07:03:37 +02:00
if err := e.runCommand(ctx, t, call, i); err != nil {
e.Logger.VerboseErrf(logger.Yellow, "task: ignored error in deferred cmd: %s\n", err.Error())
2021-12-15 07:03:37 +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 != "":
2020-06-12 20:09:53 +02:00
reacquire := e.releaseConcurrencyLimit()
defer reacquire()
err := e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars, Silent: cmd.Silent})
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 != "":
if !shouldRunOnCurrentPlatform(cmd.Platforms) {
e.Logger.VerboseOutf(logger.Yellow, "task: [%s] %s not for current platform - ignored\n", t.Name(), cmd.Cmd)
return nil
}
if e.Verbose || (!call.Silent && !cmd.Silent && !t.Silent && !e.Taskfile.Silent && !e.Silent) {
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 {
return nil
}
outputWrapper := e.Output
if t.Interactive {
outputWrapper = output.Interleaved{}
}
vars, err := e.Compiler.FastGetVariables(t, call)
outputTemplater := &templater.Templater{Vars: vars, RemoveNoValue: true}
if err != nil {
return fmt.Errorf("task: failed to get variables: %w", err)
}
stdOut, stdErr, close := outputWrapper.WrapWriter(e.Stdout, e.Stderr, t.Prefix, outputTemplater)
2018-06-24 15:29:46 +02:00
err = execext.RunCommand(ctx, &execext.RunCommandOptions{
Command: cmd.Cmd,
Dir: t.Dir,
Env: env.Get(t),
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 {
e.Logger.Errf(logger.Red, "task: unable to close writer: %v\n", closeErr)
2023-03-09 03:37:04 +02:00
}
2018-09-01 16:02:23 +02:00
if execext.IsExitError(err) && cmd.IgnoreError {
e.Logger.VerboseErrf(logger.Yellow, "task: [%s] command error ignored: %v\n", t.Name(), err)
return nil
}
return err
2018-06-24 15:29:46 +02:00
default:
return nil
2017-09-16 19:05:07 +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()
if otherExecutionCtx, ok := e.executionHashes[h]; ok {
e.executionHashesMutex.Unlock()
e.Logger.VerboseErrf(logger.Magenta, "task: skipping execution of task: %s\n", h)
// 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-07-28 22:39:00 +02:00
e.executionHashes[h] = ctx
e.executionHashesMutex.Unlock()
2021-07-28 22:39:00 +02:00
return execute(ctx)
2020-08-17 21:25:17 +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.
func (e *Executor) GetTask(call taskfile.Call) (*taskfile.Task, error) {
// Search for a matching task
matchingTask := e.Taskfile.Tasks.Get(call.Task)
if matchingTask != nil {
return matchingTask, nil
}
// If didn't find one, search for a task with a matching alias
var aliasedTasks []string
for _, task := range e.Taskfile.Tasks.Values() {
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{
AliasName: call.Task,
TaskNames: aliasedTasks,
}
}
// If we found no tasks
if len(aliasedTasks) == 0 {
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,
}
}
return matchingTask, nil
}
2023-01-14 21:45:52 +02:00
type FilterFunc func(task *taskfile.Task) bool
2023-01-14 21:45:52 +02:00
func (e *Executor) GetTaskList(filters ...FilterFunc) ([]*taskfile.Task, error) {
tasks := make([]*taskfile.Task, 0, e.Taskfile.Tasks.Len())
2023-01-14 21:45:52 +02:00
// Create an error group to wait for each task to be compiled
var g errgroup.Group
// 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
}
}
if !shouldFilter {
tasks = append(tasks, task)
}
}
2023-01-14 21:45:52 +02:00
// Compile the list of tasks
for i := range tasks {
task := tasks[i]
g.Go(func() error {
2023-01-14 21:45:52 +02:00
compiledTask, err := e.FastCompiledTask(taskfile.Call{Task: task.Task})
if err == nil {
task = compiledTask
}
task = compiledTask
2023-01-14 21:45:52 +02:00
return nil
})
}
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
}
// Sort the tasks
if e.TaskSorter == nil {
e.TaskSorter = &sort.AlphaNumericWithRootTasksFirst{}
}
e.TaskSorter.Sort(tasks)
2023-01-14 21:45:52 +02:00
return tasks, nil
}
// FilterOutNoDesc removes all tasks that do not contain a description.
2023-01-14 21:45:52 +02:00
func FilterOutNoDesc(task *taskfile.Task) bool {
return task.Desc == ""
}
// FilterOutInternal removes all tasks that are marked as internal.
2023-01-14 21:45:52 +02:00
func FilterOutInternal(task *taskfile.Task) bool {
return task.Internal
}
func shouldRunOnCurrentPlatform(platforms []*taskfile.Platform) bool {
if len(platforms) == 0 {
return true
}
for _, p := range platforms {
if (p.OS == "" || p.OS == runtime.GOOS) && (p.Arch == "" || p.Arch == runtime.GOARCH) {
return true
}
}
return false
}