mirror of
https://github.com/go-task/task.git
synced 2025-01-22 05:10:17 +02:00
fix: expand variables as well cmds with dotenv
This commit is contained in:
parent
4cb0450dee
commit
fd2e2da518
@ -141,3 +141,11 @@ func ReplaceVarsWithExtra(vars *ast.Vars, cache *Cache, extra map[string]any) *a
|
|||||||
|
|
||||||
return &newVars
|
return &newVars
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MergeCacheMap merges a map into the cache
|
||||||
|
func (vs *Cache) MergeCacheMap(m map[string]any) {
|
||||||
|
for k, v := range m {
|
||||||
|
vs.cacheMap[k] = v
|
||||||
|
}
|
||||||
|
vs.Vars.MergeCacheMap(vs.cacheMap)
|
||||||
|
}
|
||||||
|
20
task_test.go
20
task_test.go
@ -1573,6 +1573,26 @@ func TestTaskDotenvWithBrackets(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
tt.Run(t)
|
tt.Run(t)
|
||||||
|
|
||||||
|
tt2 := fileContentTest{
|
||||||
|
Dir: "testdata/dotenv_task/default",
|
||||||
|
Target: "dotenv",
|
||||||
|
TrimSpace: true,
|
||||||
|
Files: map[string]string{
|
||||||
|
"dotenv-called.txt": "foo",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
tt2.Run(t)
|
||||||
|
|
||||||
|
tt3 := fileContentTest{
|
||||||
|
Dir: "testdata/dotenv_task/default",
|
||||||
|
Target: "dotenv",
|
||||||
|
TrimSpace: true,
|
||||||
|
Files: map[string]string{
|
||||||
|
"dotenv-called-2.txt": "foo",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
tt3.Run(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTaskDotenvFail(t *testing.T) {
|
func TestTaskDotenvFail(t *testing.T) {
|
||||||
|
@ -36,6 +36,14 @@ func (vs *Vars) ToCacheMap() (m map[string]any) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FromCacheMap converts a map containing only the static variables
|
||||||
|
// to Vars
|
||||||
|
func (vs *Vars) MergeCacheMap(m map[string]any) {
|
||||||
|
for k, v := range m {
|
||||||
|
vs.Set(k, Var{Value: v})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Wrapper around OrderedMap.Set to ensure we don't get nil pointer errors
|
// Wrapper around OrderedMap.Set to ensure we don't get nil pointer errors
|
||||||
func (vs *Vars) Range(f func(k string, v Var) error) error {
|
func (vs *Vars) Range(f func(k string, v Var) error) error {
|
||||||
if vs == nil {
|
if vs == nil {
|
||||||
|
8
testdata/dotenv_task/default/Taskfile.yml
vendored
8
testdata/dotenv_task/default/Taskfile.yml
vendored
@ -9,6 +9,14 @@ tasks:
|
|||||||
cmds:
|
cmds:
|
||||||
- echo "$FOO" > dotenv.txt
|
- echo "$FOO" > dotenv.txt
|
||||||
- echo "{{.FOO}}" > dotenv-2.txt
|
- echo "{{.FOO}}" > dotenv-2.txt
|
||||||
|
- task: dotenv_called
|
||||||
|
vars:
|
||||||
|
FOO: "{{.FOO}}"
|
||||||
|
|
||||||
|
dotenv_called:
|
||||||
|
cmds:
|
||||||
|
- echo "$FOO" > dotenv-called.txt
|
||||||
|
- echo "{{.FOO}}" > dotenv-called-2.txt
|
||||||
|
|
||||||
dotenv-overridden-by-env:
|
dotenv-overridden-by-env:
|
||||||
dotenv: ['.env']
|
dotenv: ['.env']
|
||||||
|
@ -107,6 +107,7 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task,
|
|||||||
new.Env.Merge(templater.ReplaceVars(e.Taskfile.Env, cache), nil)
|
new.Env.Merge(templater.ReplaceVars(e.Taskfile.Env, cache), nil)
|
||||||
new.Env.Merge(templater.ReplaceVars(dotenvEnvs, cache), nil)
|
new.Env.Merge(templater.ReplaceVars(dotenvEnvs, cache), nil)
|
||||||
new.Env.Merge(templater.ReplaceVars(origTask.Env, cache), nil)
|
new.Env.Merge(templater.ReplaceVars(origTask.Env, cache), nil)
|
||||||
|
new.Env.Merge(templater.ReplaceVars(call.Vars, cache), nil)
|
||||||
if evaluateShVars {
|
if evaluateShVars {
|
||||||
err = new.Env.Range(func(k string, v ast.Var) error {
|
err = new.Env.Range(func(k string, v ast.Var) error {
|
||||||
// If the variable is not dynamic, we can set it and return
|
// If the variable is not dynamic, we can set it and return
|
||||||
@ -126,6 +127,8 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
envCache := new.Env.ToCacheMap()
|
envCache := new.Env.ToCacheMap()
|
||||||
|
// merge envCache with cache
|
||||||
|
cache.MergeCacheMap(envCache)
|
||||||
|
|
||||||
if len(origTask.Cmds) > 0 {
|
if len(origTask.Cmds) > 0 {
|
||||||
new.Cmds = make([]*ast.Cmd, 0, len(origTask.Cmds))
|
new.Cmds = make([]*ast.Cmd, 0, len(origTask.Cmds))
|
||||||
@ -162,7 +165,7 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task,
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
newCmd := cmd.DeepCopy()
|
newCmd := cmd.DeepCopy()
|
||||||
newCmd.Cmd = templater.ReplaceWithExtra(cmd.Cmd, cache, envCache)
|
newCmd.Cmd = templater.Replace(cmd.Cmd, cache)
|
||||||
newCmd.Task = templater.Replace(cmd.Task, cache)
|
newCmd.Task = templater.Replace(cmd.Task, cache)
|
||||||
newCmd.Vars = templater.ReplaceVars(cmd.Vars, cache)
|
newCmd.Vars = templater.ReplaceVars(cmd.Vars, cache)
|
||||||
new.Cmds = append(new.Cmds, newCmd)
|
new.Cmds = append(new.Cmds, newCmd)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user