1
0
mirror of https://github.com/go-task/task.git synced 2025-08-10 22:42:19 +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{} var _ compiler.Compiler = &CompilerV2{}
type CompilerV2 struct { type CompilerV2 struct {
Dir string Dir string
Vars taskfile.Vars Taskvars taskfile.Vars
TaskfileVars taskfile.Vars
Logger *logger.Logger Logger *logger.Logger
@@ -28,12 +29,15 @@ type CompilerV2 struct {
// GetVariables returns fully resolved variables following the priority order: // GetVariables returns fully resolved variables following the priority order:
// 1. Task variables // 1. Task variables
// 2. Call variables // 2. Call variables
// 3. Taskvars file variables // 3. Taskfile variables
// 4. Environment variables // 4. Taskvars file variables
// 5. Environment variables
func (c *CompilerV2) GetVariables(t *taskfile.Task, call taskfile.Call) (taskfile.Vars, error) { func (c *CompilerV2) GetVariables(t *taskfile.Task, call taskfile.Call) (taskfile.Vars, error) {
vr := varResolver{c: c, vars: compiler.GetEnviron()} vr := varResolver{c: c, vars: compiler.GetEnviron()}
vr.merge(c.Vars) vr.merge(c.Taskvars)
vr.merge(c.Vars) vr.merge(c.Taskvars)
vr.merge(c.TaskfileVars)
vr.merge(c.TaskfileVars)
vr.merge(call.Vars) vr.merge(call.Vars)
vr.merge(call.Vars) vr.merge(call.Vars)
vr.merge(t.Vars) vr.merge(t.Vars)

View File

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

View File

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

View File

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

View File

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

View File

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