casey / just

🤖 Just a command runner
https://just.systems
Creative Commons Zero v1.0 Universal
18.65k stars 423 forks source link

Add `[cd(DIR)]` attribute #2082

Open casey opened 1 month ago

casey commented 1 month ago

Allow setting the working directory of a recipe to an arbitrary directory. Good first issue!

pedrofgodinho commented 1 week ago

Pointing out a usecase for this: it'd facilitate cross-platform support. The project I'm working on currently will likely be developed on both windows and linux. set windows-shell := ['powershell.exe'] is a good first step, but I couldn't find a better way to support both shells other than this hack:

_cd_and_run dir *cmd:
    just _cd_and_run-{{os()}} {{dir}} {{cmd}}

_cd_and_run-linux dir *cmd:
    cd {{dir}} && {{cmd}}

_cd_and_run-macos dir *cmd:
    cd {{dir}} && {{cmd}}

_cd_and_run-windows dir *cmd:
    cd {{dir}}; {{cmd}}

Even this is fairly imperfect, as it doesn't easily allow you to do things like adding a - for ignoring errors (e.g. for a lint recipe to lint 2 subdirectories, when the linter gives exit code 1 when issues are found), among other similar problems.

And adding things like just _cd_and_run frontend npm run build instead of just npm run build here and there isn't the prettiest solution... An ideal solution would probably be:

[cd(/frontend)]
build:
  npm run build