Skip to content

Commit

Permalink
Add support for default-groups
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Oct 22, 2024
1 parent 0d12174 commit 26c9454
Show file tree
Hide file tree
Showing 17 changed files with 368 additions and 114 deletions.
138 changes: 99 additions & 39 deletions crates/uv-configuration/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ impl DevMode {
matches!(self, Self::Exclude | Self::Include)
}

/// Returns `true` if the specification only includes development dependencies.
pub fn only(&self) -> bool {
matches!(self, Self::Only)
}

/// Returns the flag that was used to request development dependencies.
pub fn as_flag(&self) -> &'static str {
match self {
Expand All @@ -38,7 +43,7 @@ impl DevMode {
}
}

#[derive(Debug, Clone)]
#[derive(Default, Debug, Clone)]
pub struct DevGroupsSpecification {
/// Legacy option for `dependency-group.dev` and `tool.uv.dev-dependencies`.
///
Expand All @@ -48,15 +53,13 @@ pub struct DevGroupsSpecification {
/// The groups to include.
///
/// Requested via the `--group` and `--only-group` options.
groups: GroupsSpecification,
groups: Option<GroupsSpecification>,
}

#[derive(Debug, Clone)]
pub enum GroupsSpecification {
/// Include dependencies from the specified groups.
Include(Vec<GroupName>),
/// Do not include dependencies from groups.
Exclude,
/// Only include dependencies from the specified groups, exclude all other dependencies.
Only(Vec<GroupName>),
}
Expand All @@ -65,20 +68,23 @@ impl GroupsSpecification {
/// Returns an [`Iterator`] over the group names to include.
pub fn iter(&self) -> impl Iterator<Item = &GroupName> {
match self {
Self::Exclude => Either::Left(std::iter::empty()),
Self::Include(groups) | Self::Only(groups) => Either::Right(groups.iter()),
Self::Include(groups) | Self::Only(groups) => groups.iter(),
}
}

/// Returns `true` if the specification allows for production dependencies.
pub fn prod(&self) -> bool {
matches!(self, Self::Exclude | Self::Include(_))
matches!(self, Self::Include(_))
}

/// Returns `true` if the specification is limited to a select set of groups.
pub fn only(&self) -> bool {
matches!(self, Self::Only(_))
}

/// Returns the option that was used to request the groups, if any.
pub fn as_flag(&self) -> Option<Cow<'_, str>> {
match self {
Self::Exclude => None,
Self::Include(groups) => match groups.as_slice() {
[] => None,
[group] => Some(Cow::Owned(format!("--group {group}"))),
Expand All @@ -96,9 +102,13 @@ impl GroupsSpecification {
impl DevGroupsSpecification {
/// Returns an [`Iterator`] over the group names to include.
pub fn iter(&self) -> impl Iterator<Item = &GroupName> {
match self.dev {
None => Either::Left(self.groups.iter()),
Some(ref dev_mode) => Either::Right(self.groups.iter().chain(dev_mode.iter())),
match (self.dev.as_ref(), self.groups.as_ref()) {
(None, None) => Either::Left(Either::Left(std::iter::empty())),
(Some(dev), None) => Either::Left(Either::Right(dev.iter())),
(None, Some(groups)) => Either::Right(Either::Left(groups.iter())),
(Some(dev), Some(groups)) => {
Either::Right(Either::Right(dev.iter().chain(groups.iter())))
}
}
}

Expand All @@ -110,7 +120,7 @@ impl DevGroupsSpecification {
group: Vec<GroupName>,
only_group: Vec<GroupName>,
) -> Self {
let dev_mode = if only_dev {
let dev = if only_dev {
Some(DevMode::Only)
} else if no_dev {
Some(DevMode::Exclude)
Expand All @@ -121,70 +131,120 @@ impl DevGroupsSpecification {
};

let groups = if !group.is_empty() {
if matches!(dev_mode, Some(DevMode::Only)) {
if matches!(dev, Some(DevMode::Only)) {
unreachable!("cannot specify both `--only-dev` and `--group`")
};
GroupsSpecification::Include(group)
Some(GroupsSpecification::Include(group))
} else if !only_group.is_empty() {
if matches!(dev_mode, Some(DevMode::Include)) {
if matches!(dev, Some(DevMode::Include)) {
unreachable!("cannot specify both `--dev` and `--only-group`")
};
GroupsSpecification::Only(only_group)
Some(GroupsSpecification::Only(only_group))
} else {
GroupsSpecification::Exclude
None
};

Self {
dev: dev_mode,
groups,
}
Self { dev, groups }
}

/// Return a new [`DevGroupsSpecification`] with development dependencies included by default.
///
/// This is appropriate in projects, where the `dev` group is synced by default.
#[must_use]
pub fn with_default_dev(self) -> Self {
match self.dev {
Some(_) => self,
None => match self.groups {
// Only include the default `dev` group if `--only-group` wasn't used
GroupsSpecification::Only(_) => self,
GroupsSpecification::Exclude | GroupsSpecification::Include(_) => Self {
dev: Some(DevMode::Include),
..self
},
},
pub fn with_defaults(self, defaults: Vec<GroupName>) -> DevGroupsManifest {
DevGroupsManifest {
spec: self,
defaults,
}
}

/// Returns `true` if the specification allows for production dependencies.
pub fn prod(&self) -> bool {
(self.dev.is_none() || self.dev.as_ref().is_some_and(DevMode::prod)) && self.groups.prod()
self.dev.as_ref().map_or(true, DevMode::prod)
&& self.groups.as_ref().map_or(true, GroupsSpecification::prod)
}

/// Returns the flag that was used to request development dependencies.
/// Returns `true` if the specification is limited to a select set of groups.
pub fn only(&self) -> bool {
self.dev.as_ref().is_some_and(DevMode::only)
|| self.groups.as_ref().is_some_and(GroupsSpecification::only)
}

/// Returns the flag that was used to request development dependencies, if specified.
pub fn dev_mode(&self) -> Option<&DevMode> {
self.dev.as_ref()
}

/// Returns the list of groups to include.
pub fn groups(&self) -> &GroupsSpecification {
&self.groups
/// Returns the list of groups to include, if specified.
pub fn groups(&self) -> Option<&GroupsSpecification> {
self.groups.as_ref()
}
}

impl From<DevMode> for DevGroupsSpecification {
fn from(dev: DevMode) -> Self {
Self {
dev: Some(dev),
groups: GroupsSpecification::Exclude,
groups: None,
}
}
}

impl From<GroupsSpecification> for DevGroupsSpecification {
fn from(groups: GroupsSpecification) -> Self {
Self { dev: None, groups }
Self {
dev: None,
groups: Some(groups),
}
}
}

/// The manifest of `dependency-groups` to include, taking into account the user-provided
/// [`DevGroupsSpecification`] and the project-specific default groups.
#[derive(Debug, Clone)]
pub struct DevGroupsManifest {
/// The specification for the development dependencies.
pub(crate) spec: DevGroupsSpecification,
/// The default groups to include.
pub(crate) defaults: Vec<GroupName>,
}

impl DevGroupsManifest {
/// Returns a new [`DevGroupsManifest`] with the given default groups.
pub fn from_defaults(defaults: Vec<GroupName>) -> Self {
Self {
spec: DevGroupsSpecification::default(),
defaults,
}
}

/// Returns a new [`DevGroupsManifest`] with the given specification.
pub fn from_spec(spec: DevGroupsSpecification) -> Self {
Self {
spec,
defaults: Vec::new(),
}
}

/// Returns an [`Iterator`] over the group names to include.
pub fn iter(&self) -> impl Iterator<Item = &GroupName> {
if self.spec.only() {
Either::Left(self.spec.iter())
} else {
Either::Right(
self.spec
.iter()
.chain(self.defaults.iter().filter(|default| {
// If `--no-dev` was provided, exclude the `dev` group from the list of defaults.
!matches!(self.spec.dev_mode(), Some(DevMode::Exclude))
|| *default != &*DEV_DEPENDENCIES
})),
)
}
}

/// Returns `true` if the specification allows for production dependencies.
pub fn prod(&self) -> bool {
self.spec.prod()
}
}
4 changes: 2 additions & 2 deletions crates/uv-resolver/src/lock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use toml_edit::{value, Array, ArrayOfTables, InlineTable, Item, Table, Value};
use url::Url;

use uv_cache_key::RepositoryUrl;
use uv_configuration::{BuildOptions, DevGroupsSpecification, ExtrasSpecification, InstallOptions};
use uv_configuration::{BuildOptions, DevGroupsManifest, ExtrasSpecification, InstallOptions};
use uv_distribution::DistributionDatabase;
use uv_distribution_filename::{DistExtension, ExtensionError, SourceDistExtension, WheelFilename};
use uv_distribution_types::{
Expand Down Expand Up @@ -575,7 +575,7 @@ impl Lock {
marker_env: &ResolverMarkerEnvironment,
tags: &Tags,
extras: &ExtrasSpecification,
dev: &DevGroupsSpecification,
dev: &DevGroupsManifest,
build_options: &BuildOptions,
install_options: &InstallOptions,
) -> Result<Resolution, LockError> {
Expand Down
4 changes: 2 additions & 2 deletions crates/uv-resolver/src/lock/requirements_txt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use petgraph::{Directed, Graph};
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
use url::Url;

use uv_configuration::{DevGroupsSpecification, EditableMode, ExtrasSpecification, InstallOptions};
use uv_configuration::{DevGroupsManifest, EditableMode, ExtrasSpecification, InstallOptions};
use uv_distribution_filename::{DistExtension, SourceDistExtension};
use uv_fs::Simplified;
use uv_git::GitReference;
Expand Down Expand Up @@ -43,7 +43,7 @@ impl<'lock> RequirementsTxtExport<'lock> {
lock: &'lock Lock,
root_name: &PackageName,
extras: &ExtrasSpecification,
dev: &DevGroupsSpecification,
dev: &DevGroupsManifest,
editable: EditableMode,
hashes: bool,
install_options: &'lock InstallOptions,
Expand Down
6 changes: 3 additions & 3 deletions crates/uv-resolver/src/lock/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::BTreeSet;
use itertools::Itertools;
use rustc_hash::{FxHashMap, FxHashSet};

use uv_configuration::DevGroupsSpecification;
use uv_configuration::DevGroupsManifest;
use uv_normalize::{ExtraName, GroupName, PackageName};
use uv_pypi_types::ResolverMarkerEnvironment;

Expand All @@ -26,7 +26,7 @@ pub struct TreeDisplay<'env> {
/// Maximum display depth of the dependency tree.
depth: usize,
/// Whether to include development dependencies in the display.
dev: DevGroupsSpecification,
dev: DevGroupsManifest,
/// Prune the given packages from the display of the dependency tree.
prune: Vec<PackageName>,
/// Display only the specified packages.
Expand All @@ -43,7 +43,7 @@ impl<'env> TreeDisplay<'env> {
depth: usize,
prune: Vec<PackageName>,
packages: Vec<PackageName>,
dev: DevGroupsSpecification,
dev: DevGroupsManifest,
no_dedupe: bool,
invert: bool,
) -> Self {
Expand Down
9 changes: 6 additions & 3 deletions crates/uv-settings/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1563,11 +1563,13 @@ pub struct OptionsWire {
#[allow(dead_code)]
sources: Option<serde::de::IgnoredAny>,
#[allow(dead_code)]
dev_dependencies: Option<serde::de::IgnoredAny>,
#[allow(dead_code)]
managed: Option<serde::de::IgnoredAny>,
#[allow(dead_code)]
r#package: Option<serde::de::IgnoredAny>,
#[allow(dead_code)]
default_groups: Option<serde::de::IgnoredAny>,
#[allow(dead_code)]
dev_dependencies: Option<serde::de::IgnoredAny>,
}

impl From<OptionsWire> for Options {
Expand Down Expand Up @@ -1618,9 +1620,10 @@ impl From<OptionsWire> for Options {
trusted_publishing,
workspace: _,
sources: _,
dev_dependencies: _,
managed: _,
package: _,
default_groups: _,
dev_dependencies: _,
} = value;

Self {
Expand Down
Loading

0 comments on commit 26c9454

Please sign in to comment.