use std::fs::{self, OpenOptions}; use std::path::PathBuf; use std::sync::Mutex; use std::sync::atomic::{AtomicBool, Ordering}; use color_eyre::eyre::{Result, eyre}; use tracing_subscriber::fmt::writer::MakeWriterExt; /// Whether this is a local build (`HEX_LOCAL_BUILD=1`): made from a checkout /// for its owner's own machine, so it never checks for or offers updates. pub fn is_local_build() -> bool { option_env!("HEX_LOCAL_BUILD") == Some("1") } /// Whether this is a macOS `Hex Test.app` build (`HEX_TEST_BUILD=1`), which /// keeps its data, settings, and user config apart from the installed Hex. pub fn is_macos_test_build() -> bool { cfg!(target_os = "macos") && option_env!("HEX_TEST_BUILD") == Some("1") } /// The name menus show, so a test build running beside Hex reads as itself. #[cfg(target_os = "macos")] pub fn app_name() -> &'static str { if is_macos_test_build() { "Hex Test" } else { "Hex" } } pub fn support_dir() -> Result { if let Some(path) = std::env::var_os("HEX_APPLICATION_SUPPORT_DIR") { return Ok(path.into()); } let data = dirs::data_dir().ok_or_else(|| eyre!("application data directory is unavailable"))?; let folder = if is_macos_test_build() { "hex-test" } else { "hex" }; Ok(data.join(folder)) } pub fn logs_dir() -> Result { Ok(support_dir()?.join("logs")) } /// Creates the logs directory, appends process diagnostics to `process.log` /// alongside stderr, installs the global tracing subscriber, and routes Ctrl-C /// to `shutdown`. Returns the logs directory. pub fn init_process_logging(shutdown: &'static AtomicBool) -> Result { let log_dir = logs_dir()?; fs::create_dir_all(&log_dir)?; let process_log = OpenOptions::new() .create(true) .append(true) .open(log_dir.join("process.log"))?; let filter = tracing_subscriber::EnvFilter::try_from_default_env() .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("hex=info")); tracing_subscriber::fmt() .with_env_filter(filter) .with_writer(std::io::stderr.and(Mutex::new(process_log))) .init(); log_panics(); ctrlc::set_handler(|| shutdown.store(true, Ordering::Relaxed))?; Ok(log_dir) } /// Records every panic in `process.log` before the installed report runs. /// Desktop apps have no console, so the default stderr report is otherwise /// lost and a crashed worker thread leaves no trace. fn log_panics() { let report = std::panic::take_hook(); std::panic::set_hook(Box::new(move |info| { tracing::error!( thread = std::thread::current().name().unwrap_or("unnamed"), backtrace = %std::backtrace::Backtrace::force_capture(), "panic: {info}" ); report(info); })); } pub fn opencode_workspace() -> Result { Ok(support_dir()?.join("opencode")) } pub fn local_api_discovery_file() -> Result { Ok(support_dir()?.join("local-api.json")) } pub fn personal_commands_status_file() -> Result { Ok(support_dir()?.join("personal-commands.json")) } /// The config hash the person allowed to use `fetch` and run programs. It /// lives here, not in the config folder, so a config cannot allow itself. pub fn transform_trust_file() -> Result { Ok(support_dir()?.join("transform-trust.json")) } pub fn personal_commands_workspace() -> Result { user_config_dir() } /// The per-user config folder holding `hex.config.ts` on every OS: `~/.config/hex`, or `~/.config/hex-test` for `Hex Test.app`. fn user_config_dir() -> Result { let home = home_dir().ok_or_else(|| eyre!("home directory is unavailable"))?; Ok(user_config_dir_in(&home, is_macos_test_build())) } /// The home folder. On Windows `USERPROFILE` names it, as `HOME` does /// elsewhere, so a copy of Hex launched with another profile folder (an /// isolated benchmark) reads that folder's config; `dirs::home_dir` asks the /// shell for the signed-in user's own folder instead. Every path Hex derives /// from the user's folders goes through these, as Annotate's engine does /// (`scrawl_router::user_dirs`). pub fn home_dir() -> Option { #[cfg(windows)] if let Some(profile) = std::env::var_os("USERPROFILE").filter(|profile| !profile.is_empty()) { return Some(profile.into()); } dirs::home_dir() } /// The roaming data folder: `APPDATA` on Windows, else `dirs::data_dir`. pub fn data_dir() -> Option { #[cfg(windows)] if let Some(roaming) = std::env::var_os("APPDATA").filter(|value| !value.is_empty()) { return Some(roaming.into()); } dirs::data_dir() } /// The Desktop folder (Labs › Screenshots saves there by default): /// `dirs::desktop_dir`, else `Desktop` in the home folder. pub fn desktop_dir() -> PathBuf { dirs::desktop_dir() .or_else(|| home_dir().map(|home| home.join("Desktop"))) .unwrap_or_else(|| PathBuf::from("Desktop")) } /// The Documents folder: `dirs::document_dir`, or `Documents` in a Windows /// profile `USERPROFILE` points to that is not the signed-in user's own. pub fn document_dir() -> Option { #[cfg(windows)] if let (Some(profile), Some(own)) = (home_dir(), dirs::home_dir()) && !profile .to_string_lossy() .trim_end_matches(['\\', '/']) .eq_ignore_ascii_case(own.to_string_lossy().trim_end_matches(['\\', '/'])) { return Some(profile.join("Documents")); } dirs::document_dir() } fn user_config_dir_in(home: &std::path::Path, test_build: bool) -> PathBuf { home.join(".config") .join(if test_build { "hex-test" } else { "hex" }) } #[cfg(test)] mod tests { use super::*; #[test] fn each_build_has_its_own_config_folder() { let home = std::path::Path::new("/Users/kit"); assert_eq!( user_config_dir_in(home, false), home.join(".config").join("hex") ); assert_eq!( user_config_dir_in(home, true), home.join(".config").join("hex-test") ); } }