just icon indicating copy to clipboard operation
just copied to clipboard

Add `[cd(DIR)]` attribute

Open casey opened this issue 1 year ago • 5 comments

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

casey avatar May 25 '24 01:05 casey

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

pedrofgodinho avatar Jul 06 '24 04:07 pedrofgodinho

I started working on this together with #2291. It works in simple cases: absolute path or relative path from current directory. I wonder though, where relative paths should actually be reconed from? Is there some precedent in just already which this feature should follow? What about modules?

artm avatar Aug 25 '24 10:08 artm

I think that relative paths should always be used relative to whatever would have been the working directory otherwise.

casey avatar Aug 25 '24 21:08 casey

Ok, I think that gives me enough direction, thanks.

artm avatar Aug 26 '24 04:08 artm

i think set working-directory globally and [cd(subdirectory/nested)] per command base would be good idea

NoelJacob avatar Sep 21 '24 16:09 NoelJacob

I've raised a PR to implement this. However, I've implemented it as [working-directory(DIR)] instead of [cd(DIR)] since this is consistent with the working-directory setting.

bcheidemann avatar Oct 22 '24 17:10 bcheidemann