diff --git a/xtask/src/fixup.rs b/xtask/src/fixup.rs new file mode 100644 index 000000000..6b4fee15a --- /dev/null +++ b/xtask/src/fixup.rs @@ -0,0 +1,91 @@ +use std::{env, path::PathBuf}; + +use clap::{Args, Subcommand}; +use serde::Deserialize; +use xshell::{cmd, pushd}; + +use crate::Result; + +#[derive(Args)] +pub struct FixupArgs { + #[clap(subcommand)] + cmd: Option, +} + +#[derive(Subcommand)] +enum FixupCommand { + /// Check style + Style, + /// Check for typos + Typos, + /// Check clippy lints + Clippy, +} + +impl FixupArgs { + pub fn run(self) -> Result<()> { + let _p = pushd(&workspace_root()?)?; + + match self.cmd { + Some(cmd) => match cmd { + FixupCommand::Style => fix_style(), + FixupCommand::Typos => fix_typos(), + FixupCommand::Clippy => fix_clippy(), + }, + None => { + fix_style()?; + fix_typos()?; + fix_clippy()?; + + Ok(()) + } + } + } +} + +fn fix_style() -> Result<()> { + cmd!("rustup run nightly cargo fmt").run()?; + Ok(()) +} + +fn fix_typos() -> Result<()> { + // FIXME: Print install instructions if command-not-found (needs an xshell + // change: https://github.com/matklad/xshell/issues/46) + cmd!("typos --write-changes").run()?; + Ok(()) +} + +fn fix_clippy() -> Result<()> { + cmd!( + "rustup run nightly cargo clippy --all-targets + --fix --allow-dirty --allow-staged + -- -D warnings " + ) + .run()?; + cmd!( + "rustup run nightly cargo clippy --workspace --all-targets + --fix --allow-dirty --allow-staged + --exclude matrix-sdk-crypto --exclude xtask + --no-default-features --features native-tls,warp + -- -D warnings" + ) + .run()?; + cmd!( + "rustup run nightly cargo clippy --all-targets -p matrix-sdk-crypto + --allow-dirty --allow-staged --fix + --no-default-features -- -D warnings" + ) + .run()?; + Ok(()) +} + +fn workspace_root() -> Result { + #[derive(Deserialize)] + struct Metadata { + workspace_root: PathBuf, + } + + let cargo = env::var("CARGO").unwrap_or_else(|_| "cargo".to_owned()); + let metadata_json = cmd!("{cargo} metadata --no-deps --format-version 1").read()?; + Ok(serde_json::from_str::(&metadata_json)?.workspace_root) +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index a8745c9ac..8b6035233 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,7 +1,9 @@ mod ci; +mod fixup; use ci::CiArgs; use clap::{Parser, Subcommand}; +use fixup::FixupArgs; use xshell::cmd; type Result> = std::result::Result; @@ -16,6 +18,8 @@ struct Xtask { enum Command { /// Run continuous integration checks Ci(CiArgs), + /// Fix up automatic checks + Fixup(FixupArgs), /// Build the SDKs documentation Doc { /// Opens the docs in a browser after the operation @@ -27,6 +31,7 @@ enum Command { fn main() -> Result<()> { match Xtask::parse().cmd { Command::Ci(ci) => ci.run(), + Command::Fixup(cfg) => cfg.run(), Command::Doc { open } => build_docs(open.then(|| "--open"), DenyWarnings::No), } }