Skip to content

Commit

Permalink
Add --group and --only-group to uv run
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Oct 16, 2024
1 parent ec9bfa6 commit 2e4f484
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 21 deletions.
14 changes: 14 additions & 0 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2611,6 +2611,20 @@ pub struct RunArgs {
#[arg(long, overrides_with("dev"))]
pub no_dev: bool,

/// Include dependencies from the specified local dependency group.
///
/// May be provided multiple times.
#[arg(long, conflicts_with("only_group"))]
pub group: Vec<GroupName>,

/// Only include dependencies from the specified local dependency group.
///
/// May be provided multiple times.
///
/// The project itself will also be omitted.
#[arg(long, conflicts_with("group"))]
pub only_group: Vec<GroupName>,

/// Run a Python module.
///
/// Equivalent to `python -m <module>`.
Expand Down
25 changes: 25 additions & 0 deletions crates/uv-configuration/src/dev.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use either::Either;
use uv_normalize::{GroupName, DEV_DEPENDENCIES};

Expand Down Expand Up @@ -50,6 +52,29 @@ impl DevSpecification {
pub fn prod(&self) -> bool {
matches!(self, Self::Exclude | Self::Include(_))
}

/// Returns the name of the flag that was used to request dev dependencies, if any.
pub fn as_flag(&self) -> Option<Cow<'_, str>> {
match self {
Self::Exclude => None,
Self::Include(groups) => match groups.as_slice() {
[] => None,
[group] if *group == *DEV_DEPENDENCIES => {
Some(Cow::Borrowed("`--group dev` or `--dev`"))
}
[group] => Some(Cow::Owned(format!("`--group {group}`"))),
[..] => Some(Cow::Borrowed("`--group`")),
},
Self::Only(groups) => match groups.as_slice() {
[] => None,
[group] if *group == *DEV_DEPENDENCIES => {
Some(Cow::Borrowed("`--only-group dev` or `--only-dev`"))
}
[group] => Some(Cow::Owned(format!("`--only-group {group}`"))),
[..] => Some(Cow::Borrowed("`--only-group`")),
},
}
}
}

impl From<DevMode> for DevSpecification {
Expand Down
29 changes: 10 additions & 19 deletions crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use uv_cache::Cache;
use uv_cli::ExternalCommand;
use uv_client::{BaseClientBuilder, Connectivity};
use uv_configuration::{
Concurrency, DevMode, DevSpecification, EditableMode, ExtrasSpecification, InstallOptions,
LowerBound, SourceStrategy,
Concurrency, DevSpecification, EditableMode, ExtrasSpecification, InstallOptions, LowerBound,
SourceStrategy,
};
use uv_distribution::LoweredRequirement;
use uv_fs::which::is_executable;
Expand Down Expand Up @@ -68,7 +68,7 @@ pub(crate) async fn run(
no_project: bool,
no_config: bool,
extras: ExtrasSpecification,
dev: DevMode,
dev: DevSpecification,
editable: EditableMode,
python: Option<String>,
settings: ResolverInstallerSettings,
Expand Down Expand Up @@ -336,11 +336,8 @@ pub(crate) async fn run(
if !extras.is_empty() {
warn_user!("Extras are not supported for Python scripts with inline metadata");
}
if matches!(dev, DevMode::Exclude) {
warn_user!("`--no-dev` is not supported for Python scripts with inline metadata");
}
if matches!(dev, DevMode::Only) {
warn_user!("`--only-dev` is not supported for Python scripts with inline metadata");
if let Some(flag) = dev.as_flag() {
warn_user!("{flag} is not supported for Python scripts with inline metadata");
}
if package.is_some() {
warn_user!(
Expand Down Expand Up @@ -413,11 +410,8 @@ pub(crate) async fn run(
if !extras.is_empty() {
warn_user!("Extras have no effect when used alongside `--no-project`");
}
if matches!(dev, DevMode::Exclude) {
warn_user!("`--no-dev` has no effect when used alongside `--no-project`");
}
if matches!(dev, DevMode::Only) {
warn_user!("`--only-dev` has no effect when used alongside `--no-project`");
if let Some(flag) = dev.as_flag() {
warn_user!("{flag} has no effect when used alongside `--no-project`");
}
if locked {
warn_user!("`--locked` has no effect when used alongside `--no-project`");
Expand All @@ -433,11 +427,8 @@ pub(crate) async fn run(
if !extras.is_empty() {
warn_user!("Extras have no effect when used outside of a project");
}
if matches!(dev, DevMode::Exclude) {
warn_user!("`--no-dev` has no effect when used outside of a project");
}
if matches!(dev, DevMode::Only) {
warn_user!("`--only-dev` has no effect when used outside of a project");
if let Some(flag) = dev.as_flag() {
warn_user!("{flag} has no when used outside of a project");
}
if locked {
warn_user!("`--locked` has no effect when used outside of a project");
Expand Down Expand Up @@ -590,7 +581,7 @@ pub(crate) async fn run(
&venv,
result.lock(),
&extras,
&DevSpecification::from(dev),
&dev,
editable,
install_options,
Modifications::Sufficient,
Expand Down
6 changes: 4 additions & 2 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ pub(crate) struct RunSettings {
pub(crate) locked: bool,
pub(crate) frozen: bool,
pub(crate) extras: ExtrasSpecification,
pub(crate) dev: DevMode,
pub(crate) dev: DevSpecification,
pub(crate) editable: EditableMode,
pub(crate) with: Vec<String>,
pub(crate) with_editable: Vec<String>,
Expand All @@ -252,6 +252,8 @@ impl RunSettings {
no_all_extras,
dev,
no_dev,
group,
only_group,
module: _,
only_dev,
no_editable,
Expand Down Expand Up @@ -280,7 +282,7 @@ impl RunSettings {
flag(all_extras, no_all_extras).unwrap_or_default(),
extra.unwrap_or_default(),
),
dev: DevMode::from_args(dev, no_dev, only_dev),
dev: DevSpecification::from_args(dev, no_dev, only_dev, group, only_group),
editable: EditableMode::from_args(no_editable),
with,
with_editable,
Expand Down
Loading

0 comments on commit 2e4f484

Please sign in to comment.