Skip to content

Commit

Permalink
Eliminate dependencies on directores and dirs-sys (#8048)
Browse files Browse the repository at this point in the history
Migrate all directory related logic to `etcetera`, eliminated two
dependecies.
  • Loading branch information
zanieb committed Oct 25, 2024
1 parent f2d9274 commit e6a7c6d
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 46 deletions.
15 changes: 2 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
dunce = { version = "1.0.5" }
either = { version = "1.13.0" }
encoding_rs_io = { version = "0.1.7" }
Expand Down
2 changes: 0 additions & 2 deletions crates/uv-dirs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ workspace = true
[dependencies]
uv-static = { workspace = true }

dirs-sys = { workspace = true }
directories = { workspace = true }
etcetera = { workspace = true }
40 changes: 29 additions & 11 deletions crates/uv-dirs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::{ffi::OsString, path::PathBuf};

use etcetera::BaseStrategy;

Expand All @@ -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"))
})
}
Expand All @@ -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.
Expand All @@ -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
}
}
2 changes: 1 addition & 1 deletion crates/uv-settings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
22 changes: 6 additions & 16 deletions crates/uv-settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::env;
use std::ops::Deref;
use std::path::{Path, PathBuf};

use etcetera::BaseStrategy;
use tracing::debug;

use uv_fs::Simplified;
Expand Down Expand Up @@ -176,23 +177,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))]
Expand Down
1 change: 0 additions & 1 deletion crates/uv-tool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::fmt;

use fs_err as fs;

use uv_dirs::user_executable_directory;
Expand Down

0 comments on commit e6a7c6d

Please sign in to comment.