If I run a task that watches foo.txt for changes, I would like to dynamically set a var on every execution/change to foo.txt, however I cannot get it to work with this example configuration; DYN_VAR always remains set the first value of date.
OS: macos 10.15.5
$ task --version
Task version: 3.0.0
version: '3'
tasks:
foobar:
sources:
- ./foo.txt
generates:
- ./bar.txt
vars:
DYN_VAR:
sh: date
cmds:
- echo "{{.DYN_VAR}}" > ./bar.txt
$ task foobar -w
task: Started watching for tasks: foobar
task: echo "Sat Aug 22 23:25:20 NZST 2020" > ./bar.txt
task: Task "foobar" is up to date
# changes manually made to foo.txt
task: echo "Sat Aug 22 23:25:20 NZST 2020" > ./bar.txt
task: Task "foobar" is up to date
Is it possible to do what I am trying, if so how?
Hi @craighurley,
That happens because variables are cached in the first run.
We should probably re-evaluate that on every run.
Try this.
version: '3'
tasks:
foobar:
sources:
- ./foo.txt
generates:
- ./bar.txt
cmds:
- |
DYN_VAR=$(date)
echo "$DYN_VAR" > ./bar.txt
Thanks @dschissler, that works.