From 365d51c99ca4c39c285a8c4ff26e1b080a7281ad Mon Sep 17 00:00:00 2001 From: Martijn Date: Mon, 14 Oct 2024 11:32:58 +0200 Subject: [PATCH] Add --workspace and friends to cargo=msrv cli --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 1 + src/cli/shared_opts.rs | 3 +++ src/context.rs | 27 ++++++++++++++++++++++++++- 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index c1e884a5..a295f445 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -640,6 +640,7 @@ dependencies = [ "camino", "cargo_metadata", "clap", + "clap-cargo", "dirs", "indicatif", "once_cell", @@ -710,6 +711,17 @@ dependencies = [ "clap_derive", ] +[[package]] +name = "clap-cargo" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b2ea69cefa96b848b73ad516ad1d59a195cdf9263087d977f648a818c8b43e" +dependencies = [ + "anstyle", + "cargo_metadata", + "clap", +] + [[package]] name = "clap_builder" version = "4.5.20" diff --git a/Cargo.toml b/Cargo.toml index c23a75fb..fcb25dbd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ bisector = "0.4.0" # bisection with a custom comparator camino = "1.1" # utf-8 paths cargo_metadata = "0.18.1" # resolving Cargo manifest metadata (consider `guppy`!) clap = { version = "4.5.20", features = ["derive"] } # parse CLI arguments +clap-cargo = { version = "0.14.1", features = ["cargo_metadata"] } dirs = "5.0.1" # common directories indicatif = "0.17.8" # UI once_cell = "1.20.2" # lazy data structures and thunking diff --git a/src/cli/shared_opts.rs b/src/cli/shared_opts.rs index 29fdc340..dab1e840 100644 --- a/src/cli/shared_opts.rs +++ b/src/cli/shared_opts.rs @@ -15,6 +15,9 @@ pub struct SharedOpts { #[arg(long, value_name = "Cargo Manifest", global = true, value_hint = ValueHint::FilePath)] pub manifest_path: Option, + #[command(flatten)] + pub workspace: clap_cargo::Workspace, + #[command(flatten)] pub user_output_opts: UserOutputOpts, diff --git a/src/context.rs b/src/context.rs index 377acbb9..39450857 100644 --- a/src/context.rs +++ b/src/context.rs @@ -240,6 +240,9 @@ pub struct EnvironmentContext { /// Does not include a manifest file like Cargo.toml, so it's easy to append /// a file path like `Cargo.toml` or `Cargo.lock`. pub crate_path: Utf8PathBuf, + + /// Resolved workspace + pub workspace_packages: WorkspacePackages, } impl<'shared_opts> TryFrom<&'shared_opts SharedOpts> for EnvironmentContext { @@ -275,7 +278,16 @@ impl<'shared_opts> TryFrom<&'shared_opts SharedOpts> for EnvironmentContext { CargoMSRVError::Path(PathError::InvalidUtf8(InvalidUtf8Error::from(err))) })?; - Ok(Self { crate_path }) + let metadata = cargo_metadata::MetadataCommand::new() + .manifest_path(&crate_path) + .exec()?; + let workspace = opts.workspace.partition_packages(&metadata); + let workspace_packages = WorkspacePackages::from_iter(workspace.0.into_iter().cloned()); + + Ok(Self { + crate_path, + workspace_packages, + }) } } @@ -298,6 +310,19 @@ impl EnvironmentContext { // --- +#[derive(Clone, Debug)] +pub struct WorkspacePackages { + _selected: Vec, +} + +impl WorkspacePackages { + pub fn from_iter(selected: impl IntoIterator) -> Self { + Self { + _selected: selected.into_iter().collect(), + } + } +} + #[derive(Clone, Copy, Debug, Default, PartialEq, ValueEnum)] pub enum OutputFormat { /// Progress bar rendered to stderr