From 5268df6bfdf2b7aa5e43af015fa29cf0e41fd34e Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sat, 8 Jul 2017 16:00:17 -0300 Subject: [PATCH] only get variable map twice and implement vars template ref #40 --- task.go | 14 ++++++++++++++ variables.go | 30 ++++++++++++++++++------------ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/task.go b/task.go index dcf88d49..b330bda6 100644 --- a/task.go +++ b/task.go @@ -116,10 +116,24 @@ func (e *Executor) RunTask(ctx context.Context, call Call) error { return &MaximumTaskCallExceededError{task: call.Task} } + var err error + call.Vars, err = e.getVariables(call) + if err != nil { + return err + } + if err := e.runDeps(ctx, call); err != nil { return err } + // FIXME: doing again, since a var may have been overriden + // using the `set:` attribute of a dependecy. + // Remove this when `set` (that is deprecated) be removed + call.Vars, err = e.getVariables(call) + if err != nil { + return err + } + if !e.Force { upToDate, err := e.isTaskUpToDate(ctx, call) if err != nil { diff --git a/variables.go b/variables.go index eb936c59..705240bf 100644 --- a/variables.go +++ b/variables.go @@ -21,7 +21,9 @@ var ( ErrMultilineResultCmd = errors.New("Got multiline result from command") ) -var templateFuncs template.FuncMap +var ( + templateFuncs template.FuncMap +) func init() { taskFuncs := template.FuncMap{ @@ -51,18 +53,13 @@ func init() { // ReplaceVariables writes vars into initial string func (e *Executor) ReplaceVariables(initial string, call Call) (string, error) { - vars, err := e.getVariables(call) - if err != nil { - return "", err - } - templ, err := template.New("").Funcs(templateFuncs).Parse(initial) if err != nil { return "", err } var b bytes.Buffer - if err = templ.Execute(&b, vars); err != nil { + if err = templ.Execute(&b, call.Vars); err != nil { return "", err } return b.String(), nil @@ -85,27 +82,36 @@ func (e *Executor) getVariables(call Call) (Vars, error) { t := e.Tasks[call.Task] result := make(Vars, len(t.Vars)+len(e.taskvars)+len(call.Vars)) - merge := func(vars Vars) error { + merge := func(vars Vars, runTemplate bool) error { for k, v := range vars { + if runTemplate { + var err error + v, err = e.ReplaceVariables(v, call) + if err != nil { + return err + } + } + v, err := e.handleDynamicVariableContent(v) if err != nil { return err } + result[k] = v } return nil } - if err := merge(e.taskvars); err != nil { + if err := merge(e.taskvars, false); err != nil { return nil, err } - if err := merge(t.Vars); err != nil { + if err := merge(t.Vars, true); err != nil { return nil, err } - if err := merge(getEnvironmentVariables()); err != nil { + if err := merge(getEnvironmentVariables(), false); err != nil { return nil, err } - if err := merge(call.Vars); err != nil { + if err := merge(call.Vars, false); err != nil { return nil, err }