#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ShellKind {
Bash,
PowerShell,
}
pub fn detect() -> ShellKind {
match std::env::var("EPKG_SHELL").ok().as_deref() {
Some("powershell") | Some("pwsh") => ShellKind::PowerShell,
_ => ShellKind::Bash,
}
}
pub fn path_sep_os() -> char {
if cfg!(windows) {
';'
} else {
':'
}
}
pub fn ps_escape_single_quoted(value: &str) -> String {
value.replace('\'', "''")
}
pub fn emit_export(key: &str, value: &str, kind: ShellKind) -> String {
match kind {
ShellKind::Bash => {
format!("export {}=\"{}\"", key, value.replace('\\', "\\\\").replace('"', "\\\""))
}
ShellKind::PowerShell => {
format!(
"$env:{} = '{}'",
key,
ps_escape_single_quoted(value)
)
}
}
}
pub fn emit_path(path: &str, kind: ShellKind) -> String {
match kind {
ShellKind::Bash => {
format!("export PATH=\"{}\"", path.replace('\\', "\\\\").replace('"', "\\\""))
}
ShellKind::PowerShell => {
format!(
"$env:PATH = '{}'",
ps_escape_single_quoted(path)
)
}
}
}
pub fn deactivate_script_extension(kind: ShellKind) -> &'static str {
match kind {
ShellKind::Bash => "sh",
ShellKind::PowerShell => "ps1",
}
}