1
0
mirror of https://github.com/go-task/task.git synced 2025-11-23 22:24:45 +02:00
Files
task/task.go

191 lines
3.5 KiB
Go
Raw Normal View History

2017-02-27 09:48:50 -03:00
package task
2017-02-26 20:43:50 -03:00
import (
"context"
"fmt"
"io"
2017-02-26 20:43:50 -03:00
"os"
2017-07-05 21:03:59 -03:00
"sync"
"sync/atomic"
2017-02-26 20:43:50 -03:00
"github.com/go-task/task/internal/execext"
2017-03-12 17:18:59 -03:00
"golang.org/x/sync/errgroup"
2017-02-26 20:43:50 -03:00
)
const (
2017-02-28 09:50:40 -03:00
// TaskFilePath is the default Taskfile
TaskFilePath = "Taskfile"
// 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-26 21:18:53 -03:00
// Executor executes a Taskfile
type Executor struct {
2017-07-05 20:55:50 -03:00
Tasks Tasks
Dir string
Force bool
Watch bool
Verbose bool
Silent bool
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
taskvars Vars
2017-07-05 21:03:59 -03:00
taskCallCount map[string]*int32
dynamicCache map[string]string
2017-07-05 21:03:59 -03:00
muDynamicCache sync.Mutex
}
2017-02-26 20:43:50 -03:00
2017-06-04 16:41:38 -03:00
// Tasks representas a group of tasks
type Tasks map[string]*Task
2017-02-28 09:50:40 -03:00
// Task represents a task
2017-02-26 20:43:50 -03:00
type Task struct {
Task string
2017-07-02 15:30:50 -03:00
Cmds []*Cmd
Deps []*Dep
Desc string
Sources []string
Generates []string
Status []string
2017-03-01 14:33:10 +01:00
Dir string
2017-07-05 20:07:24 -03:00
Vars Vars
Env Vars
Silent bool
Method string
2017-02-26 20:43:50 -03:00
}
2017-02-28 09:50:40 -03:00
// Run runs Task
func (e *Executor) Run(calls ...Call) error {
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.taskCallCount = make(map[string]*int32, len(e.Tasks))
for k := range e.Tasks {
e.taskCallCount[k] = new(int32)
}
2017-07-05 21:03:59 -03:00
if e.dynamicCache == nil {
e.dynamicCache = make(map[string]string, 10)
2017-07-05 21:03:59 -03:00
}
// check if given tasks exist
for _, c := range calls {
if _, ok := e.Tasks[c.Task]; !ok {
// FIXME: move to the main package
e.PrintTasksHelp()
return &taskNotFoundError{taskName: c.Task}
}
}
if e.Watch {
return e.watchTasks(calls...)
2017-04-01 16:04:52 -03:00
}
for _, c := range calls {
if err := e.RunTask(context.TODO(), c); err != nil {
return err
2017-02-27 10:13:06 -03:00
}
2017-02-26 20:43:50 -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
func (e *Executor) RunTask(ctx context.Context, call 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-15 20:19:29 -03:00
return err
2017-02-26 20:43:50 -03:00
}
2017-03-05 10:15:49 +01:00
if !e.Force {
upToDate, err := t.isUpToDate(ctx)
if err != nil {
return err
}
if upToDate {
if !e.Silent {
e.errf(`task: Task "%s" is up to date`, t.Task)
}
return nil
}
2017-03-05 10:15:49 +01:00
}
for i := range t.Cmds {
if err := e.runCommand(ctx, t, call, i); err != nil {
if err2 := t.statusOnError(); err2 != nil {
e.verboseErrf("task: error cleaning status on error: %v", err2)
}
return &taskRunError{t.Task, err}
2017-02-26 20:43:50 -03:00
}
}
return nil
}
func (e *Executor) runDeps(ctx context.Context, t *Task) error {
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
g.Go(func() error {
return e.RunTask(ctx, Call{Task: d.Task, Vars: d.Vars})
})
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
}
func (e *Executor) runCommand(ctx context.Context, t *Task, call Call, i int) error {
2017-07-02 15:30:50 -03:00
cmd := t.Cmds[i]
2017-07-02 15:30:50 -03:00
if cmd.Cmd == "" {
return e.RunTask(ctx, Call{Task: cmd.Task, Vars: cmd.Vars})
2017-03-25 15:26:42 -03:00
}
2017-09-16 14:05:07 -03:00
if e.Verbose || (!cmd.Silent && !t.Silent && !e.Silent) {
e.errf(cmd.Cmd)
2017-09-16 14:05:07 -03:00
}
return execext.RunCommand(&execext.RunCommandOptions{
Context: ctx,
Command: cmd.Cmd,
Dir: t.Dir,
Env: t.getEnviron(),
Stdin: e.Stdin,
2017-09-16 14:05:07 -03:00
Stdout: e.Stdout,
Stderr: e.Stderr,
2017-09-16 14:05:07 -03:00
})
}
func (t *Task) getEnviron() []string {
if t.Env == nil {
return nil
}
envs := os.Environ()
for k, v := range t.Env.toStringMap() {
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
}
return envs
}