use std::path::PathBuf;
const SIDECAR_NAMES: &[&str] = if cfg!(windows) {
&[
"sidecar-slim.exe",
"truffle-sidecar.exe",
"sidecar-slim",
"truffle-sidecar",
]
} else {
&["sidecar-slim", "truffle-sidecar"]
};
pub fn sidecar_path() -> PathBuf {
if let Ok(path) = std::env::var("TRUFFLE_SIDECAR_PATH") {
let p = PathBuf::from(&path);
if p.exists() {
return p;
}
}
let exe_dir = std::env::current_exe()
.ok()
.and_then(|p| p.parent().map(|d| d.to_path_buf()));
if let Some(ref dir) = exe_dir {
for name in SIDECAR_NAMES {
let candidate = dir.join(name);
if candidate.exists() {
return candidate;
}
}
}
if let Some(config_dir) = config_bin_dir() {
for name in SIDECAR_NAMES {
let candidate = config_dir.join(name);
if candidate.exists() {
return candidate;
}
}
}
#[cfg(not(windows))]
{
for path in &[
"/usr/local/bin/sidecar-slim",
"/usr/local/bin/truffle-sidecar",
] {
let p = PathBuf::from(path);
if p.exists() {
return p;
}
}
}
let build_path = PathBuf::from(env!("TRUFFLE_SIDECAR_PATH"));
if build_path.exists() {
return build_path;
}
PathBuf::from("sidecar-slim")
}
pub fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}
fn config_bin_dir() -> Option<PathBuf> {
#[cfg(target_os = "macos")]
{
std::env::var("HOME")
.ok()
.map(|h| PathBuf::from(h).join("Library/Application Support/truffle/bin"))
}
#[cfg(target_os = "linux")]
{
std::env::var("XDG_CONFIG_HOME")
.ok()
.map(PathBuf::from)
.or_else(|| {
std::env::var("HOME")
.ok()
.map(|h| PathBuf::from(h).join(".config"))
})
.map(|d| d.join("truffle/bin"))
}
#[cfg(target_os = "windows")]
{
std::env::var("APPDATA")
.ok()
.map(|d| PathBuf::from(d).join("truffle\\bin"))
}
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
{
None
}
}