Skip to content

Commit

Permalink
Rewrite workspace inherited metadata fields in Cargo.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Sep 25, 2022
1 parent 3097b47 commit cd7efc7
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/source_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ use std::process::Command;
use std::str;

const LOCAL_DEPENDENCIES_FOLDER: &str = "local_dependencies";
/// Inheritable workspace fields, see
/// https://github.com/rust-lang/cargo/blob/13ae438cf079da58272edc71f4d4968043dbd27b/src/cargo/util/toml/mod.rs#L1140-L1158
const WORKSPACE_INHERITABLE_FIELDS: &[&str] = &[
"version",
"authors",
"description",
"homepage",
"documentation",
"readme",
"keywords",
"categories",
"license",
"license-file",
"repository",
"publish",
"edition",
"badges",
"exclude",
"include",
"rust-version",
];

/// We need cargo to load the local dependencies from the location where we put them in the source
/// distribution. Since there is no cargo-backed way to replace dependencies
Expand Down Expand Up @@ -44,7 +65,7 @@ fn rewrite_cargo_toml(
let workspace_deps = workspace_manifest
.get("workspace")
.and_then(|x| x.get(dep_category))
.and_then(|x| x.as_table());
.and_then(|x| x.as_table_like());
let dep_names: Vec<_> = table.iter().map(|(key, _)| key.to_string()).collect();
for dep_name in dep_names {
let workspace_inherit = table
Expand Down Expand Up @@ -138,6 +159,28 @@ fn rewrite_cargo_toml(
}
}
}

// Update workspace inherited metadata
if let Some(package) = data.get_mut("package").and_then(|x| x.as_table_mut()) {
let workspace_package = workspace_manifest
.get("workspace")
.and_then(|x| x.get("package"))
.and_then(|x| x.as_table_like());
for key in WORKSPACE_INHERITABLE_FIELDS.iter().copied() {
let workspace_inherited = package
.get(key)
.and_then(|x| x.get("workspace"))
.and_then(|x| x.as_bool())
.unwrap_or_default();
if workspace_inherited {
if let Some(workspace_value) = workspace_package.and_then(|ws| ws.get(key)) {
package[key] = workspace_value.clone();
rewritten = true;
}
}
}
}

if root_crate {
// Update workspace members
if let Some(workspace) = data.get_mut("workspace").and_then(|x| x.as_table_mut()) {
Expand Down

0 comments on commit cd7efc7

Please sign in to comment.