Skip to content

Commit

Permalink
Add fallback search sections to cargo remove
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <[email protected]>
  • Loading branch information
Rustin170506 committed Oct 22, 2023
1 parent d2f6a04 commit a2a2cf0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/bin/cargo/commands/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,14 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
.cloned()
.collect::<Vec<_>>();

let section = parse_section(args);
let (section, fallback_search_sections) = parse_section(args);

let options = RemoveOptions {
config,
spec,
dependencies,
section,
fallback_search_sections,
dry_run,
};
remove(&options)?;
Expand Down Expand Up @@ -134,26 +135,38 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
Ok(())
}

fn parse_section(args: &ArgMatches) -> DepTable {
fn parse_section(args: &ArgMatches) -> (DepTable, Vec<DepTable>) {
let dev = args.flag("dev");
let build = args.flag("build");

let mut search_kinds = Vec::new();

let kind = if dev {
search_kinds.extend_from_slice(&[DepKind::Build, DepKind::Normal]);
DepKind::Development
} else if build {
search_kinds.extend_from_slice(&[DepKind::Development, DepKind::Normal]);
DepKind::Build
} else {
search_kinds.extend_from_slice(&[DepKind::Build, DepKind::Development]);
DepKind::Normal
};

let mut table = DepTable::new().set_kind(kind);

let mut search_tables = search_kinds
.iter()
.map(|k| DepTable::new().set_kind(*k))
.collect::<Vec<_>>();
if let Some(target) = args.get_one::<String>("target") {
assert!(!target.is_empty(), "Target specification may not be empty");
table = table.set_target(target);
search_tables = search_tables
.into_iter()
.map(|t| t.set_target(target.clone()))
.collect::<Vec<_>>();
}

table
(table, search_tables)
}

/// Clean up the workspace.dependencies, profile, patch, and replace sections of the root manifest
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/ops/cargo_remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub struct RemoveOptions<'a> {
pub dependencies: Vec<String>,
/// Which dependency section to remove these from
pub section: DepTable,
/// Try to search for the dependency in these sections if it is not found in the specified section
pub fallback_search_sections: Vec<DepTable>,
/// Whether or not to actually write the manifest
pub dry_run: bool,
}
Expand Down

0 comments on commit a2a2cf0

Please sign in to comment.