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 executable to the shell trait and implement it #224

Merged
merged 7 commits into from
Jun 22, 2023
48 changes: 37 additions & 11 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,8 +232,12 @@ 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
}
Expand All @@ -244,9 +263,12 @@ impl Shell for PowerShell {
fn extension(&self) -> &str {
"ps1"
}
fn executable(&self) -> &str {
"powershell"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pwsh[.exe] is the executable file name of PowerShell [Core] (v6+), the cross-platform edition of PowerShell built on .NET Core / .NET 5+; by contrast, powershell.exe is the executable name of the legacy Windows PowerShell edition (v5.1-), built on the Windows-only .NET Framework (v4.8-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use pwsh? cc @baszalmstra

}

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 +295,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