Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run cargo fmt after code generation #993

Merged
merged 1 commit into from
Aug 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion services/autorust/azure-autorust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use autorust_codegen::{
crates::{list_crate_names, list_dirs},
gen, get_mgmt_readmes, get_svc_readmes,
jinja::{CargoToml, CheckAllServicesYml, PublishSdksYml, PublishServicesYml},
Result, RunConfig,
Error, ErrorKind, Result, RunConfig,
};
use clap::Parser;

Expand All @@ -18,6 +18,10 @@ struct Args {
/// Specify specific package to generate. Multiple accepted.
#[clap(long = "package", short = 'p')]
package: Vec<String>,

/// Run `cargo fmt` after generating the code
#[clap(long, default_value = "true", action = clap::ArgAction::Set)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks to clap-rs/clap#1649.

fmt: bool,
}

impl Args {
Expand All @@ -38,6 +42,9 @@ fn main() -> Result<()> {
gen_workflow_publish_services()?;
}
}
if args.fmt {
fmt(&args.packages())?;
}
Ok(())
}

Expand Down Expand Up @@ -122,3 +129,22 @@ fn gen_workflow_publish_services() -> Result<()> {
yml.create("../../.github/workflows/publish-services.yml")?;
Ok(())
}

/// Run `cargo fmt` on the services workspace or a subset of packages.
fn fmt(only_packages: &Vec<&str>) -> Result<()> {
let services_dir = "../";
let mut args = vec!["fmt"];
if !only_packages.is_empty() {
args.push("-p");
for package in only_packages {
args.push(package);
}
}
let out = std::process::Command::new("cargo").current_dir(services_dir).args(args).output()?;
if !out.status.success() {
println!("cargo fmt failed");
println!("{}", std::str::from_utf8(&out.stderr)?);
return Err(Error::new(ErrorKind::Io, "cargo fmt failed"));
}
Ok(())
}