use std::ffi::{OsStr, OsString};
use base64::Engine;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Shell {
Sh,
PowerShell,
}
impl Shell {
pub fn host() -> Self {
if cfg!(windows) { Self::PowerShell } else { Self::Sh }
}
pub fn describe(self) -> &'static str {
match self {
Self::Sh => "`sh -c`",
Self::PowerShell => "PowerShell",
}
}
pub fn argv(self, command: &str) -> (OsString, Vec<OsString>) {
match self {
Self::Sh => ("sh".into(), vec!["-c".into(), command.into()]),
Self::PowerShell => {
let program = if byob_sandbox::which(OsStr::new("pwsh")).is_some() { "pwsh" } else { "powershell" };
let args = ["-NoLogo", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-EncodedCommand"];
let mut v: Vec<OsString> = args.iter().map(OsString::from).collect();
v.push(encode(&powershell_script(command)).into());
(program.into(), v)
}
}
}
}
fn powershell_script(command: &str) -> String {
format!(
"$ProgressPreference = 'SilentlyContinue'\n\
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8\n\
$OutputEncoding = [System.Text.Encoding]::UTF8\n\
if ($PSVersionTable.PSVersion.Major -lt 6) {{ $PSDefaultParameterValues['*:Encoding'] = 'utf8' }}\n\
{command}\n\
if ($LASTEXITCODE) {{ exit $LASTEXITCODE }}\n"
)
}
fn encode(script: &str) -> String {
let bytes: Vec<u8> = script.encode_utf16().flat_map(u16::to_le_bytes).collect();
base64::engine::general_purpose::STANDARD.encode(bytes)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sh_takes_the_command_as_is() {
let (p, a) = Shell::Sh.argv("echo 'hi' | wc -c");
assert_eq!((p, a), ("sh".into(), vec![OsString::from("-c"), "echo 'hi' | wc -c".into()]));
}
#[test]
fn powershell_gets_the_command_encoded_with_quotes_intact() {
let (p, a) = Shell::PowerShell.argv(r#"Get-ChildItem "my files" | Select-Object -First 2"#);
assert!(p == "pwsh" || p == "powershell", "{p:?}");
assert_eq!(a.get(a.len() - 2).map(OsString::as_os_str), Some(OsStr::new("-EncodedCommand")));
let b64 = a.last().and_then(|s| s.to_str()).unwrap();
let raw = base64::engine::general_purpose::STANDARD.decode(b64).unwrap();
let units: Vec<u16> = raw.chunks(2).map(|c| u16::from_le_bytes([c[0], c[1]])).collect();
let script = String::from_utf16(&units).unwrap();
assert!(script.contains(r#"Get-ChildItem "my files" | Select-Object -First 2"#), "{script}");
assert!(script.contains("$PSDefaultParameterValues['*:Encoding'] = 'utf8'"), "{script}");
assert!(script.contains("[Console]::OutputEncoding") && script.trim_end().ends_with("exit $LASTEXITCODE }"), "{script}");
}
#[test]
fn the_host_shell_matches_the_os() {
assert_eq!(Shell::host(), if cfg!(windows) { Shell::PowerShell } else { Shell::Sh });
}
}