Skip to content

Commit

Permalink
Use XDG_DATA_HOME on *nix if no ~/.pkgx
Browse files Browse the repository at this point in the history
Fixes #1103
  • Loading branch information
mxcl committed Jan 29, 2025
1 parent d47d9e0 commit 33eb552
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
31 changes: 21 additions & 10 deletions crates/lib/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env;
use std::fs;
use std::io;
use std::path::PathBuf;

Expand Down Expand Up @@ -42,18 +43,28 @@ fn get_pantry_dir() -> io::Result<PathBuf> {
}

fn get_pkgx_dir() -> io::Result<PathBuf> {
if let Ok(env_dir) = env::var("PKGX_DIR") {
let path = PathBuf::from(env_dir);
if let Ok(path) = env::var("PKGX_DIR").map(PathBuf::from) {
if !path.is_absolute() {
return Ok(env::current_dir()?.join(path));
Ok(env::current_dir()?.join(path))
} else {
return Ok(path);
Ok(path)
}
} else {
let home = dirs_next::home_dir();
let default = home.clone().map(|x| x.join(".pkgx"));

#[cfg(target_os = "macos")]
if let Some(true) = home.clone().map(|x| x.join("Library/Packages").exists()) {
Ok(home.unwrap().join("Library/Packages"))
} else {
Ok(default.unwrap())
}

#[cfg(not(target_os = "macos"))]
if let Some(true) = default.clone().map(|x| x.exists()) {
Ok(default.unwrap())
} else {
Ok(dirs_next::data_local_dir().unwrap().join("pkgx"))
}
}
#[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")
}
13 changes: 11 additions & 2 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,17 @@ following [semver] syntax:

## Where does `pkgx` store files

* pkgs are downloaded to `~/.pkgx` (`$PKGX_DIR` overrides)
* runtime data like the [pantry] is stored in:
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
Expand Down

0 comments on commit 33eb552

Please sign in to comment.