Skip to content

Commit

Permalink
Merge workspace settings with CLI settings (#3045)
Browse files Browse the repository at this point in the history
## Summary

This PR adds the structs and logic necessary to respect settings from
the workspace. It's a ton of code, but it's mostly mechanical. And,
believe it or not, I pulled out a few refactors in advance to trim down
the code and complexity.

The highlights are:

- All CLI arguments are now `Option`, so that we can detect whether they
were provided (i.e., we can't let Clap fill in the defaults).
- We now have a `*Settings` struct for each command, which merges the
CLI and workspace options (e.g., `PipCompileSettings`).

I've only implemented `PipCompileSettings` for now. If approved, I'll
implement the others prior to merging, but it's very mechanical and I
both didn't want to do the conversion prior to receiving feedback _and_
realized it would make the PR harder to review.
  • Loading branch information
charliermarsh authored Apr 17, 2024
1 parent dcc2c68 commit e5d4ea5
Show file tree
Hide file tree
Showing 7 changed files with 1,160 additions and 245 deletions.
38 changes: 27 additions & 11 deletions crates/uv-cache/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,39 @@ pub struct CacheArgs {
alias = "no-cache-dir",
env = "UV_NO_CACHE"
)]
no_cache: bool,
pub no_cache: Option<bool>,

/// Path to the cache directory.
///
/// Defaults to `$HOME/Library/Caches/uv` on macOS, `$XDG_CACHE_HOME/uv` or `$HOME/.cache/uv` on
/// Linux, and `$HOME/.cache/<project_path> {FOLDERID_LocalAppData}/<project_path>/cache/uv`
/// on Windows.
#[arg(global = true, long, env = "UV_CACHE_DIR")]
cache_dir: Option<PathBuf>,
pub cache_dir: Option<PathBuf>,
}

impl Cache {
/// Prefer, in order:
/// 1. A temporary cache directory, if the user requested `--no-cache`.
/// 2. The specific cache directory specified by the user via `--cache-dir` or `UV_CACHE_DIR`.
/// 3. The system-appropriate cache directory.
/// 4. A `.uv_cache` directory in the current working directory.
///
/// Returns an absolute cache dir.
pub fn from_settings(
no_cache: Option<bool>,
cache_dir: Option<PathBuf>,
) -> Result<Self, io::Error> {
if no_cache.unwrap_or(false) {
Cache::temp()
} else if let Some(cache_dir) = cache_dir {
Cache::from_path(cache_dir)
} else if let Some(project_dirs) = ProjectDirs::from("", "", "uv") {
Cache::from_path(project_dirs.cache_dir())
} else {
Cache::from_path(".uv_cache")
}
}
}

impl TryFrom<CacheArgs> for Cache {
Expand All @@ -38,14 +62,6 @@ impl TryFrom<CacheArgs> for Cache {
///
/// Returns an absolute cache dir.
fn try_from(value: CacheArgs) -> Result<Self, Self::Error> {
if value.no_cache {
Self::temp()
} else if let Some(cache_dir) = value.cache_dir {
Self::from_path(cache_dir)
} else if let Some(project_dirs) = ProjectDirs::from("", "", "uv") {
Self::from_path(project_dirs.cache_dir())
} else {
Self::from_path(".uv_cache")
}
Cache::from_settings(value.no_cache, value.cache_dir)
}
}
22 changes: 15 additions & 7 deletions crates/uv-workspace/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::Deserialize;
use distribution_types::{FlatIndexLocation, IndexUrl};
use install_wheel_rs::linker::LinkMode;
use uv_configuration::{ConfigSettings, IndexStrategy, KeyringProviderType, PackageNameSpecifier};
use uv_normalize::PackageName;
use uv_normalize::{ExtraName, PackageName};
use uv_resolver::{AnnotationStyle, ExcludeNewer, PreReleaseMode, ResolutionMode};
use uv_toolchain::PythonVersion;

Expand All @@ -28,10 +28,8 @@ pub(crate) struct Tools {
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct Options {
pub quiet: Option<bool>,
pub verbose: Option<bool>,
pub native_tls: Option<bool>,
pub no_cache: bool,
pub no_cache: Option<bool>,
pub cache_dir: Option<PathBuf>,
pub pip: Option<PipOptions>,
}
Expand All @@ -41,10 +39,12 @@ pub struct Options {
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct PipOptions {
pub python: Option<String>,
pub system: Option<bool>,
pub break_system_packages: Option<bool>,
pub offline: Option<bool>,
pub index_url: Option<IndexUrl>,
pub extra_index_url: Option<IndexUrl>,
pub extra_index_url: Option<Vec<IndexUrl>>,
pub no_index: Option<bool>,
pub find_links: Option<Vec<FlatIndexLocation>>,
pub index_strategy: Option<IndexStrategy>,
Expand All @@ -53,21 +53,29 @@ pub struct PipOptions {
pub no_binary: Option<Vec<PackageNameSpecifier>>,
pub only_binary: Option<Vec<PackageNameSpecifier>>,
pub no_build_isolation: Option<bool>,
pub strict: Option<bool>,
pub extra: Option<Vec<ExtraName>>,
pub all_extras: Option<bool>,
pub no_deps: Option<bool>,
pub resolution: Option<ResolutionMode>,
pub prerelease: Option<PreReleaseMode>,
pub output_file: Option<PathBuf>,
pub no_strip_extras: Option<bool>,
pub no_annotate: Option<bool>,
pub no_header: Option<bool>,
pub custom_compile_command: Option<String>,
pub generate_hashes: Option<bool>,
pub legacy_setup_py: Option<bool>,
pub config_setting: Option<ConfigSettings>,
pub config_settings: Option<ConfigSettings>,
pub python_version: Option<PythonVersion>,
pub exclude_newer: Option<ExcludeNewer>,
pub no_emit_package: Option<Vec<PackageName>>,
pub emit_index_url: Option<bool>,
pub emit_find_links: Option<bool>,
pub emit_marker_expression: Option<bool>,
pub emit_index_annotation: Option<bool>,
pub annotation_style: Option<AnnotationStyle>,
pub require_hashes: Option<bool>,
pub link_mode: Option<LinkMode>,
pub compile_bytecode: Option<bool>,
pub require_hashes: Option<bool>,
}
4 changes: 2 additions & 2 deletions crates/uv-workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::{Options, PyProjectToml};
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct Workspace {
options: Options,
root: PathBuf,
pub options: Options,
pub root: PathBuf,
}

impl Workspace {
Expand Down
Loading

0 comments on commit e5d4ea5

Please sign in to comment.