Skip to content

Commit

Permalink
Add xtask for pre-push checks
Browse files Browse the repository at this point in the history
  • Loading branch information
pfmooney committed Dec 4, 2023
1 parent 66b4303 commit 244a6ef
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
28 changes: 26 additions & 2 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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(())
}
}
}
7 changes: 5 additions & 2 deletions xtask/src/task_clippy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> {
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"]);
}
Expand Down Expand Up @@ -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(())
Expand Down
22 changes: 22 additions & 0 deletions xtask/src/task_fmt.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
1 change: 0 additions & 1 deletion xtask/src/task_license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,5 @@ pub(crate) fn cmd_license() -> Result<()> {
bail!("License errors detected")
}

println!("License checks happy!");
Ok(())
}
25 changes: 25 additions & 0 deletions xtask/src/task_prepush.rs
Original file line number Diff line number Diff line change
@@ -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(())
}

0 comments on commit 244a6ef

Please sign in to comment.