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

119 lines
2.9 KiB
Go
Raw Normal View History

2017-03-02 11:46:20 +02:00
package task
import (
"path/filepath"
"strings"
"github.com/go-task/task/v2/internal/execext"
2018-11-05 01:23:35 +02:00
"github.com/go-task/task/v2/internal/taskfile"
"github.com/go-task/task/v2/internal/templater"
2017-03-02 11:46:20 +02:00
)
// CompiledTask returns a copy of a task, but replacing variables in almost all
// properties using the Go template package.
func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
origTask, ok := e.Taskfile.Tasks[call.Task]
if !ok {
return nil, &taskNotFoundError{call.Task}
}
vars, err := e.Compiler.GetVariables(origTask, call)
if err != nil {
return nil, err
}
r := templater.Templater{Vars: vars}
new := taskfile.Task{
2018-08-05 17:53:42 +02:00
Task: origTask.Task,
Desc: r.Replace(origTask.Desc),
Sources: r.ReplaceSlice(origTask.Sources),
Generates: r.ReplaceSlice(origTask.Generates),
Dir: r.Replace(origTask.Dir),
Vars: nil,
Env: nil,
2018-08-05 17:53:42 +02:00
Silent: origTask.Silent,
Method: r.Replace(origTask.Method),
Prefix: r.Replace(origTask.Prefix),
IgnoreError: origTask.IgnoreError,
}
new.Dir, err = execext.Expand(new.Dir)
if err != nil {
return nil, err
}
if e.Dir != "" && !filepath.IsAbs(new.Dir) {
new.Dir = filepath.Join(e.Dir, new.Dir)
}
if new.Prefix == "" {
new.Prefix = new.Task
}
new.Env = make(taskfile.Vars, len(e.Taskfile.Env)+len(origTask.Env))
for k, v := range r.ReplaceVars(e.Taskfile.Env) {
new.Env[k] = v
}
for k, v := range r.ReplaceVars(origTask.Env) {
new.Env[k] = v
}
for k, v := range new.Env {
static, err := e.Compiler.HandleDynamicVar(v)
if err != nil {
return nil, err
}
new.Env[k] = taskfile.Var{Static: static}
}
if len(origTask.Cmds) > 0 {
new.Cmds = make([]*taskfile.Cmd, len(origTask.Cmds))
for i, cmd := range origTask.Cmds {
new.Cmds[i] = &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,
}
}
}
if len(origTask.Deps) > 0 {
new.Deps = make([]*taskfile.Dep, len(origTask.Deps))
for i, dep := range origTask.Deps {
new.Deps[i] = &taskfile.Dep{
Task: r.Replace(dep.Task),
Vars: r.ReplaceVars(dep.Vars),
}
}
}
2019-05-28 22:02:59 +02:00
if len(origTask.Preconditions) > 0 {
new.Preconditions = make([]*taskfile.Precondition, len(origTask.Preconditions))
for i, precond := range origTask.Preconditions {
new.Preconditions[i] = &taskfile.Precondition{
Sh: r.Replace(precond.Sh),
Msg: r.Replace(precond.Msg),
2019-05-17 22:13:47 +02:00
}
}
}
2019-08-25 19:30:00 +02:00
if len(origTask.Status) > 0 {
checker, err := e.getStatusChecker(&new)
if err != nil {
return nil, err
}
value, err := checker.Value()
if err != nil {
return nil, err
}
2019-08-25 22:16:59 +02:00
vars[strings.ToUpper(checker.Kind())] = taskfile.Var{Live: value}
// Adding new variables, requires us to refresh the templaters
// cache of the the values manually
r.RefreshCacheMap()
2019-08-25 19:30:00 +02:00
new.Status = r.ReplaceSlice(origTask.Status)
}
return &new, r.Err()
}