Eliminate dependencies on directores and dirs-sys (#8048)

Migrate all directory related logic to `etcetera`, eliminated two
dependecies.
This commit is contained in:
Zanie Blue 2024-11-05 10:57:55 -06:00
parent 4874b32d85
commit 1df8f86c22
7 changed files with 39 additions and 46 deletions

15
Cargo.lock generated
View file

@ -914,15 +914,6 @@ dependencies = [
"crypto-common",
]
[[package]]
name = "directories"
version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs"
version = "5.0.1"
@ -4599,8 +4590,6 @@ dependencies = [
name = "uv-dirs"
version = "0.0.1"
dependencies = [
"directories",
"dirs-sys",
"etcetera",
"uv-static",
]
@ -5236,7 +5225,7 @@ version = "0.0.1"
dependencies = [
"assert_fs",
"clap",
"dirs-sys",
"etcetera",
"fs-err",
"indoc",
"schemars",
@ -5632,7 +5621,7 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys 0.59.0",
"windows-sys 0.48.0",
]
[[package]]

View file

@ -91,8 +91,6 @@ csv = { version = "1.3.0" }
ctrlc = { version = "3.4.5" }
dashmap = { version = "6.1.0" }
data-encoding = { version = "2.6.0" }
directories = { version = "5.0.1" }
dirs-sys = { version = "0.4.1" }
dotenvy = { version = "0.15.7" }
dunce = { version = "1.0.5" }
either = { version = "1.13.0" }

View file

@ -19,6 +19,4 @@ workspace = true
[dependencies]
uv-static = { workspace = true }
dirs-sys = { workspace = true }
directories = { workspace = true }
etcetera = { workspace = true }

View file

@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::{ffi::OsString, path::PathBuf};
use etcetera::BaseStrategy;
@ -20,19 +20,15 @@ use uv_static::EnvVars;
pub fn user_executable_directory(override_variable: Option<&'static str>) -> Option<PathBuf> {
override_variable
.and_then(std::env::var_os)
.and_then(dirs_sys::is_absolute_path)
.or_else(|| std::env::var_os(EnvVars::XDG_BIN_HOME).and_then(dirs_sys::is_absolute_path))
.and_then(parse_path)
.or_else(|| std::env::var_os(EnvVars::XDG_BIN_HOME).and_then(parse_path))
.or_else(|| {
std::env::var_os(EnvVars::XDG_DATA_HOME)
.and_then(dirs_sys::is_absolute_path)
.and_then(parse_path)
.map(|path| path.join("../bin"))
})
.or_else(|| {
// See https://github.com/dirs-dev/dirs-rs/blob/50b50f31f3363b7656e5e63b3fa1060217cbc844/src/win.rs#L5C58-L5C78
#[cfg(windows)]
let home_dir = dirs_sys::known_folder_profile();
#[cfg(not(windows))]
let home_dir = dirs_sys::home_dir();
let home_dir = etcetera::home_dir().ok();
home_dir.map(|path| path.join(".local").join("bin"))
})
}
@ -51,7 +47,16 @@ pub fn user_cache_dir() -> Option<PathBuf> {
/// Uses `/Users/user/Library/Application Support/uv` on macOS, in contrast to the new preference
/// for using the XDG directories on all Unix platforms.
pub fn legacy_user_cache_dir() -> Option<PathBuf> {
directories::ProjectDirs::from("", "", "uv").map(|dirs| dirs.cache_dir().to_path_buf())
etcetera::base_strategy::choose_native_strategy()
.ok()
.map(|dirs| dirs.cache_dir().join("uv"))
.map(|dir| {
if cfg!(windows) {
dir.join("cache")
} else {
dir
}
})
}
/// Returns an appropriate user-level directory for storing application state.
@ -68,5 +73,18 @@ pub fn user_state_dir() -> Option<PathBuf> {
/// Uses `/Users/user/Library/Application Support/uv` on macOS, in contrast to the new preference
/// for using the XDG directories on all Unix platforms.
pub fn legacy_user_state_dir() -> Option<PathBuf> {
directories::ProjectDirs::from("", "", "uv").map(|dirs| dirs.data_dir().to_path_buf())
etcetera::base_strategy::choose_native_strategy()
.ok()
.map(|dirs| dirs.data_dir().join("uv"))
.map(|dir| if cfg!(windows) { dir.join("data") } else { dir })
}
/// Return a [`PathBuf`] if the given [`OsString`] is an absolute path.
fn parse_path(path: OsString) -> Option<PathBuf> {
let path = PathBuf::from(path);
if path.is_absolute() {
Some(path)
} else {
None
}
}

View file

@ -32,7 +32,7 @@ uv-static = { workspace = true }
uv-warnings = { workspace = true }
clap = { workspace = true }
dirs-sys = { workspace = true }
etcetera = { workspace = true }
fs-err = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true }

View file

@ -2,6 +2,8 @@ use std::env;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use etcetera::BaseStrategy;
use uv_fs::Simplified;
use uv_static::EnvVars;
use uv_warnings::warn_user;
@ -174,23 +176,12 @@ impl From<Options> for FilesystemOptions {
/// Returns the path to the user configuration directory.
///
/// This is similar to the `config_dir()` returned by the `dirs` crate, but it uses the
/// `XDG_CONFIG_HOME` environment variable on both Linux _and_ macOS, rather than the
/// `Application Support` directory on macOS.
/// On Windows, use, e.g., C:\Users\Alice\AppData\Roaming
/// On Linux and macOS, use `XDG_CONFIG_HOME` or $HOME/.config, e.g., /home/alice/.config.
fn user_config_dir() -> Option<PathBuf> {
// On Windows, use, e.g., `C:\Users\Alice\AppData\Roaming`.
#[cfg(windows)]
{
dirs_sys::known_folder_roaming_app_data()
}
// On Linux and macOS, use, e.g., `/home/alice/.config`.
#[cfg(not(windows))]
{
env::var_os(EnvVars::XDG_CONFIG_HOME)
.and_then(dirs_sys::is_absolute_path)
.or_else(|| dirs_sys::home_dir().map(|path| path.join(".config")))
}
etcetera::choose_base_strategy()
.map(|dirs| dirs.config_dir())
.ok()
}
#[cfg(not(windows))]

View file

@ -1,5 +1,4 @@
use core::fmt;
use fs_err as fs;
use uv_dirs::user_executable_directory;