From aa5dfc76bfdf3707bd86423bb9805fec21d441c1 Mon Sep 17 00:00:00 2001 From: Artur Sinila Date: Mon, 15 Nov 2021 03:39:29 +0200 Subject: [PATCH] Port to `clap-cargo` --- Cargo.lock | 11 ++++++++ Cargo.toml | 1 + src/bin/add/args.rs | 53 +++++++++++++++++-------------------- src/bin/add/main.rs | 6 ++--- src/bin/rm/main.rs | 24 +++++++---------- src/bin/set-version/args.rs | 35 +++--------------------- src/bin/set-version/main.rs | 18 ++++++++----- src/bin/upgrade/main.rs | 50 +++++++++++----------------------- 8 files changed, 78 insertions(+), 120 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1e17e95437..ff82303bcc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -156,6 +156,7 @@ dependencies = [ "assert_fs", "atty", "cargo_metadata", + "clap-cargo", "crates-index", "dirs-next", "dunce", @@ -238,6 +239,16 @@ dependencies = [ "vec_map", ] +[[package]] +name = "clap-cargo" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c577d31f9cb28e3d7a1a8ed7df1e45e58c5446f80e446fcdebe0a18261d68135" +dependencies = [ + "doc-comment", + "structopt", +] + [[package]] name = "combine" version = "4.6.1" diff --git a/Cargo.toml b/Cargo.toml index 8da759a86f..d050fc97c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,6 +69,7 @@ toml_edit = { version = "0.4.0", features = ["easy"] } url = "2.1.1" ureq = { version = "1.5.1", default-features = false, features = ["tls", "json", "socks"] } pathdiff = "0.2" +clap-cargo = "0.6.1" [dependencies.semver] features = ["serde"] diff --git a/src/bin/add/args.rs b/src/bin/add/args.rs index c4c546c265..15006b42c1 100644 --- a/src/bin/add/args.rs +++ b/src/bin/add/args.rs @@ -35,7 +35,7 @@ pub struct Args { pub crates: Vec, /// Rename a dependency in Cargo.toml, - /// https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml. + /// . /// Only works when specifying a single dependency. #[structopt(long = "rename", short = "r")] pub rename: Option, @@ -85,17 +85,11 @@ pub struct Args { pub optional: bool, /// Path to the manifest to add a dependency to. - #[structopt(long = "manifest-path", value_name = "path", conflicts_with = "pkgid")] - pub manifest_path: Option, + #[structopt(flatten)] + pub manifest: clap_cargo::Manifest, - /// Package id of the crate to add this dependency to. - #[structopt( - long = "package", - short = "p", - value_name = "pkgid", - conflicts_with = "path" - )] - pub pkgid: Option, + #[structopt(flatten)] + pub workspace: clap_cargo::Workspace, /// Choose method of semantic version upgrade. Must be one of "none" (exact version, `=` /// modifier), "patch" (`~` modifier), "minor" (`^` modifier), "all" (`>=`), or "default" (no @@ -117,14 +111,9 @@ pub struct Args { #[structopt(long = "allow-prerelease")] pub allow_prerelease: bool, - /// Space-separated list of features to add. For an alternative approach to - /// enabling features, consider installing the `cargo-feature` utility. - #[structopt(long = "features", number_of_values = 1)] - pub features: Option>, - - /// Set `default-features = false` for the added dependency. - #[structopt(long = "no-default-features")] - pub no_default_features: bool, + /// For an alternative approach to enabling features, consider installing `cargo-feature`. + #[structopt(flatten)] + pub features: clap_cargo::Features, /// Do not print any output in case of success. #[structopt(long = "quiet", short = "q")] @@ -229,7 +218,10 @@ impl Args { dependency = dependency.set_version(parse_version_req(version)?); } let registry_url = if let Some(registry) = &self.registry { - Some(registry_url(&find(&self.manifest_path)?, Some(registry))?) + Some(registry_url( + &find(&self.manifest.manifest_path)?, + Some(registry), + )?) } else { None }; @@ -262,7 +254,7 @@ impl Args { dependency = get_latest_dependency( crate_name.name(), self.allow_prerelease, - &find(&self.manifest_path)?, + &find(&self.manifest.manifest_path)?, ®istry_url, )?; let v = format!( @@ -287,7 +279,7 @@ impl Args { /// Build dependencies from arguments pub fn parse_dependencies(&self) -> Result> { - let workspace_members = workspace_members(self.manifest_path.as_deref())?; + let workspace_members = workspace_members(self.manifest.manifest_path.as_deref())?; if self.crates.len() > 1 && (self.git.is_some() || self.path.is_some() || self.vers.is_some()) @@ -299,7 +291,7 @@ impl Args { return Err(ErrorKind::MultipleCratesWithRename.into()); } - if self.crates.len() > 1 && self.features.is_some() { + if self.crates.len() > 1 && !self.features.features.is_empty() { return Err(ErrorKind::MultipleCratesWithFeatures.into()); } @@ -310,8 +302,12 @@ impl Args { .map(|x| { let mut x = x .set_optional(self.optional) - .set_features(self.features.clone()) - .set_default_features(!self.no_default_features); + .set_features(if self.features.features.is_empty() { + None + } else { + Some(self.features.features.clone()) + }) + .set_default_features(!self.features.no_default_features); if let Some(ref rename) = self.rename { x = x.set_rename(rename); } @@ -347,16 +343,15 @@ impl Default for Args { path: None, target: None, optional: false, - manifest_path: None, - pkgid: None, upgrade: "minor".to_string(), allow_prerelease: false, - features: None, - no_default_features: false, quiet: false, offline: true, sort: false, registry: None, + manifest: Default::default(), + workspace: Default::default(), + features: Default::default(), } } } diff --git a/src/bin/add/main.rs b/src/bin/add/main.rs index a1e8e084e4..2043bd13b2 100644 --- a/src/bin/add/main.rs +++ b/src/bin/add/main.rs @@ -120,11 +120,11 @@ fn is_sorted(mut it: impl Iterator) -> bool { } fn handle_add(args: &Args) -> Result<()> { - let manifest_path = if let Some(ref pkgid) = args.pkgid { - let pkg = manifest_from_pkgid(args.manifest_path.as_deref(), pkgid)?; + let manifest_path = if let Some(pkgid) = args.workspace.package.get(0) { + let pkg = manifest_from_pkgid(args.manifest.manifest_path.as_deref(), pkgid)?; Cow::Owned(Some(pkg.manifest_path.into_std_path_buf())) } else { - Cow::Borrowed(&args.manifest_path) + Cow::Borrowed(&args.manifest.manifest_path) }; let mut manifest = LocalManifest::find(&manifest_path)?; diff --git a/src/bin/rm/main.rs b/src/bin/rm/main.rs index c10832cc7b..684ed5f21b 100644 --- a/src/bin/rm/main.rs +++ b/src/bin/rm/main.rs @@ -17,7 +17,7 @@ extern crate error_chain; use cargo_edit::{manifest_from_pkgid, LocalManifest}; use std::borrow::Cow; use std::io::Write; -use std::path::PathBuf; + use std::process; use structopt::{clap::AppSettings, StructOpt}; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; @@ -58,17 +58,11 @@ struct Args { build: bool, /// Path to the manifest to remove a dependency from. - #[structopt(long = "manifest-path", value_name = "path", conflicts_with = "pkgid")] - manifest_path: Option, - - /// Package id of the crate to remove this dependency from. - #[structopt( - long = "package", - short = "p", - value_name = "pkgid", - conflicts_with = "path" - )] - pkgid: Option, + #[structopt(flatten)] + manifest: clap_cargo::Manifest, + + #[structopt(flatten)] + workspace: clap_cargo::Workspace, /// Do not print any output in case of success. #[structopt(long = "quiet", short = "q")] @@ -103,11 +97,11 @@ fn print_msg(name: &str, section: &str) -> Result<()> { } fn handle_rm(args: &Args) -> Result<()> { - let manifest_path = if let Some(ref pkgid) = args.pkgid { - let pkg = manifest_from_pkgid(args.manifest_path.as_deref(), pkgid)?; + let manifest_path = if !args.workspace.package.is_empty() { + let pkg = manifest_from_pkgid(args.manifest.manifest_path.as_deref(), &args.workspace.package[0])?; Cow::Owned(Some(pkg.manifest_path.into_std_path_buf())) } else { - Cow::Borrowed(&args.manifest_path) + Cow::Borrowed(&args.manifest.manifest_path) }; let mut manifest = LocalManifest::find(&manifest_path)?; let deps = &args.crates; diff --git a/src/bin/set-version/args.rs b/src/bin/set-version/args.rs index 57e8fc5fa0..2c12a7fb80 100644 --- a/src/bin/set-version/args.rs +++ b/src/bin/set-version/args.rs @@ -1,5 +1,3 @@ -use std::path::PathBuf; - use structopt::{clap::AppSettings, StructOpt}; #[derive(Debug, StructOpt)] @@ -27,38 +25,13 @@ pub(crate) struct Args { pub metadata: Option, /// Path to the manifest to upgrade - #[structopt(long = "manifest-path", value_name = "path", conflicts_with = "pkgid")] - pub(crate) manifest_path: Option, - - /// Package id of the crate to change the version of. - #[structopt( - long = "package", - short = "p", - value_name = "pkgid", - conflicts_with = "path", - conflicts_with = "all", - conflicts_with = "workspace" - )] - pub(crate) pkgid: Option, + #[structopt(flatten)] + pub(crate) manifest: clap_cargo::Manifest, - /// Modify all packages in the workspace. - #[structopt( - long = "all", - help = "[deprecated in favor of `--workspace`]", - conflicts_with = "workspace", - conflicts_with = "pkgid" - )] - pub(crate) all: bool, - - /// Modify all packages in the workspace. - #[structopt(long = "workspace", conflicts_with = "all", conflicts_with = "pkgid")] - pub(crate) workspace: bool, + #[structopt(flatten)] + pub(crate) workspace: clap_cargo::Workspace, /// Print changes to be made without making them. #[structopt(long = "dry-run")] pub(crate) dry_run: bool, - - /// Crates to exclude and not modify. - #[structopt(long)] - pub(crate) exclude: Vec, } diff --git a/src/bin/set-version/main.rs b/src/bin/set-version/main.rs index fb32ce074e..97cf0a1332 100644 --- a/src/bin/set-version/main.rs +++ b/src/bin/set-version/main.rs @@ -57,12 +57,16 @@ fn process(args: Args) -> Result<()> { target, bump, metadata, - manifest_path, - pkgid, - all, + manifest: clap_cargo::Manifest { manifest_path, .. }, + workspace: + clap_cargo::Workspace { + package: pkgid, + workspace, + all, + exclude, + .. + }, dry_run, - workspace, - exclude, } = args; let target = match (target, bump) { @@ -79,8 +83,8 @@ fn process(args: Args) -> Result<()> { let manifests = if all { Manifests::get_all(&manifest_path) - } else if let Some(ref pkgid) = pkgid { - Manifests::get_pkgid(manifest_path.as_deref(), pkgid) + } else if let Some(id) = pkgid.get(0) { + Manifests::get_pkgid(manifest_path.as_deref(), id) } else { Manifests::get_local_one(&manifest_path) }?; diff --git a/src/bin/upgrade/main.rs b/src/bin/upgrade/main.rs index ee8464f951..a1a04bce6d 100644 --- a/src/bin/upgrade/main.rs +++ b/src/bin/upgrade/main.rs @@ -70,33 +70,13 @@ struct Args { dependency: Vec, /// Path to the manifest to upgrade - #[structopt(long = "manifest-path", value_name = "path", conflicts_with = "pkgid")] - manifest_path: Option, - - /// Package id of the crate to add this dependency to. - #[structopt( - long = "package", - short = "p", - value_name = "pkgid", - conflicts_with = "path", - conflicts_with = "all", - conflicts_with = "workspace" - )] - pkgid: Option, - - /// Upgrade all packages in the workspace. - #[structopt( - long = "all", - help = "[deprecated in favor of `--workspace`]", - conflicts_with = "workspace", - conflicts_with = "pkgid" - )] - all: bool, + #[structopt(flatten)] + manifest: clap_cargo::Manifest, /// Upgrade all packages in the workspace. /// Implied by default when running in a directory with virtual manifest. - #[structopt(long = "workspace", conflicts_with = "all", conflicts_with = "pkgid")] - workspace: bool, + #[structopt(flatten)] + workspace: clap_cargo::Workspace, /// Include prerelease versions when fetching from crates.io (e.g. 0.6.0-alpha'). #[structopt(long = "allow-prerelease")] @@ -117,10 +97,6 @@ struct Args { /// Upgrade all packages to the version in the lockfile. #[structopt(long = "to-lockfile", conflicts_with = "dependency")] pub to_lockfile: bool, - - /// Crates to exclude and not upgrade. - #[structopt(long)] - exclude: Vec, } /// A collection of manifests. @@ -453,15 +429,19 @@ impl DesiredUpgrades { fn process(args: Args) -> Result<()> { let Args { dependency, - manifest_path, - pkgid, - all, + manifest: clap_cargo::Manifest { manifest_path, .. }, + workspace: + clap_cargo::Workspace { + package: pkgid, + workspace, + all, + exclude, + .. + }, allow_prerelease, dry_run, skip_compatible, to_lockfile, - workspace, - exclude, .. } = args; @@ -479,8 +459,8 @@ fn process(args: Args) -> Result<()> { let manifests = if all { Manifests::get_all(&manifest_path) - } else if let Some(ref pkgid) = pkgid { - Manifests::get_pkgid(manifest_path.as_deref(), pkgid) + } else if !pkgid.is_empty() { + Manifests::get_pkgid(manifest_path.as_deref(), &pkgid[0]) } else { Manifests::get_local_one(&manifest_path) }?;