1
0
mirror of https://github.com/go-task/task.git synced 2025-01-06 03:53:54 +02:00

fix: expand variables as well cmds with dotenv

This commit is contained in:
Amin Yahyaabadi 2024-05-03 11:59:17 -07:00
parent 4cb0450dee
commit fd2e2da518
No known key found for this signature in database
GPG Key ID: F52AF77F636088F0
5 changed files with 48 additions and 1 deletions

View File

@ -141,3 +141,11 @@ func ReplaceVarsWithExtra(vars *ast.Vars, cache *Cache, extra map[string]any) *a
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)
}

View File

@ -1573,6 +1573,26 @@ func TestTaskDotenvWithBrackets(t *testing.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) {

View File

@ -36,6 +36,14 @@ func (vs *Vars) ToCacheMap() (m map[string]any) {
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
func (vs *Vars) Range(f func(k string, v Var) error) error {
if vs == nil {

View File

@ -9,6 +9,14 @@ tasks:
cmds:
- echo "$FOO" > dotenv.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: ['.env']

View File

@ -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(dotenvEnvs, cache), nil)
new.Env.Merge(templater.ReplaceVars(origTask.Env, cache), nil)
new.Env.Merge(templater.ReplaceVars(call.Vars, cache), nil)
if evaluateShVars {
err = new.Env.Range(func(k string, v ast.Var) error {
// 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()
// merge envCache with cache
cache.MergeCacheMap(envCache)
if len(origTask.Cmds) > 0 {
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
}
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.Vars = templater.ReplaceVars(cmd.Vars, cache)
new.Cmds = append(new.Cmds, newCmd)