From 32f70c7540fdced7727b40bf6e3e89b1835feb0f Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Fri, 24 Mar 2023 09:22:43 -0700 Subject: [PATCH] add check to autoformat --- src/cli/autoformat/mod.rs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/cli/autoformat/mod.rs b/src/cli/autoformat/mod.rs index 825ef8222..b4e8b6f21 100644 --- a/src/cli/autoformat/mod.rs +++ b/src/cli/autoformat/mod.rs @@ -3,9 +3,17 @@ use std::process::exit; use super::*; +// For reference, the rustfmt main.rs file +// https://github.com/rust-lang/rustfmt/blob/master/src/bin/main.rs + /// Build the Rust WASM app and all of its assets. #[derive(Clone, Debug, Parser)] pub struct Autoformat { + /// Run in 'check' mode. Exits with 0 if input is formatted correctly. Exits + /// with 1 and prints a diff if formatting is required. + #[clap(short, long)] + pub check: bool, + /// Input rsx (selection) #[clap(short, long)] pub raw: Option, @@ -20,7 +28,7 @@ impl Autoformat { pub async fn autoformat(self) -> Result<()> { // Default to formatting the project if self.raw.is_none() && self.file.is_none() { - if let Err(e) = autoformat_project().await { + if let Err(e) = autoformat_project(self.check).await { eprintln!("error formatting project: {}", e); exit(1); } @@ -51,7 +59,7 @@ impl Autoformat { /// Runs using Tokio for multithreading, so it should be really really fast /// /// Doesn't do mod-descending, so it will still try to format unreachable files. TODO. -async fn autoformat_project() -> Result<()> { +async fn autoformat_project(check: bool) -> Result<()> { let crate_config = crate::CrateConfig::new()?; let mut files_to_format = vec![]; @@ -59,8 +67,16 @@ async fn autoformat_project() -> Result<()> { let counts = files_to_format .into_iter() - .map(|path| { - tokio::spawn(async move { + .filter(|file| { + if file.components().any(|f| f.as_os_str() == "target") { + return false; + } + + true + }) + .map(|path| async { + let _path = path.clone(); + let res = tokio::spawn(async move { let contents = tokio::fs::read_to_string(&path).await?; let edits = dioxus_autofmt::fmt_file(&contents); @@ -73,6 +89,13 @@ async fn autoformat_project() -> Result<()> { Ok(len) as Result }) + .await; + + if res.is_err() { + eprintln!("error formatting file: {}", _path.display()); + } + + res }) .collect::>() .collect::>() @@ -86,7 +109,10 @@ async fn autoformat_project() -> Result<()> { }) .sum(); - println!("formatted {} blocks of rsx", files_formatted); + if files_formatted > 0 && check { + eprintln!("{} files needed formatting", files_formatted); + exit(1); + } Ok(()) }