Skip to content

Commit 6ed30f1

Browse files
committed
Refactor variables: Keep order of declaration
This shouldn't have any behavior changes for now. This is a code refactor that should allow us to do further improvements on how variables are handled, specially regarding respecting the declaration order in Taskfiles, which should make it easier for the users. Initial work on #218
1 parent a044c41 commit 6ed30f1

File tree

17 files changed

+188
-115
lines changed

17 files changed

+188
-115
lines changed

cmd/task/task.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ func main() {
140140
}
141141

142142
calls, globals := args.Parse(pflag.Args()...)
143-
for name, value := range globals {
144-
e.Taskfile.Vars[name] = value
145-
}
143+
e.Taskfile.Vars.Merge(globals)
146144

147145
ctx := context.Background()
148146
if !watch {

internal/args/args.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
)
88

99
// Parse parses command line argument: tasks and vars of each task
10-
func Parse(args ...string) ([]taskfile.Call, taskfile.Vars) {
10+
func Parse(args ...string) ([]taskfile.Call, *taskfile.Vars) {
1111
var calls []taskfile.Call
12-
var globals taskfile.Vars
12+
var globals *taskfile.Vars
1313

1414
for _, arg := range args {
1515
if !strings.Contains(arg, "=") {
@@ -19,18 +19,16 @@ func Parse(args ...string) ([]taskfile.Call, taskfile.Vars) {
1919

2020
if len(calls) < 1 {
2121
if globals == nil {
22-
globals = taskfile.Vars{}
22+
globals = &taskfile.Vars{}
2323
}
24-
2524
name, value := splitVar(arg)
26-
globals[name] = taskfile.Var{Static: value}
25+
globals.Set(name, taskfile.Var{Static: value})
2726
} else {
2827
if calls[len(calls)-1].Vars == nil {
29-
calls[len(calls)-1].Vars = make(taskfile.Vars)
28+
calls[len(calls)-1].Vars = &taskfile.Vars{}
3029
}
31-
32-
name, value := splitVar((arg))
33-
calls[len(calls)-1].Vars[name] = taskfile.Var{Static: value}
30+
name, value := splitVar(arg)
31+
calls[len(calls)-1].Vars.Set(name, taskfile.Var{Static: value})
3432
}
3533
}
3634

internal/args/args_test.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestArgs(t *testing.T) {
1414
tests := []struct {
1515
Args []string
1616
ExpectedCalls []taskfile.Call
17-
ExpectedGlobals taskfile.Vars
17+
ExpectedGlobals *taskfile.Vars
1818
}{
1919
{
2020
Args: []string{"task-a", "task-b", "task-c"},
@@ -29,16 +29,22 @@ func TestArgs(t *testing.T) {
2929
ExpectedCalls: []taskfile.Call{
3030
{
3131
Task: "task-a",
32-
Vars: taskfile.Vars{
33-
"FOO": taskfile.Var{Static: "bar"},
32+
Vars: &taskfile.Vars{
33+
Keys: []string{"FOO"},
34+
Mapping: map[string]taskfile.Var{
35+
"FOO": taskfile.Var{Static: "bar"},
36+
},
3437
},
3538
},
3639
{Task: "task-b"},
3740
{
3841
Task: "task-c",
39-
Vars: taskfile.Vars{
40-
"BAR": taskfile.Var{Static: "baz"},
41-
"BAZ": taskfile.Var{Static: "foo"},
42+
Vars: &taskfile.Vars{
43+
Keys: []string{"BAR", "BAZ"},
44+
Mapping: map[string]taskfile.Var{
45+
"BAR": taskfile.Var{Static: "baz"},
46+
"BAZ": taskfile.Var{Static: "foo"},
47+
},
4248
},
4349
},
4450
},
@@ -48,8 +54,11 @@ func TestArgs(t *testing.T) {
4854
ExpectedCalls: []taskfile.Call{
4955
{
5056
Task: "task-a",
51-
Vars: taskfile.Vars{
52-
"CONTENT": taskfile.Var{Static: "with some spaces"},
57+
Vars: &taskfile.Vars{
58+
Keys: []string{"CONTENT"},
59+
Mapping: map[string]taskfile.Var{
60+
"CONTENT": taskfile.Var{Static: "with some spaces"},
61+
},
5362
},
5463
},
5564
},
@@ -60,8 +69,11 @@ func TestArgs(t *testing.T) {
6069
{Task: "task-a"},
6170
{Task: "task-b"},
6271
},
63-
ExpectedGlobals: taskfile.Vars{
64-
"FOO": {Static: "bar"},
72+
ExpectedGlobals: &taskfile.Vars{
73+
Keys: []string{"FOO"},
74+
Mapping: map[string]taskfile.Var{
75+
"FOO": {Static: "bar"},
76+
},
6577
},
6678
},
6779
{
@@ -81,9 +93,12 @@ func TestArgs(t *testing.T) {
8193
ExpectedCalls: []taskfile.Call{
8294
{Task: "default"},
8395
},
84-
ExpectedGlobals: taskfile.Vars{
85-
"FOO": {Static: "bar"},
86-
"BAR": {Static: "baz"},
96+
ExpectedGlobals: &taskfile.Vars{
97+
Keys: []string{"FOO", "BAR"},
98+
Mapping: map[string]taskfile.Var{
99+
"FOO": {Static: "bar"},
100+
"BAR": {Static: "baz"},
101+
},
87102
},
88103
},
89104
}

internal/compiler/compiler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ import (
77
// Compiler handles compilation of a task before its execution.
88
// E.g. variable merger, template processing, etc.
99
type Compiler interface {
10-
GetVariables(t *taskfile.Task, call taskfile.Call) (taskfile.Vars, error)
10+
GetVariables(t *taskfile.Task, call taskfile.Call) (*taskfile.Vars, error)
1111
HandleDynamicVar(v taskfile.Var) (string, error)
1212
}

internal/compiler/env.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@ import (
99

1010
// GetEnviron the all return all environment variables encapsulated on a
1111
// taskfile.Vars
12-
func GetEnviron() taskfile.Vars {
13-
var (
14-
env = os.Environ()
15-
m = make(taskfile.Vars, len(env))
16-
)
17-
18-
for _, e := range env {
12+
func GetEnviron() *taskfile.Vars {
13+
m := &taskfile.Vars{}
14+
for _, e := range os.Environ() {
1915
keyVal := strings.SplitN(e, "=", 2)
2016
key, val := keyVal[0], keyVal[1]
21-
m[key] = taskfile.Var{Static: val}
17+
m.Set(key, taskfile.Var{Static: val})
2218
}
2319
return m
2420
}

internal/compiler/v2/compiler_v2.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ var _ compiler.Compiler = &CompilerV2{}
1919
type CompilerV2 struct {
2020
Dir string
2121

22-
Taskvars taskfile.Vars
23-
TaskfileVars taskfile.Vars
22+
Taskvars *taskfile.Vars
23+
TaskfileVars *taskfile.Vars
2424

2525
Expansions int
2626

@@ -36,10 +36,10 @@ type CompilerV2 struct {
3636
// 3. Taskfile variables
3737
// 4. Taskvars file variables
3838
// 5. Environment variables
39-
func (c *CompilerV2) GetVariables(t *taskfile.Task, call taskfile.Call) (taskfile.Vars, error) {
39+
func (c *CompilerV2) GetVariables(t *taskfile.Task, call taskfile.Call) (*taskfile.Vars, error) {
4040
vr := varResolver{c: c, vars: compiler.GetEnviron()}
41-
vr.vars["TASK"] = taskfile.Var{Static: t.Task}
42-
for _, vars := range []taskfile.Vars{c.Taskvars, c.TaskfileVars, call.Vars, t.Vars} {
41+
vr.vars.Set("TASK", taskfile.Var{Static: t.Task})
42+
for _, vars := range []*taskfile.Vars{c.Taskvars, c.TaskfileVars, call.Vars, t.Vars} {
4343
for i := 0; i < c.Expansions; i++ {
4444
vr.merge(vars)
4545
}
@@ -49,27 +49,28 @@ func (c *CompilerV2) GetVariables(t *taskfile.Task, call taskfile.Call) (taskfil
4949

5050
type varResolver struct {
5151
c *CompilerV2
52-
vars taskfile.Vars
52+
vars *taskfile.Vars
5353
err error
5454
}
5555

56-
func (vr *varResolver) merge(vars taskfile.Vars) {
56+
func (vr *varResolver) merge(vars *taskfile.Vars) {
5757
if vr.err != nil {
5858
return
5959
}
6060
tr := templater.Templater{Vars: vr.vars}
61-
for k, v := range vars {
61+
vars.Range(func(k string, v taskfile.Var) error {
6262
v = taskfile.Var{
6363
Static: tr.Replace(v.Static),
6464
Sh: tr.Replace(v.Sh),
6565
}
6666
static, err := vr.c.HandleDynamicVar(v)
6767
if err != nil {
6868
vr.err = err
69-
return
69+
return err
7070
}
71-
vr.vars[k] = taskfile.Var{Static: static}
72-
}
71+
vr.vars.Set(k, taskfile.Var{Static: static})
72+
return nil
73+
})
7374
vr.err = tr.Err()
7475
}
7576

internal/taskfile/call.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package taskfile
33
// Call is the parameters to a task call
44
type Call struct {
55
Task string
6-
Vars Vars
6+
Vars *Vars
77
}

internal/taskfile/cmd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ type Cmd struct {
1010
Cmd string
1111
Silent bool
1212
Task string
13-
Vars Vars
13+
Vars *Vars
1414
IgnoreError bool
1515
}
1616

1717
// Dep is a task dependency
1818
type Dep struct {
1919
Task string
20-
Vars Vars
20+
Vars *Vars
2121
}
2222

2323
var (
@@ -51,7 +51,7 @@ func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error {
5151
}
5252
var taskCall struct {
5353
Task string
54-
Vars Vars
54+
Vars *Vars
5555
}
5656
if err := unmarshal(&taskCall); err == nil {
5757
c.Task = taskCall.Task
@@ -70,7 +70,7 @@ func (d *Dep) UnmarshalYAML(unmarshal func(interface{}) error) error {
7070
}
7171
var taskCall struct {
7272
Task string
73-
Vars Vars
73+
Vars *Vars
7474
}
7575
if err := unmarshal(&taskCall); err == nil {
7676
d.Task = taskCall.Task

internal/taskfile/merge.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,13 @@ func Merge(t1, t2 *Taskfile, namespaces ...string) error {
2929
}
3030

3131
if t1.Vars == nil {
32-
t1.Vars = make(Vars)
32+
t1.Vars = &Vars{}
3333
}
34-
for k, v := range t2.Vars {
35-
t1.Vars[k] = v
36-
}
37-
3834
if t1.Env == nil {
39-
t1.Env = make(Vars)
40-
}
41-
for k, v := range t2.Env {
42-
t1.Env[k] = v
35+
t1.Env = &Vars{}
4336
}
37+
t1.Vars.Merge(t2.Vars)
38+
t1.Env.Merge(t2.Env)
4439

4540
if t1.Tasks == nil {
4641
t1.Tasks = make(Tasks)

internal/taskfile/read/taskvars.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
)
1313

1414
// Taskvars reads a Taskvars for a given directory
15-
func Taskvars(dir string) (taskfile.Vars, error) {
16-
vars := make(taskfile.Vars)
15+
func Taskvars(dir string) (*taskfile.Vars, error) {
16+
vars := &taskfile.Vars{}
1717

1818
path := filepath.Join(dir, "Taskvars.yml")
1919
if _, err := os.Stat(path); err == nil {
@@ -29,24 +29,17 @@ func Taskvars(dir string) (taskfile.Vars, error) {
2929
if err != nil {
3030
return nil, err
3131
}
32-
33-
if vars == nil {
34-
vars = osVars
35-
} else {
36-
for k, v := range osVars {
37-
vars[k] = v
38-
}
39-
}
32+
vars.Merge(osVars)
4033
}
4134

4235
return vars, nil
4336
}
4437

45-
func readTaskvars(file string) (taskfile.Vars, error) {
38+
func readTaskvars(file string) (*taskfile.Vars, error) {
4639
f, err := os.Open(file)
4740
if err != nil {
4841
return nil, err
4942
}
5043
var vars taskfile.Vars
51-
return vars, yaml.NewDecoder(f).Decode(&vars)
44+
return &vars, yaml.NewDecoder(f).Decode(&vars)
5245
}

0 commit comments

Comments
 (0)