From 88de5fdd60bd8110575c31e9cc9b994f5788f811 Mon Sep 17 00:00:00 2001 From: Wolf Vollprecht Date: Sun, 23 Jul 2023 23:48:42 +0200 Subject: [PATCH 1/3] add initial nushell support --- crates/rattler_shell/src/shell/mod.rs | 64 +++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/crates/rattler_shell/src/shell/mod.rs b/crates/rattler_shell/src/shell/mod.rs index 24847d72b..f113b5581 100644 --- a/crates/rattler_shell/src/shell/mod.rs +++ b/crates/rattler_shell/src/shell/mod.rs @@ -422,6 +422,70 @@ impl Shell for Fish { } } +/// A [`Shell`] implementation for the Bash shell. +#[derive(Debug, Clone, Copy, Default)] +pub struct Nushell; + +impl Shell for Nushell { + fn set_env_var(&self, f: &mut impl Write, env_var: &str, value: &str) -> std::fmt::Result { + writeln!(f, "let-env {} = \"{}\"", env_var, value) + } + + fn unset_env_var(&self, f: &mut impl Write, env_var: &str) -> std::fmt::Result { + writeln!(f, "hide-env {}", env_var) + } + + fn run_script(&self, f: &mut impl Write, path: &Path) -> std::fmt::Result { + writeln!(f, "source \"{}\"", path.to_string_lossy()) + } + + fn set_path( + &self, + f: &mut impl Write, + paths: &[PathBuf], + modification_behaviour: PathModificationBehaviour, + _platform: &Platform, + ) -> std::fmt::Result { + let path = paths + .iter() + .map(|path| format!("\"{}\"", path.to_string_lossy().into_owned())) + .join(" "); + // Replace, Append, or Prepend the path variable to the paths. + match modification_behaviour { + PathModificationBehaviour::Replace => { + self.set_env_var(f, "PATH", &format!("[{}]", path)) + } + PathModificationBehaviour::Prepend => { + writeln!(f, "let-env PATH = ($env.PATH | prepend [{}])", path) + } + PathModificationBehaviour::Append => { + writeln!(f, "let-env PATH = ($env.PATH | append [{}])", path) + } + } + } + + fn extension(&self) -> &str { + "nu" + } + + fn executable(&self) -> &str { + "nu" + } + + fn create_run_script_command(&self, path: &Path) -> Command { + let mut cmd = Command::new(self.executable()); + + // check if we are on Windows, and if yes, convert native path to unix for (Git) Bash + if cfg!(windows) { + cmd.arg(native_path_to_unix(path.to_str().unwrap()).unwrap()); + } else { + cmd.arg(path); + } + + cmd + } +} + /// A generic [`Shell`] implementation for concrete shell types. #[enum_dispatch] #[allow(missing_docs)] From a40fa29fa6b28d6d2d08a1ac19860a51e3a1cfdf Mon Sep 17 00:00:00 2001 From: Wolf Vollprecht Date: Mon, 24 Jul 2023 09:59:08 +0200 Subject: [PATCH 2/3] finish implementation --- crates/rattler_shell/src/shell/mod.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/crates/rattler_shell/src/shell/mod.rs b/crates/rattler_shell/src/shell/mod.rs index f113b5581..5fac6e2a7 100644 --- a/crates/rattler_shell/src/shell/mod.rs +++ b/crates/rattler_shell/src/shell/mod.rs @@ -424,9 +424,9 @@ impl Shell for Fish { /// A [`Shell`] implementation for the Bash shell. #[derive(Debug, Clone, Copy, Default)] -pub struct Nushell; +pub struct NuShell; -impl Shell for Nushell { +impl Shell for NuShell { fn set_env_var(&self, f: &mut impl Write, env_var: &str, value: &str) -> std::fmt::Result { writeln!(f, "let-env {} = \"{}\"", env_var, value) } @@ -449,7 +449,8 @@ impl Shell for Nushell { let path = paths .iter() .map(|path| format!("\"{}\"", path.to_string_lossy().into_owned())) - .join(" "); + .join(", "); + // Replace, Append, or Prepend the path variable to the paths. match modification_behaviour { PathModificationBehaviour::Replace => { @@ -474,14 +475,7 @@ impl Shell for Nushell { fn create_run_script_command(&self, path: &Path) -> Command { let mut cmd = Command::new(self.executable()); - - // check if we are on Windows, and if yes, convert native path to unix for (Git) Bash - if cfg!(windows) { - cmd.arg(native_path_to_unix(path.to_str().unwrap()).unwrap()); - } else { - cmd.arg(path); - } - + cmd.arg(path); cmd } } @@ -497,6 +491,7 @@ pub enum ShellEnum { CmdExe, PowerShell, Fish, + NuShell, } // The default shell is determined by the current OS. @@ -573,6 +568,8 @@ impl ShellEnum { Some(Xonsh.into()) } else if parent_process_name.contains("fish") { Some(Fish.into()) + } else if parent_process_name.contains("nu") { + Some(NuShell.into()) } else if parent_process_name.contains("powershell") || parent_process_name.contains("pwsh") { Some( @@ -604,6 +601,7 @@ impl FromStr for ShellEnum { "xonsh" => Ok(Xonsh.into()), "fish" => Ok(Fish.into()), "cmd" => Ok(CmdExe.into()), + "nu" | "nushell" => Ok(NuShell.into()), "powershell" | "powershell_ise" => Ok(PowerShell::default().into()), _ => Err(ParseShellEnumError(format!( "'{}' is an unknown shell variant", From f4feea080cd3ce4144d188c5d718d495fd4aad88 Mon Sep 17 00:00:00 2001 From: Wolf Vollprecht Date: Wed, 27 Sep 2023 17:17:11 +0200 Subject: [PATCH 3/3] use modern way of setting env vars in nushell --- crates/rattler_shell/src/shell/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rattler_shell/src/shell/mod.rs b/crates/rattler_shell/src/shell/mod.rs index 5fac6e2a7..fa1007e82 100644 --- a/crates/rattler_shell/src/shell/mod.rs +++ b/crates/rattler_shell/src/shell/mod.rs @@ -428,7 +428,7 @@ pub struct NuShell; impl Shell for NuShell { fn set_env_var(&self, f: &mut impl Write, env_var: &str, value: &str) -> std::fmt::Result { - writeln!(f, "let-env {} = \"{}\"", env_var, value) + writeln!(f, "$env.{} = \"{}\"", env_var, value) } fn unset_env_var(&self, f: &mut impl Write, env_var: &str) -> std::fmt::Result {