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

216 lines
3.7 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 (
"fmt"
2017-02-27 01:43:50 +02:00
"log"
"os"
"strings"
2017-03-16 01:19:29 +02:00
"sync"
2017-02-27 01:43:50 +02:00
2017-03-12 22:18:59 +02:00
"github.com/go-task/task/execext"
"github.com/spf13/pflag"
2017-02-27 01:43:50 +02:00
)
var (
2017-02-28 14:50:40 +02:00
// TaskFilePath is the default Taskfile
TaskFilePath = "Taskfile"
2017-02-27 02:18:53 +02:00
// Force (--force or -f flag) forces a task to run even when it's up-to-date
Force bool
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-03-02 10:38:23 +02:00
2017-03-03 01:45:52 +02:00
runnedTasks = make(map[string]struct{})
2017-03-16 01:19:29 +02:00
mu sync.Mutex
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
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
2017-02-27 14:48:50 +02:00
func Run() {
2017-02-27 01:43:50 +02:00
log.SetFlags(0)
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
}
var err error
Tasks, err = readTaskfile()
2017-02-27 01:43:50 +02:00
if err != nil {
log.Fatal(err)
}
// 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-02-27 15:13:06 +02:00
for _, a := range args {
if err = RunTask(a); err != nil {
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-02-27 02:18:53 +02:00
func RunTask(name string) error {
2017-03-16 01:19:29 +02:00
mu.Lock()
2017-03-03 01:45:52 +02:00
if _, found := runnedTasks[name]; found {
2017-03-16 01:19:29 +02:00
mu.Unlock()
2017-03-03 01:45:52 +02:00
return &cyclicDepError{name}
2017-03-02 10:38:23 +02:00
}
2017-03-03 01:45:52 +02:00
runnedTasks[name] = struct{}{}
2017-03-16 01:19:29 +02:00
mu.Unlock()
2017-03-02 10:38:23 +02:00
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-03-16 01:19:29 +02:00
if err := t.runDeps(); err != nil {
return err
2017-02-27 01:43:50 +02:00
}
2017-03-05 11:15:49 +02:00
if !Force && t.isUpToDate() {
2017-03-05 11:15:49 +02:00
log.Printf(`task: Task "%s" is up to date`, name)
return nil
}
for i := range t.Cmds {
2017-03-16 01:19:29 +02:00
if err := t.runCommand(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-03-16 01:19:29 +02:00
func (t *Task) runDeps() error {
vars, err := t.handleVariables()
if err != nil {
return err
}
var (
wg sync.WaitGroup
errChan = make(chan error)
doneChan = make(chan struct{})
)
for _, d := range t.Deps {
wg.Add(1)
go func(dep string) {
defer wg.Done()
dep, err := ReplaceVariables(dep, vars)
if err != nil {
errChan <- err
return
}
if err := RunTask(dep); err != nil {
errChan <- err
}
}(d)
}
go func() {
wg.Wait()
doneChan <- struct{}{}
}()
select {
case err := <-errChan:
return err
case <-doneChan:
return nil
}
}
func (t *Task) isUpToDate() bool {
if len(t.Sources) == 0 || len(t.Generates) == 0 {
return false
}
sourcesMaxTime, err := getPatternsMaxTime(t.Sources)
if err != nil || sourcesMaxTime.IsZero() {
return false
}
generatesMinTime, err := getPatternsMinTime(t.Generates)
if err != nil || generatesMinTime.IsZero() {
return false
}
return generatesMinTime.After(sourcesMaxTime)
}
func (t *Task) runCommand(i int) error {
vars, err := t.handleVariables()
if err != nil {
return err
}
c, err := ReplaceVariables(t.Cmds[i], vars)
if err != nil {
return err
}
dir, err := ReplaceVariables(t.Dir, vars)
if err != nil {
return err
}
2017-03-12 22:18:59 +02:00
cmd := execext.NewCommand(c)
if dir != "" {
cmd.Dir = dir
2017-03-01 14:35:00 +02:00
}
if t.Env != nil {
cmd.Env = os.Environ()
for key, value := range t.Env {
2017-03-06 14:52:46 +02:00
replacedValue, err := ReplaceVariables(value, vars)
if err != nil {
return err
}
replacedKey, err := ReplaceVariables(key, vars)
if err != nil {
return err
}
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", replacedKey, replacedValue))
}
}
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
if t.Set != "" {
bytes, err := cmd.Output()
if err != nil {
return err
}
os.Setenv(t.Set, strings.TrimSpace(string(bytes)))
return nil
}
cmd.Stdout = os.Stdout
2017-03-12 23:02:33 +02:00
log.Println(c)
if err = cmd.Run(); err != nil {
return err
}
return nil
}