#[cfg(any(target_os = "macos", target_os = "linux"))]
use crate::tools::common::run;
use crate::tools::common::{InstallError, has};
pub fn ensure(_min_hint: &str) -> Result<(), InstallError> {
if has("brew") {
return Ok(());
}
install()
}
pub fn add_handler(min_hint: &str) -> Result<(), InstallError> {
let _ = min_hint;
ensure("")
}
fn install() -> Result<(), InstallError> {
#[cfg(target_os = "macos")]
{
return install_macos();
}
#[cfg(target_os = "linux")]
{
return install_linux();
}
#[cfg(target_os = "windows")]
{
return install_windows();
}
#[allow(unreachable_code)]
Err(InstallError::Unsupported)
}
#[cfg(target_os = "macos")]
fn install_macos() -> Result<(), InstallError> {
let script = "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)";
let _ = run("/bin/bash", &["-c", script])?;
Ok(())
}
#[cfg(target_os = "linux")]
fn install_linux() -> Result<(), InstallError> {
let script = "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)";
let _ = run("/bin/bash", &["-c", script])?;
Ok(())
}
#[cfg(target_os = "windows")]
fn install_windows() -> Result<(), InstallError> {
Err(InstallError::Unsupported)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ensure_brew_no_panic() {
let res = ensure("");
assert!(res.is_ok() || res.is_err());
}
#[cfg(target_os = "windows")]
#[test]
fn brew_windows_is_unsupported() {
let res = ensure("");
assert!(
matches!(res, Err(InstallError::Unsupported)),
"brew on Windows should be Unsupported"
);
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[test]
fn brew_unix_not_unsupported() {
let res = ensure("");
assert!(
!matches!(res, Err(InstallError::Unsupported)),
"brew on Unix should not return Unsupported"
);
}
}