1
0
mirror of https://github.com/go-task/task.git synced 2025-03-05 15:05:42 +02:00

Allow global variables in the Taskfile

Closes #66
This commit is contained in:
Andrey Nering 2018-03-04 15:39:14 -03:00
parent 975f262ac0
commit 1a3df08aca
6 changed files with 23 additions and 9 deletions

View File

@ -16,8 +16,9 @@ import (
var _ compiler.Compiler = &CompilerV2{}
type CompilerV2 struct {
Dir string
Vars taskfile.Vars
Dir string
Taskvars taskfile.Vars
TaskfileVars taskfile.Vars
Logger *logger.Logger
@ -28,12 +29,15 @@ type CompilerV2 struct {
// GetVariables returns fully resolved variables following the priority order:
// 1. Task variables
// 2. Call variables
// 3. Taskvars file variables
// 4. Environment variables
// 3. Taskfile variables
// 4. Taskvars file variables
// 5. Environment variables
func (c *CompilerV2) GetVariables(t *taskfile.Task, call taskfile.Call) (taskfile.Vars, error) {
vr := varResolver{c: c, vars: compiler.GetEnviron()}
vr.merge(c.Vars)
vr.merge(c.Vars)
vr.merge(c.Taskvars)
vr.merge(c.Taskvars)
vr.merge(c.TaskfileVars)
vr.merge(c.TaskfileVars)
vr.merge(call.Vars)
vr.merge(call.Vars)
vr.merge(t.Vars)

View File

@ -3,6 +3,7 @@ package taskfile
// Taskfile represents a Taskfile.yml
type Taskfile struct {
Version string
Vars Vars
Tasks Tasks
}
@ -15,12 +16,14 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error {
var taskfile struct {
Version string
Vars Vars
Tasks Tasks
}
if err := unmarshal(&taskfile); err != nil {
return err
}
tf.Version = taskfile.Version
tf.Vars = taskfile.Vars
tf.Tasks = taskfile.Tasks
return nil
}

View File

@ -109,9 +109,10 @@ func (e *Executor) setup() error {
}
case version.IsV2(v):
e.Compiler = &compilerv2.CompilerV2{
Dir: e.Dir,
Vars: e.taskvars,
Logger: e.Logger,
Dir: e.Dir,
Taskvars: e.taskvars,
TaskfileVars: e.Taskfile.Vars,
Logger: e.Logger,
}
if !e.Silent {

View File

@ -132,6 +132,7 @@ func TestVarsV2(t *testing.T) {
"shtmpl2_foo2.txt": "foo2",
"nestedtmpl2_foo2.txt": "<no value>",
"override.txt": "bar",
"nested.txt": "Taskvars-TaskfileVars-TaskVars",
},
}
tt.Run(t)

View File

@ -1,4 +1,6 @@
version: '2'
vars:
NESTED2: "{{.NESTED1}}-TaskfileVars"
tasks:
default:
deps: [hello]
@ -27,6 +29,7 @@ tasks:
- echo '{{.SHTMPL2_FOO2}}' > shtmpl2_foo2.txt
- echo '{{.NESTEDTMPL2_FOO2}}' > nestedtmpl2_foo2.txt
- echo {{.OVERRIDE}} > override.txt
- echo '{{.NESTED3}}' > nested.txt
vars:
FOO: foo
BAR: $echo bar
@ -43,6 +46,7 @@ tasks:
NESTEDTMPL_FOO: "{{.TMPL_FOO}}"
NESTEDTMPL_FOO2: "{{.TMPL2_FOO2}}"
OVERRIDE: "bar"
NESTED3: "{{.NESTED2}}-TaskVars"
invalid-var-tmpl:
vars:

View File

@ -10,3 +10,4 @@ SHTMPL2_FOO2:
sh: "echo '{{.FOO2}}'"
NESTEDTMPL2_FOO2: "{{.TMPL2_FOO2}}"
OVERRIDE: "foo"
NESTED1: "Taskvars"