From 18c843b5509b76a661dbf141da7cb900401f5074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Tj=C3=A4der?= Date: Fri, 24 Feb 2023 00:10:01 +0100 Subject: [PATCH] xtask: Add format and format-check --- xtask/src/command.rs | 28 +++++++++++++++++++++++++++- xtask/src/main.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/xtask/src/command.rs b/xtask/src/command.rs index d894fae282..b1f885c141 100644 --- a/xtask/src/command.rs +++ b/xtask/src/command.rs @@ -61,6 +61,11 @@ pub enum CargoCommand<'a> { target: &'a str, features: Option<&'a str>, }, + Format { + cargoarg: &'a Option<&'a str>, + package: Vec, + 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, diff --git a/xtask/src/main.rs b/xtask/src/main.rs index f6d897529d..2191384049 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -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() {