I've searched and Googled a lot, but can't find an answer. If this is not possible then just let me know, and if you can give me more context about "why" I'd appreciate it.
I inherited a makefile which is making the same check in many different make-tasks, but tries to DRY out the logic by moving the body of the check itself into a single upstream make-task, that the other tasks depend on. Basically this:
DEV_FILE_PRESENT:=false
setup:
@if [ -f ./dev_file.json ]; then \
DEV_FILE_PRESENT=true; \
echo "found dev json file"; \
fi;
check-overwrite: setup
@if [ DEV_FILE_PRESENT ]; then \
echo "Existing dev files- overwrite? [Y/n]" && read ans && [ $${ans:-Y} = Y ]; \
fi;
preview: setup
@if [ ! DEV_FILE_PRESENT ]; then \
echo "Can't preview- missing dev file"; \
else \
echo "placeholder for moar bash scripting here"; \
fi;
This doesn't work: The global variable DEV_FILE_PRESENT
seems to revert to "false"
as soon as the setup
task is complete and has been exited. I think that's because "every sub-command is run in its own shell" (From
this StackOverflow answer: https://stackoverflow.com/a/29085684/1358187)
Much googling and searching later I settled on this "fix" - removing any attempt to DRY the body of the if-clause itself, and instead duplicating it across tasks:
check-overwrite:
@if [ -f ./dev_file.json ]; then \
echo "Existing dev files- overwrite? [Y/n]" && read ans && [ $${ans:-Y} = Y ]; \
fi;
preview:
@if [ ! -f ./dev_file.json ]; then \
echo "Can't preview- missing dev file"; \
else \
echo "placeholder for moar bash scripting here"; \
fi;
Are there any other solutions here? Is there any way to set a variable in one make-task, and reference it in another make-task?