xtask: Add format and format-check

This commit is contained in:
Henrik Tjäder 2023-02-24 00:10:01 +01:00
parent 0fac174936
commit 18c843b550
2 changed files with 60 additions and 1 deletions

View file

@ -61,6 +61,11 @@ pub enum CargoCommand<'a> {
target: &'a str,
features: Option<&'a str>,
},
Format {
cargoarg: &'a Option<&'a str>,
package: Vec<String>,
check_only: bool,
},
ExampleSize {
cargoarg: &'a Option<&'a str>,
example: &'a str,
@ -79,8 +84,8 @@ impl<'a> CargoCommand<'a> {
CargoCommand::ExampleBuild { .. } | CargoCommand::Build { .. } => "build",
CargoCommand::ExampleSize { .. } => "size",
CargoCommand::Clippy { .. } => "clippy",
CargoCommand::Format { .. } => "fmt",
// TODO
// CargoCommand::Fmt { .. } => "fmt",
// CargoCommand::Test { .. } => "test",
// CargoCommand::Doc { .. } => "doc",
}
@ -203,6 +208,27 @@ impl<'a> CargoCommand<'a> {
}
args
}
CargoCommand::Format {
cargoarg,
package,
check_only,
} => {
let mut args = vec!["+nightly", self.name()];
if let Some(cargoarg) = cargoarg {
args.extend_from_slice(&[cargoarg]);
}
if !package.is_empty() {
for package in package {
args.extend_from_slice(&["--package", package]);
}
}
if *check_only {
args.extend_from_slice(&["--check"]);
}
args
}
CargoCommand::ExampleBuild {
cargoarg,
example,

View file

@ -136,6 +136,12 @@ enum Commands {
/// Check all packages
Check(Package),
/// Check formatting
FormatCheck(Package),
/// Format code
Format(Package),
/// Run clippy
Clippy(Package),
}
@ -360,6 +366,16 @@ fn main() -> anyhow::Result<()> {
info!("Running clippy on backend: {backend:?}");
cargo_clippy(&cargoarg, &args, backend)?;
}
Commands::FormatCheck(args) => {
info!("Running cargo fmt: {args:?}");
let check_only = true;
cargo_format(&cargoarg, &args, check_only)?;
}
Commands::Format(args) => {
info!("Running cargo fmt --check: {args:?}");
let check_only = false;
cargo_format(&cargoarg, &args, check_only)?;
}
}
Ok(())
@ -421,6 +437,22 @@ fn cargo_clippy(
Ok(())
}
fn cargo_format(
cargoarg: &Option<&str>,
package: &Package,
check_only: bool,
) -> anyhow::Result<()> {
command_parser(
&CargoCommand::Format {
cargoarg,
package: package_filter(package),
check_only,
},
false,
)?;
Ok(())
}
fn run_test(
cargoarg: &Option<&str>,
backend: Backends,
@ -621,6 +653,7 @@ fn command_parser(command: &CargoCommand, overwrite: bool) -> anyhow::Result<()>
| CargoCommand::Build { .. }
| CargoCommand::Check { .. }
| CargoCommand::Clippy { .. }
| CargoCommand::Format { .. }
| CargoCommand::ExampleSize { .. } => {
let cargo_result = run_command(command)?;
if let Some(exit_code) = cargo_result.exit_status.code() {