Skip to content

Commit

Permalink
fix(cli): preserve Cargo manifest formatting when possible (#4431)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Jun 22, 2022
1 parent 672174b commit 6650e5d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changes/keep-manifest-fmt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---

Preserve the `Cargo.toml` formatting when the features array is not changed.
22 changes: 21 additions & 1 deletion tooling/cli/src/helpers/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,27 @@ fn write_features(
}
}
}
*manifest_features = Item::Value(Value::Array(toml_array(features)));
if let Some(features_array) = manifest_features.as_array_mut() {
// add features that aren't in the manifest
for feature in features.iter() {
if !features_array.iter().any(|f| f.as_str() == Some(feature)) {
features_array.insert(0, feature.as_str());
}
}

// remove features that shouldn't be in the manifest anymore
let mut i = 0;
while i < features_array.len() {
if let Some(f) = features_array.get(i).and_then(|f| f.as_str()) {
if !features.contains(f) {
features_array.remove(i);
}
}
i += 1;
}
} else {
*manifest_features = Item::Value(Value::Array(toml_array(features)));
}
Ok(true)
} else if let Some(dep) = item.as_value_mut() {
match dep {
Expand Down

0 comments on commit 6650e5d

Please sign in to comment.