2017-03-02 11:46:20 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
2017-04-16 22:16:56 +02:00
|
|
|
"path/filepath"
|
2019-06-11 20:49:37 +02:00
|
|
|
"strings"
|
2017-03-02 12:28:34 +02:00
|
|
|
|
2020-08-16 20:48:19 +02:00
|
|
|
"github.com/go-task/task/v3/internal/execext"
|
|
|
|
"github.com/go-task/task/v3/internal/status"
|
|
|
|
"github.com/go-task/task/v3/internal/templater"
|
2020-08-19 10:59:58 +02:00
|
|
|
"github.com/go-task/task/v3/taskfile"
|
2017-03-02 11:46:20 +02:00
|
|
|
)
|
|
|
|
|
2017-09-03 12:48:06 +02:00
|
|
|
// CompiledTask returns a copy of a task, but replacing variables in almost all
|
|
|
|
// properties using the Go template package.
|
2018-02-17 18:22:18 +02:00
|
|
|
func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
|
2021-01-12 17:03:04 +02:00
|
|
|
return e.compiledTask(call, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FastCompiledTask is like CompiledTask, but it skippes dynamic variables.
|
|
|
|
func (e *Executor) FastCompiledTask(call taskfile.Call) (*taskfile.Task, error) {
|
|
|
|
return e.compiledTask(call, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskfile.Task, error) {
|
2017-12-29 22:27:32 +02:00
|
|
|
origTask, ok := e.Taskfile.Tasks[call.Task]
|
2017-08-16 13:04:58 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, &taskNotFoundError{call.Task}
|
|
|
|
}
|
|
|
|
|
2021-01-12 17:03:04 +02:00
|
|
|
var vars *taskfile.Vars
|
|
|
|
var err error
|
|
|
|
if evaluateShVars {
|
|
|
|
vars, err = e.Compiler.GetVariables(origTask, call)
|
|
|
|
} else {
|
|
|
|
vars, err = e.Compiler.FastGetVariables(origTask, call)
|
|
|
|
}
|
2017-11-02 14:25:50 +02:00
|
|
|
if err != nil {
|
2017-08-16 13:04:58 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-11 20:49:37 +02:00
|
|
|
|
2020-05-17 20:42:27 +02:00
|
|
|
v, err := e.Taskfile.ParsedVersion()
|
2020-05-16 20:45:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := templater.Templater{Vars: vars, RemoveNoValue: v >= 3.0}
|
2017-07-20 09:05:37 +02:00
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
new := taskfile.Task{
|
2018-08-05 17:53:42 +02:00
|
|
|
Task: origTask.Task,
|
2020-06-03 22:19:12 +02:00
|
|
|
Label: r.Replace(origTask.Label),
|
2018-08-05 17:53:42 +02:00
|
|
|
Desc: r.Replace(origTask.Desc),
|
2020-01-20 15:38:18 +02:00
|
|
|
Summary: r.Replace(origTask.Summary),
|
2018-08-05 17:53:42 +02:00
|
|
|
Sources: r.ReplaceSlice(origTask.Sources),
|
|
|
|
Generates: r.ReplaceSlice(origTask.Generates),
|
|
|
|
Dir: r.Replace(origTask.Dir),
|
|
|
|
Vars: nil,
|
2019-01-02 16:05:40 +02:00
|
|
|
Env: nil,
|
2018-08-05 17:53:42 +02:00
|
|
|
Silent: origTask.Silent,
|
2021-09-01 18:28:38 +02:00
|
|
|
Interactive: origTask.Interactive,
|
2018-08-05 17:53:42 +02:00
|
|
|
Method: r.Replace(origTask.Method),
|
|
|
|
Prefix: r.Replace(origTask.Prefix),
|
|
|
|
IgnoreError: origTask.IgnoreError,
|
2020-08-17 21:25:17 +02:00
|
|
|
Run: r.Replace(origTask.Run),
|
2017-07-16 21:09:55 +02:00
|
|
|
}
|
2018-12-24 19:19:53 +02:00
|
|
|
new.Dir, err = execext.Expand(new.Dir)
|
2017-11-02 14:25:50 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-16 13:04:58 +02:00
|
|
|
if e.Dir != "" && !filepath.IsAbs(new.Dir) {
|
|
|
|
new.Dir = filepath.Join(e.Dir, new.Dir)
|
|
|
|
}
|
2018-04-22 20:41:53 +02:00
|
|
|
if new.Prefix == "" {
|
|
|
|
new.Prefix = new.Task
|
|
|
|
}
|
2019-01-02 16:05:40 +02:00
|
|
|
|
2020-03-29 21:54:59 +02:00
|
|
|
new.Env = &taskfile.Vars{}
|
|
|
|
new.Env.Merge(r.ReplaceVars(e.Taskfile.Env))
|
|
|
|
new.Env.Merge(r.ReplaceVars(origTask.Env))
|
2021-01-12 17:03:04 +02:00
|
|
|
if evaluateShVars {
|
|
|
|
err = new.Env.Range(func(k string, v taskfile.Var) error {
|
|
|
|
static, err := e.Compiler.HandleDynamicVar(v, new.Dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
new.Env.Set(k, taskfile.Var{Static: static})
|
|
|
|
return nil
|
|
|
|
})
|
2017-08-16 13:04:58 +02:00
|
|
|
if err != nil {
|
2021-01-12 17:03:04 +02:00
|
|
|
return nil, err
|
2017-08-16 13:04:58 +02:00
|
|
|
}
|
2017-07-31 00:45:01 +02:00
|
|
|
}
|
2017-07-16 21:09:55 +02:00
|
|
|
|
2017-08-16 13:04:58 +02:00
|
|
|
if len(origTask.Cmds) > 0 {
|
2021-09-14 17:01:33 +02:00
|
|
|
new.Cmds = make([]*taskfile.Cmd, 0, len(origTask.Cmds))
|
|
|
|
for _, cmd := range origTask.Cmds {
|
|
|
|
if cmd == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
new.Cmds = append(new.Cmds, &taskfile.Cmd{
|
2018-07-10 10:44:58 +02:00
|
|
|
Task: r.Replace(cmd.Task),
|
|
|
|
Silent: cmd.Silent,
|
|
|
|
Cmd: r.Replace(cmd.Cmd),
|
|
|
|
Vars: r.ReplaceVars(cmd.Vars),
|
|
|
|
IgnoreError: cmd.IgnoreError,
|
2021-12-15 07:03:37 +02:00
|
|
|
Defer: cmd.Defer,
|
2021-09-14 17:01:33 +02:00
|
|
|
})
|
2017-07-16 21:09:55 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-16 13:04:58 +02:00
|
|
|
if len(origTask.Deps) > 0 {
|
2021-09-14 17:01:33 +02:00
|
|
|
new.Deps = make([]*taskfile.Dep, 0, len(origTask.Deps))
|
|
|
|
for _, dep := range origTask.Deps {
|
|
|
|
if dep == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
new.Deps = append(new.Deps, &taskfile.Dep{
|
2018-02-17 20:12:41 +02:00
|
|
|
Task: r.Replace(dep.Task),
|
|
|
|
Vars: r.ReplaceVars(dep.Vars),
|
2021-09-14 17:01:33 +02:00
|
|
|
})
|
2017-07-16 21:09:55 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-28 22:02:59 +02:00
|
|
|
|
|
|
|
if len(origTask.Preconditions) > 0 {
|
2021-09-14 17:01:33 +02:00
|
|
|
new.Preconditions = make([]*taskfile.Precondition, 0, len(origTask.Preconditions))
|
|
|
|
for _, precond := range origTask.Preconditions {
|
|
|
|
if precond == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
new.Preconditions = append(new.Preconditions, &taskfile.Precondition{
|
2019-05-28 22:02:59 +02:00
|
|
|
Sh: r.Replace(precond.Sh),
|
|
|
|
Msg: r.Replace(precond.Msg),
|
2021-09-14 17:01:33 +02:00
|
|
|
})
|
2019-05-17 22:13:47 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-16 21:09:55 +02:00
|
|
|
|
2019-08-25 19:30:00 +02:00
|
|
|
if len(origTask.Status) > 0 {
|
2019-09-14 23:04:41 +02:00
|
|
|
for _, checker := range []status.Checker{e.timestampChecker(&new), e.checksumChecker(&new)} {
|
|
|
|
value, err := checker.Value()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-29 21:54:59 +02:00
|
|
|
vars.Set(strings.ToUpper(checker.Kind()), taskfile.Var{Live: value})
|
2019-08-25 19:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-08-25 22:16:59 +02:00
|
|
|
// Adding new variables, requires us to refresh the templaters
|
|
|
|
// cache of the the values manually
|
2019-09-14 22:54:41 +02:00
|
|
|
r.ResetCache()
|
2019-08-25 19:30:00 +02:00
|
|
|
|
|
|
|
new.Status = r.ReplaceSlice(origTask.Status)
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:12:41 +02:00
|
|
|
return &new, r.Err()
|
2017-09-03 12:48:06 +02:00
|
|
|
}
|