Skip to content

Commit

Permalink
Add settings struct
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Apr 16, 2024
1 parent b139389 commit f3bc067
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 61 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)
}
}
14 changes: 9 additions & 5 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 @@ -44,7 +42,7 @@ pub struct PipOptions {
pub system: 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 @@ -62,8 +60,12 @@ pub struct PipOptions {
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct ResolverOptions {
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>,
Expand All @@ -75,6 +77,8 @@ pub struct ResolverOptions {
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>,
}

Expand Down
4 changes: 2 additions & 2 deletions crates/uv-workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,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
55 changes: 35 additions & 20 deletions crates/uv/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ pub(crate) struct PipCompileArgs {

/// Include optional dependencies in the given extra group name; may be provided more than once.
#[clap(long, conflicts_with = "all_extras", value_parser = extra_name_with_clap_error)]
pub(crate) extra: Vec<ExtraName>,
pub(crate) extra: Option<Vec<ExtraName>>,

/// Include all optional dependencies.
#[clap(long, conflicts_with = "extra")]
Expand All @@ -257,11 +257,16 @@ pub(crate) struct PipCompileArgs {
#[clap(long)]
pub(crate) no_deps: bool,

#[clap(long, value_enum, default_value_t = ResolutionMode::default(), env = "UV_RESOLUTION")]
pub(crate) resolution: ResolutionMode,
#[clap(long, value_enum, default_value = "highest", env = "UV_RESOLUTION")]
pub(crate) resolution: Option<ResolutionMode>,

#[clap(long, value_enum, default_value_t = PreReleaseMode::default(), env = "UV_PRERELEASE")]
pub(crate) prerelease: PreReleaseMode,
#[clap(
long,
value_enum,
default_value = "if-necessary-or-explicit",
env = "UV_PRERELEASE"
)]
pub(crate) prerelease: Option<PreReleaseMode>,

#[clap(long, hide = true)]
pub(crate) pre: bool,
Expand All @@ -287,8 +292,8 @@ pub(crate) struct PipCompileArgs {
pub(crate) no_header: bool,

/// Choose the style of the annotation comments, which indicate the source of each package.
#[clap(long, default_value_t=AnnotationStyle::Split, value_enum)]
pub(crate) annotation_style: AnnotationStyle,
#[clap(long, default_value = "split", value_enum)]
pub(crate) annotation_style: Option<AnnotationStyle>,

/// Change header comment to reflect custom command wrapping `uv pip compile`.
#[clap(long, env = "UV_CUSTOM_COMPILE_COMMAND")]
Expand All @@ -309,16 +314,16 @@ pub(crate) struct PipCompileArgs {

/// Refresh cached data for a specific package.
#[clap(long)]
pub(crate) refresh_package: Vec<PackageName>,
pub(crate) refresh_package: Option<Vec<PackageName>>,

/// The method to use when installing packages from the global cache.
///
/// This option is only used when creating build environments for source distributions.
///
/// Defaults to `clone` (also known as Copy-on-Write) on macOS, and `hardlink` on Linux and
/// Windows.
#[clap(long, value_enum, default_value_t = install_wheel_rs::linker::LinkMode::default())]
pub(crate) link_mode: install_wheel_rs::linker::LinkMode,
#[clap(long, value_enum)]
pub(crate) link_mode: Option<install_wheel_rs::linker::LinkMode>,

/// The URL of the Python package index (by default: <https://pypi.org/simple>).
///
Expand All @@ -341,7 +346,7 @@ pub(crate) struct PipCompileArgs {
/// as it finds it in an index. That is, it isn't possible for `uv` to
/// consider versions of the same package across multiple indexes.
#[clap(long, env = "UV_EXTRA_INDEX_URL", value_delimiter = ' ', value_parser = parse_index_url)]
pub(crate) extra_index_url: Vec<Maybe<IndexUrl>>,
pub(crate) extra_index_url: Option<Vec<Maybe<IndexUrl>>>,

/// Ignore the registry index (e.g., PyPI), instead relying on direct URL dependencies and those
/// discovered via `--find-links`.
Expand All @@ -354,15 +359,25 @@ pub(crate) struct PipCompileArgs {
/// limit resolutions to those present on that first index. This prevents "dependency confusion"
/// attacks, whereby an attack can upload a malicious package under the same name to a secondary
/// index.
#[clap(long, default_value_t, value_enum, env = "UV_INDEX_STRATEGY")]
pub(crate) index_strategy: IndexStrategy,
#[clap(
long,
default_value = "first-match",
value_enum,
env = "UV_INDEX_STRATEGY"
)]
pub(crate) index_strategy: Option<IndexStrategy>,

/// Attempt to use `keyring` for authentication for index urls
///
/// Due to not having Python imports, only `--keyring-provider subprocess` argument is currently
/// implemented `uv` will try to use `keyring` via CLI when this flag is used.
#[clap(long, default_value_t, value_enum, env = "UV_KEYRING_PROVIDER")]
pub(crate) keyring_provider: KeyringProviderType,
#[clap(
long,
default_value = "disabled",
value_enum,
env = "UV_KEYRING_PROVIDER"
)]
pub(crate) keyring_provider: Option<KeyringProviderType>,

/// Locations to search for candidate distributions, beyond those found in the indexes.
///
Expand All @@ -371,7 +386,7 @@ pub(crate) struct PipCompileArgs {
///
/// If a URL, the page must contain a flat list of links to package files.
#[clap(long, short)]
pub(crate) find_links: Vec<FlatIndexLocation>,
pub(crate) find_links: Option<Vec<FlatIndexLocation>>,

/// Allow package upgrades, ignoring pinned versions in the existing output file.
#[clap(long, short = 'U')]
Expand All @@ -380,7 +395,7 @@ pub(crate) struct PipCompileArgs {
/// Allow upgrades for a specific package, ignoring pinned versions in the existing output
/// file.
#[clap(long, short = 'P')]
pub(crate) upgrade_package: Vec<PackageName>,
pub(crate) upgrade_package: Option<Vec<PackageName>>,

/// Include distribution hashes in the output file.
#[clap(long)]
Expand Down Expand Up @@ -416,11 +431,11 @@ pub(crate) struct PipCompileArgs {
/// Multiple packages may be provided. Disable binaries for all packages with `:all:`.
/// Clear previously specified packages with `:none:`.
#[clap(long, conflicts_with = "no_build")]
pub(crate) only_binary: Vec<PackageNameSpecifier>,
pub(crate) only_binary: Option<Vec<PackageNameSpecifier>>,

/// Settings to pass to the PEP 517 build backend, specified as `KEY=VALUE` pairs.
#[clap(long, short = 'C', alias = "config-settings")]
pub(crate) config_setting: Vec<ConfigSettingEntry>,
pub(crate) config_setting: Option<Vec<ConfigSettingEntry>>,

/// The minimum Python version that should be supported by the compiled requirements (e.g.,
/// `3.7` or `3.7.9`).
Expand All @@ -440,7 +455,7 @@ pub(crate) struct PipCompileArgs {
/// Specify a package to omit from the output resolution. Its dependencies will still be
/// included in the resolution. Equivalent to pip-compile's `--unsafe-package` option.
#[clap(long, alias = "unsafe-package")]
pub(crate) no_emit_package: Vec<PackageName>,
pub(crate) no_emit_package: Option<Vec<PackageName>>,

/// Include `--index-url` and `--extra-index-url` entries in the generated output file.
#[clap(long)]
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ pub(crate) async fn pip_compile(
python_version: Option<PythonVersion>,
exclude_newer: Option<ExcludeNewer>,
annotation_style: AnnotationStyle,
link_mode: LinkMode,
native_tls: bool,
quiet: bool,
link_mode: LinkMode,
cache: Cache,
printer: Printer,
) -> Result<ExitStatus> {
Expand Down
Loading

0 comments on commit f3bc067

Please sign in to comment.