-
-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #311 from go-task/vars-refactor-for-v3
v3: Variables refactoring
- Loading branch information
Showing
49 changed files
with
420 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package v3 | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/go-task/task/v2/internal/compiler" | ||
"github.com/go-task/task/v2/internal/execext" | ||
"github.com/go-task/task/v2/internal/logger" | ||
"github.com/go-task/task/v2/internal/taskfile" | ||
"github.com/go-task/task/v2/internal/templater" | ||
) | ||
|
||
var _ compiler.Compiler = &CompilerV3{} | ||
|
||
type CompilerV3 struct { | ||
Dir string | ||
|
||
TaskfileVars *taskfile.Vars | ||
|
||
Logger *logger.Logger | ||
|
||
dynamicCache map[string]string | ||
muDynamicCache sync.Mutex | ||
} | ||
|
||
func (c *CompilerV3) GetVariables(t *taskfile.Task, call taskfile.Call) (*taskfile.Vars, error) { | ||
result := compiler.GetEnviron() | ||
result.Set("TASK", taskfile.Var{Static: t.Task}) | ||
|
||
rangeFunc := func(k string, v taskfile.Var) error { | ||
tr := templater.Templater{Vars: result, RemoveNoValue: true} | ||
v = taskfile.Var{ | ||
Static: tr.Replace(v.Static), | ||
Sh: tr.Replace(v.Sh), | ||
} | ||
if err := tr.Err(); err != nil { | ||
return err | ||
} | ||
static, err := c.HandleDynamicVar(v) | ||
if err != nil { | ||
return err | ||
} | ||
result.Set(k, taskfile.Var{Static: static}) | ||
return nil | ||
} | ||
|
||
if err := c.TaskfileVars.Range(rangeFunc); err != nil { | ||
return nil, err | ||
} | ||
if err := call.Vars.Range(rangeFunc); err != nil { | ||
return nil, err | ||
} | ||
if err := t.Vars.Range(rangeFunc); err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (c *CompilerV3) HandleDynamicVar(v taskfile.Var) (string, error) { | ||
if v.Static != "" || v.Sh == "" { | ||
return v.Static, nil | ||
} | ||
|
||
c.muDynamicCache.Lock() | ||
defer c.muDynamicCache.Unlock() | ||
|
||
if c.dynamicCache == nil { | ||
c.dynamicCache = make(map[string]string, 30) | ||
} | ||
if result, ok := c.dynamicCache[v.Sh]; ok { | ||
return result, nil | ||
} | ||
|
||
var stdout bytes.Buffer | ||
opts := &execext.RunCommandOptions{ | ||
Command: v.Sh, | ||
Dir: c.Dir, | ||
Stdout: &stdout, | ||
Stderr: c.Logger.Stderr, | ||
} | ||
if err := execext.RunCommand(context.Background(), opts); err != nil { | ||
return "", fmt.Errorf(`task: Command "%s" in taskvars file failed: %s`, opts.Command, err) | ||
} | ||
|
||
// Trim a single trailing newline from the result to make most command | ||
// output easier to use in shell commands. | ||
result := strings.TrimSuffix(stdout.String(), "\n") | ||
|
||
c.dynamicCache[v.Sh] = result | ||
c.Logger.VerboseErrf(logger.Magenta, `task: dynamic variable: '%s' result: '%s'`, v.Sh, result) | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ package taskfile | |
// Call is the parameters to a task call | ||
type Call struct { | ||
Task string | ||
Vars Vars | ||
Vars *Vars | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.