From 6047ffa6aeffa2d6c7b503beecffaebf1d2b7766 Mon Sep 17 00:00:00 2001 From: Max Howell Date: Wed, 29 Jan 2025 13:53:46 -0500 Subject: [PATCH] Use XDG_DATA_HOME on *nix if no ~/.pkgx * Fixes #1103 * Ordering is backwards compatible * Not allowing relative PKGX_DIR which is more sensible and same as pkgx^1 --- crates/lib/src/config.rs | 24 +++++++++++++----------- docs/faq.md | 19 ++++++++++++++----- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/crates/lib/src/config.rs b/crates/lib/src/config.rs index 58381318..44389fe2 100644 --- a/crates/lib/src/config.rs +++ b/crates/lib/src/config.rs @@ -42,18 +42,20 @@ fn get_pantry_dir() -> io::Result { } fn get_pkgx_dir() -> io::Result { - if let Ok(env_dir) = env::var("PKGX_DIR") { - let path = PathBuf::from(env_dir); - if !path.is_absolute() { - return Ok(env::current_dir()?.join(path)); - } else { + if let Ok(path) = env::var("PKGX_DIR") { + let path = PathBuf::from(path); + if path.is_absolute() { return Ok(path); } } - #[cfg(target_os = "macos")] - return Ok(dirs_next::home_dir().unwrap().join(".pkgx")); - #[cfg(target_os = "linux")] - return Ok(dirs_next::home_dir().unwrap().join(".pkgx")); - #[cfg(not(any(target_os = "macos", target_os = "linux")))] - panic!("Unsupported platform") + + let default = dirs_next::home_dir().map(|x| x.join(".pkgx")); + + if default.clone().is_some_and(|x| x.exists()) { + Ok(default.unwrap()) + } else if let Ok(xdg) = env::var("XDG_DATA_HOME") { + Ok(PathBuf::from(xdg).join("pkgx")) + } else { + Ok(default.unwrap()) + } } diff --git a/docs/faq.md b/docs/faq.md index 570f7c50..4389d5a0 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -214,11 +214,20 @@ following [semver] syntax: ## Where does `pkgx` store files -* pkgs are downloaded to `~/.pkgx` (`$PKGX_DIR` overrides) -* runtime data like the [pantry] is stored in: - * `~/Library/Caches/pkgx` on Mac - * `${XDG_CACHE_HOME:-$HOME/.cache}/pkgx` on *nix - * `%LOCALAPPDATA%/pkgx` on Windows +Packages are downloaded to `$PKGX_DIR` if set. If not set: + +* macOS + * `~/Library/Packages` if the directory exists + * `~/.pkgx` otherwise +* *nix + * `~/.pkgx` if the directory exists + * `${XDG_DATA_HOME:-$HOME/.local/share}/pkgx` otherwise + +Some cache data is stored: + +* `~/Library/Caches/pkgx` on Mac +* `${XDG_CACHE_HOME:-$HOME/.cache}/pkgx` on *nix +* `%LOCALAPPDATA%/pkgx` on Windows ## What happens if two packages provide the same named program?