Skip to content

Commit

Permalink
Enable global configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Apr 17, 2024
1 parent c859405 commit ba0f96e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ dashmap = { version = "5.5.3" }
data-encoding = { version = "2.5.0" }
derivative = { version = "2.2.0" }
directories = { version = "5.0.1" }
dirs-sys = { version = "0.4.1" }
dunce = { version = "1.0.4" }
either = { version = "1.9.0" }
encoding_rs_io = { version = "0.1.7" }
Expand Down
1 change: 1 addition & 0 deletions crates/uv-workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ uv-resolver = { workspace = true, features = ["schemars", "serde"] }
uv-toolchain = { workspace = true, features = ["schemars", "serde"] }
uv-warnings = { workspace = true }

dirs-sys = { workspace = true }
fs-err = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true }
Expand Down
34 changes: 34 additions & 0 deletions crates/uv-workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ pub struct Workspace {
}

impl Workspace {
/// Load the user [`Workspace`].
pub fn user() -> Result<Option<Self>, WorkspaceError> {
let Some(dir) = config_dir() else {
return Ok(None);
};
let root = dir.join("uv");
let file = root.join("uv.toml");
Ok(Some(Self {
options: find_in_directory(&file)?.unwrap_or_default(),
root,
}))
}

/// Find the [`Workspace`] for the given path.
///
/// The search starts at the given path and goes up the directory tree until a workspace is
Expand Down Expand Up @@ -52,6 +65,27 @@ impl Workspace {
}
}

/// 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.
fn 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))]
{
std::env::var_os("XDG_CONFIG_HOME")
.and_then(dirs_sys::is_absolute_path)
.or_else(|| dirs_sys::home_dir().map(|path| path.join(".config")))
}
}

/// Read a `uv.toml` or `pyproject.toml` file in the given directory.
fn find_in_directory(dir: &Path) -> Result<Option<Options>, WorkspaceError> {
// Read a `uv.toml` file in the current directory.
Expand Down
9 changes: 7 additions & 2 deletions crates/uv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,16 @@ async fn run() -> Result<ExitStatus> {
}
};

// Load the workspace settings.
// Load the workspace settings, prioritizing (in order):
// 1. The configuration file specified on the command-line.
// 2. The configuration file in the current directory.
// 3. The user configuration file.
let workspace = if let Some(config_file) = cli.config_file.as_ref() {
Some(uv_workspace::Workspace::from_file(config_file)?)
} else if let Some(workspace) = uv_workspace::Workspace::find(env::current_dir()?)? {
Some(workspace)
} else {
uv_workspace::Workspace::find(env::current_dir()?)?
uv_workspace::Workspace::user()?
};

// Resolve the global settings.
Expand Down

0 comments on commit ba0f96e

Please sign in to comment.