Skip to content

Commit 0e9fd03

Browse files
authored
run cargo fmt after code generation (#993)
1 parent 4c36f55 commit 0e9fd03

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

  • services/autorust/azure-autorust/src

services/autorust/azure-autorust/src/main.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use autorust_codegen::{
55
crates::{list_crate_names, list_dirs},
66
gen, get_mgmt_readmes, get_svc_readmes,
77
jinja::{CargoToml, CheckAllServicesYml, PublishSdksYml, PublishServicesYml},
8-
Result, RunConfig,
8+
Error, ErrorKind, Result, RunConfig,
99
};
1010
use clap::Parser;
1111

@@ -18,6 +18,10 @@ struct Args {
1818
/// Specify specific package to generate. Multiple accepted.
1919
#[clap(long = "package", short = 'p')]
2020
package: Vec<String>,
21+
22+
/// Run `cargo fmt` after generating the code
23+
#[clap(long, default_value = "true", action = clap::ArgAction::Set)]
24+
fmt: bool,
2125
}
2226

2327
impl Args {
@@ -38,6 +42,9 @@ fn main() -> Result<()> {
3842
gen_workflow_publish_services()?;
3943
}
4044
}
45+
if args.fmt {
46+
fmt(&args.packages())?;
47+
}
4148
Ok(())
4249
}
4350

@@ -122,3 +129,22 @@ fn gen_workflow_publish_services() -> Result<()> {
122129
yml.create("../../.github/workflows/publish-services.yml")?;
123130
Ok(())
124131
}
132+
133+
/// Run `cargo fmt` on the services workspace or a subset of packages.
134+
fn fmt(only_packages: &Vec<&str>) -> Result<()> {
135+
let services_dir = "../";
136+
let mut args = vec!["fmt"];
137+
if !only_packages.is_empty() {
138+
args.push("-p");
139+
for package in only_packages {
140+
args.push(package);
141+
}
142+
}
143+
let out = std::process::Command::new("cargo").current_dir(services_dir).args(args).output()?;
144+
if !out.status.success() {
145+
println!("cargo fmt failed");
146+
println!("{}", std::str::from_utf8(&out.stderr)?);
147+
return Err(Error::new(ErrorKind::Io, "cargo fmt failed"));
148+
}
149+
Ok(())
150+
}

0 commit comments

Comments
 (0)