Skip to content

Commit

Permalink
Merge pull request #834 from epage/rm
Browse files Browse the repository at this point in the history
fix(rm)!: Remove `rm` command in favor of cargo
  • Loading branch information
epage committed May 23, 2023
2 parents 547ab7b + cee32b4 commit 281f551
Show file tree
Hide file tree
Showing 112 changed files with 5 additions and 1,754 deletions.
71 changes: 5 additions & 66 deletions src/bin/rm/rm.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use cargo_edit::shell_status;
use cargo_edit::shell_warn;
use cargo_edit::CargoResult;
use cargo_edit::{manifest_from_pkgid, LocalManifest};
use clap::Args;
use std::borrow::Cow;
use std::path::PathBuf;

/// Remove a dependency from a Cargo.toml manifest file.
Expand Down Expand Up @@ -49,70 +45,13 @@ pub struct RmArgs {

impl RmArgs {
pub fn exec(&self) -> CargoResult<()> {
exec(self)
}

/// Get dependency section
pub fn get_section(&self) -> Vec<String> {
let section_name = if self.dev {
"dev-dependencies"
} else if self.build {
"build-dependencies"
} else {
"dependencies"
};

if let Some(ref target) = self.target {
assert!(!target.is_empty(), "Target specification may not be empty");

vec!["target".to_owned(), target.clone(), section_name.to_owned()]
} else {
vec![section_name.to_owned()]
}
anyhow::bail!(
"`cargo rm` has been merged into cargo 1.66+ as of cargo-edit 0.12, either
- Upgrade cargo, like with `rustup update`
- Downgrade `cargo-edit`, like with `cargo install cargo-edit --version 0.11`"
);
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, clap::ValueEnum)]
enum UnstableOptions {}

fn exec(args: &RmArgs) -> CargoResult<()> {
let manifest_path = if let Some(ref pkgid) = args.pkgid {
let pkg = manifest_from_pkgid(args.manifest_path.as_deref(), pkgid)?;
Cow::Owned(Some(pkg.manifest_path.into_std_path_buf()))
} else {
Cow::Borrowed(&args.manifest_path)
};
let mut manifest = LocalManifest::find(manifest_path.as_deref())?;
let deps = &args.crates;

deps.iter()
.map(|dep| {
if !args.quiet {
let section = args.get_section();
let section = if section.len() >= 3 {
format!("{} for target `{}`", &section[2], &section[1])
} else {
section[0].clone()
};
shell_status("Removing", &format!("{dep} from {section}",))?;
}
let result = manifest
.remove_from_table(&args.get_section(), dep)
.map_err(Into::into);

// 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);

result
})
.collect::<CargoResult<Vec<_>>>()?;

if args.dry_run {
shell_warn("aborting rm due to dry run")?;
} else {
manifest.write()?;
}

Ok(())
}
26 changes: 0 additions & 26 deletions tests/cargo-rm/dry_run/in/Cargo.toml

This file was deleted.

Empty file.
25 changes: 0 additions & 25 deletions tests/cargo-rm/dry_run/mod.rs

This file was deleted.

26 changes: 0 additions & 26 deletions tests/cargo-rm/dry_run/out/Cargo.toml

This file was deleted.

Empty file.
2 changes: 0 additions & 2 deletions tests/cargo-rm/dry_run/stderr.log

This file was deleted.

Empty file removed tests/cargo-rm/dry_run/stdout.log
Empty file.
26 changes: 0 additions & 26 deletions tests/cargo-rm/invalid_arg/in/Cargo.toml

This file was deleted.

Empty file.
25 changes: 0 additions & 25 deletions tests/cargo-rm/invalid_arg/mod.rs

This file was deleted.

26 changes: 0 additions & 26 deletions tests/cargo-rm/invalid_arg/out/Cargo.toml

This file was deleted.

7 changes: 0 additions & 7 deletions tests/cargo-rm/invalid_arg/stderr.log

This file was deleted.

Empty file.
26 changes: 0 additions & 26 deletions tests/cargo-rm/invalid_dep/in/Cargo.toml

This file was deleted.

Empty file.
25 changes: 0 additions & 25 deletions tests/cargo-rm/invalid_dep/mod.rs

This file was deleted.

26 changes: 0 additions & 26 deletions tests/cargo-rm/invalid_dep/out/Cargo.toml

This file was deleted.

2 changes: 0 additions & 2 deletions tests/cargo-rm/invalid_dep/stderr.log

This file was deleted.

Empty file.
35 changes: 0 additions & 35 deletions tests/cargo-rm/invalid_rm_target/in/Cargo.toml

This file was deleted.

0 comments on commit 281f551

Please sign in to comment.