1
0
mirror of https://github.com/go-task/task.git synced 2025-04-23 12:18:57 +02:00

Fixed some bugs and regressions regarding dynamic variables and directories

Closes #426
This commit is contained in:
Andrey Nering 2021-01-09 12:09:23 -03:00
parent 141b377b4e
commit 4afc0e8ed0
10 changed files with 103 additions and 29 deletions

View File

@ -2,6 +2,8 @@
## Unreleased ## Unreleased
- Fixed some bugs and regressions regarding dynamic variables and directories
([#426](https://github.com/go-task/task/issues/426)).
- The [slim-sprig](https://github.com/go-task/slim-sprig) package was updated - The [slim-sprig](https://github.com/go-task/slim-sprig) package was updated
with the upstream [sprig](https://github.com/Masterminds/sprig). with the upstream [sprig](https://github.com/Masterminds/sprig).

View File

@ -32,18 +32,14 @@ func (c *CompilerV3) GetVariables(t *taskfile.Task, call taskfile.Call) (*taskfi
result := compiler.GetEnviron() result := compiler.GetEnviron()
result.Set("TASK", taskfile.Var{Static: t.Task}) result.Set("TASK", taskfile.Var{Static: t.Task})
// NOTE(@andreynering): We're manually joining these paths here because getRangeFunc := func(dir string) func(k string, v taskfile.Var) error {
// this is the raw task, not the compiled one. return func(k string, v taskfile.Var) error {
dir := t.Dir
if !filepath.IsAbs(dir) {
dir = filepath.Join(c.Dir, dir)
}
rangeFunc := func(k string, v taskfile.Var) error {
tr := templater.Templater{Vars: result, RemoveNoValue: true} tr := templater.Templater{Vars: result, RemoveNoValue: true}
v = taskfile.Var{ v = taskfile.Var{
Static: tr.Replace(v.Static), Static: tr.Replace(v.Static),
Sh: tr.Replace(v.Sh), Sh: tr.Replace(v.Sh),
Dir: v.Dir,
} }
if err := tr.Err(); err != nil { if err := tr.Err(); err != nil {
return err return err
@ -55,6 +51,8 @@ func (c *CompilerV3) GetVariables(t *taskfile.Task, call taskfile.Call) (*taskfi
result.Set(k, taskfile.Var{Static: static}) result.Set(k, taskfile.Var{Static: static})
return nil return nil
} }
}
rangeFunc := getRangeFunc(c.Dir)
if err := c.TaskfileVars.Range(rangeFunc); err != nil { if err := c.TaskfileVars.Range(rangeFunc); err != nil {
return nil, err return nil, err
@ -62,7 +60,20 @@ func (c *CompilerV3) GetVariables(t *taskfile.Task, call taskfile.Call) (*taskfi
if err := call.Vars.Range(rangeFunc); err != nil { if err := call.Vars.Range(rangeFunc); err != nil {
return nil, err return nil, err
} }
if err := t.Vars.Range(rangeFunc); err != nil {
// NOTE(@andreynering): We're manually joining these paths here because
// this is the raw task, not the compiled one.
tr := templater.Templater{Vars: result, RemoveNoValue: true}
dir := tr.Replace(t.Dir)
if err := tr.Err(); err != nil {
return nil, err
}
if !filepath.IsAbs(dir) {
dir = filepath.Join(c.Dir, dir)
}
taskRangeFunc := getRangeFunc(dir)
if err := t.Vars.Range(taskRangeFunc); err != nil {
return nil, err return nil, err
} }
@ -84,6 +95,11 @@ func (c *CompilerV3) HandleDynamicVar(v taskfile.Var, dir string) (string, error
return result, nil return result, nil
} }
// NOTE(@andreynering): If a var have a specific dir, use this instead
if v.Dir != "" {
dir = v.Dir
}
var stdout bytes.Buffer var stdout bytes.Buffer
opts := &execext.RunCommandOptions{ opts := &execext.RunCommandOptions{
Command: v.Sh, Command: v.Sh,

View File

@ -308,7 +308,7 @@ func TestGenerates(t *testing.T) {
const ( const (
srcTask = "sub/src.txt" srcTask = "sub/src.txt"
relTask = "rel.txt" relTask = "rel.txt"
absTask = "sub/abs.txt" absTask = "abs.txt"
fileWithSpaces = "my text file.txt" fileWithSpaces = "my text file.txt"
) )
@ -805,7 +805,10 @@ func TestDynamicVariablesShouldRunOnTheTaskDir(t *testing.T) {
Target: "default", Target: "default",
TrimSpace: false, TrimSpace: false,
Files: map[string]string{ Files: map[string]string{
"subdirectory/dir.txt": "subdirectory\n", "subdirectory/from_root_taskfile.txt": "subdirectory\n",
"subdirectory/from_included_taskfile.txt": "subdirectory\n",
"subdirectory/from_included_taskfile_task.txt": "subdirectory\n",
"subdirectory/from_interpolated_dir.txt": "subdirectory\n",
}, },
} }
tt.Run(t) tt.Run(t)

View File

@ -97,6 +97,17 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
} }
if includedTask.AdvancedImport { if includedTask.AdvancedImport {
for k, v := range includedTaskfile.Vars.Mapping {
o := v
o.Dir = filepath.Join(dir, includedTask.Dir)
includedTaskfile.Vars.Mapping[k] = o
}
for k, v := range includedTaskfile.Env.Mapping {
o := v
o.Dir = filepath.Join(dir, includedTask.Dir)
includedTaskfile.Env.Mapping[k] = o
}
for _, task := range includedTaskfile.Tasks { for _, task := range includedTaskfile.Tasks {
if !filepath.IsAbs(task.Dir) { if !filepath.IsAbs(task.Dir) {
task.Dir = filepath.Join(includedTask.Dir, task.Dir) task.Dir = filepath.Join(includedTask.Dir, task.Dir)

View File

@ -98,6 +98,7 @@ type Var struct {
Static string Static string
Live interface{} Live interface{}
Sh string Sh string
Dir string
} }
// UnmarshalYAML implements yaml.Unmarshaler interface. // UnmarshalYAML implements yaml.Unmarshaler interface.

View File

@ -1 +1 @@
subdirectory/dir.txt *.txt

View File

@ -1,11 +1,33 @@
version: '3' version: '3'
includes:
sub:
taskfile: subdirectory
dir: subdirectory
vars:
DIRECTORY: subdirectory
tasks: tasks:
default: default:
- task: from-root-taskfile
- task: sub:from-included-taskfile
- task: sub:from-included-taskfile-task
- task: from-interpolated-dir
from-root-taskfile:
cmds: cmds:
- echo '{{.FOLDER}}' > dir.txt - echo '{{.TASK_DIR}}' > from_root_taskfile.txt
dir: subdirectory dir: subdirectory
vars: vars:
FOLDER: TASK_DIR:
sh: basename $(pwd) sh: basename $(pwd)
silent: true silent: true
from-interpolated-dir:
cmds:
- echo '{{.INTERPOLATED_DIR}}' > from_interpolated_dir.txt
dir: '{{.DIRECTORY}}'
vars:
INTERPOLATED_DIR:
sh: basename $(pwd)

View File

@ -0,0 +1,19 @@
version: '3'
vars:
TASKFILE_DIR:
sh: basename $(pwd)
tasks:
from-included-taskfile:
cmds:
- echo '{{.TASKFILE_DIR}}' > from_included_taskfile.txt
silent: true
from-included-taskfile-task:
cmds:
- echo '{{.TASKFILE_TASK_DIR}}' > from_included_taskfile_task.txt
silent: true
vars:
TASKFILE_TASK_DIR:
sh: basename $(pwd)

View File

@ -1 +0,0 @@
subdirectory

View File

@ -1,10 +1,11 @@
version: '3' version: '3'
vars: vars:
BUILD_DIR: $pwd BUILD_DIR:
sh: pwd
tasks: tasks:
sub/abs.txt: abs.txt:
desc: generates dest file based on absolute paths desc: generates dest file based on absolute paths
deps: deps:
- sub/src.txt - sub/src.txt