use std::{
io,
path::{Path, PathBuf},
sync::OnceLock,
};
static EXECUTABLE: OnceLock<Option<PathBuf>> = OnceLock::new();
pub(crate) fn current_executable() -> io::Result<PathBuf> {
if let Some(Some(path)) = EXECUTABLE.get() {
return Ok(path.clone());
}
let path = std::env::current_exe()?.canonicalize()?;
let pinned = EXECUTABLE.get_or_init(|| {
let releases = path.parent()?.parent()?;
(releases.file_name()? == "releases"
&& releases.parent()?.join("proa-install.json").is_file())
.then(|| path.clone())
});
Ok(pinned.as_ref().unwrap_or(&path).clone())
}
pub(crate) fn external_path(path: &Path) -> PathBuf {
#[cfg(windows)]
if let Some(text) = path.to_str() {
if let Some(unc) = text.strip_prefix(r"\\?\UNC\") {
return PathBuf::from(format!(r"\\{unc}"));
}
if let Some(plain) = text.strip_prefix(r"\\?\") {
return PathBuf::from(plain);
}
}
path.to_owned()
}