2017-02-27 14:48:50 +02:00
|
|
|
package task
|
2017-02-27 01:43:50 +02:00
|
|
|
|
|
|
|
import (
|
2017-04-22 20:46:29 +02:00
|
|
|
"bytes"
|
2017-04-13 01:32:56 +02:00
|
|
|
"context"
|
2017-03-06 14:48:56 +02:00
|
|
|
"fmt"
|
2017-02-27 01:43:50 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
2017-03-07 09:01:20 +02:00
|
|
|
"strings"
|
2017-02-27 01:43:50 +02:00
|
|
|
|
2017-03-12 22:18:59 +02:00
|
|
|
"github.com/go-task/task/execext"
|
|
|
|
|
2017-03-02 01:43:27 +02:00
|
|
|
"github.com/spf13/pflag"
|
2017-04-13 01:39:52 +02:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-02-27 01:43:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-02-28 14:50:40 +02:00
|
|
|
// TaskFilePath is the default Taskfile
|
2017-02-28 18:13:48 +02:00
|
|
|
TaskFilePath = "Taskfile"
|
2017-02-27 02:18:53 +02:00
|
|
|
|
2017-03-02 01:43:27 +02:00
|
|
|
// Force (--force or -f flag) forces a task to run even when it's up-to-date
|
2017-03-02 01:21:36 +02:00
|
|
|
Force bool
|
2017-04-01 21:04:52 +02:00
|
|
|
// Watch (--watch or -w flag) enables watch of a task
|
|
|
|
Watch bool
|
2017-03-02 01:21:36 +02:00
|
|
|
|
2017-02-28 14:50:40 +02:00
|
|
|
// Tasks constains the tasks parsed from Taskfile
|
2017-02-27 02:18:53 +02:00
|
|
|
Tasks = make(map[string]*Task)
|
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
|
2017-03-06 23:25:50 +02:00
|
|
|
Desc string
|
2017-02-27 22:03:25 +02:00
|
|
|
Sources []string
|
|
|
|
Generates []string
|
2017-05-17 19:37:11 +02:00
|
|
|
Status []string
|
2017-03-01 15:33:10 +02:00
|
|
|
Dir string
|
2017-03-05 20:56:07 +02:00
|
|
|
Vars map[string]string
|
2017-03-02 21:19:25 +02:00
|
|
|
Set string
|
2017-03-06 14:48:56 +02:00
|
|
|
Env map[string]string
|
2017-02-27 01:43:50 +02:00
|
|
|
}
|
|
|
|
|
2017-02-28 14:50:40 +02:00
|
|
|
// Run runs Task
|
2017-02-27 14:48:50 +02:00
|
|
|
func Run() {
|
2017-02-27 01:43:50 +02:00
|
|
|
log.SetFlags(0)
|
|
|
|
|
2017-03-02 01:43:27 +02:00
|
|
|
args := pflag.Args()
|
2017-02-27 01:43:50 +02:00
|
|
|
if len(args) == 0 {
|
2017-03-09 18:18:46 +02:00
|
|
|
log.Println("task: No argument given, trying default task")
|
|
|
|
args = []string{"default"}
|
2017-02-27 01:43:50 +02:00
|
|
|
}
|
|
|
|
|
2017-02-28 18:13:48 +02:00
|
|
|
var err error
|
|
|
|
Tasks, err = readTaskfile()
|
2017-02-27 01:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-03-25 21:06:49 +02:00
|
|
|
if HasCyclicDep(Tasks) {
|
|
|
|
log.Fatal("Cyclic dependency detected")
|
|
|
|
}
|
|
|
|
|
2017-03-19 20:18:18 +02:00
|
|
|
// check if given tasks exist
|
|
|
|
for _, a := range args {
|
|
|
|
if _, ok := Tasks[a]; !ok {
|
|
|
|
var err error = &taskNotFoundError{taskName: a}
|
|
|
|
fmt.Println(err)
|
|
|
|
printExistingTasksHelp()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 21:04:52 +02:00
|
|
|
if Watch {
|
|
|
|
if err := WatchTasks(args); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-27 15:13:06 +02:00
|
|
|
for _, a := range args {
|
2017-04-13 01:53:41 +02:00
|
|
|
if err = RunTask(context.Background(), a); err != nil {
|
2017-02-27 15:13:06 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-02-27 01:43:50 +02:00
|
|
|
}
|
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
|
2017-04-13 01:53:41 +02:00
|
|
|
func RunTask(ctx context.Context, name string) error {
|
2017-02-27 02:18:53 +02:00
|
|
|
t, ok := 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
|
|
|
}
|
|
|
|
|
2017-04-13 01:53:41 +02:00
|
|
|
if err := t.runDeps(ctx); 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
|
|
|
|
2017-05-01 00:32:28 +02:00
|
|
|
if !Force {
|
|
|
|
upToDate, err := t.isUpToDate()
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-05 20:52:58 +02:00
|
|
|
for i := range t.Cmds {
|
2017-04-13 01:53:41 +02:00
|
|
|
if err := t.runCommand(ctx, 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
|
|
|
|
}
|
2017-02-27 15:07:54 +02:00
|
|
|
|
2017-04-13 01:53:41 +02:00
|
|
|
func (t *Task) runDeps(ctx context.Context) error {
|
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
2017-03-16 01:19:29 +02:00
|
|
|
|
|
|
|
for _, d := range t.Deps {
|
2017-04-13 01:39:52 +02:00
|
|
|
dep := d
|
2017-03-16 01:19:29 +02:00
|
|
|
|
2017-04-13 01:39:52 +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 {
|
2017-04-13 01:39:52 +02:00
|
|
|
return err
|
2017-03-16 01:19:29 +02:00
|
|
|
}
|
|
|
|
|
2017-04-13 01:53:41 +02:00
|
|
|
if err = RunTask(ctx, dep); err != nil {
|
2017-04-13 01:39:52 +02:00
|
|
|
return err
|
2017-03-16 01:19:29 +02:00
|
|
|
}
|
2017-04-13 01:39:52 +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
|
|
|
|
}
|
2017-04-13 01:39:52 +02:00
|
|
|
return nil
|
2017-03-16 01:19:29 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 00:32:28 +02:00
|
|
|
func (t *Task) isUpToDate() (bool, error) {
|
2017-05-17 19:37:11 +02:00
|
|
|
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{
|
|
|
|
Command: s,
|
|
|
|
Dir: t.Dir,
|
|
|
|
Env: environ,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2017-02-27 22:03:25 +02:00
|
|
|
if len(t.Sources) == 0 || len(t.Generates) == 0 {
|
2017-05-01 00:32:28 +02:00
|
|
|
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
|
2017-02-27 21:44:08 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 00:32:28 +02:00
|
|
|
sourcesMaxTime, err := getPatternsMaxTime(sources)
|
2017-02-27 21:44:08 +02:00
|
|
|
if err != nil || sourcesMaxTime.IsZero() {
|
2017-05-01 00:32:28 +02:00
|
|
|
return false, nil
|
2017-02-27 21:44:08 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 00:32:28 +02:00
|
|
|
generatesMinTime, err := getPatternsMinTime(generates)
|
2017-02-27 21:44:08 +02:00
|
|
|
if err != nil || generatesMinTime.IsZero() {
|
2017-05-01 00:32:28 +02:00
|
|
|
return false, nil
|
2017-02-27 21:44:08 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 00:32:28 +02:00
|
|
|
return generatesMinTime.After(sourcesMaxTime), nil
|
2017-02-27 21:44:08 +02:00
|
|
|
}
|
|
|
|
|
2017-04-13 01:53:41 +02:00
|
|
|
func (t *Task) runCommand(ctx context.Context, i int) error {
|
2017-05-01 00:13:21 +02:00
|
|
|
c, err := t.ReplaceVariables(t.Cmds[i])
|
2017-03-05 21:49:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-25 20:26:42 +02:00
|
|
|
|
|
|
|
if strings.HasPrefix(c, "^") {
|
2017-03-25 21:06:49 +02:00
|
|
|
c = strings.TrimPrefix(c, "^")
|
2017-04-13 01:53:41 +02:00
|
|
|
if err = 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)
|
2017-03-05 21:49:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-22 20:46:29 +02:00
|
|
|
|
2017-05-01 00:13:21 +02:00
|
|
|
envs, err := t.getEnviron()
|
2017-04-22 20:46:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-03-01 14:35:00 +02:00
|
|
|
}
|
2017-04-22 20:46:29 +02:00
|
|
|
opts := &execext.RunCommandOptions{
|
|
|
|
Context: ctx,
|
|
|
|
Command: c,
|
|
|
|
Dir: dir,
|
|
|
|
Env: envs,
|
|
|
|
Stdin: os.Stdin,
|
|
|
|
Stderr: os.Stderr,
|
2017-03-06 14:48:56 +02:00
|
|
|
}
|
2017-04-22 20:46:29 +02:00
|
|
|
|
|
|
|
if t.Set == "" {
|
|
|
|
log.Println(c)
|
|
|
|
opts.Stdout = os.Stdout
|
|
|
|
if err = execext.RunCommand(opts); err != nil {
|
2017-03-05 20:52:58 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-04-22 20:46:29 +02:00
|
|
|
} 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()))
|
2017-02-27 15:07:54 +02:00
|
|
|
}
|
2017-03-05 20:52:58 +02:00
|
|
|
return nil
|
2017-02-27 15:07:54 +02:00
|
|
|
}
|
2017-04-22 20:46:29 +02:00
|
|
|
|
2017-05-01 00:13:21 +02:00
|
|
|
func (t *Task) getEnviron() ([]string, error) {
|
2017-04-22 20:46:29 +02:00
|
|
|
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))
|
2017-04-22 20:46:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-01 00:13:21 +02:00
|
|
|
envs = append(envs, env)
|
2017-04-22 20:46:29 +02:00
|
|
|
}
|
|
|
|
return envs, nil
|
|
|
|
}
|