diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 0cab1d3a4..5aafdb7c0 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -6,7 +6,9 @@ use anyhow::Result; use clap::{Parser, Subcommand}; mod task_clippy; +mod task_fmt; mod task_license; +mod task_prepush; mod util; #[derive(Parser)] @@ -23,14 +25,36 @@ enum Cmds { /// Treat warnings as errors #[arg(short, long)] strict: bool, + + /// Suppress non-essential output + #[arg(short, long)] + quiet: bool, }, + /// Check style according to `rustfmt` + Fmt, /// (Crudely) Check for appropriate license headers License, + /// Preform pre-push checks (clippy, license, fmt, etc) + Prepush, } fn main() -> Result<()> { match Args::parse().cmd { - Cmds::Clippy { strict } => task_clippy::cmd_clippy(strict), - Cmds::License => task_license::cmd_license(), + Cmds::Clippy { strict, quiet } => { + task_clippy::cmd_clippy(strict, quiet) + } + Cmds::Fmt => task_fmt::cmd_fmt(), + Cmds::License => { + task_license::cmd_license()?; + + println!("License checks pass"); + Ok(()) + } + Cmds::Prepush => { + task_prepush::cmd_prepush()?; + + println!("Pre-push checks pass"); + Ok(()) + } } } diff --git a/xtask/src/task_clippy.rs b/xtask/src/task_clippy.rs index 19bb6bd5d..0e467792c 100644 --- a/xtask/src/task_clippy.rs +++ b/xtask/src/task_clippy.rs @@ -8,13 +8,16 @@ use anyhow::{bail, Result}; use crate::util::*; -pub(crate) fn cmd_clippy(strict: bool) -> Result<()> { +pub(crate) fn cmd_clippy(strict: bool, quiet: bool) -> Result<()> { let wroot = workspace_root()?; let run_clippy = |args: &[&str]| -> Result { let mut cmd = Command::new("cargo"); cmd.arg("clippy").arg("--no-deps").args(args).current_dir(&wroot); + if quiet { + cmd.arg("--quiet"); + } if strict { cmd.args(["--", "-Dwarnings"]); } @@ -53,7 +56,7 @@ pub(crate) fn cmd_clippy(strict: bool) -> Result<()> { failed |= run_clippy(&["-p", "phd-runner"])?; if failed { - bail!("Clippy failures detected") + bail!("Clippy failure(s) detected") } Ok(()) diff --git a/xtask/src/task_fmt.rs b/xtask/src/task_fmt.rs new file mode 100644 index 000000000..2094e46ea --- /dev/null +++ b/xtask/src/task_fmt.rs @@ -0,0 +1,22 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. + +use std::process::Command; + +use anyhow::{bail, Result}; + +use crate::util::*; + +pub(crate) fn cmd_fmt() -> Result<()> { + let wroot = workspace_root()?; + + let mut cmd = Command::new("cargo"); + cmd.arg("fmt").arg("--check").current_dir(&wroot); + + if !cmd.spawn()?.wait()?.success() { + bail!("rustfmt failure(s) detected") + } + + Ok(()) +} diff --git a/xtask/src/task_license.rs b/xtask/src/task_license.rs index e030048a8..4385886de 100644 --- a/xtask/src/task_license.rs +++ b/xtask/src/task_license.rs @@ -109,6 +109,5 @@ pub(crate) fn cmd_license() -> Result<()> { bail!("License errors detected") } - println!("License checks happy!"); Ok(()) } diff --git a/xtask/src/task_prepush.rs b/xtask/src/task_prepush.rs new file mode 100644 index 000000000..2a981e7d4 --- /dev/null +++ b/xtask/src/task_prepush.rs @@ -0,0 +1,25 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. + +use anyhow::{bail, Result}; + +use crate::{task_clippy, task_fmt, task_license}; + +pub(crate) fn cmd_prepush() -> Result<()> { + let mut errs = Vec::new(); + if task_clippy::cmd_clippy(true, true).is_err() { + errs.push("clippy"); + } + if task_fmt::cmd_fmt().is_err() { + errs.push("fmt"); + } + if task_license::cmd_license().is_err() { + errs.push("license"); + } + + if !errs.is_empty() { + bail!("Pre-push error(s) in: {}", errs.join(", ")) + } + Ok(()) +}