|
1 | 1 | use cargo::core::dependency::DepKind;
|
| 2 | +use cargo::core::Dependency; |
| 3 | +use cargo::core::Package; |
| 4 | +use cargo::core::Workspace; |
2 | 5 | use cargo::ops::cargo_remove::remove;
|
3 | 6 | use cargo::ops::cargo_remove::RemoveOptions;
|
4 | 7 | use cargo::ops::resolve_ws;
|
5 | 8 | use cargo::util::command_prelude::*;
|
6 | 9 | use cargo::util::toml_mut::manifest::DepTable;
|
| 10 | +use cargo::util::toml_mut::manifest::LocalManifest; |
| 11 | +use cargo::CargoResult; |
7 | 12 |
|
8 | 13 | pub fn cli() -> clap::Command {
|
9 | 14 | clap::Command::new("remove")
|
@@ -85,6 +90,9 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
|
85 | 90 | remove(&options)?;
|
86 | 91 |
|
87 | 92 | if !dry_run {
|
| 93 | + // Clean up workspace dependencies |
| 94 | + gc_workspace(&workspace, &options.dependencies)?; |
| 95 | + |
88 | 96 | // Reload the workspace since we've changed dependencies
|
89 | 97 | let ws = args.workspace(config)?;
|
90 | 98 | resolve_ws(&ws)?;
|
@@ -114,3 +122,76 @@ fn parse_section(args: &ArgMatches) -> DepTable {
|
114 | 122 |
|
115 | 123 | table
|
116 | 124 | }
|
| 125 | + |
| 126 | +/// Clean up workspace dependencies which no longer have a reference to them. |
| 127 | +fn gc_workspace(workspace: &Workspace<'_>, dependencies: &[String]) -> CargoResult<()> { |
| 128 | + let mut manifest: toml_edit::Document = |
| 129 | + cargo_util::paths::read(workspace.root_manifest())?.parse()?; |
| 130 | + |
| 131 | + let members = workspace |
| 132 | + .members() |
| 133 | + .map(|p| LocalManifest::try_new(p.manifest_path()).map(|m| (p, m))) |
| 134 | + .collect::<CargoResult<Vec<_>>>()?; |
| 135 | + |
| 136 | + for dep in dependencies { |
| 137 | + if !dep_in_workspace(dep, &members) { |
| 138 | + remove_workspace_dep(dep, &mut manifest); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + cargo_util::paths::write(workspace.root_manifest(), manifest.to_string().as_bytes())?; |
| 143 | + |
| 144 | + Ok(()) |
| 145 | +} |
| 146 | + |
| 147 | +/// Get whether or not a dependency is marked as `workspace`. |
| 148 | +fn dep_is_workspace(dep: &str, dep_table: &[String], manifest: &LocalManifest) -> bool { |
| 149 | + if let Ok(toml_edit::Item::Table(table)) = manifest.get_table(dep_table) { |
| 150 | + let value = table.get(dep).and_then(|i| i.get("workspace")); |
| 151 | + if let Some(toml_edit::Item::Value(value)) = value { |
| 152 | + return value.as_bool() == Some(true); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + false |
| 157 | +} |
| 158 | + |
| 159 | +/// Get whether or not a dependency is depended upon in a workspace. |
| 160 | +fn dep_in_workspace(dep: &str, members: &[(&Package, LocalManifest)]) -> bool { |
| 161 | + members.iter().any(|(pkg, manifest)| { |
| 162 | + pkg.dependencies() |
| 163 | + .iter() |
| 164 | + .filter(|d| d.name_in_toml() == dep) |
| 165 | + .map(|d| (d, dep_to_table(d))) |
| 166 | + .any(|(d, t)| { |
| 167 | + // Information about workspace dependencies is not preserved at this stage, so we |
| 168 | + // have to manually read from the TOML manifest |
| 169 | + let dep_table = t |
| 170 | + .to_table() |
| 171 | + .into_iter() |
| 172 | + .map(String::from) |
| 173 | + .collect::<Vec<_>>(); |
| 174 | + dep_is_workspace(&d.package_name(), &dep_table, manifest) |
| 175 | + }) |
| 176 | + }) |
| 177 | +} |
| 178 | + |
| 179 | +/// Find the corresponding [`DepTable`] for a [`Dependency`]. |
| 180 | +fn dep_to_table(dep: &Dependency) -> DepTable { |
| 181 | + let mut dep_table = DepTable::new().set_kind(dep.kind()); |
| 182 | + if let Some(target) = dep.platform().map(|p| p.to_string()) { |
| 183 | + dep_table = dep_table.set_target(target); |
| 184 | + } |
| 185 | + dep_table |
| 186 | +} |
| 187 | + |
| 188 | +/// Remove a dependency from a workspace manifest. |
| 189 | +fn remove_workspace_dep(dep: &str, ws_manifest: &mut toml_edit::Document) { |
| 190 | + if let Some(toml_edit::Item::Table(table)) = ws_manifest |
| 191 | + .get_mut("workspace") |
| 192 | + .and_then(|t| t.get_mut("dependencies")) |
| 193 | + { |
| 194 | + table.set_implicit(true); |
| 195 | + table.remove(dep); |
| 196 | + } |
| 197 | +} |
0 commit comments