From c1e14c461b39a90a239225ae6af93063769e8349 Mon Sep 17 00:00:00 2001 From: Niklas Rousset <75939868+niklasr22@users.noreply.github.com> Date: Tue, 31 Dec 2024 16:04:42 +0100 Subject: [PATCH] feat: make CHECKSUM and TIMESTAMP vars available in cmds commands (#1872) --- task_test.go | 33 +++++++++++++++++++++++++++++++++ testdata/cmds_vars/Taskfile.yml | 10 ++++++++++ testdata/cmds_vars/source.txt | 1 + variables.go | 32 +++++++++++++++++--------------- website/docs/usage.mdx | 2 +- 5 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 testdata/cmds_vars/Taskfile.yml create mode 100644 testdata/cmds_vars/source.txt diff --git a/task_test.go b/task_test.go index a0f1e7bf..8920460b 100644 --- a/task_test.go +++ b/task_test.go @@ -976,6 +976,39 @@ func TestStatusVariables(t *testing.T) { assert.Contains(t, buff.String(), tf) } +func TestCmdsVariables(t *testing.T) { + t.Parallel() + + const dir = "testdata/cmds_vars" + + _ = os.RemoveAll(filepathext.SmartJoin(dir, ".task")) + + var buff bytes.Buffer + e := task.Executor{ + Dir: dir, + TempDir: task.TempDir{ + Remote: filepathext.SmartJoin(dir, ".task"), + Fingerprint: filepathext.SmartJoin(dir, ".task"), + }, + Stdout: &buff, + Stderr: &buff, + Silent: false, + Verbose: true, + } + require.NoError(t, e.Setup()) + require.NoError(t, e.Run(context.Background(), &ast.Call{Task: "build"})) + + assert.Contains(t, buff.String(), "3e464c4b03f4b65d740e1e130d4d108a") + + inf, err := os.Stat(filepathext.SmartJoin(dir, "source.txt")) + require.NoError(t, err) + ts := fmt.Sprintf("%d", inf.ModTime().Unix()) + tf := inf.ModTime().String() + + assert.Contains(t, buff.String(), ts) + assert.Contains(t, buff.String(), tf) +} + func TestInit(t *testing.T) { t.Parallel() diff --git a/testdata/cmds_vars/Taskfile.yml b/testdata/cmds_vars/Taskfile.yml new file mode 100644 index 00000000..4857129f --- /dev/null +++ b/testdata/cmds_vars/Taskfile.yml @@ -0,0 +1,10 @@ +version: '3' + +tasks: + build: + sources: + - ./source.txt + cmds: + - echo "{{.CHECKSUM}}" + - echo "{{.TIMESTAMP.Unix}}" + - echo "{{.TIMESTAMP}}" diff --git a/testdata/cmds_vars/source.txt b/testdata/cmds_vars/source.txt new file mode 100644 index 00000000..8ab686ea --- /dev/null +++ b/testdata/cmds_vars/source.txt @@ -0,0 +1 @@ +Hello, World! diff --git a/variables.go b/variables.go index 108834fa..601d6abc 100644 --- a/variables.go +++ b/variables.go @@ -127,6 +127,23 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task, } } + if len(origTask.Sources) > 0 { + timestampChecker := fingerprint.NewTimestampChecker(e.TempDir.Fingerprint, e.Dry) + checksumChecker := fingerprint.NewChecksumChecker(e.TempDir.Fingerprint, e.Dry) + + for _, checker := range []fingerprint.SourcesCheckable{timestampChecker, checksumChecker} { + value, err := checker.Value(&new) + if err != nil { + return nil, err + } + vars.Set(strings.ToUpper(checker.Kind()), ast.Var{Live: value}) + } + + // Adding new variables, requires us to refresh the templaters + // cache of the the values manually + cache.ResetCache() + } + if len(origTask.Cmds) > 0 { new.Cmds = make([]*ast.Cmd, 0, len(origTask.Cmds)) for _, cmd := range origTask.Cmds { @@ -228,21 +245,6 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task, } if len(origTask.Status) > 0 { - timestampChecker := fingerprint.NewTimestampChecker(e.TempDir.Fingerprint, e.Dry) - checksumChecker := fingerprint.NewChecksumChecker(e.TempDir.Fingerprint, e.Dry) - - for _, checker := range []fingerprint.SourcesCheckable{timestampChecker, checksumChecker} { - value, err := checker.Value(&new) - if err != nil { - return nil, err - } - vars.Set(strings.ToUpper(checker.Kind()), ast.Var{Live: value}) - } - - // Adding new variables, requires us to refresh the templaters - // cache of the the values manually - cache.ResetCache() - new.Status = templater.Replace(origTask.Status, cache) } diff --git a/website/docs/usage.mdx b/website/docs/usage.mdx index 8e2d5618..a12e4699 100644 --- a/website/docs/usage.mdx +++ b/website/docs/usage.mdx @@ -926,7 +926,7 @@ checksum source and timestamps require either access to the artifact or for an out-of-band refresh of the `.checksum` fingerprint file. Two special variables `{{.CHECKSUM}}` and `{{.TIMESTAMP}}` are available for -interpolation within `status` commands, depending on the method assigned to +interpolation within `cmds` and `status` commands, depending on the method assigned to fingerprint the sources. Only `source` globs are fingerprinted. Note that the `{{.TIMESTAMP}}` variable is a "live" Go `time.Time` struct, and