mirror of
https://github.com/go-task/task.git
synced 2025-09-16 09:26:16 +02:00
feat: change Var.Value from string to an any type
This commit is contained in:
@@ -72,11 +72,22 @@ func (vr *varResolver) merge(vars *taskfile.Vars) {
|
|||||||
}
|
}
|
||||||
tr := templater.Templater{Vars: vr.vars}
|
tr := templater.Templater{Vars: vr.vars}
|
||||||
_ = vars.Range(func(k string, v taskfile.Var) error {
|
_ = vars.Range(func(k string, v taskfile.Var) error {
|
||||||
v = taskfile.Var{
|
// Replace values
|
||||||
Value: tr.Replace(v.Value),
|
newVar := taskfile.Var{}
|
||||||
Sh: tr.Replace(v.Sh),
|
switch value := v.Value.(type) {
|
||||||
|
case string:
|
||||||
|
newVar.Value = tr.Replace(value)
|
||||||
|
default:
|
||||||
|
newVar.Value = value
|
||||||
}
|
}
|
||||||
static, err := vr.c.HandleDynamicVar(v, "")
|
newVar.Sh = tr.Replace(v.Sh)
|
||||||
|
// If the variable is not dynamic, we can set it and return
|
||||||
|
if newVar.Value != nil || newVar.Sh == "" {
|
||||||
|
vr.vars.Set(k, taskfile.Var{Value: newVar.Value})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// If the variable is dynamic, we need to resolve it first
|
||||||
|
static, err := vr.c.HandleDynamicVar(newVar, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
vr.err = err
|
vr.err = err
|
||||||
return err
|
return err
|
||||||
@@ -88,10 +99,6 @@ func (vr *varResolver) merge(vars *taskfile.Vars) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *CompilerV2) HandleDynamicVar(v taskfile.Var, _ string) (string, error) {
|
func (c *CompilerV2) HandleDynamicVar(v taskfile.Var, _ string) (string, error) {
|
||||||
if v.Value != "" || v.Sh == "" {
|
|
||||||
return v.Value, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
c.muDynamicCache.Lock()
|
c.muDynamicCache.Lock()
|
||||||
defer c.muDynamicCache.Unlock()
|
defer c.muDynamicCache.Unlock()
|
||||||
|
|
||||||
|
@@ -58,21 +58,26 @@ func (c *CompilerV3) getVariables(t *taskfile.Task, call *taskfile.Call, evaluat
|
|||||||
getRangeFunc := func(dir string) func(k string, v taskfile.Var) error {
|
getRangeFunc := func(dir string) func(k string, v taskfile.Var) error {
|
||||||
return func(k string, v taskfile.Var) error {
|
return func(k string, v taskfile.Var) error {
|
||||||
tr := templater.Templater{Vars: result, RemoveNoValue: true}
|
tr := templater.Templater{Vars: result, RemoveNoValue: true}
|
||||||
|
// Replace values
|
||||||
if !evaluateShVars {
|
newVar := taskfile.Var{}
|
||||||
result.Set(k, taskfile.Var{Value: tr.Replace(v.Value)})
|
switch value := v.Value.(type) {
|
||||||
return nil
|
case string:
|
||||||
}
|
newVar.Value = tr.Replace(value)
|
||||||
|
default:
|
||||||
v = taskfile.Var{
|
newVar.Value = value
|
||||||
Value: tr.Replace(v.Value),
|
|
||||||
Sh: tr.Replace(v.Sh),
|
|
||||||
Dir: v.Dir,
|
|
||||||
}
|
}
|
||||||
|
newVar.Sh = tr.Replace(v.Sh)
|
||||||
|
newVar.Dir = v.Dir
|
||||||
if err := tr.Err(); err != nil {
|
if err := tr.Err(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
static, err := c.HandleDynamicVar(v, dir)
|
// If the variable is not dynamic, we can set it and return
|
||||||
|
if !evaluateShVars || newVar.Value != nil || newVar.Sh == "" {
|
||||||
|
result.Set(k, taskfile.Var{Value: newVar.Value})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// If the variable is dynamic, we need to resolve it first
|
||||||
|
static, err := c.HandleDynamicVar(newVar, dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -125,10 +130,6 @@ func (c *CompilerV3) getVariables(t *taskfile.Task, call *taskfile.Call, evaluat
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *CompilerV3) HandleDynamicVar(v taskfile.Var, dir string) (string, error) {
|
func (c *CompilerV3) HandleDynamicVar(v taskfile.Var, dir string) (string, error) {
|
||||||
if v.Value != "" || v.Sh == "" {
|
|
||||||
return v.Value, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
c.muDynamicCache.Lock()
|
c.muDynamicCache.Lock()
|
||||||
defer c.muDynamicCache.Unlock()
|
defer c.muDynamicCache.Unlock()
|
||||||
|
|
||||||
|
@@ -108,17 +108,20 @@ func (r *Templater) replaceVars(vars *taskfile.Vars, extra map[string]any) *task
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var new taskfile.Vars
|
var newVars taskfile.Vars
|
||||||
_ = vars.Range(func(k string, v taskfile.Var) error {
|
_ = vars.Range(func(k string, v taskfile.Var) error {
|
||||||
new.Set(k, taskfile.Var{
|
var newVar taskfile.Var
|
||||||
Value: r.ReplaceWithExtra(v.Value, extra),
|
switch value := v.Value.(type) {
|
||||||
Live: v.Live,
|
case string:
|
||||||
Sh: r.ReplaceWithExtra(v.Sh, extra),
|
newVar.Value = r.ReplaceWithExtra(value, extra)
|
||||||
})
|
}
|
||||||
|
newVar.Live = v.Live
|
||||||
|
newVar.Sh = r.ReplaceWithExtra(v.Sh, extra)
|
||||||
|
newVars.Set(k, newVar)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
return &new
|
return &newVars
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Templater) Err() error {
|
func (r *Templater) Err() error {
|
||||||
|
@@ -71,7 +71,7 @@ func (vs *Vars) DeepCopy() *Vars {
|
|||||||
|
|
||||||
// Var represents either a static or dynamic variable.
|
// Var represents either a static or dynamic variable.
|
||||||
type Var struct {
|
type Var struct {
|
||||||
Value string
|
Value any
|
||||||
Live any
|
Live any
|
||||||
Sh string
|
Sh string
|
||||||
Dir string
|
Dir string
|
||||||
|
16
variables.go
16
variables.go
@@ -108,6 +108,11 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
|
|||||||
new.Env.Merge(r.ReplaceVars(origTask.Env))
|
new.Env.Merge(r.ReplaceVars(origTask.Env))
|
||||||
if evaluateShVars {
|
if evaluateShVars {
|
||||||
err = new.Env.Range(func(k string, v taskfile.Var) error {
|
err = new.Env.Range(func(k string, v taskfile.Var) error {
|
||||||
|
// If the variable is not dynamic, we can set it and return
|
||||||
|
if v.Value != nil || v.Sh == "" {
|
||||||
|
new.Env.Set(k, taskfile.Var{Value: v.Value})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
static, err := e.Compiler.HandleDynamicVar(v, new.Dir)
|
static, err := e.Compiler.HandleDynamicVar(v, new.Dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -149,10 +154,13 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
|
|||||||
if cmd.For.Var != "" {
|
if cmd.For.Var != "" {
|
||||||
if vars != nil {
|
if vars != nil {
|
||||||
v := vars.Get(cmd.For.Var)
|
v := vars.Get(cmd.For.Var)
|
||||||
if cmd.For.Split != "" {
|
switch value := v.Value.(type) {
|
||||||
list = strings.Split(v.Value, cmd.For.Split)
|
case string:
|
||||||
} else {
|
if cmd.For.Split != "" {
|
||||||
list = strings.Fields(v.Value)
|
list = strings.Split(value, cmd.For.Split)
|
||||||
|
} else {
|
||||||
|
list = strings.Fields(value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user