Skip to content

Commit

Permalink
feat: add executable name to the shell trait and implement it (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruben-arts authored Jun 22, 2023
1 parent a08f0ec commit f89bb21
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/rattler_shell/src/activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ mod tests {
#[test]
#[cfg(unix)]
fn test_activation_script_powershell() {
let script = get_script(shell::PowerShell);
let script = get_script(shell::PowerShell::default());
insta::assert_snapshot!(script);
}

Expand Down
66 changes: 50 additions & 16 deletions crates/rattler_shell/src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ pub trait Shell {
/// The extension that shell scripts for this interpreter usually use.
fn extension(&self) -> &str;

/// The executable that can be called to start this shell.
fn executable(&self) -> &str;

/// Constructs a [`Command`] that will execute the specified script by this shell.
fn create_run_script_command(&self, path: &Path) -> Command;
}

/// Convert a native PATH on Windows to a Unix style path usign cygpath.
fn native_path_to_unix(path: &str) -> Result<String, std::io::Error> {
// call cygpath on Windows to convert paths to Unix style
let output = std::process::Command::new("cygpath")
let output = Command::new("cygpath")
.arg("--unix")
.arg("--path")
.arg(path)
Expand Down Expand Up @@ -102,10 +105,6 @@ impl Shell for Bash {
writeln!(f, ". \"{}\"", path.to_string_lossy())
}

fn extension(&self) -> &str {
"sh"
}

fn set_path(&self, f: &mut impl Write, paths: &[PathBuf]) -> std::fmt::Result {
let path = std::env::join_paths(paths).unwrap();

Expand All @@ -118,8 +117,16 @@ impl Shell for Bash {
self.set_env_var(f, "PATH", path.to_str().unwrap())
}

fn extension(&self) -> &str {
"sh"
}

fn executable(&self) -> &str {
"bash"
}

fn create_run_script_command(&self, path: &Path) -> Command {
let mut cmd = Command::new("bash");
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) {
Expand Down Expand Up @@ -153,8 +160,12 @@ impl Shell for Zsh {
"sh"
}

fn executable(&self) -> &str {
"zsh"
}

fn create_run_script_command(&self, path: &Path) -> Command {
let mut cmd = Command::new("zsh");
let mut cmd = Command::new(self.executable());
cmd.arg(path);
cmd
}
Expand All @@ -181,8 +192,12 @@ impl Shell for Xonsh {
"sh"
}

fn executable(&self) -> &str {
"xonsh"
}

fn create_run_script_command(&self, path: &Path) -> Command {
let mut cmd = Command::new("xonsh");
let mut cmd = Command::new(self.executable());
cmd.arg(path);
cmd
}
Expand Down Expand Up @@ -217,16 +232,22 @@ impl Shell for CmdExe {
"bat"
}

fn executable(&self) -> &str {
"cmd.exe"
}

fn create_run_script_command(&self, path: &Path) -> Command {
let mut cmd = Command::new("cmd.exe");
let mut cmd = Command::new(self.executable());
cmd.arg("/D").arg("/C").arg(path);
cmd
}
}

/// A [`Shell`] implementation for PowerShell.
#[derive(Debug, Clone, Copy)]
pub struct PowerShell;
#[derive(Debug, Clone, Default)]
pub struct PowerShell {
executable_path: Option<String>,
}

impl Shell for PowerShell {
fn set_env_var(&self, f: &mut impl Write, env_var: &str, value: &str) -> std::fmt::Result {
Expand All @@ -245,8 +266,12 @@ impl Shell for PowerShell {
"ps1"
}

fn executable(&self) -> &str {
self.executable_path.as_deref().unwrap_or("pwsh")
}

fn create_run_script_command(&self, path: &Path) -> Command {
let mut cmd = Command::new("powershell");
let mut cmd = Command::new(self.executable());
cmd.arg(path);
cmd
}
Expand All @@ -273,8 +298,12 @@ impl Shell for Fish {
"fish"
}

fn executable(&self) -> &str {
"fish"
}

fn create_run_script_command(&self, path: &Path) -> Command {
let mut cmd = Command::new("fish");
let mut cmd = Command::new(self.executable());
cmd.arg(path);
cmd
}
Expand Down Expand Up @@ -321,7 +350,7 @@ impl ShellEnum {
if let Some(env_shell) = std::env::var_os("SHELL") {
Self::from_shell_path(env_shell)
} else if cfg!(windows) {
Some(PowerShell.into())
Some(PowerShell::default().into())
} else {
None
}
Expand Down Expand Up @@ -361,7 +390,12 @@ impl ShellEnum {
Some(Fish.into())
} else if parent_process_name.contains("powershell") || parent_process_name.contains("pwsh")
{
Some(PowerShell.into())
Some(
PowerShell {
executable_path: Some(parent_process_name),
}
.into(),
)
} else if parent_process_name.contains("cmd.exe") {
Some(CmdExe.into())
} else {
Expand All @@ -379,7 +413,7 @@ fn parse_shell_from_path(path: &Path) -> Option<ShellEnum> {
"xonsh" => Some(Xonsh.into()),
"fish" => Some(Fish.into()),
"cmd" => Some(CmdExe.into()),
"powershell" | "powershell_ise" => Some(PowerShell.into()),
"powershell" | "powershell_ise" => Some(PowerShell::default().into()),
_ => None,
}
}
Expand Down

0 comments on commit f89bb21

Please sign in to comment.