2017-02-27 09:48:50 -03:00
|
|
|
package task
|
2017-02-26 20:43:50 -03:00
|
|
|
|
|
|
|
import (
|
2017-03-06 13:48:56 +01:00
|
|
|
"fmt"
|
2017-02-26 20:43:50 -03:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2017-03-07 08:01:20 +01:00
|
|
|
"strings"
|
2017-02-26 20:43:50 -03:00
|
|
|
|
2017-03-01 20:43:27 -03:00
|
|
|
"github.com/spf13/pflag"
|
2017-02-26 20:43:50 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-02-28 09:50:40 -03:00
|
|
|
// TaskFilePath is the default Taskfile
|
2017-02-28 08:13:48 -08:00
|
|
|
TaskFilePath = "Taskfile"
|
2017-02-28 09:50:40 -03:00
|
|
|
// ShExists is true if Bash was found
|
|
|
|
ShExists bool
|
|
|
|
// ShPath constains the Bash path if found
|
|
|
|
ShPath string
|
2017-02-26 21:18:53 -03:00
|
|
|
|
2017-03-01 20:43:27 -03:00
|
|
|
// Force (--force or -f flag) forces a task to run even when it's up-to-date
|
2017-03-01 20:21:36 -03:00
|
|
|
Force bool
|
|
|
|
|
2017-02-28 09:50:40 -03:00
|
|
|
// Tasks constains the tasks parsed from Taskfile
|
2017-02-26 21:18:53 -03:00
|
|
|
Tasks = make(map[string]*Task)
|
2017-03-02 09:38:23 +01:00
|
|
|
|
2017-03-02 20:45:52 -03:00
|
|
|
runnedTasks = make(map[string]struct{})
|
2017-02-26 20:43:50 -03:00
|
|
|
)
|
|
|
|
|
2017-02-27 10:07:54 -03:00
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
ShPath, err = exec.LookPath("sh")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ShExists = true
|
|
|
|
}
|
|
|
|
|
2017-02-28 09:50:40 -03:00
|
|
|
// Task represents a task
|
2017-02-26 20:43:50 -03:00
|
|
|
type Task struct {
|
|
|
|
Cmds []string
|
|
|
|
Deps []string
|
2017-02-27 17:03:25 -03:00
|
|
|
Sources []string
|
|
|
|
Generates []string
|
2017-03-01 14:33:10 +01:00
|
|
|
Dir string
|
2017-03-05 15:56:07 -03:00
|
|
|
Vars map[string]string
|
2017-03-02 20:19:25 +01:00
|
|
|
Set string
|
2017-03-06 13:48:56 +01:00
|
|
|
Env map[string]string
|
2017-02-26 20:43:50 -03:00
|
|
|
}
|
|
|
|
|
2017-02-28 09:50:40 -03:00
|
|
|
// Run runs Task
|
2017-02-27 09:48:50 -03:00
|
|
|
func Run() {
|
2017-02-26 20:43:50 -03:00
|
|
|
log.SetFlags(0)
|
|
|
|
|
2017-03-01 20:43:27 -03:00
|
|
|
args := pflag.Args()
|
2017-02-26 20:43:50 -03:00
|
|
|
if len(args) == 0 {
|
2017-03-02 20:56:37 -03:00
|
|
|
log.Fatal("task: No argument given")
|
2017-02-26 20:43:50 -03:00
|
|
|
}
|
|
|
|
|
2017-02-28 08:13:48 -08:00
|
|
|
var err error
|
|
|
|
Tasks, err = readTaskfile()
|
2017-02-26 20:43:50 -03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-27 10:13:06 -03:00
|
|
|
for _, a := range args {
|
|
|
|
if err = RunTask(a); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-02-26 20:43:50 -03:00
|
|
|
}
|
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
|
2017-02-26 21:18:53 -03:00
|
|
|
func RunTask(name string) error {
|
2017-03-02 20:45:52 -03:00
|
|
|
if _, found := runnedTasks[name]; found {
|
|
|
|
return &cyclicDepError{name}
|
2017-03-02 09:38:23 +01:00
|
|
|
}
|
2017-03-02 20:45:52 -03:00
|
|
|
runnedTasks[name] = struct{}{}
|
2017-03-02 09:38:23 +01:00
|
|
|
|
2017-02-26 21:18:53 -03:00
|
|
|
t, ok := Tasks[name]
|
2017-02-26 20:43:50 -03:00
|
|
|
if !ok {
|
2017-02-28 09:50:40 -03:00
|
|
|
return &taskNotFoundError{name}
|
2017-02-26 20:43:50 -03:00
|
|
|
}
|
|
|
|
|
2017-03-02 11:30:59 +01:00
|
|
|
vars, err := t.handleVariables()
|
|
|
|
if err != nil {
|
|
|
|
return &taskRunError{name, err}
|
|
|
|
}
|
2017-03-05 10:15:49 +01:00
|
|
|
|
2017-02-26 21:18:53 -03:00
|
|
|
for _, d := range t.Deps {
|
2017-03-05 16:49:44 -03:00
|
|
|
d, err = ReplaceVariables(d, vars)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = RunTask(d); err != nil {
|
2017-02-26 21:18:53 -03:00
|
|
|
return err
|
|
|
|
}
|
2017-02-26 20:43:50 -03:00
|
|
|
}
|
2017-03-05 10:15:49 +01:00
|
|
|
|
2017-03-05 15:59:30 -03:00
|
|
|
if !Force && t.isUpToDate() {
|
2017-03-05 10:15:49 +01:00
|
|
|
log.Printf(`task: Task "%s" is up to date`, name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:52:58 -03:00
|
|
|
for i := range t.Cmds {
|
2017-03-06 13:48:56 +01:00
|
|
|
if err = t.runCommand(i, t.Env); err != nil {
|
2017-02-28 09:50:40 -03:00
|
|
|
return &taskRunError{name, err}
|
2017-02-26 20:43:50 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-27 10:07:54 -03:00
|
|
|
|
2017-03-05 15:59:30 -03:00
|
|
|
func (t *Task) isUpToDate() bool {
|
2017-02-27 17:03:25 -03:00
|
|
|
if len(t.Sources) == 0 || len(t.Generates) == 0 {
|
2017-02-27 16:44:08 -03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-02-27 17:03:25 -03:00
|
|
|
sourcesMaxTime, err := getPatternsMaxTime(t.Sources)
|
2017-02-27 16:44:08 -03:00
|
|
|
if err != nil || sourcesMaxTime.IsZero() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-02-27 17:03:25 -03:00
|
|
|
generatesMinTime, err := getPatternsMinTime(t.Generates)
|
2017-02-27 16:44:08 -03:00
|
|
|
if err != nil || generatesMinTime.IsZero() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return generatesMinTime.After(sourcesMaxTime)
|
|
|
|
}
|
|
|
|
|
2017-03-06 13:48:56 +01:00
|
|
|
func (t *Task) runCommand(i int, envVariables map[string]string) error {
|
2017-03-05 15:52:58 -03:00
|
|
|
vars, err := t.handleVariables()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-05 16:49:44 -03:00
|
|
|
c, err := ReplaceVariables(t.Cmds[i], vars)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dir, err := ReplaceVariables(t.Dir, vars)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var cmd *exec.Cmd
|
2017-02-27 10:07:54 -03:00
|
|
|
if ShExists {
|
|
|
|
cmd = exec.Command(ShPath, "-c", c)
|
|
|
|
} else {
|
|
|
|
cmd = exec.Command("cmd", "/C", c)
|
|
|
|
}
|
2017-03-05 15:52:58 -03:00
|
|
|
if dir != "" {
|
|
|
|
cmd.Dir = dir
|
2017-03-01 13:35:00 +01:00
|
|
|
}
|
2017-03-06 13:48:56 +01:00
|
|
|
if nil != envVariables {
|
|
|
|
env := os.Environ()
|
|
|
|
for key, value := range envVariables {
|
2017-03-06 13:52:46 +01:00
|
|
|
replacedValue, err := ReplaceVariables(value, vars)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
replacedKey, err := ReplaceVariables(key, vars)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
env = append(env, fmt.Sprintf("%s=%s", replacedKey, replacedValue))
|
2017-03-06 13:48:56 +01:00
|
|
|
}
|
|
|
|
cmd.Env = env
|
|
|
|
}
|
2017-03-05 15:52:58 -03:00
|
|
|
cmd.Stdin = os.Stdin
|
2017-02-27 10:07:54 -03:00
|
|
|
cmd.Stderr = os.Stderr
|
2017-03-05 15:52:58 -03:00
|
|
|
if t.Set != "" {
|
|
|
|
bytes, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-07 08:01:20 +01:00
|
|
|
os.Setenv(t.Set, strings.TrimSpace(string(bytes)))
|
2017-03-05 15:52:58 -03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
if err = cmd.Run(); err != nil {
|
|
|
|
return err
|
2017-02-27 10:07:54 -03:00
|
|
|
}
|
2017-03-05 15:52:58 -03:00
|
|
|
return nil
|
2017-02-27 10:07:54 -03:00
|
|
|
}
|