-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Import cargo remove
into cargo
#11099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use cargo::core::dependency::DepKind; | ||
use cargo::ops::cargo_remove::remove; | ||
use cargo::ops::cargo_remove::RemoveOptions; | ||
use cargo::ops::resolve_ws; | ||
use cargo::util::command_prelude::*; | ||
use cargo::util::toml_mut::manifest::DepTable; | ||
|
||
pub fn cli() -> clap::Command { | ||
clap::Command::new("remove") | ||
// Subcommand aliases are handled in `aliased_command()`. | ||
// .alias("rm") | ||
.about("Remove dependencies from a Cargo.toml manifest file") | ||
.args([clap::Arg::new("dependencies") | ||
.action(clap::ArgAction::Append) | ||
.required(true) | ||
.num_args(1..) | ||
.value_name("DEP_ID") | ||
.help("Dependencies to be removed")]) | ||
.arg_package("Package to remove from") | ||
.arg_manifest_path() | ||
.arg_quiet() | ||
.arg_dry_run("Don't actually write the manifest") | ||
.next_help_heading("SECTION") | ||
.args([ | ||
clap::Arg::new("dev") | ||
.long("dev") | ||
.conflicts_with("build") | ||
.action(clap::ArgAction::SetTrue) | ||
.group("section") | ||
.help("Remove as development dependency"), | ||
clap::Arg::new("build") | ||
.long("build") | ||
.conflicts_with("dev") | ||
.action(clap::ArgAction::SetTrue) | ||
.group("section") | ||
.help("Remove as build dependency"), | ||
clap::Arg::new("target") | ||
.long("target") | ||
.num_args(1) | ||
.value_name("TARGET") | ||
.value_parser(clap::builder::NonEmptyStringValueParser::new()) | ||
.help("Remove as dependency from the given target platform"), | ||
]) | ||
} | ||
|
||
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { | ||
let dry_run = args.dry_run(); | ||
|
||
let workspace = args.workspace(config)?; | ||
let packages = args.packages_from_flags()?; | ||
let packages = packages.get_packages(&workspace)?; | ||
let spec = match packages.len() { | ||
0 => { | ||
return Err(CliError::new( | ||
anyhow::format_err!("no packages selected. Please specify one with `-p <PKG_ID>`"), | ||
101, | ||
)); | ||
} | ||
1 => packages[0], | ||
len => { | ||
return Err(CliError::new( | ||
anyhow::format_err!( | ||
"{len} packages selected. Please specify one with `-p <PKG_ID>`", | ||
), | ||
101, | ||
)); | ||
epage marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}; | ||
|
||
let dependencies = args | ||
.get_many::<String>("dependencies") | ||
.expect("required(true)") | ||
.cloned() | ||
.collect(); | ||
|
||
let section = parse_section(args); | ||
|
||
let options = RemoveOptions { | ||
config, | ||
spec, | ||
dependencies, | ||
section, | ||
dry_run, | ||
}; | ||
remove(&options)?; | ||
|
||
if !dry_run { | ||
// Reload the workspace since we've changed dependencies | ||
let ws = args.workspace(config)?; | ||
resolve_ws(&ws)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn parse_section(args: &ArgMatches) -> DepTable { | ||
weihanglo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let dev = args.flag("dev"); | ||
let build = args.flag("build"); | ||
|
||
let kind = if dev { | ||
DepKind::Development | ||
} else if build { | ||
DepKind::Build | ||
} else { | ||
DepKind::Normal | ||
}; | ||
|
||
let mut table = DepTable::new().set_kind(kind); | ||
|
||
if let Some(target) = args.get_one::<String>("target") { | ||
assert!(!target.is_empty(), "Target specification may not be empty"); | ||
table = table.set_target(target); | ||
} | ||
|
||
table | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//! Core of cargo-remove command | ||
|
||
use crate::core::Package; | ||
use crate::util::toml_mut::manifest::DepTable; | ||
use crate::util::toml_mut::manifest::LocalManifest; | ||
use crate::CargoResult; | ||
use crate::Config; | ||
|
||
/// Remove a dependency from a Cargo.toml manifest file. | ||
#[derive(Debug)] | ||
pub struct RemoveOptions<'a> { | ||
/// Configuration information for Cargo operations | ||
pub config: &'a Config, | ||
/// Package to remove dependencies from | ||
pub spec: &'a Package, | ||
/// Dependencies to remove | ||
pub dependencies: Vec<String>, | ||
/// Which dependency section to remove these from | ||
pub section: DepTable, | ||
/// Whether or not to actually write the manifest | ||
pub dry_run: bool, | ||
} | ||
|
||
/// Remove dependencies from a manifest | ||
pub fn remove(options: &RemoveOptions<'_>) -> CargoResult<()> { | ||
let dep_table = options | ||
.section | ||
.to_table() | ||
.into_iter() | ||
.map(String::from) | ||
.collect::<Vec<_>>(); | ||
|
||
let manifest_path = options.spec.manifest_path().to_path_buf(); | ||
let mut manifest = LocalManifest::try_new(&manifest_path)?; | ||
|
||
for dep in &options.dependencies { | ||
let section = if dep_table.len() >= 3 { | ||
format!("{} for target `{}`", &dep_table[2], &dep_table[1]) | ||
} else { | ||
dep_table[0].clone() | ||
}; | ||
options | ||
.config | ||
.shell() | ||
.status("Removing", format!("{dep} from {section}"))?; | ||
|
||
manifest.remove_from_table(&dep_table, dep)?; | ||
|
||
// Now that we have removed the crate, if that was the last reference to that | ||
// crate, then we need to drop any explicitly activated features on | ||
// that crate. | ||
manifest.gc_dep(dep); | ||
} | ||
|
||
if options.dry_run { | ||
options | ||
.config | ||
.shell() | ||
.warn("aborting remove due to dry run")?; | ||
} else { | ||
manifest.write()?; | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../remove-basic.in/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use cargo_test_support::compare::assert_ui; | ||
use cargo_test_support::curr_dir; | ||
use cargo_test_support::CargoCommand; | ||
use cargo_test_support::Project; | ||
|
||
use crate::cargo_remove::init_registry; | ||
|
||
#[cargo_test] | ||
fn case() { | ||
init_registry(); | ||
let project = Project::from_template(curr_dir!().join("in")); | ||
let project_root = project.root(); | ||
let cwd = &project_root; | ||
|
||
snapbox::cmd::Command::cargo_ui() | ||
.arg("remove") | ||
.args(["clippy"]) | ||
.current_dir(cwd) | ||
.assert() | ||
.success() | ||
.stdout_matches_path(curr_dir!().join("stdout.log")) | ||
.stderr_matches_path(curr_dir!().join("stderr.log")); | ||
|
||
assert_ui().subset_matches(curr_dir!().join("out"), &project_root); | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/testsuite/cargo_remove/avoid_empty_tables/out/Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "cargo-remove-test-fixture" | ||
version = "0.1.0" | ||
|
||
[[bin]] | ||
name = "main" | ||
path = "src/main.rs" | ||
|
||
[build-dependencies] | ||
semver = "0.1.0" | ||
|
||
[dependencies] | ||
docopt = "0.6" | ||
rustc-serialize = "0.4" | ||
semver = "0.1" | ||
toml = "0.1" | ||
|
||
[dev-dependencies] | ||
regex = "0.1.1" | ||
serde = "1.0.90" | ||
|
||
[features] | ||
std = ["serde/std", "semver/std"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Removing clippy from dependencies | ||
Updating `dummy-registry` index |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../remove-basic.in/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use cargo_test_support::compare::assert_ui; | ||
use cargo_test_support::curr_dir; | ||
use cargo_test_support::CargoCommand; | ||
use cargo_test_support::Project; | ||
|
||
use crate::cargo_remove::init_registry; | ||
|
||
#[cargo_test] | ||
fn case() { | ||
init_registry(); | ||
let project = Project::from_template(curr_dir!().join("in")); | ||
let project_root = project.root(); | ||
let cwd = &project_root; | ||
|
||
snapbox::cmd::Command::cargo_ui() | ||
.arg("remove") | ||
.args(["--build", "semver"]) | ||
.current_dir(cwd) | ||
.assert() | ||
.success() | ||
.stdout_matches_path(curr_dir!().join("stdout.log")) | ||
.stderr_matches_path(curr_dir!().join("stderr.log")); | ||
|
||
assert_ui().subset_matches(curr_dir!().join("out"), &project_root); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "cargo-remove-test-fixture" | ||
version = "0.1.0" | ||
|
||
[[bin]] | ||
name = "main" | ||
path = "src/main.rs" | ||
|
||
[dependencies] | ||
docopt = "0.6" | ||
rustc-serialize = "0.4" | ||
semver = "0.1" | ||
toml = "0.1" | ||
clippy = "0.4" | ||
|
||
[dev-dependencies] | ||
regex = "0.1.1" | ||
serde = "1.0.90" | ||
|
||
[features] | ||
std = ["serde/std", "semver/std"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Removing semver from build-dependencies | ||
Updating `dummy-registry` index |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../remove-basic.in/ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.