aboutsummaryrefslogtreecommitdiff
path: root/release/src/main.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2025-08-15 16:11:35 +0200
committerUlf Lilleengen <[email protected]>2025-08-25 19:44:50 +0200
commit2f540a4d2449234367e8d18c9e3fc8dc740d3ee5 (patch)
tree14bdeaea510e347c4a37eee28dbdae721c789ea1 /release/src/main.rs
parent6df077be0663cf4e45d9573ea8fc685a9f9dcd99 (diff)
Add Context struct.
Diffstat (limited to 'release/src/main.rs')
-rw-r--r--release/src/main.rs130
1 files changed, 78 insertions, 52 deletions
diff --git a/release/src/main.rs b/release/src/main.rs
index a49d8f36c..25f31486a 100644
--- a/release/src/main.rs
+++ b/release/src/main.rs
@@ -13,6 +13,7 @@ use simple_logger::SimpleLogger;
13use toml_edit::{DocumentMut, Item, Value}; 13use toml_edit::{DocumentMut, Item, Value};
14use types::*; 14use types::*;
15 15
16mod build;
16mod cargo; 17mod cargo;
17mod semver_check; 18mod semver_check;
18mod types; 19mod types;
@@ -47,6 +48,12 @@ enum Command {
47 crate_name: String, 48 crate_name: String,
48 }, 49 },
49 50
51 /// Build
52 Build {
53 /// Crate to check. If not specified checks all crates.
54 #[arg(value_name = "CRATE")]
55 crate_name: Option<String>,
56 },
50 /// SemverCheck 57 /// SemverCheck
51 SemverCheck { 58 SemverCheck {
52 /// Crate to check. Will traverse that crate an it's dependents. If not specified checks all crates. 59 /// Crate to check. Will traverse that crate an it's dependents. If not specified checks all crates.
@@ -186,40 +193,55 @@ fn build_graph(crates: &BTreeMap<CrateId, Crate>) -> (Graph<CrateId, ()>, HashMa
186 (graph, node_indices) 193 (graph, node_indices)
187} 194}
188 195
196struct Context {
197 root: PathBuf,
198 crates: BTreeMap<String, Crate>,
199 graph: Graph<String, ()>,
200 indices: HashMap<String, NodeIndex>,
201}
202
203fn load_context(args: &Args) -> Result<Context> {
204 let root = args.repo.canonicalize()?;
205 let crates = list_crates(&root)?;
206 let (graph, indices) = build_graph(&crates);
207
208 Ok(Context {
209 root,
210 crates,
211 graph,
212 indices,
213 })
214}
215
189fn main() -> Result<()> { 216fn main() -> Result<()> {
190 SimpleLogger::new().init().unwrap(); 217 SimpleLogger::new().init().unwrap();
191 let args = Args::parse(); 218 let args = Args::parse();
192 219 let mut ctx = load_context(&args)?;
193 let root = args.repo.canonicalize()?;
194 let mut crates = list_crates(&root)?;
195 let (mut graph, indices) = build_graph(&crates);
196 220
197 match args.command { 221 match args.command {
198 Command::List => { 222 Command::List => {
199 let ordered = petgraph::algo::toposort(&graph, None).unwrap(); 223 let ordered = petgraph::algo::toposort(&ctx.graph, None).unwrap();
200 for node in ordered.iter() { 224 for node in ordered.iter() {
201 if graph.neighbors_directed(*node, Direction::Incoming).count() == 0 { 225 let start = ctx.graph.node_weight(*node).unwrap();
202 let start = graph.node_weight(*node).unwrap(); 226 let mut bfs = Bfs::new(&ctx.graph, *node);
203 let mut bfs = Bfs::new(&graph, *node); 227 while let Some(node) = bfs.next(&ctx.graph) {
204 while let Some(node) = bfs.next(&graph) { 228 let weight = ctx.graph.node_weight(node).unwrap();
205 let weight = graph.node_weight(node).unwrap(); 229 let c = ctx.crates.get(weight).unwrap();
206 let c = crates.get(weight).unwrap(); 230 if weight == start {
207 if weight == start { 231 println!("+ {}-{}", weight, c.version);
208 println!("+ {}-{}", weight, c.version); 232 } else {
209 } else { 233 println!("|- {}-{}", weight, c.version);
210 println!("|- {}-{}", weight, c.version);
211 }
212 } 234 }
213 println!("");
214 } 235 }
236 println!("");
215 } 237 }
216 } 238 }
217 Command::Dependencies { crate_name } => { 239 Command::Dependencies { crate_name } => {
218 let idx = indices.get(&crate_name).expect("unable to find crate in tree"); 240 let idx = ctx.indices.get(&crate_name).expect("unable to find crate in tree");
219 let mut bfs = Bfs::new(&graph, *idx); 241 let mut bfs = Bfs::new(&ctx.graph, *idx);
220 while let Some(node) = bfs.next(&graph) { 242 while let Some(node) = bfs.next(&ctx.graph) {
221 let weight = graph.node_weight(node).unwrap(); 243 let weight = ctx.graph.node_weight(node).unwrap();
222 let crt = crates.get(weight).unwrap(); 244 let crt = ctx.crates.get(weight).unwrap();
223 if *weight == crate_name { 245 if *weight == crate_name {
224 println!("+ {}-{}", weight, crt.version); 246 println!("+ {}-{}", weight, crt.version);
225 } else { 247 } else {
@@ -228,30 +250,34 @@ fn main() -> Result<()> {
228 } 250 }
229 } 251 }
230 Command::Dependents { crate_name } => { 252 Command::Dependents { crate_name } => {
231 let idx = indices.get(&crate_name).expect("unable to find crate in tree"); 253 let idx = ctx.indices.get(&crate_name).expect("unable to find crate in tree");
232 let weight = graph.node_weight(*idx).unwrap(); 254 let weight = ctx.graph.node_weight(*idx).unwrap();
233 let crt = crates.get(weight).unwrap(); 255 let crt = ctx.crates.get(weight).unwrap();
234 println!("+ {}-{}", weight, crt.version); 256 println!("+ {}-{}", weight, crt.version);
235 for parent in graph.neighbors_directed(*idx, Direction::Incoming) { 257 for parent in ctx.graph.neighbors_directed(*idx, Direction::Incoming) {
236 let weight = graph.node_weight(parent).unwrap(); 258 let weight = ctx.graph.node_weight(parent).unwrap();
237 let crt = crates.get(weight).unwrap(); 259 let crt = ctx.crates.get(weight).unwrap();
238 println!("|- {}-{}", weight, crt.version); 260 println!("|- {}-{}", weight, crt.version);
239 } 261 }
240 } 262 }
263 Command::Build { crate_name } => {
264 build::build(&ctx)?;
265 }
241 Command::SemverCheck { crate_name } => { 266 Command::SemverCheck { crate_name } => {
242 let c = crates.get(&crate_name).unwrap(); 267 let c = ctx.crates.get(&crate_name).unwrap();
243 check_semver(&c)?; 268 check_semver(&c)?;
244 } 269 }
245 Command::PrepareRelease { crate_name } => { 270 Command::PrepareRelease { crate_name } => {
246 let start = indices.get(&crate_name).expect("unable to find crate in tree"); 271 let start = ctx.indices.get(&crate_name).expect("unable to find crate in tree");
247 graph.reverse(); 272 let mut rgraph = ctx.graph.clone();
273 rgraph.reverse();
248 274
249 let mut bfs = Bfs::new(&graph, *start); 275 let mut bfs = Bfs::new(&rgraph, *start);
250 276
251 while let Some(node) = bfs.next(&graph) { 277 while let Some(node) = bfs.next(&rgraph) {
252 let weight = graph.node_weight(node).unwrap(); 278 let weight = rgraph.node_weight(node).unwrap();
253 println!("Preparing {}", weight); 279 println!("Preparing {}", weight);
254 let mut c = crates.get_mut(weight).unwrap(); 280 let mut c = ctx.crates.get_mut(weight).unwrap();
255 let ver = semver::Version::parse(&c.version)?; 281 let ver = semver::Version::parse(&c.version)?;
256 let newver = if let Err(_) = check_semver(&c) { 282 let newver = if let Err(_) = check_semver(&c) {
257 println!("Semver check failed, bumping minor!"); 283 println!("Semver check failed, bumping minor!");
@@ -264,41 +290,41 @@ fn main() -> Result<()> {
264 let newver = newver.to_string(); 290 let newver = newver.to_string();
265 291
266 update_version(&mut c, &newver)?; 292 update_version(&mut c, &newver)?;
267 let c = crates.get(weight).unwrap(); 293 let c = ctx.crates.get(weight).unwrap();
268 294
269 // Update all nodes further down the tree 295 // Update all nodes further down the tree
270 let mut bfs = Bfs::new(&graph, node); 296 let mut bfs = Bfs::new(&rgraph, node);
271 while let Some(dep_node) = bfs.next(&graph) { 297 while let Some(dep_node) = bfs.next(&rgraph) {
272 let dep_weight = graph.node_weight(dep_node).unwrap(); 298 let dep_weight = rgraph.node_weight(dep_node).unwrap();
273 let dep = crates.get(dep_weight).unwrap(); 299 let dep = ctx.crates.get(dep_weight).unwrap();
274 update_versions(dep, &c.name, &newver)?; 300 update_versions(dep, &c.name, &newver)?;
275 } 301 }
276 302
277 // Update changelog 303 // Update changelog
278 update_changelog(&root, &c)?; 304 update_changelog(&ctx.root, &c)?;
279 } 305 }
280 306
281 let weight = graph.node_weight(*start).unwrap(); 307 let weight = rgraph.node_weight(*start).unwrap();
282 let c = crates.get(weight).unwrap(); 308 let c = ctx.crates.get(weight).unwrap();
283 publish_release(&root, &c, false)?; 309 publish_release(&ctx.root, &c, false)?;
284 310
285 println!("# Please inspect changes and run the following commands when happy:"); 311 println!("# Please inspect changes and run the following commands when happy:");
286 312
287 println!("git commit -a -m 'chore: prepare crate releases'"); 313 println!("git commit -a -m 'chore: prepare crate releases'");
288 let mut bfs = Bfs::new(&graph, *start); 314 let mut bfs = Bfs::new(&rgraph, *start);
289 while let Some(node) = bfs.next(&graph) { 315 while let Some(node) = bfs.next(&rgraph) {
290 let weight = graph.node_weight(node).unwrap(); 316 let weight = rgraph.node_weight(node).unwrap();
291 let c = crates.get(weight).unwrap(); 317 let c = ctx.crates.get(weight).unwrap();
292 println!("git tag {}-v{}", weight, c.version); 318 println!("git tag {}-v{}", weight, c.version);
293 } 319 }
294 320
295 println!(""); 321 println!("");
296 println!("# Run these commands to publish the crate and dependents:"); 322 println!("# Run these commands to publish the crate and dependents:");
297 323
298 let mut bfs = Bfs::new(&graph, *start); 324 let mut bfs = Bfs::new(&rgraph, *start);
299 while let Some(node) = bfs.next(&graph) { 325 while let Some(node) = bfs.next(&rgraph) {
300 let weight = graph.node_weight(node).unwrap(); 326 let weight = rgraph.node_weight(node).unwrap();
301 let c = crates.get(weight).unwrap(); 327 let c = ctx.crates.get(weight).unwrap();
302 328
303 let mut args: Vec<String> = vec![ 329 let mut args: Vec<String> = vec![
304 "publish".to_string(), 330 "publish".to_string(),