| 1 | use clap::{Args, Subcommand}; |
| 2 | use xshell::cmd; |
| 3 | |
| 4 | use crate::{NIGHTLY, Result, sh, workspace}; |
| 5 | |
| 6 | #[derive(Args)] |
| 7 | pub struct FixupArgs { |
| 8 | #[clap(subcommand)] |
| 9 | cmd: Option<FixupCommand>, |
| 10 | } |
| 11 | |
| 12 | #[derive(Subcommand)] |
| 13 | enum FixupCommand { |
| 14 | /// Fix style |
| 15 | Style, |
| 16 | /// Fix typos |
| 17 | Typos, |
| 18 | /// Fix clippy lints |
| 19 | Clippy, |
| 20 | } |
| 21 | |
| 22 | impl FixupArgs { |
| 23 | pub fn run(self) -> Result<()> { |
| 24 | let sh = sh(); |
| 25 | let _p = sh.push_dir(workspace::root_path()?); |
| 26 | |
| 27 | match self.cmd { |
| 28 | Some(cmd) => match cmd { |
| 29 | FixupCommand::Style => fix_style(), |
| 30 | FixupCommand::Typos => fix_typos(), |
| 31 | FixupCommand::Clippy => fix_clippy(), |
| 32 | }, |
| 33 | None => { |
| 34 | fix_style()?; |
| 35 | fix_typos()?; |
| 36 | fix_clippy()?; |
| 37 | |
| 38 | Ok(()) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | fn fix_style() -> Result<()> { |
| 45 | let sh = sh(); |
| 46 | cmd!(sh, "rustup run {NIGHTLY} cargo fmt").run()?; |
| 47 | Ok(()) |
| 48 | } |
| 49 | |
| 50 | fn fix_typos() -> Result<()> { |
| 51 | let sh = sh(); |
| 52 | // FIXME: Print install instructions if command-not-found (needs an xshell |
| 53 | // change: https://github.com/matklad/xshell/issues/46) |
| 54 | cmd!(sh, "typos --write-changes").run()?; |
| 55 | Ok(()) |
| 56 | } |
| 57 | |
| 58 | fn fix_clippy() -> Result<()> { |
| 59 | let sh = sh(); |
| 60 | cmd!( |
| 61 | sh, |
| 62 | "rustup run {NIGHTLY} cargo clippy --all-targets |
| 63 | --fix --allow-dirty --allow-staged |
| 64 | -- -D warnings " |
| 65 | ) |
| 66 | .run()?; |
| 67 | cmd!( |
| 68 | sh, |
| 69 | "rustup run {NIGHTLY} cargo clippy --workspace --all-targets |
| 70 | --fix --allow-dirty --allow-staged |
| 71 | --exclude matrix-sdk-crypto --exclude xtask |
| 72 | --no-default-features --features rustls-tls,sso-login |
| 73 | -- -D warnings" |
| 74 | ) |
| 75 | .run()?; |
| 76 | cmd!( |
| 77 | sh, |
| 78 | "rustup run {NIGHTLY} cargo clippy --all-targets -p matrix-sdk-crypto |
| 79 | --allow-dirty --allow-staged --fix |
| 80 | --no-default-features -- -D warnings" |
| 81 | ) |
| 82 | .run()?; |
| 83 | Ok(()) |
| 84 | } |