Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make CHECKSUM and TIMESTAMP vars available in cmds commands #1872

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
10 changes: 10 additions & 0 deletions testdata/cmds_vars/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3'

tasks:
build:
sources:
- ./source.txt
cmds:
- echo "{{.CHECKSUM}}"
- echo "{{.TIMESTAMP.Unix}}"
- echo "{{.TIMESTAMP}}"
1 change: 1 addition & 0 deletions testdata/cmds_vars/source.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World!
32 changes: 17 additions & 15 deletions variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -228,21 +245,6 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task,
}

if len(origTask.Status) > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a big fan of those two nested if statements.
TIMESTAMP and CHECKSUM should only be available if there are sources defined. I'd replace len(origTask.Status) > 0 with len(origTask.Source) > 0 and move it above cmds.

Copy link
Contributor Author

@niklasr22 niklasr22 Dec 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, good idea. I changed it like you said but now it also has the effect that .TIMESTAMP and .CHECKSUM are available in preconditions and deps. Do you want to keep it like that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed this is wanted 🙂

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)
}

Expand Down
2 changes: 1 addition & 1 deletion website/docs/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading