Skip to content

Commit

Permalink
Add working-directory setting (#2283)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik authored Jul 31, 2024
1 parent b70546a commit 23a53fb
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 14 deletions.
1 change: 1 addition & 0 deletions GRAMMAR.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ setting : 'allow-duplicate-recipes' boolean?
| 'unstable' boolean?
| 'windows-powershell' boolean?
| 'windows-shell' ':=' string_list
| 'working-directory' ':=' string
boolean : ':=' ('true' | 'false')
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,7 @@ foo:
| `unstable`<sup>1.31.0</sup> | boolean | `false` | Enable unstable features. |
| `windows-powershell` | boolean | `false` | Use PowerShell on Windows as default shell. (Deprecated. Use `windows-shell` instead. |
| `windows-shell` | `[COMMAND, ARGS…]` | - | Set the command used to invoke recipes and evaluate backticks. |
| `working-directory`<sup>master</sup> | string | - | Set the working directory for recipes and backticks, relative to the default working directory. |

Boolean settings can be written as:

Expand Down
12 changes: 11 additions & 1 deletion src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,17 @@ impl<'src, 'run> Evaluator<'src, 'run> {
let mut cmd = self.context.settings.shell_command(self.context.config);
cmd.arg(command);
cmd.args(args);
cmd.current_dir(&self.context.search.working_directory);
if let Some(working_directory) = &self.context.settings.working_directory {
cmd.current_dir(
self
.context
.search
.working_directory
.join(working_directory),
)
} else {
cmd.current_dir(&self.context.search.working_directory)
};
cmd.export(
self.context.settings,
self.context.dotenv,
Expand Down
1 change: 1 addition & 0 deletions src/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(crate) enum Keyword {
Unstable,
WindowsPowershell,
WindowsShell,
WorkingDirectory,
X,
}

Expand Down
5 changes: 4 additions & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,10 @@ impl<'src> Node<'src> for Set<'src> {
set.push_mut(Tree::string(&argument.cooked));
}
}
Setting::DotenvFilename(value) | Setting::DotenvPath(value) | Setting::Tempdir(value) => {
Setting::DotenvFilename(value)
| Setting::DotenvPath(value)
| Setting::Tempdir(value)
| Setting::WorkingDirectory(value) => {
set.push_mut(Tree::string(&value.cooked));
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ impl<'run, 'src> Parser<'run, 'src> {
Keyword::Shell => Some(Setting::Shell(self.parse_interpreter()?)),
Keyword::Tempdir => Some(Setting::Tempdir(self.parse_string_literal()?)),
Keyword::WindowsShell => Some(Setting::WindowsShell(self.parse_interpreter()?)),
Keyword::WorkingDirectory => Some(Setting::WorkingDirectory(self.parse_string_literal()?)),
_ => None,
};

Expand Down Expand Up @@ -2146,6 +2147,12 @@ mod tests {
tree: (justfile (set windows_powershell false)),
}

test! {
name: set_working_directory,
text: "set working-directory := 'foo'",
tree: (justfile (set working_directory "foo")),
}

test! {
name: conditional,
text: "a := if b == c { d } else { e }",
Expand Down
31 changes: 20 additions & 11 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,21 @@ impl<'src, D> Recipe<'src, D> {
!self.attributes.contains(&Attribute::NoExitMessage)
}

fn working_directory<'a>(&'a self, search: &'a Search) -> Option<&Path> {
if self.change_directory() {
Some(if self.submodule_depth > 0 {
&self.working_directory
} else {
&search.working_directory
})
fn working_directory<'a>(&'a self, context: &'a ExecutionContext) -> Option<PathBuf> {
if !self.change_directory() {
return None;
}

let base = if self.submodule_depth > 0 {
&self.working_directory
} else {
&context.search.working_directory
};

if let Some(setting) = &context.settings.working_directory {
Some(base.join(setting))
} else {
None
Some(base.into())
}
}

Expand Down Expand Up @@ -265,7 +271,7 @@ impl<'src, D> Recipe<'src, D> {

let mut cmd = context.settings.shell_command(config);

if let Some(working_directory) = self.working_directory(context.search) {
if let Some(working_directory) = self.working_directory(context) {
cmd.current_dir(working_directory);
}

Expand Down Expand Up @@ -408,8 +414,11 @@ impl<'src, D> Recipe<'src, D> {
io_error: error,
})?;

let mut command =
executor.command(&path, self.name(), self.working_directory(context.search))?;
let mut command = executor.command(
&path,
self.name(),
self.working_directory(context).as_deref(),
)?;

if self.takes_positional_arguments(context.settings) {
command.args(positional);
Expand Down
6 changes: 5 additions & 1 deletion src/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub(crate) enum Setting<'src> {
Unstable(bool),
WindowsPowerShell(bool),
WindowsShell(Interpreter<'src>),
WorkingDirectory(StringLiteral<'src>),
}

impl<'src> Display for Setting<'src> {
Expand All @@ -38,7 +39,10 @@ impl<'src> Display for Setting<'src> {
Self::ScriptInterpreter(shell) | Self::Shell(shell) | Self::WindowsShell(shell) => {
write!(f, "[{shell}]")
}
Self::DotenvFilename(value) | Self::DotenvPath(value) | Self::Tempdir(value) => {
Self::DotenvFilename(value)
| Self::DotenvPath(value)
| Self::Tempdir(value)
| Self::WorkingDirectory(value) => {
write!(f, "{value}")
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub(crate) struct Settings<'src> {
pub(crate) unstable: bool,
pub(crate) windows_powershell: bool,
pub(crate) windows_shell: Option<Interpreter<'src>>,
pub(crate) working_directory: Option<PathBuf>,
}

impl<'src> Settings<'src> {
Expand Down Expand Up @@ -84,6 +85,9 @@ impl<'src> Settings<'src> {
Setting::Tempdir(tempdir) => {
settings.tempdir = Some(tempdir.cooked);
}
Setting::WorkingDirectory(working_directory) => {
settings.working_directory = Some(working_directory.cooked.into());
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fn alias() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -105,6 +106,7 @@ fn assignment() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -162,6 +164,7 @@ fn body() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -231,6 +234,7 @@ fn dependencies() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -338,6 +342,7 @@ fn dependency_argument() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -407,6 +412,7 @@ fn duplicate_recipes() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -454,6 +460,7 @@ fn duplicate_variables() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -504,6 +511,7 @@ fn doc_comment() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -540,6 +548,7 @@ fn empty_justfile() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -697,6 +706,7 @@ fn parameters() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -787,6 +797,7 @@ fn priors() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -837,6 +848,7 @@ fn private() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -887,6 +899,7 @@ fn quiet() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -952,6 +965,7 @@ fn settings() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -1005,6 +1019,7 @@ fn shebang() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -1055,6 +1070,7 @@ fn simple() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -1108,6 +1124,7 @@ fn attribute() {
"ignore_comments": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -1176,6 +1193,7 @@ fn module() {
"ignore_comments": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand All @@ -1199,6 +1217,7 @@ fn module() {
"ignore_comments": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -1269,6 +1288,7 @@ fn module_group() {
"ignore_comments": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand All @@ -1292,6 +1312,7 @@ fn module_group() {
"ignore_comments": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
},
"unexports": [],
"warnings": [],
Expand Down
Loading

0 comments on commit 23a53fb

Please sign in to comment.