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

244 lines
4.2 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 (
"bytes"
"context"
"fmt"
2017-02-27 01:43:50 +02:00
"log"
"os"
"strings"
2017-02-27 01:43:50 +02:00
2017-03-12 22:18:59 +02:00
"github.com/go-task/task/execext"
"golang.org/x/sync/errgroup"
2017-02-27 01:43:50 +02:00
)
const (
2017-02-28 14:50:40 +02:00
// TaskFilePath is the default Taskfile
TaskFilePath = "Taskfile"
)
2017-02-27 02:18:53 +02:00
// Executor executes a Taskfile
type Executor struct {
Tasks map[string]*Task
Force bool
2017-04-01 21:04:52 +02:00
Watch bool
watchingFiles map[string]struct{}
}
2017-02-27 01:43:50 +02:00
2017-02-28 14:50:40 +02:00
// Task represents a task
2017-02-27 01:43:50 +02:00
type Task struct {
Cmds []string
Deps []string
Desc string
Sources []string
Generates []string
Status []string
2017-03-01 15:33:10 +02:00
Dir string
Vars map[string]string
2017-03-02 21:19:25 +02:00
Set string
Env map[string]string
2017-02-27 01:43:50 +02:00
}
2017-02-28 14:50:40 +02:00
// Run runs Task
func (e *Executor) Run(args ...string) error {
if e.HasCyclicDep() {
return ErrCyclicDependencyDetected
}
// check if given tasks exist
for _, a := range args {
if _, ok := e.Tasks[a]; !ok {
// FIXME: move to the main package
e.printExistingTasksHelp()
return &taskNotFoundError{taskName: a}
}
}
if e.Watch {
if err := e.watchTasks(args...); err != nil {
return err
2017-04-01 21:04:52 +02:00
}
return nil
2017-04-01 21:04:52 +02:00
}
2017-02-27 15:13:06 +02:00
for _, a := range args {
if err := e.RunTask(context.Background(), a); err != nil {
return err
2017-02-27 15:13:06 +02:00
}
2017-02-27 01:43:50 +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
func (e *Executor) RunTask(ctx context.Context, name string) error {
t, ok := e.Tasks[name]
2017-02-27 01:43:50 +02:00
if !ok {
2017-02-28 14:50:40 +02:00
return &taskNotFoundError{name}
2017-02-27 01:43:50 +02:00
}
if err := e.runDeps(ctx, name); 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 {
2017-05-17 19:53:39 +02:00
upToDate, err := t.isUpToDate(ctx)
if err != nil {
return err
}
if upToDate {
log.Printf(`task: Task "%s" is up to date`, name)
return nil
}
2017-03-05 11:15:49 +02:00
}
for i := range t.Cmds {
if err := e.runCommand(ctx, name, i); err != nil {
2017-02-28 14:50:40 +02:00
return &taskRunError{name, err}
2017-02-27 01:43:50 +02:00
}
}
return nil
}
func (e *Executor) runDeps(ctx context.Context, task string) error {
g, ctx := errgroup.WithContext(ctx)
t := e.Tasks[task]
2017-03-16 01:19:29 +02:00
for _, d := range t.Deps {
dep := d
2017-03-16 01:19:29 +02:00
g.Go(func() error {
2017-05-01 00:13:21 +02:00
dep, err := t.ReplaceVariables(dep)
2017-03-16 01:19:29 +02:00
if err != nil {
return err
2017-03-16 01:19:29 +02:00
}
if err = e.RunTask(ctx, dep); err != nil {
return err
2017-03-16 01:19:29 +02:00
}
return nil
})
2017-03-16 01:19:29 +02:00
}
2017-05-01 00:13:21 +02:00
if err := g.Wait(); err != nil {
2017-03-16 01:19:29 +02:00
return err
}
return nil
2017-03-16 01:19:29 +02:00
}
2017-05-17 19:53:39 +02:00
func (t *Task) isUpToDate(ctx context.Context) (bool, error) {
if len(t.Status) > 0 {
environ, err := t.getEnviron()
if err != nil {
return false, err
}
for _, s := range t.Status {
err = execext.RunCommand(&execext.RunCommandOptions{
2017-05-17 19:53:39 +02:00
Context: ctx,
Command: s,
Dir: t.Dir,
Env: environ,
})
if err != nil {
return false, nil
}
}
return true, nil
}
if len(t.Sources) == 0 || len(t.Generates) == 0 {
return false, nil
}
sources, err := t.ReplaceSliceVariables(t.Sources)
if err != nil {
return false, err
}
generates, err := t.ReplaceSliceVariables(t.Generates)
if err != nil {
return false, err
}
sourcesMaxTime, err := getPatternsMaxTime(sources)
if err != nil || sourcesMaxTime.IsZero() {
return false, nil
}
generatesMinTime, err := getPatternsMinTime(generates)
if err != nil || generatesMinTime.IsZero() {
return false, nil
}
return generatesMinTime.After(sourcesMaxTime), nil
}
func (e *Executor) runCommand(ctx context.Context, task string, i int) error {
t := e.Tasks[task]
2017-05-01 00:13:21 +02:00
c, err := t.ReplaceVariables(t.Cmds[i])
if err != nil {
return err
}
2017-03-25 20:26:42 +02:00
if strings.HasPrefix(c, "^") {
c = strings.TrimPrefix(c, "^")
if err = e.RunTask(ctx, c); err != nil {
2017-03-25 20:26:42 +02:00
return err
}
return nil
}
2017-05-01 00:13:21 +02:00
dir, err := t.ReplaceVariables(t.Dir)
if err != nil {
return err
}
2017-05-01 00:13:21 +02:00
envs, err := t.getEnviron()
if err != nil {
return err
2017-03-01 14:35:00 +02:00
}
opts := &execext.RunCommandOptions{
Context: ctx,
Command: c,
Dir: dir,
Env: envs,
Stdin: os.Stdin,
Stderr: os.Stderr,
}
if t.Set == "" {
log.Println(c)
opts.Stdout = os.Stdout
if err = execext.RunCommand(opts); err != nil {
return err
}
} else {
buff := bytes.NewBuffer(nil)
opts.Stdout = buff
if err = execext.RunCommand(opts); err != nil {
return err
}
os.Setenv(t.Set, strings.TrimSpace(buff.String()))
}
return nil
}
2017-05-01 00:13:21 +02:00
func (t *Task) getEnviron() ([]string, error) {
if t.Env == nil {
return nil, nil
}
envs := os.Environ()
for k, v := range t.Env {
2017-05-01 00:13:21 +02:00
env, err := t.ReplaceVariables(fmt.Sprintf("%s=%s", k, v))
if err != nil {
return nil, err
}
2017-05-01 00:13:21 +02:00
envs = append(envs, env)
}
return envs, nil
}