Skip to content

Commit

Permalink
Port to clap-cargo
Browse files Browse the repository at this point in the history
  • Loading branch information
Logarithmus committed Nov 15, 2021
1 parent 28ee3f7 commit aa5dfc7
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 120 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
53 changes: 24 additions & 29 deletions src/bin/add/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct Args {
pub crates: Vec<String>,

/// Rename a dependency in Cargo.toml,
/// https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml.
/// <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<String>,
Expand Down Expand Up @@ -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<PathBuf>,
#[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<String>,
#[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
Expand All @@ -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<Vec<String>>,

/// 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")]
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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)?,
&registry_url,
)?;
let v = format!(
Expand All @@ -287,7 +279,7 @@ impl Args {

/// Build dependencies from arguments
pub fn parse_dependencies(&self) -> Result<Vec<Dependency>> {
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())
Expand All @@ -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());
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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(),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/bin/add/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ fn is_sorted(mut it: impl Iterator<Item = impl PartialOrd>) -> 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)?;

Expand Down
24 changes: 9 additions & 15 deletions src/bin/rm/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<PathBuf>,

/// Package id of the crate to remove this dependency from.
#[structopt(
long = "package",
short = "p",
value_name = "pkgid",
conflicts_with = "path"
)]
pkgid: Option<String>,
#[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")]
Expand Down Expand Up @@ -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;
Expand Down
35 changes: 4 additions & 31 deletions src/bin/set-version/args.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::path::PathBuf;

use structopt::{clap::AppSettings, StructOpt};

#[derive(Debug, StructOpt)]
Expand Down Expand Up @@ -27,38 +25,13 @@ pub(crate) struct Args {
pub metadata: Option<String>,

/// Path to the manifest to upgrade
#[structopt(long = "manifest-path", value_name = "path", conflicts_with = "pkgid")]
pub(crate) manifest_path: Option<PathBuf>,

/// 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<String>,
#[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<String>,
}
18 changes: 11 additions & 7 deletions src/bin/set-version/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}?;
Expand Down
50 changes: 15 additions & 35 deletions src/bin/upgrade/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,13 @@ struct Args {
dependency: Vec<String>,

/// Path to the manifest to upgrade
#[structopt(long = "manifest-path", value_name = "path", conflicts_with = "pkgid")]
manifest_path: Option<PathBuf>,

/// 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<String>,

/// 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")]
Expand All @@ -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<String>,
}

/// A collection of manifests.
Expand Down Expand Up @@ -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;

Expand All @@ -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)
}?;
Expand Down

0 comments on commit aa5dfc7

Please sign in to comment.