mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* Labs: ⌘⇧4 screenshots on macOS (t1300) * Fix the thumbnail golden's render (t1300) * Round the thumbnail's size; list its scene in the playground (t1300) * Keep rune_ui.rs under the size limit (t1300) * Thumbnail goldens (t1300) * Allow macOS-only re-exports elsewhere (t1300)
180 lines
6 KiB
Rust
180 lines
6 KiB
Rust
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<PathBuf> {
|
||
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<PathBuf> {
|
||
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<PathBuf> {
|
||
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<PathBuf> {
|
||
Ok(support_dir()?.join("opencode"))
|
||
}
|
||
|
||
pub fn local_api_discovery_file() -> Result<PathBuf> {
|
||
Ok(support_dir()?.join("local-api.json"))
|
||
}
|
||
|
||
pub fn personal_commands_status_file() -> Result<PathBuf> {
|
||
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<PathBuf> {
|
||
Ok(support_dir()?.join("transform-trust.json"))
|
||
}
|
||
|
||
pub fn personal_commands_workspace() -> Result<PathBuf> {
|
||
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<PathBuf> {
|
||
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<PathBuf> {
|
||
#[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<PathBuf> {
|
||
#[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<PathBuf> {
|
||
#[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")
|
||
);
|
||
}
|
||
}
|