@@ -8,7 +8,6 @@ description = "In all Cargo.toml files in the repo, for all dependencies that ha
88regex = "1.5"
99toml_edit = "0.22"
1010---
11-
1211use regex:: Regex ;
1312use std:: io:: Write ;
1413use std:: { env, error:: Error , fs, path:: PathBuf } ;
@@ -23,8 +22,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2322 } )
2423 . expect ( "requires 'add' or 'update' mode argument" ) ;
2524
25+ println ! (
26+ "Running update-pathversions in {} mode" ,
27+ if add_mode { "add" } else { "update" }
28+ ) ;
29+
2630 let script_root = PathBuf :: from ( env:: var ( "CARGO_MANIFEST_DIR" ) ?) ;
27- let repo_root = script_root. join ( "../../.." ) . canonicalize ( ) ?;
31+ let repo_root = script_root. join ( "../.." ) . canonicalize ( ) ?;
32+
33+ println ! (
34+ "Scanning for Cargo.toml files under {}" ,
35+ repo_root. display( )
36+ ) ;
2837
2938 // find all Cargo.toml files in the repo_root directory
3039 let exclude_dirs = vec ! [
@@ -39,15 +48,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3948 for mut toml_file in toml_files {
4049 let should_add = add_mode && !toml_file. is_publish_disabled ;
4150
42- update_package_versions ( toml_file. document . as_table_mut ( ) , & package_versions, should_add) ;
51+ let mut updated = update_package_versions (
52+ toml_file. document . as_table_mut ( ) ,
53+ & package_versions,
54+ should_add,
55+ ) ;
4356
4457 // if the toml file has a workspace table, update the workspace table
4558 if let Some ( workspace) = toml_file. document . get_mut ( "workspace" ) {
59+ // print out that we're working on a workspace
4660 if let Some ( table) = workspace. as_table_mut ( ) {
47- update_package_versions ( table, & package_versions, should_add) ;
61+ updated = update_package_versions ( table, & package_versions, should_add) || updated ;
4862 }
4963 }
5064
65+ if !updated {
66+ continue ;
67+ }
68+
69+ println ! ( "Updating {}" , toml_file. path. display( ) ) ;
70+
5171 // write the updated document back to the file
5272 let mut file = fs:: File :: create ( toml_file. path ) ?;
5373 fs:: File :: write_all ( & mut file, toml_file. document . to_string ( ) . as_bytes ( ) ) ?;
@@ -56,32 +76,46 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5676 Ok ( ( ) )
5777}
5878
59- fn load_cargo_toml_files ( repo_root : & PathBuf , exclude_dirs : & Vec < PathBuf > ) -> Result < Vec < TomlInfo > , Box < dyn Error > > {
79+ fn load_cargo_toml_files ( ,
80+ repo_root : & PathBuf ,
81+ exclude_dirs : & Vec < PathBuf > ,
82+ ) -> Result < Vec < TomlInfo > , Box < dyn Error > > {
6083 let mut toml_paths = Vec :: new ( ) ;
6184 find_cargo_toml_files ( repo_root, exclude_dirs, & mut toml_paths) ?;
6285
6386 let mut toml_files = Vec :: new ( ) ;
87+
6488 for path in toml_paths {
6589 let content = fs:: read_to_string ( & path) ?;
6690 let doc = content. parse :: < DocumentMut > ( ) ?;
6791 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) ;
92+ let publish_property = package_table
93+ . and_then ( |table| table. get ( "publish" ) )
94+ . and_then ( Item :: as_bool) ;
95+ let package_name = package_table
96+ . and_then ( |table| table. get ( "name" ) )
97+ . and_then ( Item :: as_str) ;
98+ let package_version = package_table
99+ . and_then ( |table| table. get ( "version" ) )
100+ . and_then ( Item :: as_str) ;
71101
72102 toml_files. push ( TomlInfo {
73103 path,
74104 package_name : package_name. map ( |s| s. to_string ( ) ) ,
75105 package_version : package_version. map ( |s| s. to_string ( ) ) ,
76106 is_publish_disabled : publish_property == Some ( false ) ,
77- document : doc
107+ document : doc,
78108 } ) ;
79109 }
80110
81111 Ok ( toml_files)
82112}
83113
84- fn find_cargo_toml_files ( dir : & PathBuf , exclude_dirs : & Vec < PathBuf > , toml_paths : & mut Vec < PathBuf > ) -> Result < ( ) , Box < dyn Error > > {
114+ fn find_cargo_toml_files (
115+ dir : & PathBuf ,
116+ exclude_dirs : & Vec < PathBuf > ,
117+ toml_paths : & mut Vec < PathBuf > ,
118+ ) -> Result < ( ) , Box < dyn Error > > {
85119 for entry in fs:: read_dir ( dir) ? {
86120 let entry = entry?;
87121 let path = entry. path ( ) ;
@@ -103,13 +137,21 @@ fn get_package_versions(toml_files: &Vec<TomlInfo>) -> Vec<(String, String, bool
103137 continue ;
104138 }
105139
106- package_versions. push ( ( toml_file. package_name . clone ( ) . unwrap ( ) , toml_file. package_version . clone ( ) . unwrap ( ) , toml_file. is_publish_disabled ) ) ;
140+ package_versions. push ( (
141+ toml_file. package_name . clone ( ) . unwrap ( ) ,
142+ toml_file. package_version . clone ( ) . unwrap ( ) ,
143+ toml_file. is_publish_disabled ,
144+ ) ) ;
107145 }
108146
109147 package_versions
110148}
111149
112- fn update_package_versions ( toml : & mut Table , package_versions : & Vec < ( String , String , bool ) > , add : bool ) {
150+ fn update_package_versions (
151+ toml : & mut Table ,
152+ package_versions : & Vec < ( String , String , bool ) > ,
153+ add : bool ,
154+ ) -> bool {
113155 // for each dependency table, for each package in package_versions
114156 // if the package is in the dependency table
115157 // if the dependency has both path and version properties, update the version property
@@ -118,23 +160,58 @@ fn update_package_versions(toml: &mut Table, package_versions: &Vec<(String, Str
118160 // 2. the package is not publish disabled
119161 // 3. the add flag is true
120162
121- let dependency_tables = get_dependency_tables ( toml) ;
163+ let crate_name = toml
164+ . get ( "package" )
165+ . and_then ( Item :: as_table)
166+ . and_then ( |table| table. get ( "name" ) )
167+ . and_then ( Item :: as_str)
168+ . unwrap_or ( "<unknown>" )
169+ . trim_matches ( '"' )
170+ . trim ( )
171+ . to_string ( ) ;
122172
173+ let dependency_tables = get_dependency_tables ( toml) ;
174+ let mut updated = false ;
123175 for ( table_name, table) in dependency_tables {
124176 for ( package, version, is_publish_disabled) in package_versions {
125177 if let Some ( dependency) = table. get_mut ( package) {
126178 // azure_idenentity will only be a transitive dev-dependency
127- let should_add = add && table_name != "dev-dependencies" && !is_publish_disabled && package != "azure_identity" ;
179+ let should_add = add
180+ && table_name != "dev-dependencies"
181+ && !is_publish_disabled
182+ && package != "azure_identity" ;
128183
129184 let has_path_property = dependency. get ( "path" ) . is_some ( ) ;
130185 let has_version_property = dependency. get ( "version" ) . is_some ( ) ;
131186
132- if has_path_property && ( has_version_property || should_add) {
133- dependency[ "version" ] = value ( version) ;
187+ if has_path_property {
188+ if has_version_property {
189+ let current_version = dependency
190+ . get ( "version" )
191+ . and_then ( Item :: as_str)
192+ . unwrap_or ( "" ) ;
193+ if current_version != version {
194+ dependency[ "version" ] = value ( version) ;
195+ println ! (
196+ "set {} to version {} in {} {}" ,
197+ package, version, crate_name, table_name
198+ ) ;
199+ updated = true ;
200+ }
201+ } else if should_add {
202+ dependency[ "version" ] = value ( version) ;
203+ println ! (
204+ "added version {} to {} in {} {}" ,
205+ version, package, crate_name, table_name
206+ ) ;
207+ updated = true ;
208+ }
134209 }
135210 }
136211 }
137212 }
213+
214+ updated
138215}
139216
140217fn get_dependency_tables ( toml : & mut Table ) -> Vec < ( String , & mut Table ) > {
0 commit comments