Skip to content

Commit 33eb552

Browse files
committed
Use XDG_DATA_HOME on *nix if no ~/.pkgx
Fixes #1103
1 parent d47d9e0 commit 33eb552

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

crates/lib/src/config.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::env;
2+
use std::fs;
23
use std::io;
34
use std::path::PathBuf;
45

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

4445
fn get_pkgx_dir() -> io::Result<PathBuf> {
45-
if let Ok(env_dir) = env::var("PKGX_DIR") {
46-
let path = PathBuf::from(env_dir);
46+
if let Ok(path) = env::var("PKGX_DIR").map(PathBuf::from) {
4747
if !path.is_absolute() {
48-
return Ok(env::current_dir()?.join(path));
48+
Ok(env::current_dir()?.join(path))
4949
} else {
50-
return Ok(path);
50+
Ok(path)
51+
}
52+
} else {
53+
let home = dirs_next::home_dir();
54+
let default = home.clone().map(|x| x.join(".pkgx"));
55+
56+
#[cfg(target_os = "macos")]
57+
if let Some(true) = home.clone().map(|x| x.join("Library/Packages").exists()) {
58+
Ok(home.unwrap().join("Library/Packages"))
59+
} else {
60+
Ok(default.unwrap())
61+
}
62+
63+
#[cfg(not(target_os = "macos"))]
64+
if let Some(true) = default.clone().map(|x| x.exists()) {
65+
Ok(default.unwrap())
66+
} else {
67+
Ok(dirs_next::data_local_dir().unwrap().join("pkgx"))
5168
}
5269
}
53-
#[cfg(target_os = "macos")]
54-
return Ok(dirs_next::home_dir().unwrap().join(".pkgx"));
55-
#[cfg(target_os = "linux")]
56-
return Ok(dirs_next::home_dir().unwrap().join(".pkgx"));
57-
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
58-
panic!("Unsupported platform")
5970
}

docs/faq.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,17 @@ following [semver] syntax:
214214

215215
## Where does `pkgx` store files
216216

217-
* pkgs are downloaded to `~/.pkgx` (`$PKGX_DIR` overrides)
218-
* runtime data like the [pantry] is stored in:
217+
Packages are downloaded to `$PKGX_DIR` if set. If not set:
218+
219+
* macOS
220+
* `~/Library/Packages` if the directory exists
221+
* `~/.pkgx` otherwise
222+
* *nix
223+
* `~/.pkgx` if the directory exists
224+
* `${XDG_DATA_HOME:-$HOME/.local/share}/pkgx` otherwise
225+
226+
Some cache data is stored:
227+
219228
* `~/Library/Caches/pkgx` on Mac
220229
* `${XDG_CACHE_HOME:-$HOME/.cache}/pkgx` on *nix
221230
* `%LOCALAPPDATA%/pkgx` on Windows

0 commit comments

Comments
 (0)