Skip to content

Commit bf6d0c0

Browse files
committed
Improve performance of --list and --summary flags
Closes #332
1 parent c11672f commit bf6d0c0

File tree

7 files changed

+50
-11
lines changed

7 files changed

+50
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- Improve performance of `--list` and `--summary` by skipping running shell
6+
variables for these flags
7+
([#332](https://github.com/go-task/task/issues/332)).
58
- Fixed a bug where an environment in a Taskfile was not always overridable
69
by the system environment
710
([#425](https://github.com/go-task/task/issues/425)).

help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (e *Executor) tasksWithDesc() (tasks []*taskfile.Task) {
3030
tasks = make([]*taskfile.Task, 0, len(e.Taskfile.Tasks))
3131
for _, task := range e.Taskfile.Tasks {
3232
if task.Desc != "" {
33-
compiledTask, err := e.CompiledTask(taskfile.Call{Task: task.Task})
33+
compiledTask, err := e.FastCompiledTask(taskfile.Call{Task: task.Task})
3434
if err == nil {
3535
task = compiledTask
3636
}

internal/compiler/compiler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
// E.g. variable merger, template processing, etc.
99
type Compiler interface {
1010
GetVariables(t *taskfile.Task, call taskfile.Call) (*taskfile.Vars, error)
11+
FastGetVariables(t *taskfile.Task, call taskfile.Call) (*taskfile.Vars, error)
1112
HandleDynamicVar(v taskfile.Var, dir string) (string, error)
1213
ResetCache()
1314
}

internal/compiler/v2/compiler_v2.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ type CompilerV2 struct {
3030
muDynamicCache sync.Mutex
3131
}
3232

33+
// FastGetVariables is a no-op on v2
34+
func (c *CompilerV2) FastGetVariables(t *taskfile.Task, call taskfile.Call) (*taskfile.Vars, error) {
35+
return c.GetVariables(t, call)
36+
}
37+
3338
// GetVariables returns fully resolved variables following the priority order:
3439
// 1. Task variables
3540
// 2. Call variables

internal/compiler/v3/compiler_v3.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,26 @@ type CompilerV3 struct {
3030
}
3131

3232
func (c *CompilerV3) GetVariables(t *taskfile.Task, call taskfile.Call) (*taskfile.Vars, error) {
33+
return c.getVariables(t, call, true)
34+
}
35+
36+
func (c *CompilerV3) FastGetVariables(t *taskfile.Task, call taskfile.Call) (*taskfile.Vars, error) {
37+
return c.getVariables(t, call, false)
38+
}
39+
40+
func (c *CompilerV3) getVariables(t *taskfile.Task, call taskfile.Call, evaluateShVars bool) (*taskfile.Vars, error) {
3341
result := compiler.GetEnviron()
3442
result.Set("TASK", taskfile.Var{Static: t.Task})
3543

3644
getRangeFunc := func(dir string) func(k string, v taskfile.Var) error {
3745
return func(k string, v taskfile.Var) error {
3846
tr := templater.Templater{Vars: result, RemoveNoValue: true}
3947

48+
if !evaluateShVars {
49+
result.Set(k, taskfile.Var{Static: tr.Replace(v.Static)})
50+
return nil
51+
}
52+
4053
v = taskfile.Var{
4154
Static: tr.Replace(v.Static),
4255
Sh: tr.Replace(v.Sh),

task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (e *Executor) Run(ctx context.Context, calls ...taskfile.Call) error {
7171

7272
if e.Summary {
7373
for i, c := range calls {
74-
compiledTask, err := e.CompiledTask(c)
74+
compiledTask, err := e.FastCompiledTask(c)
7575
if err != nil {
7676
return nil
7777
}

variables.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,27 @@ import (
1313
// CompiledTask returns a copy of a task, but replacing variables in almost all
1414
// properties using the Go template package.
1515
func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
16+
return e.compiledTask(call, true)
17+
}
18+
19+
// FastCompiledTask is like CompiledTask, but it skippes dynamic variables.
20+
func (e *Executor) FastCompiledTask(call taskfile.Call) (*taskfile.Task, error) {
21+
return e.compiledTask(call, false)
22+
}
23+
24+
func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskfile.Task, error) {
1625
origTask, ok := e.Taskfile.Tasks[call.Task]
1726
if !ok {
1827
return nil, &taskNotFoundError{call.Task}
1928
}
2029

21-
vars, err := e.Compiler.GetVariables(origTask, call)
30+
var vars *taskfile.Vars
31+
var err error
32+
if evaluateShVars {
33+
vars, err = e.Compiler.GetVariables(origTask, call)
34+
} else {
35+
vars, err = e.Compiler.FastGetVariables(origTask, call)
36+
}
2237
if err != nil {
2338
return nil, err
2439
}
@@ -59,16 +74,18 @@ func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
5974
new.Env = &taskfile.Vars{}
6075
new.Env.Merge(r.ReplaceVars(e.Taskfile.Env))
6176
new.Env.Merge(r.ReplaceVars(origTask.Env))
62-
err = new.Env.Range(func(k string, v taskfile.Var) error {
63-
static, err := e.Compiler.HandleDynamicVar(v, new.Dir)
77+
if evaluateShVars {
78+
err = new.Env.Range(func(k string, v taskfile.Var) error {
79+
static, err := e.Compiler.HandleDynamicVar(v, new.Dir)
80+
if err != nil {
81+
return err
82+
}
83+
new.Env.Set(k, taskfile.Var{Static: static})
84+
return nil
85+
})
6486
if err != nil {
65-
return err
87+
return nil, err
6688
}
67-
new.Env.Set(k, taskfile.Var{Static: static})
68-
return nil
69-
})
70-
if err != nil {
71-
return nil, err
7289
}
7390

7491
if len(origTask.Cmds) > 0 {

0 commit comments

Comments
 (0)