mirror of
https://github.com/go-task/task.git
synced 2025-03-17 21:08:01 +02:00
Merge branch 'variables'
This commit is contained in:
commit
bb84b067c5
64
task.go
64
task.go
@ -95,9 +95,15 @@ func RunTask(ctx context.Context, name string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if !Force && t.isUpToDate() {
|
||||
log.Printf(`task: Task "%s" is up to date`, name)
|
||||
return nil
|
||||
if !Force {
|
||||
upToDate, err := t.isUpToDate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if upToDate {
|
||||
log.Printf(`task: Task "%s" is up to date`, name)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
for i := range t.Cmds {
|
||||
@ -109,18 +115,13 @@ func RunTask(ctx context.Context, name string) error {
|
||||
}
|
||||
|
||||
func (t *Task) runDeps(ctx context.Context) error {
|
||||
vars, err := t.handleVariables()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g, ctx := errgroup.WithContext(ctx)
|
||||
|
||||
for _, d := range t.Deps {
|
||||
dep := d
|
||||
|
||||
g.Go(func() error {
|
||||
dep, err := ReplaceVariables(dep, vars)
|
||||
dep, err := t.ReplaceVariables(dep)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -132,36 +133,41 @@ func (t *Task) runDeps(ctx context.Context) error {
|
||||
})
|
||||
}
|
||||
|
||||
if err = g.Wait(); err != nil {
|
||||
if err := g.Wait(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Task) isUpToDate() bool {
|
||||
func (t *Task) isUpToDate() (bool, error) {
|
||||
if len(t.Sources) == 0 || len(t.Generates) == 0 {
|
||||
return false
|
||||
return false, nil
|
||||
}
|
||||
|
||||
sourcesMaxTime, err := getPatternsMaxTime(t.Sources)
|
||||
sources, err := t.ReplaceSliceVariables(t.Sources)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
generates, err := t.ReplaceSliceVariables(t.Generates)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
sourcesMaxTime, err := getPatternsMaxTime(sources)
|
||||
if err != nil || sourcesMaxTime.IsZero() {
|
||||
return false
|
||||
return false, nil
|
||||
}
|
||||
|
||||
generatesMinTime, err := getPatternsMinTime(t.Generates)
|
||||
generatesMinTime, err := getPatternsMinTime(generates)
|
||||
if err != nil || generatesMinTime.IsZero() {
|
||||
return false
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return generatesMinTime.After(sourcesMaxTime)
|
||||
return generatesMinTime.After(sourcesMaxTime), nil
|
||||
}
|
||||
|
||||
func (t *Task) runCommand(ctx context.Context, i int) error {
|
||||
vars, err := t.handleVariables()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c, err := ReplaceVariables(t.Cmds[i], vars)
|
||||
c, err := t.ReplaceVariables(t.Cmds[i])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -174,12 +180,12 @@ func (t *Task) runCommand(ctx context.Context, i int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
dir, err := ReplaceVariables(t.Dir, vars)
|
||||
dir, err := t.ReplaceVariables(t.Dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
envs, err := t.getEnviron(vars)
|
||||
envs, err := t.getEnviron()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -209,7 +215,7 @@ func (t *Task) runCommand(ctx context.Context, i int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Task) getEnviron(vars map[string]string) ([]string, error) {
|
||||
func (t *Task) getEnviron() ([]string, error) {
|
||||
if t.Env == nil {
|
||||
return nil, nil
|
||||
}
|
||||
@ -217,15 +223,11 @@ func (t *Task) getEnviron(vars map[string]string) ([]string, error) {
|
||||
envs := os.Environ()
|
||||
|
||||
for k, v := range t.Env {
|
||||
replacedValue, err := ReplaceVariables(v, vars)
|
||||
env, err := t.ReplaceVariables(fmt.Sprintf("%s=%s", k, v))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
replacedKey, err := ReplaceVariables(k, vars)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
envs = append(envs, fmt.Sprintf("%s=%s", replacedKey, replacedValue))
|
||||
envs = append(envs, env)
|
||||
}
|
||||
return envs, nil
|
||||
}
|
||||
|
@ -25,15 +25,10 @@ var (
|
||||
ErrMultilineResultCmd = errors.New("Got multiline result from command")
|
||||
)
|
||||
|
||||
var varCmds = make(map[string]string)
|
||||
|
||||
func handleDynamicVariableContent(value string) (string, error) {
|
||||
if !strings.HasPrefix(value, "$") {
|
||||
return value, nil
|
||||
}
|
||||
if result, ok := varCmds[value]; ok {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
buff := bytes.NewBuffer(nil)
|
||||
|
||||
@ -53,11 +48,10 @@ func handleDynamicVariableContent(value string) (string, error) {
|
||||
}
|
||||
|
||||
result = strings.TrimSpace(result)
|
||||
varCmds[value] = result
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (t *Task) handleVariables() (map[string]string, error) {
|
||||
func (t *Task) getVariables() (map[string]string, error) {
|
||||
localVariables := make(map[string]string)
|
||||
for key, value := range t.Vars {
|
||||
val, err := handleDynamicVariableContent(value)
|
||||
@ -105,14 +99,33 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// ReplaceSliceVariables writes vars into initial string slice
|
||||
func (t *Task) ReplaceSliceVariables(initials []string) ([]string, error) {
|
||||
result := make([]string, len(initials))
|
||||
for i, s := range initials {
|
||||
var err error
|
||||
result[i], err = t.ReplaceVariables(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ReplaceVariables writes vars into initial string
|
||||
func ReplaceVariables(initial string, vars map[string]string) (string, error) {
|
||||
t, err := template.New("").Funcs(templateFuncs).Parse(initial)
|
||||
func (t *Task) ReplaceVariables(initial string) (string, error) {
|
||||
vars, err := t.getVariables()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
templ, err := template.New("").Funcs(templateFuncs).Parse(initial)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
b := bytes.NewBuffer(nil)
|
||||
if err = t.Execute(b, vars); err != nil {
|
||||
if err = templ.Execute(b, vars); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return b.String(), nil
|
||||
|
Loading…
x
Reference in New Issue
Block a user