use std::env;
use std::path::{Path, PathBuf};
#[cfg(target_os = "macos")]
pub fn is_app_bundle(path: &Path) -> bool {
if path.is_dir() {
let mut info_plist = path.to_path_buf();
info_plist.push("Contents");
info_plist.push("Info.plist");
return info_plist.exists();
}
false
}
#[cfg(unix)]
fn is_executable(path: &Path) -> bool {
use std::fs;
use std::os::unix::fs::PermissionsExt;
match fs::metadata(path).ok() {
Some(meta) => meta.permissions().mode() & 0o111 != 0,
None => false,
}
}
#[cfg(not(unix))]
fn is_executable(_: &Path) -> bool {
true
}
pub fn is_binary(path: &Path) -> bool {
path.exists() && path.is_file() && is_executable(path)
}
pub fn find_binary(binary_name: &str) -> Option<PathBuf> {
env::var_os("PATH").and_then(|path_env| {
for mut path in env::split_paths(&path_env) {
path.push(binary_name);
if is_binary(&path) {
return Some(path);
}
}
None
})
}