From b269c6e1623246258e273f96184b50029fdefe73 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sun, 30 Apr 2017 19:32:28 -0300 Subject: [PATCH] Allow interpolation on "generates" and "sources" attributes Closes #26 --- task.go | 35 +++++++++++++++++++++++++---------- variable_handling.go | 13 +++++++++++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/task.go b/task.go index d26cfe51..330e60c4 100644 --- a/task.go +++ b/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 { @@ -133,22 +139,31 @@ func (t *Task) runDeps(ctx context.Context) error { 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 { diff --git a/variable_handling.go b/variable_handling.go index 18ae24ab..5e362875 100644 --- a/variable_handling.go +++ b/variable_handling.go @@ -99,6 +99,19 @@ 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 (t *Task) ReplaceVariables(initial string) (string, error) { vars, err := t.getVariables()