Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add initial nushell support #271

Merged
merged 3 commits into from
Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions crates/rattler_shell/src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,64 @@ 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, "$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());
cmd.arg(path);
cmd
}
}

/// A generic [`Shell`] implementation for concrete shell types.
#[enum_dispatch]
#[allow(missing_docs)]
Expand All @@ -433,6 +491,7 @@ pub enum ShellEnum {
CmdExe,
PowerShell,
Fish,
NuShell,
}

// The default shell is determined by the current OS.
Expand Down Expand Up @@ -509,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(
Expand Down Expand Up @@ -540,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",
Expand Down
Loading