Allow dry run of publish

This commit is contained in:
Gwilym Kuiper 2022-08-04 21:36:24 +01:00
parent 375c878849
commit 4277f07ec0
2 changed files with 32 additions and 10 deletions

View file

@ -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`"),
};

View file

@ -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::<bool>("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)?;