From 8020284b12e8d5d274a05ef23b5d497a886e3e04 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sun, 8 Sep 2019 22:26:24 -0300 Subject: [PATCH 1/3] Add global method: option to set default method --- internal/taskfile/taskfile.go | 3 +++ status.go | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/internal/taskfile/taskfile.go b/internal/taskfile/taskfile.go index c7931788..da966691 100644 --- a/internal/taskfile/taskfile.go +++ b/internal/taskfile/taskfile.go @@ -5,6 +5,7 @@ type Taskfile struct { Version string Expansions int Output string + Method string Includes map[string]string Vars Vars Env Vars @@ -17,6 +18,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error { Version string Expansions int Output string + Method string Includes map[string]string Vars Vars Env Vars @@ -28,6 +30,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error { tf.Version = taskfile.Version tf.Expansions = taskfile.Expansions tf.Output = taskfile.Output + tf.Method = taskfile.Method tf.Includes = taskfile.Includes tf.Vars = taskfile.Vars tf.Env = taskfile.Env diff --git a/status.go b/status.go index da8a8472..554bb569 100644 --- a/status.go +++ b/status.go @@ -50,7 +50,11 @@ func (e *Executor) statusOnError(t *taskfile.Task) error { } func (e *Executor) getStatusChecker(t *taskfile.Task) (status.Checker, error) { - switch t.Method { + method := t.Method + if method == "" { + method = e.Taskfile.Method + } + switch method { case "", "timestamp": return &status.Timestamp{ Dir: t.Dir, @@ -68,7 +72,7 @@ func (e *Executor) getStatusChecker(t *taskfile.Task) (status.Checker, error) { case "none": return status.None{}, nil default: - return nil, fmt.Errorf(`task: invalid method "%s"`, t.Method) + return nil, fmt.Errorf(`task: invalid method "%s"`, method) } } From 78595fba0b775128107e5687214ab63c0f3e8220 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sun, 8 Sep 2019 22:51:56 -0300 Subject: [PATCH 2/3] Make "checksum" the default method in v3 --- internal/status/checksum.go | 4 ++++ status.go | 2 +- task.go | 8 ++++++++ task_test.go | 4 ++-- testdata/generates/Taskfile.yml | 4 ++++ 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/internal/status/checksum.go b/internal/status/checksum.go index 572d4650..00fe9d60 100644 --- a/internal/status/checksum.go +++ b/internal/status/checksum.go @@ -23,6 +23,10 @@ type Checksum struct { // IsUpToDate implements the Checker interface func (c *Checksum) IsUpToDate() (bool, error) { + if len(c.Sources) == 0 { + return false, nil + } + checksumFile := c.checksumFilePath() data, _ := ioutil.ReadFile(checksumFile) diff --git a/status.go b/status.go index 554bb569..59a128fe 100644 --- a/status.go +++ b/status.go @@ -55,7 +55,7 @@ func (e *Executor) getStatusChecker(t *taskfile.Task) (status.Checker, error) { method = e.Taskfile.Method } switch method { - case "", "timestamp": + case "timestamp": return &status.Timestamp{ Dir: t.Dir, Sources: t.Sources, diff --git a/task.go b/task.go index 4d8097e7..a96742af 100644 --- a/task.go +++ b/task.go @@ -169,6 +169,14 @@ func (e *Executor) Setup() error { return fmt.Errorf(`task: output option "%s" not recognized`, e.Taskfile.Output) } + if e.Taskfile.Method == "" { + if v >= 3 { + e.Taskfile.Method = "checksum" + } else { + e.Taskfile.Method = "timestamp" + } + } + if v <= 2.1 { err := errors.New(`task: Taskfile option "ignore_error" is only available starting on Taskfile version v2.1`) diff --git a/task_test.go b/task_test.go index 39bedb01..4c7c2d0b 100644 --- a/task_test.go +++ b/task_test.go @@ -630,7 +630,7 @@ func TestWhenDirAttributeItCreatesMissingAndRunsInThatDir(t *testing.T) { } // Ensure that the directory to be created doesn't actually exist. - _ = os.Remove(toBeCreated) + _ = os.RemoveAll(toBeCreated) if _, err := os.Stat(toBeCreated); err == nil { t.Errorf("Directory should not exist: %v", err) } @@ -641,7 +641,7 @@ func TestWhenDirAttributeItCreatesMissingAndRunsInThatDir(t *testing.T) { assert.Equal(t, expected, got, "Mismatch in the working directory") // Clean-up after ourselves only if no error. - _ = os.Remove(toBeCreated) + _ = os.RemoveAll(toBeCreated) } func TestDisplaysErrorOnUnsupportedVersion(t *testing.T) { diff --git a/testdata/generates/Taskfile.yml b/testdata/generates/Taskfile.yml index d050aa6b..9f05cfd0 100644 --- a/testdata/generates/Taskfile.yml +++ b/testdata/generates/Taskfile.yml @@ -8,6 +8,7 @@ tasks: dir: sub cmds: - cat src.txt > '{{.BUILD_DIR}}/abs.txt' + method: timestamp sources: - src.txt generates: @@ -20,6 +21,7 @@ tasks: dir: sub cmds: - cat src.txt > '../rel.txt' + method: timestamp sources: - src.txt generates: @@ -30,6 +32,7 @@ tasks: cmds: - mkdir -p sub - echo "hello world" > sub/src.txt + method: timestamp status: - test -f sub/src.txt @@ -38,6 +41,7 @@ tasks: deps: [sub/src.txt] cmds: - cat sub/src.txt > 'my text file.txt' + method: timestamp sources: - sub/src.txt generates: From 948e6bd57c9ecd67f342226886f18b55712df479 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sun, 8 Sep 2019 22:59:27 -0300 Subject: [PATCH 3/3] Update v3 CHANGELOG --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 114bb16f..8be63d98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ - Taskfiles in version 1 are not supported anymore ([#237](https://github.com/go-task/task/pull/237)). +- Added global `method:` option. With this option, you can set a default + method to all tasks in a Taskfile + ([#246](https://github.com/go-task/task/issues/246)). +- Changed default method from `timestamp` to `checksum` + ([#246](https://github.com/go-task/task/issues/246)). + ## v3.0.0 - Preview 1