From 4277f07ec0e5e5d1fc8dbefb884a71fe92289597 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Thu, 4 Aug 2022 21:36:24 +0100 Subject: [PATCH] Allow dry run of publish --- tools/src/main.rs | 4 ++-- tools/src/publish.rs | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/tools/src/main.rs b/tools/src/main.rs index 0ed92e17..aba2046c 100644 --- a/tools/src/main.rs +++ b/tools/src/main.rs @@ -6,11 +6,11 @@ fn main() { let matches = Command::new("Agb tools") .subcommand_required(true) .arg_required_else_help(true) - .subcommand(clap::Command::new("publish").about("Publishes agb and all subcrates")) + .subcommand(publish::command()) .get_matches(); let result = match matches.subcommand() { - Some(("publish", _)) => publish::publish(), + Some(("publish", arg_matches)) => publish::publish(arg_matches), _ => unreachable!("Exhausted list of subcommands and subcommand_required prevents `None`"), }; diff --git a/tools/src/publish.rs b/tools/src/publish.rs index 6911ad75..ada644d5 100644 --- a/tools/src/publish.rs +++ b/tools/src/publish.rs @@ -1,3 +1,4 @@ +use clap::{Arg, ArgAction, ArgMatches}; use std::fs; use std::path::{Path, PathBuf}; use std::process::Command; @@ -21,19 +22,40 @@ pub enum Error { CrateVersion, } -pub fn publish() -> Result<(), Error> { +pub fn command() -> clap::Command<'static> { + clap::Command::new("publish") + .about("Publishes agb and all subcrates") + .arg( + Arg::new("Dry run") + .long("dry-run") + .help("Don't actually publish") + .action(ArgAction::SetTrue), + ) +} + +pub fn publish(matches: &ArgMatches) -> Result<(), Error> { + let dry_run = matches.get_one::("Dry run").expect("defined by clap"); + let root_directory = find_agb_root_directory()?; for crate_to_publish in CRATES_TO_PUBLISH.iter() { let crate_dir = root_directory.join(crate_to_publish); - let publish_result = Command::new("cargo") - .arg("publish") - .current_dir(&crate_dir) - .spawn(); - if let Err(err) = publish_result { - println!("Error while publishing crate {crate_to_publish}: {err}"); - return Err(Error::PublishCrate); + if *dry_run { + println!( + "Would run `cargo publish` in {}", + crate_dir.to_string_lossy() + ); + } else { + let publish_result = Command::new("cargo") + .arg("publish") + .current_dir(&crate_dir) + .spawn(); + + if let Err(err) = publish_result { + println!("Error while publishing crate {crate_to_publish}: {err}"); + return Err(Error::PublishCrate); + } } let expected_version = read_cargo_toml_version(&crate_dir)?;