Skip to content

Commit dfff29f

Browse files
committed
Output debug lines from update-pathversions.rs
1 parent 20e9c13 commit dfff29f

File tree

2 files changed

+105
-23
lines changed

2 files changed

+105
-23
lines changed

eng/scripts/Update-PackageVersion.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ if ($content -ne $updated) {
8484
Write-Host "Updated version in $tomlPath from $($pkgProperties.Version) to $packageSemVer."
8585

8686
Write-Host "Updaging dependencies in Cargo.toml files."
87-
Invoke-LoggedCommand "cargo +nightly -Zscript '$RepoRoot/eng/scripts/update-pathversions.rs' update" | Out-Null
87+
Invoke-LoggedCommand "cargo +nightly -Zscript '$RepoRoot/eng/scripts/update-pathversions.rs' update"
8888

8989
Write-Host "Updating Cargo.lock using 'cargo update --workspace'."
90-
Invoke-LoggedCommand "cargo update --workspace" | Out-Null
90+
Invoke-LoggedCommand "cargo update --workspace"
9191
}
9292
else {
9393
Write-Host "$tomlPath already contains version $packageSemVer"

eng/scripts/update-pathversions.rs

Lines changed: 103 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,30 @@ use std::{env, error::Error, fs, path::PathBuf};
1515
use toml_edit::{value, DocumentMut, Item, Table};
1616

1717
fn main() -> Result<(), Box<dyn std::error::Error>> {
18-
let add_mode = env::args().nth(1)
18+
let add_mode = env::args()
19+
.nth(1)
1920
.map(|arg| match arg.as_str() {
2021
"add" => true,
2122
"update" => false,
22-
_ => panic!("Invalid mode. Use 'add' or 'update'.")
23+
_ => panic!("Invalid mode. Use 'add' or 'update'."),
2324
})
2425
.expect("requires 'add' or 'update' mode argument");
2526

27+
println!(
28+
"Running update-pathversions in {} mode",
29+
if add_mode { "add" } else { "update" }
30+
);
31+
2632
let script_root = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
27-
let repo_root = script_root.join("../../..").canonicalize()?;
33+
let repo_root = script_root.join("../..").canonicalize()?;
34+
35+
println!(
36+
"Scanning for Cargo.toml files under {}",
37+
repo_root.display()
38+
);
2839

2940
// find all Cargo.toml files in the repo_root directory
30-
let exclude_dirs = vec![
31-
repo_root.join("eng"),
32-
repo_root.join("target")
33-
];
41+
let exclude_dirs = vec![repo_root.join("eng"), repo_root.join("target")];
3442

3543
let toml_files = load_cargo_toml_files(&repo_root, &exclude_dirs)?;
3644

@@ -39,15 +47,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3947
for mut toml_file in toml_files {
4048
let should_add = add_mode && !toml_file.is_publish_disabled;
4149

42-
update_package_versions(toml_file.document.as_table_mut(), &package_versions, should_add);
50+
let mut updated = update_package_versions(
51+
toml_file.document.as_table_mut(),
52+
&package_versions,
53+
should_add,
54+
);
4355

4456
// if the toml file has a workspace table, update the workspace table
4557
if let Some(workspace) = toml_file.document.get_mut("workspace") {
58+
// print out that we're working on a workspace
4659
if let Some(table) = workspace.as_table_mut() {
47-
update_package_versions(table, &package_versions, should_add);
60+
updated = update_package_versions(table, &package_versions, should_add) || updated;
4861
}
4962
}
5063

64+
if !updated {
65+
continue;
66+
}
67+
68+
println!("Updating {}", toml_file.path.display());
69+
5170
// write the updated document back to the file
5271
let mut file = fs::File::create(toml_file.path)?;
5372
fs::File::write_all(&mut file, toml_file.document.to_string().as_bytes())?;
@@ -56,32 +75,49 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5675
Ok(())
5776
}
5877

59-
fn load_cargo_toml_files(repo_root: &PathBuf, exclude_dirs: &Vec<PathBuf>) -> Result<Vec<TomlInfo>, Box<dyn Error>> {
78+
fn load_cargo_toml_files(
79+
repo_root: &PathBuf,
80+
exclude_dirs: &Vec<PathBuf>,
81+
) -> Result<Vec<TomlInfo>, Box<dyn Error>> {
6082
let mut toml_paths = Vec::new();
6183
find_cargo_toml_files(repo_root, exclude_dirs, &mut toml_paths)?;
6284

6385
let mut toml_files = Vec::new();
86+
6487
for path in toml_paths {
6588
let content = fs::read_to_string(&path)?;
6689
let doc = content.parse::<DocumentMut>()?;
6790
let package_table = doc.get("package").and_then(Item::as_table);
68-
let publish_property = package_table.and_then(|table| table.get("publish")).and_then(Item::as_bool);
69-
let package_name = package_table.and_then(|table| table.get("name")).and_then(Item::as_str);
70-
let package_version = package_table.and_then(|table| table.get("version")).and_then(Item::as_str);
91+
92+
let publish_property = package_table
93+
.and_then(|table| table.get("publish"))
94+
.and_then(Item::as_bool);
95+
96+
let package_name = package_table
97+
.and_then(|table| table.get("name"))
98+
.and_then(Item::as_str);
99+
100+
let package_version = package_table
101+
.and_then(|table| table.get("version"))
102+
.and_then(Item::as_str);
71103

72104
toml_files.push(TomlInfo {
73105
path,
74106
package_name: package_name.map(|s| s.to_string()),
75107
package_version: package_version.map(|s| s.to_string()),
76108
is_publish_disabled: publish_property == Some(false),
77-
document: doc
109+
document: doc,
78110
});
79111
}
80112

81113
Ok(toml_files)
82114
}
83115

84-
fn find_cargo_toml_files(dir: &PathBuf, exclude_dirs: &Vec<PathBuf>, toml_paths: &mut Vec<PathBuf>) -> Result<(), Box<dyn Error>> {
116+
fn find_cargo_toml_files(
117+
dir: &PathBuf,
118+
exclude_dirs: &Vec<PathBuf>,
119+
toml_paths: &mut Vec<PathBuf>,
120+
) -> Result<(), Box<dyn Error>> {
85121
for entry in fs::read_dir(dir)? {
86122
let entry = entry?;
87123
let path = entry.path();
@@ -103,38 +139,84 @@ fn get_package_versions(toml_files: &Vec<TomlInfo>) -> Vec<(String, String, bool
103139
continue;
104140
}
105141

106-
package_versions.push((toml_file.package_name.clone().unwrap(), toml_file.package_version.clone().unwrap(), toml_file.is_publish_disabled));
142+
package_versions.push((
143+
toml_file.package_name.clone().unwrap(),
144+
toml_file.package_version.clone().unwrap(),
145+
toml_file.is_publish_disabled,
146+
));
107147
}
108148

109149
package_versions
110150
}
111151

112-
fn update_package_versions(toml: &mut Table, package_versions: &Vec<(String, String, bool)>, add: bool) {
152+
fn update_package_versions(
153+
toml: &mut Table,
154+
package_versions: &Vec<(String, String, bool)>,
155+
add: bool,
156+
) -> bool {
157+
// Returns `true` if any modifications were made to the TOML table, `false` otherwise.
158+
// This indicates whether the file needs to be written back to disk.
159+
//
113160
// for each dependency table, for each package in package_versions
114161
// if the package is in the dependency table
115162
// if the dependency has both path and version properties, update the version property
116163
// if the dependency has has path, but not version, add the version property only if
117164
// 1. the table name is not "dev-dependencies"
118165
// 2. the package is not publish disabled
119166
// 3. the add flag is true
167+
//
168+
let crate_name = toml
169+
.get("package")
170+
.and_then(Item::as_table)
171+
.and_then(|table| table.get("name"))
172+
.and_then(Item::as_str)
173+
.unwrap_or("<unknown>")
174+
.trim_matches('"')
175+
.trim()
176+
.to_string();
120177

121178
let dependency_tables = get_dependency_tables(toml);
122-
179+
let mut updated = false;
123180
for (table_name, table) in dependency_tables {
124181
for (package, version, is_publish_disabled) in package_versions {
125182
if let Some(dependency) = table.get_mut(package) {
126183
// azure_idenentity will only be a transitive dev-dependency
127-
let should_add = add && table_name != "dev-dependencies" && !is_publish_disabled && package != "azure_identity";
184+
let should_add = add
185+
&& table_name != "dev-dependencies"
186+
&& !is_publish_disabled
187+
&& package != "azure_identity";
128188

129189
let has_path_property = dependency.get("path").is_some();
130190
let has_version_property = dependency.get("version").is_some();
131191

132-
if has_path_property && (has_version_property || should_add) {
133-
dependency["version"] = value(version);
192+
if has_path_property {
193+
if has_version_property {
194+
let current_version = dependency
195+
.get("version")
196+
.and_then(Item::as_str)
197+
.unwrap_or("");
198+
if current_version != version {
199+
dependency["version"] = value(version);
200+
println!(
201+
"set {} to version {} in {} {}",
202+
package, version, crate_name, table_name
203+
);
204+
updated = true;
205+
}
206+
} else if should_add {
207+
dependency["version"] = value(version);
208+
println!(
209+
"added version {} to {} in {} {}",
210+
version, package, crate_name, table_name
211+
);
212+
updated = true;
213+
}
134214
}
135215
}
136216
}
137217
}
218+
219+
updated
138220
}
139221

140222
fn get_dependency_tables(toml: &mut Table) -> Vec<(String, &mut Table)> {

0 commit comments

Comments
 (0)