Skip to content

Commit 60c8ee0

Browse files
authored
refactor: ast.Call should be in main task package (#2084)
1 parent cdaf69e commit 60c8ee0

File tree

18 files changed

+235
-240
lines changed

18 files changed

+235
-240
lines changed

args/args.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ package args
33
import (
44
"strings"
55

6+
"github.com/go-task/task/v3"
67
"github.com/go-task/task/v3/taskfile/ast"
78
)
89

910
// Parse parses command line argument: tasks and global variables
10-
func Parse(args ...string) ([]*ast.Call, *ast.Vars) {
11-
calls := []*ast.Call{}
11+
func Parse(args ...string) ([]*task.Call, *ast.Vars) {
12+
calls := []*task.Call{}
1213
globals := ast.NewVars()
1314

1415
for _, arg := range args {
1516
if !strings.Contains(arg, "=") {
16-
calls = append(calls, &ast.Call{Task: arg})
17+
calls = append(calls, &task.Call{Task: arg})
1718
continue
1819
}
1920

args/args_test.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/stretchr/testify/assert"
88

9+
"github.com/go-task/task/v3"
910
"github.com/go-task/task/v3/args"
1011
"github.com/go-task/task/v3/taskfile/ast"
1112
)
@@ -15,20 +16,20 @@ func TestArgs(t *testing.T) {
1516

1617
tests := []struct {
1718
Args []string
18-
ExpectedCalls []*ast.Call
19+
ExpectedCalls []*task.Call
1920
ExpectedGlobals *ast.Vars
2021
}{
2122
{
2223
Args: []string{"task-a", "task-b", "task-c"},
23-
ExpectedCalls: []*ast.Call{
24+
ExpectedCalls: []*task.Call{
2425
{Task: "task-a"},
2526
{Task: "task-b"},
2627
{Task: "task-c"},
2728
},
2829
},
2930
{
3031
Args: []string{"task-a", "FOO=bar", "task-b", "task-c", "BAR=baz", "BAZ=foo"},
31-
ExpectedCalls: []*ast.Call{
32+
ExpectedCalls: []*task.Call{
3233
{Task: "task-a"},
3334
{Task: "task-b"},
3435
{Task: "task-c"},
@@ -56,7 +57,7 @@ func TestArgs(t *testing.T) {
5657
},
5758
{
5859
Args: []string{"task-a", "CONTENT=with some spaces"},
59-
ExpectedCalls: []*ast.Call{
60+
ExpectedCalls: []*task.Call{
6061
{Task: "task-a"},
6162
},
6263
ExpectedGlobals: ast.NewVars(
@@ -70,7 +71,7 @@ func TestArgs(t *testing.T) {
7071
},
7172
{
7273
Args: []string{"FOO=bar", "task-a", "task-b"},
73-
ExpectedCalls: []*ast.Call{
74+
ExpectedCalls: []*task.Call{
7475
{Task: "task-a"},
7576
{Task: "task-b"},
7677
},
@@ -85,15 +86,15 @@ func TestArgs(t *testing.T) {
8586
},
8687
{
8788
Args: nil,
88-
ExpectedCalls: []*ast.Call{},
89+
ExpectedCalls: []*task.Call{},
8990
},
9091
{
9192
Args: []string{},
92-
ExpectedCalls: []*ast.Call{},
93+
ExpectedCalls: []*task.Call{},
9394
},
9495
{
9596
Args: []string{"FOO=bar", "BAR=baz"},
96-
ExpectedCalls: []*ast.Call{},
97+
ExpectedCalls: []*task.Call{},
9798
ExpectedGlobals: ast.NewVars(
9899
&ast.VarElement{
99100
Key: "FOO",
+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package ast
1+
package task
2+
3+
import "github.com/go-task/task/v3/taskfile/ast"
24

35
// Call is the parameters to a task call
46
type Call struct {
57
Task string
6-
Vars *Vars
8+
Vars *ast.Vars
79
Silent bool
810
Indirect bool // True if the task was called by another task
911
}

cmd/task/task.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func run() error {
201201
}
202202

203203
var (
204-
calls []*ast.Call
204+
calls []*task.Call
205205
globals *ast.Vars
206206
)
207207

@@ -214,7 +214,7 @@ func run() error {
214214

215215
// If there are no calls, run the default task instead
216216
if len(calls) == 0 {
217-
calls = append(calls, &ast.Call{Task: "default"})
217+
calls = append(calls, &task.Call{Task: "default"})
218218
}
219219

220220
globals.Set("CLI_ARGS", ast.Var{Value: cliArgs})

internal/compiler/compiler.go renamed to compiler.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package compiler
1+
package task
22

33
import (
44
"bytes"
@@ -36,16 +36,16 @@ func (c *Compiler) GetTaskfileVariables() (*ast.Vars, error) {
3636
return c.getVariables(nil, nil, true)
3737
}
3838

39-
func (c *Compiler) GetVariables(t *ast.Task, call *ast.Call) (*ast.Vars, error) {
39+
func (c *Compiler) GetVariables(t *ast.Task, call *Call) (*ast.Vars, error) {
4040
return c.getVariables(t, call, true)
4141
}
4242

43-
func (c *Compiler) FastGetVariables(t *ast.Task, call *ast.Call) (*ast.Vars, error) {
43+
func (c *Compiler) FastGetVariables(t *ast.Task, call *Call) (*ast.Vars, error) {
4444
return c.getVariables(t, call, false)
4545
}
4646

47-
func (c *Compiler) getVariables(t *ast.Task, call *ast.Call, evaluateShVars bool) (*ast.Vars, error) {
48-
result := GetEnviron()
47+
func (c *Compiler) getVariables(t *ast.Task, call *Call, evaluateShVars bool) (*ast.Vars, error) {
48+
result := env.GetEnviron()
4949
specialVars, err := c.getSpecialVars(t, call)
5050
if err != nil {
5151
return nil, err
@@ -196,7 +196,7 @@ func (c *Compiler) ResetCache() {
196196
c.dynamicCache = nil
197197
}
198198

199-
func (c *Compiler) getSpecialVars(t *ast.Task, call *ast.Call) (map[string]string, error) {
199+
func (c *Compiler) getSpecialVars(t *ast.Task, call *Call) (map[string]string, error) {
200200
allVars := map[string]string{
201201
"TASK_EXE": filepath.ToSlash(os.Args[0]),
202202
"ROOT_TASKFILE": filepathext.SmartJoin(c.Dir, c.Entrypoint),

internal/compiler/env.go

-20
This file was deleted.

internal/env/env.go

+13
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,26 @@ package env
33
import (
44
"fmt"
55
"os"
6+
"strings"
67

78
"github.com/go-task/task/v3/internal/experiments"
89
"github.com/go-task/task/v3/taskfile/ast"
910
)
1011

1112
const taskVarPrefix = "TASK_"
1213

14+
// GetEnviron the all return all environment variables encapsulated on a
15+
// ast.Vars
16+
func GetEnviron() *ast.Vars {
17+
m := ast.NewVars()
18+
for _, e := range os.Environ() {
19+
keyVal := strings.SplitN(e, "=", 2)
20+
key, val := keyVal[0], keyVal[1]
21+
m.Set(key, ast.Var{Value: val})
22+
}
23+
return m
24+
}
25+
1326
func Get(t *ast.Task) []string {
1427
if t.Env == nil {
1528
return nil

internal/summary/summary.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"github.com/go-task/task/v3/taskfile/ast"
88
)
99

10-
func PrintTasks(l *logger.Logger, t *ast.Taskfile, c []*ast.Call) {
10+
func PrintTasks(l *logger.Logger, t *ast.Taskfile, c []string) {
1111
for i, call := range c {
1212
PrintSpaceBetweenSummaries(l, i)
13-
if task, ok := t.Tasks.Get(call.Task); ok {
13+
if task, ok := t.Tasks.Get(call); ok {
1414
PrintTask(l, task)
1515
}
1616
}

internal/summary/summary_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ func TestPrintAllWithSpaces(t *testing.T) {
179179

180180
summary.PrintTasks(&l,
181181
&ast.Taskfile{Tasks: tasks},
182-
[]*ast.Call{{Task: "t1"}, {Task: "t2"}, {Task: "t3"}})
182+
[]string{"t1", "t2", "t3"},
183+
)
183184

184185
assert.True(t, strings.HasPrefix(buffer.String(), "task: t1"))
185186
assert.Contains(t, buffer.String(), "\n(task does not have description or summary)\n\n\ntask: t2")

setup.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/sajari/fuzzy"
1414

1515
"github.com/go-task/task/v3/errors"
16-
"github.com/go-task/task/v3/internal/compiler"
1716
"github.com/go-task/task/v3/internal/env"
1817
"github.com/go-task/task/v3/internal/execext"
1918
"github.com/go-task/task/v3/internal/filepathext"
@@ -198,7 +197,7 @@ func (e *Executor) setupCompiler() error {
198197
}
199198
}
200199

201-
e.Compiler = &compiler.Compiler{
200+
e.Compiler = &Compiler{
202201
Dir: e.Dir,
203202
Entrypoint: e.Entrypoint,
204203
UserWorkingDir: e.UserWorkingDir,
@@ -214,7 +213,12 @@ func (e *Executor) readDotEnvFiles() error {
214213
return nil
215214
}
216215

217-
env, err := taskfile.Dotenv(e.Compiler, e.Taskfile, e.Dir)
216+
vars, err := e.Compiler.GetTaskfileVariables()
217+
if err != nil {
218+
return err
219+
}
220+
221+
env, err := taskfile.Dotenv(vars, e.Taskfile, e.Dir)
218222
if err != nil {
219223
return err
220224
}

status.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
// Status returns an error if any the of given tasks is not up-to-date
12-
func (e *Executor) Status(ctx context.Context, calls ...*ast.Call) error {
12+
func (e *Executor) Status(ctx context.Context, calls ...*Call) error {
1313
for _, call := range calls {
1414

1515
// Compile the task

0 commit comments

Comments
 (0)