From b1c0e8df20257f250bae22fe569cfe8628294ac6 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 16:45:01 -0700 Subject: [PATCH 01/21] Add test --- tests/windows_shell.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/windows_shell.rs b/tests/windows_shell.rs index 7db698826f..b3c247cfb0 100644 --- a/tests/windows_shell.rs +++ b/tests/windows_shell.rs @@ -6,6 +6,25 @@ fn windows_shell_setting() { .justfile( r#" set windows-shell := ["pwsh.exe", "-NoLogo", "-Command"] + set shell := ["asdfasdfasdfasdf"] + + foo: + Write-Output bar + "#, + ) + .shell(false) + .stdout("bar\r\n") + .stderr("Write-Output bar\n") + .run(); +} + +#[test] +fn windows_powershell_setting_uses_powershell() { + Test::new() + .justfile( + r#" + set windows-powershell + set shell := ["asdfasdfasdfasdf"] foo: Write-Output bar From 1e67d27387f21eec8a2de5df59461b5e3d607377 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 16:47:12 -0700 Subject: [PATCH 02/21] Refactor into shell method --- src/settings.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/settings.rs b/src/settings.rs index ae3767fa0c..b9ad338c4b 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -37,6 +37,10 @@ impl<'src> Settings<'src> { cmd } + pub(crate) fn shell<'a>(&'a self, config: &'a Config) -> (&'a str, Vec<&'a str>) { + (self.shell_binary(config), self.shell_arguments(config)) + } + pub(crate) fn shell_binary<'a>(&'a self, config: &'a Config) -> &'a str { let shell_or_args_present = config.shell.is_some() || config.shell_args.is_some(); From 754ffe84728b8a0f6ebc6923833c9e8d3b554677 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 16:47:55 -0700 Subject: [PATCH 03/21] Movve shell_binary into shell --- src/settings.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index b9ad338c4b..ec109a7c04 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -38,13 +38,9 @@ impl<'src> Settings<'src> { } pub(crate) fn shell<'a>(&'a self, config: &'a Config) -> (&'a str, Vec<&'a str>) { - (self.shell_binary(config), self.shell_arguments(config)) - } - - pub(crate) fn shell_binary<'a>(&'a self, config: &'a Config) -> &'a str { let shell_or_args_present = config.shell.is_some() || config.shell_args.is_some(); - if let (Some(shell), false) = (&self.shell, shell_or_args_present) { + let binary = if let (Some(shell), false) = (&self.shell, shell_or_args_present) { shell.command.cooked.as_ref() } else if let Some(shell) = &config.shell { shell @@ -54,7 +50,13 @@ impl<'src> Settings<'src> { WINDOWS_POWERSHELL_SHELL } else { DEFAULT_SHELL - } + }; + + (binary, self.shell_arguments(config)) + } + + pub(crate) fn shell_binary<'a>(&'a self, config: &'a Config) -> &'a str { + self.shell(config).0 } pub(crate) fn shell_arguments<'a>(&'a self, config: &'a Config) -> Vec<&'a str> { From f43e3896f187a35bd35dc83d354bddff1ec7417a Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 16:48:30 -0700 Subject: [PATCH 04/21] shell_arguments -> shell --- src/settings.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index ec109a7c04..5a46b0bb26 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -52,17 +52,9 @@ impl<'src> Settings<'src> { DEFAULT_SHELL }; - (binary, self.shell_arguments(config)) - } - - pub(crate) fn shell_binary<'a>(&'a self, config: &'a Config) -> &'a str { - self.shell(config).0 - } - - pub(crate) fn shell_arguments<'a>(&'a self, config: &'a Config) -> Vec<&'a str> { let shell_or_args_present = config.shell.is_some() || config.shell_args.is_some(); - if let (Some(shell), false) = (&self.shell, shell_or_args_present) { + let shell_arguments = if let (Some(shell), false) = (&self.shell, shell_or_args_present) { shell .arguments .iter() @@ -80,7 +72,17 @@ impl<'src> Settings<'src> { WINDOWS_POWERSHELL_ARGS.to_vec() } else { DEFAULT_SHELL_ARGS.to_vec() - } + }; + + (binary, shell_arguments) + } + + pub(crate) fn shell_binary<'a>(&'a self, config: &'a Config) -> &'a str { + self.shell(config).0 + } + + pub(crate) fn shell_arguments<'a>(&'a self, config: &'a Config) -> Vec<&'a str> { + self.shell(config).1 } } From 66551d3703fadcc3a3939a190425060dbfb1cef3 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 16:52:24 -0700 Subject: [PATCH 05/21] Refactor --- src/settings.rs | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index 5a46b0bb26..53a4c8c920 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -41,35 +41,39 @@ impl<'src> Settings<'src> { let shell_or_args_present = config.shell.is_some() || config.shell_args.is_some(); let binary = if let (Some(shell), false) = (&self.shell, shell_or_args_present) { - shell.command.cooked.as_ref() + return ( + shell.command.cooked.as_ref(), + shell + .arguments + .iter() + .map(|argument| argument.cooked.as_ref()) + .collect(), + ); } else if let Some(shell) = &config.shell { shell } else if let (true, Some(shell)) = (cfg!(windows), &self.windows_shell) { - shell.command.cooked.as_ref() + return ( + shell.command.cooked.as_ref(), + shell + .arguments + .iter() + .map(|argument| argument.cooked.as_ref()) + .collect(), + ); } else if cfg!(windows) && self.windows_powershell { - WINDOWS_POWERSHELL_SHELL + return (WINDOWS_POWERSHELL_SHELL, WINDOWS_POWERSHELL_ARGS.to_vec()); } else { DEFAULT_SHELL }; - let shell_or_args_present = config.shell.is_some() || config.shell_args.is_some(); - let shell_arguments = if let (Some(shell), false) = (&self.shell, shell_or_args_present) { - shell - .arguments - .iter() - .map(|argument| argument.cooked.as_ref()) - .collect() + unreachable!(); } else if let Some(shell_args) = &config.shell_args { shell_args.iter().map(String::as_ref).collect() } else if let (true, Some(shell)) = (cfg!(windows), &self.windows_shell) { - shell - .arguments - .iter() - .map(|argument| argument.cooked.as_ref()) - .collect() + unreachable!(); } else if cfg!(windows) && self.windows_powershell { - WINDOWS_POWERSHELL_ARGS.to_vec() + unreachable!(); } else { DEFAULT_SHELL_ARGS.to_vec() }; From bbc886a243d707786620589da81651467a6d32d6 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 16:54:56 -0700 Subject: [PATCH 06/21] Simplify --- src/settings.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index 53a4c8c920..53cdcaefaa 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -66,14 +66,8 @@ impl<'src> Settings<'src> { DEFAULT_SHELL }; - let shell_arguments = if let (Some(shell), false) = (&self.shell, shell_or_args_present) { - unreachable!(); - } else if let Some(shell_args) = &config.shell_args { + let shell_arguments = if let Some(shell_args) = &config.shell_args { shell_args.iter().map(String::as_ref).collect() - } else if let (true, Some(shell)) = (cfg!(windows), &self.windows_shell) { - unreachable!(); - } else if cfg!(windows) && self.windows_powershell { - unreachable!(); } else { DEFAULT_SHELL_ARGS.to_vec() }; From a610ee005b2ae61c2d0fce6c037cee1787a0059c Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 17:00:56 -0700 Subject: [PATCH 07/21] Refactor --- src/settings.rs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index 53cdcaefaa..56a7edd21f 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -40,39 +40,45 @@ impl<'src> Settings<'src> { pub(crate) fn shell<'a>(&'a self, config: &'a Config) -> (&'a str, Vec<&'a str>) { let shell_or_args_present = config.shell.is_some() || config.shell_args.is_some(); - let binary = if let (Some(shell), false) = (&self.shell, shell_or_args_present) { - return ( + if let (Some(shell), false) = (&self.shell, shell_or_args_present) { + ( shell.command.cooked.as_ref(), shell .arguments .iter() .map(|argument| argument.cooked.as_ref()) .collect(), - ); + ) } else if let Some(shell) = &config.shell { - shell + ( + shell, + if let Some(shell_args) = &config.shell_args { + shell_args.iter().map(String::as_ref).collect() + } else { + DEFAULT_SHELL_ARGS.to_vec() + }, + ) } else if let (true, Some(shell)) = (cfg!(windows), &self.windows_shell) { - return ( + ( shell.command.cooked.as_ref(), shell .arguments .iter() .map(|argument| argument.cooked.as_ref()) .collect(), - ); + ) } else if cfg!(windows) && self.windows_powershell { return (WINDOWS_POWERSHELL_SHELL, WINDOWS_POWERSHELL_ARGS.to_vec()); } else { - DEFAULT_SHELL - }; - - let shell_arguments = if let Some(shell_args) = &config.shell_args { - shell_args.iter().map(String::as_ref).collect() - } else { - DEFAULT_SHELL_ARGS.to_vec() - }; - - (binary, shell_arguments) + ( + DEFAULT_SHELL, + if let Some(shell_args) = &config.shell_args { + shell_args.iter().map(String::as_ref).collect() + } else { + DEFAULT_SHELL_ARGS.to_vec() + }, + ) + } } pub(crate) fn shell_binary<'a>(&'a self, config: &'a Config) -> &'a str { From 7fb47e4b2c3d0be06435ad70189272fb43f7aec5 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 17:05:41 -0700 Subject: [PATCH 08/21] Complete refactor --- src/settings.rs | 42 ++++++++++++++---------------------------- src/subcommand.rs | 5 +++-- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index 56a7edd21f..edc983f176 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -30,9 +30,11 @@ impl<'src> Settings<'src> { } pub(crate) fn shell_command(&self, config: &Config) -> Command { - let mut cmd = Command::new(self.shell_binary(config)); + let (command, args) = self.shell(config); - cmd.args(self.shell_arguments(config)); + let mut cmd = Command::new(command); + + cmd.args(args); cmd } @@ -80,14 +82,6 @@ impl<'src> Settings<'src> { ) } } - - pub(crate) fn shell_binary<'a>(&'a self, config: &'a Config) -> &'a str { - self.shell(config).0 - } - - pub(crate) fn shell_arguments<'a>(&'a self, config: &'a Config) -> Vec<&'a str> { - self.shell(config).1 - } } #[cfg(test)] @@ -103,8 +97,7 @@ mod tests { ..testing::config(&[]) }; - assert_eq!(settings.shell_binary(&config), "sh"); - assert_eq!(settings.shell_arguments(&config), vec!["-cu"]); + assert_eq!(settings.shell(&config), ("sh", vec!["-cu"])); } #[test] @@ -118,14 +111,12 @@ mod tests { }; if cfg!(windows) { - assert_eq!(settings.shell_binary(&config), "powershell.exe"); assert_eq!( - settings.shell_arguments(&config), - vec!["-NoLogo", "-Command"] + settings.shell(&config), + ("powershell.exe", vec!["-NoLogo", "-Command"]) ); } else { - assert_eq!(settings.shell_binary(&config), "sh"); - assert_eq!(settings.shell_arguments(&config), vec!["-cu"]); + assert_eq!(settings.shell(&config), ("sh", vec!["-cu"])); } } @@ -140,8 +131,7 @@ mod tests { ..testing::config(&[]) }; - assert_eq!(settings.shell_binary(&config), "lol"); - assert_eq!(settings.shell_arguments(&config), vec!["-nice"]); + assert_eq!(settings.shell(&config), ("lol", vec!["-nice"])); } #[test] @@ -156,8 +146,7 @@ mod tests { ..testing::config(&[]) }; - assert_eq!(settings.shell_binary(&config), "lol"); - assert_eq!(settings.shell_arguments(&config), vec!["-nice"]); + assert_eq!(settings.shell(&config), ("lol", vec!["-nice"])); } #[test] @@ -182,8 +171,7 @@ mod tests { ..testing::config(&[]) }; - assert_eq!(settings.shell_binary(&config), "asdf.exe"); - assert_eq!(settings.shell_arguments(&config), vec!["-nope"]); + assert_eq!(settings.shell(&config), ("asdf.exe", vec!["-nope"])); } #[test] @@ -196,7 +184,7 @@ mod tests { ..testing::config(&[]) }; - assert_eq!(settings.shell_binary(&config), "lol"); + assert_eq!(settings.shell(&config).0, "lol"); } #[test] @@ -211,11 +199,9 @@ mod tests { }; if cfg!(windows) { - assert_eq!(settings.shell_binary(&config), "powershell.exe"); + assert_eq!(settings.shell(&config), ("powershell.exe", vec!["-nice"])); } else { - assert_eq!(settings.shell_binary(&config), "sh"); + assert_eq!(settings.shell(&config), ("sh", vec!["-nice"])); } - - assert_eq!(settings.shell_arguments(&config), vec!["-nice"]); } } diff --git a/src/subcommand.rs b/src/subcommand.rs index 1a56e9eebe..60ab95cd76 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -217,9 +217,10 @@ impl Subcommand { let mut child = match result { Ok(child) => child, Err(io_error) => { + let (shell_binary, shell_arguments) = justfile.settings.shell(config); return Err(Error::ChooserInvoke { - shell_binary: justfile.settings.shell_binary(config).to_owned(), - shell_arguments: justfile.settings.shell_arguments(config).join(" "), + shell_binary: shell_binary.to_owned(), + shell_arguments: shell_arguments.join(" "), chooser, io_error, }); From d341456e1a4ce3f15625d1fe50e1da6a6f719748 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 17:07:12 -0700 Subject: [PATCH 09/21] Consolodate tests --- tests/lib.rs | 2 -- tests/windows_powershell.rs | 18 ------------------ tests/windows_shell.rs | 17 +++++++++++++++++ 3 files changed, 17 insertions(+), 20 deletions(-) delete mode 100644 tests/windows_powershell.rs diff --git a/tests/lib.rs b/tests/lib.rs index 1c34bdf91d..b6fa7a4efd 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -76,7 +76,5 @@ mod subsequents; mod tempdir; mod undefined_variables; #[cfg(target_family = "windows")] -mod windows_powershell; -#[cfg(target_family = "windows")] mod windows_shell; mod working_directory; diff --git a/tests/windows_powershell.rs b/tests/windows_powershell.rs deleted file mode 100644 index 1a450bd358..0000000000 --- a/tests/windows_powershell.rs +++ /dev/null @@ -1,18 +0,0 @@ -use super::*; - -#[test] -fn windows_poweshell_setting_uses_powershell() { - Test::new() - .justfile( - r#" - set windows-powershell - - foo: - Write-Output bar - "#, - ) - .shell(false) - .stdout("bar\r\n") - .stderr("Write-Output bar\n") - .run(); -} diff --git a/tests/windows_shell.rs b/tests/windows_shell.rs index b3c247cfb0..be93034678 100644 --- a/tests/windows_shell.rs +++ b/tests/windows_shell.rs @@ -35,3 +35,20 @@ fn windows_powershell_setting_uses_powershell() { .stderr("Write-Output bar\n") .run(); } + +#[test] +fn windows_poweshell_setting_uses_powershell() { + Test::new() + .justfile( + r#" + set windows-powershell + + foo: + Write-Output bar + "#, + ) + .shell(false) + .stdout("bar\r\n") + .stderr("Write-Output bar\n") + .run(); +} From 9476b4b44e7e87860c99dee0017da73dc05a69be Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 17:16:18 -0700 Subject: [PATCH 10/21] tweak --- src/settings.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/settings.rs b/src/settings.rs index edc983f176..d58809c532 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -42,6 +42,14 @@ impl<'src> Settings<'src> { pub(crate) fn shell<'a>(&'a self, config: &'a Config) -> (&'a str, Vec<&'a str>) { let shell_or_args_present = config.shell.is_some() || config.shell_args.is_some(); + match (&config.shell, &config.shell_args) { + (Some(shell), Some(shell_args)) => { + return (shell, shell_args.iter().map(String::as_ref).collect()) + } + (Some(shell), None) => return (shell, DEFAULT_SHELL_ARGS.to_vec()), + _ => {} + } + if let (Some(shell), false) = (&self.shell, shell_or_args_present) { ( shell.command.cooked.as_ref(), From e6530ed6754f0d97a05a0c1ea00d7872b8173342 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 17:20:49 -0700 Subject: [PATCH 11/21] Still confused --- src/settings.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index d58809c532..112168a825 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -40,17 +40,16 @@ impl<'src> Settings<'src> { } pub(crate) fn shell<'a>(&'a self, config: &'a Config) -> (&'a str, Vec<&'a str>) { - let shell_or_args_present = config.shell.is_some() || config.shell_args.is_some(); - - match (&config.shell, &config.shell_args) { - (Some(shell), Some(shell_args)) => { - return (shell, shell_args.iter().map(String::as_ref).collect()) - } - (Some(shell), None) => return (shell, DEFAULT_SHELL_ARGS.to_vec()), - _ => {} - } - - if let (Some(shell), false) = (&self.shell, shell_or_args_present) { + if let Some(shell) = &config.shell { + ( + shell, + if let Some(shell_args) = &config.shell_args { + shell_args.iter().map(String::as_ref).collect() + } else { + DEFAULT_SHELL_ARGS.to_vec() + }, + ) + } else if let (Some(shell), false) = (&self.shell, config.shell_args.is_some()) { ( shell.command.cooked.as_ref(), shell From f026855f666370bb2941c6d4683d3d80c74c0b10 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 17:26:14 -0700 Subject: [PATCH 12/21] Fix precedence --- src/settings.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index 112168a825..94ad118ec0 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -58,15 +58,6 @@ impl<'src> Settings<'src> { .map(|argument| argument.cooked.as_ref()) .collect(), ) - } else if let Some(shell) = &config.shell { - ( - shell, - if let Some(shell_args) = &config.shell_args { - shell_args.iter().map(String::as_ref).collect() - } else { - DEFAULT_SHELL_ARGS.to_vec() - }, - ) } else if let (true, Some(shell)) = (cfg!(windows), &self.windows_shell) { ( shell.command.cooked.as_ref(), @@ -78,6 +69,15 @@ impl<'src> Settings<'src> { ) } else if cfg!(windows) && self.windows_powershell { return (WINDOWS_POWERSHELL_SHELL, WINDOWS_POWERSHELL_ARGS.to_vec()); + } else if let Some(shell) = &config.shell { + ( + shell, + if let Some(shell_args) = &config.shell_args { + shell_args.iter().map(String::as_ref).collect() + } else { + DEFAULT_SHELL_ARGS.to_vec() + }, + ) } else { ( DEFAULT_SHELL, From 2d16b1e0f460f237f1e9e1691366e5eeb38cf806 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 17:32:34 -0700 Subject: [PATCH 13/21] Document and fix precedence --- src/settings.rs | 54 ++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index 94ad118ec0..ec93060a98 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -39,17 +39,27 @@ impl<'src> Settings<'src> { cmd } + // Shell precedence rules: + // 1. `--shell` and/or `--shell-arg` + // 2. `set windows-shell := [...]` + // 3. `set windows-powershell` + // 4. `set shell := [...]` pub(crate) fn shell<'a>(&'a self, config: &'a Config) -> (&'a str, Vec<&'a str>) { - if let Some(shell) = &config.shell { - ( - shell, - if let Some(shell_args) = &config.shell_args { - shell_args.iter().map(String::as_ref).collect() - } else { - DEFAULT_SHELL_ARGS.to_vec() - }, - ) - } else if let (Some(shell), false) = (&self.shell, config.shell_args.is_some()) { + match (&config.shell, &config.shell_args) { + (Some(shell), Some(shell_args)) => { + return (shell, shell_args.iter().map(String::as_ref).collect()) + } + (Some(shell), None) => return (shell, DEFAULT_SHELL_ARGS.to_vec()), + (None, Some(shell_args)) => { + return ( + DEFAULT_SHELL, + shell_args.iter().map(String::as_ref).collect(), + ) + } + (None, None) => {} + } + + if let (true, Some(shell)) = (cfg!(windows), &self.windows_shell) { ( shell.command.cooked.as_ref(), shell @@ -58,7 +68,9 @@ impl<'src> Settings<'src> { .map(|argument| argument.cooked.as_ref()) .collect(), ) - } else if let (true, Some(shell)) = (cfg!(windows), &self.windows_shell) { + } else if cfg!(windows) && self.windows_powershell { + return (WINDOWS_POWERSHELL_SHELL, WINDOWS_POWERSHELL_ARGS.to_vec()); + } else if let Some(shell) = &self.shell { ( shell.command.cooked.as_ref(), shell @@ -67,26 +79,8 @@ impl<'src> Settings<'src> { .map(|argument| argument.cooked.as_ref()) .collect(), ) - } else if cfg!(windows) && self.windows_powershell { - return (WINDOWS_POWERSHELL_SHELL, WINDOWS_POWERSHELL_ARGS.to_vec()); - } else if let Some(shell) = &config.shell { - ( - shell, - if let Some(shell_args) = &config.shell_args { - shell_args.iter().map(String::as_ref).collect() - } else { - DEFAULT_SHELL_ARGS.to_vec() - }, - ) } else { - ( - DEFAULT_SHELL, - if let Some(shell_args) = &config.shell_args { - shell_args.iter().map(String::as_ref).collect() - } else { - DEFAULT_SHELL_ARGS.to_vec() - }, - ) + (DEFAULT_SHELL, DEFAULT_SHELL_ARGS.to_vec()) } } } From ae7dcf008833ad91db10f50a53693b7b6eb71b85 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 17:36:29 -0700 Subject: [PATCH 14/21] Add section to readme --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index d360e24d1f..60bc1653cb 100644 --- a/README.md +++ b/README.md @@ -2126,6 +2126,17 @@ foo $argument: This defeats `just`'s ability to catch typos, for example if you type `$argumant`, but works for all possible values of `argument`, including those with double quotes. +### Configuring the Shell + +There are a number of ways to configure the shell for linewise recipes, which are the default when a recipe does not start with a `#!` shebang. Their precedence, from highest to lowest, is: + +1. The `--shell` and `--shell-arg` command line options. Passing either of these will cause `just` to ignore any settings in the current justfile. +2. `set windows-shell := [...]` +3. `set windows-powershell` (deprecated) +4. `set shell := [...]` + +Since `set windows-shell` has higher precedence than `set shell`, you can use `set windows-shell` to pick a shell on Windows, and `set shell` to pick a shell for all other platforms. + Changelog --------- From a922ff4da7a9a7c68a1ea9502f99e4a5b32c8921 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 17:39:03 -0700 Subject: [PATCH 15/21] Placate clippy --- src/settings.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index ec93060a98..739d6a4d1a 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -39,11 +39,6 @@ impl<'src> Settings<'src> { cmd } - // Shell precedence rules: - // 1. `--shell` and/or `--shell-arg` - // 2. `set windows-shell := [...]` - // 3. `set windows-powershell` - // 4. `set shell := [...]` pub(crate) fn shell<'a>(&'a self, config: &'a Config) -> (&'a str, Vec<&'a str>) { match (&config.shell, &config.shell_args) { (Some(shell), Some(shell_args)) => { @@ -69,7 +64,7 @@ impl<'src> Settings<'src> { .collect(), ) } else if cfg!(windows) && self.windows_powershell { - return (WINDOWS_POWERSHELL_SHELL, WINDOWS_POWERSHELL_ARGS.to_vec()); + (WINDOWS_POWERSHELL_SHELL, WINDOWS_POWERSHELL_ARGS.to_vec()) } else if let Some(shell) = &self.shell { ( shell.command.cooked.as_ref(), From eece30a6335d3dd322033e8cff2fa948e48477de Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 7 Aug 2022 17:45:08 -0700 Subject: [PATCH 16/21] Bump MSRV to get rid of spurious clippy lint --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index a29ae96cb0..ba768a1922 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -44,7 +44,7 @@ jobs: with: components: clippy, rustfmt override: true - toolchain: 1.53.0 + toolchain: 1.54.0 - uses: Swatinem/rust-cache@v1 From 911469d77f0fe278780bcdffadd895760b98d7a8 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 8 Aug 2022 18:01:29 -0700 Subject: [PATCH 17/21] Add windows-shell to settings table --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 60bc1653cb..3b98b7bbee 100644 --- a/README.md +++ b/README.md @@ -648,7 +648,8 @@ foo: | `export` | boolean | Export all variables as environment variables. | | `positional-arguments` | boolean | Pass positional arguments. | | `shell` | `[COMMAND, ARGS…]` | Set the command used to invoke recipes and evaluate backticks. | -| `windows-powershell` | boolean | Use PowerShell on Windows as default shell. | +| `windows-shell` | `[COMMAND, ARGS…]` | Set the command used to invoke recipes and evaluate backticks. | +| `windows-powershell` | boolean | Use PowerShell on Windows as default shell. (Deprecated. Use `windows-shell` instead. | Boolean settings can be written as: From 5dacbd59448d37f60de6afbd10878d0b3f911a8b Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 8 Aug 2022 18:08:27 -0700 Subject: [PATCH 18/21] Add powershell example --- README.md | 2 ++ examples/powershell.just | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 examples/powershell.just diff --git a/README.md b/README.md index 3b98b7bbee..998bdb9361 100644 --- a/README.md +++ b/README.md @@ -776,6 +776,8 @@ hello: Write-Host "Hello, world!" ``` +See [powershell.just](https://github.com/casey/just/blob/master/examples/powershell.just) for a justfile that uses PowerShell on all platforms. + ##### Windows PowerShell *`set windows-powershell` uses the legacy `powershell.exe` binary, and is no longer recommended. See the `windows-shell` setting above for a more flexible way to control which shell is used on Windows.* diff --git a/examples/powershell.just b/examples/powershell.just new file mode 100644 index 0000000000..7e57c4de95 --- /dev/null +++ b/examples/powershell.just @@ -0,0 +1,29 @@ +# Cross platform shebang: +shebang := if os() == 'windows' { + 'powershell.exe' +} else { + '/usr/bin/env pwsh' +} + +# Set shell for non-Windows OSs: +set shell := ["powershell", "-c"] + +# Set shell for Windows OSs: +set windows-shell := ["powershell.exe", "-NoLogo", "-Command"] + +# If you have PowerShell Core installed and want to use it, +# use `pwsh.exe` instead of `powershell.exe` + +linewise: + Write-Host "Hello, world!" + +shebang: + #!{{shebang}} + $PSV = $PSVersionTable.PSVersion | % {"$_" -split "\." } + $psver = $PSV[0] + "." + $PSV[1] + if ($PSV[2].Length -lt 4) { + $psver += "." + $PSV[2] + " Core" + } else { + $psver += " Desktop" + } + echo "PowerShell $psver" From 315df5ba28774923a86b4ae1834fc323e54ede26 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 8 Aug 2022 19:38:15 -0700 Subject: [PATCH 19/21] Collapss shell function --- src/settings.rs | 64 +++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index 739d6a4d1a..7aeb65a2ff 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -41,41 +41,37 @@ impl<'src> Settings<'src> { pub(crate) fn shell<'a>(&'a self, config: &'a Config) -> (&'a str, Vec<&'a str>) { match (&config.shell, &config.shell_args) { - (Some(shell), Some(shell_args)) => { - return (shell, shell_args.iter().map(String::as_ref).collect()) + (Some(shell), Some(shell_args)) => (shell, shell_args.iter().map(String::as_ref).collect()), + (Some(shell), None) => (shell, DEFAULT_SHELL_ARGS.to_vec()), + (None, Some(shell_args)) => ( + DEFAULT_SHELL, + shell_args.iter().map(String::as_ref).collect(), + ), + (None, None) => { + if let (true, Some(shell)) = (cfg!(windows), &self.windows_shell) { + ( + shell.command.cooked.as_ref(), + shell + .arguments + .iter() + .map(|argument| argument.cooked.as_ref()) + .collect(), + ) + } else if cfg!(windows) && self.windows_powershell { + (WINDOWS_POWERSHELL_SHELL, WINDOWS_POWERSHELL_ARGS.to_vec()) + } else if let Some(shell) = &self.shell { + ( + shell.command.cooked.as_ref(), + shell + .arguments + .iter() + .map(|argument| argument.cooked.as_ref()) + .collect(), + ) + } else { + (DEFAULT_SHELL, DEFAULT_SHELL_ARGS.to_vec()) + } } - (Some(shell), None) => return (shell, DEFAULT_SHELL_ARGS.to_vec()), - (None, Some(shell_args)) => { - return ( - DEFAULT_SHELL, - shell_args.iter().map(String::as_ref).collect(), - ) - } - (None, None) => {} - } - - if let (true, Some(shell)) = (cfg!(windows), &self.windows_shell) { - ( - shell.command.cooked.as_ref(), - shell - .arguments - .iter() - .map(|argument| argument.cooked.as_ref()) - .collect(), - ) - } else if cfg!(windows) && self.windows_powershell { - (WINDOWS_POWERSHELL_SHELL, WINDOWS_POWERSHELL_ARGS.to_vec()) - } else if let Some(shell) = &self.shell { - ( - shell.command.cooked.as_ref(), - shell - .arguments - .iter() - .map(|argument| argument.cooked.as_ref()) - .collect(), - ) - } else { - (DEFAULT_SHELL, DEFAULT_SHELL_ARGS.to_vec()) } } } From f1ddab1d3a2a62415b8fa7d86a1b7eec27e0fc2d Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 8 Aug 2022 19:45:02 -0700 Subject: [PATCH 20/21] tweak --- src/settings.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/settings.rs b/src/settings.rs index 7aeb65a2ff..20bcdd9764 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -191,7 +191,7 @@ mod tests { }; if cfg!(windows) { - assert_eq!(settings.shell(&config), ("powershell.exe", vec!["-nice"])); + assert_eq!(settings.shell(&config), ("sh", vec!["-nice"])); } else { assert_eq!(settings.shell(&config), ("sh", vec!["-nice"])); } From 13106ceaa821f7347c1b679b528b67b55754988d Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 8 Aug 2022 19:45:10 -0700 Subject: [PATCH 21/21] tweak --- src/settings.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index 20bcdd9764..c755a60459 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -190,10 +190,6 @@ mod tests { ..testing::config(&[]) }; - if cfg!(windows) { - assert_eq!(settings.shell(&config), ("sh", vec!["-nice"])); - } else { - assert_eq!(settings.shell(&config), ("sh", vec!["-nice"])); - } + assert_eq!(settings.shell(&config), ("sh", vec!["-nice"])); } }