use std::collections::HashMap;
use std::process::Command;
use std::sync::Mutex;
static BINARY_CACHE: std::sync::LazyLock<Mutex<HashMap<String, bool>>> =
std::sync::LazyLock::new(|| Mutex::new(HashMap::new()));
pub async fn is_binary_installed(command: &str) -> bool {
let trimmed = command.trim();
if trimmed.is_empty() {
return false;
}
if let Some(cached) = BINARY_CACHE.lock().unwrap().get(trimmed) {
return *cached;
}
let exists = which_sync(trimmed);
BINARY_CACHE
.lock()
.unwrap()
.insert(trimmed.to_string(), exists);
exists
}
pub fn which_sync(command: &str) -> bool {
#[cfg(unix)]
{
Command::new("which")
.arg(command)
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
#[cfg(windows)]
{
Command::new("where")
.arg(command)
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
}
pub fn clear_binary_cache() {
BINARY_CACHE.lock().unwrap().clear();
}