|
| 1 | +use std::process::Command; |
| 2 | + |
| 3 | +use anyhow::{bail, Context, Result}; |
| 4 | +use clap::{Parser, Subcommand}; |
| 5 | + |
| 6 | +#[derive(Parser)] |
| 7 | +#[command(name = "cargo xtask")] |
| 8 | +struct Args { |
| 9 | + #[command(subcommand)] |
| 10 | + command: CliCommand, |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(Subcommand)] |
| 14 | +enum CliCommand { |
| 15 | + /// Runs `cargo clippy`. |
| 16 | + Clippy(ClippyArgs), |
| 17 | +} |
| 18 | + |
| 19 | +fn main() -> Result<()> { |
| 20 | + let args = Args::parse(); |
| 21 | + |
| 22 | + match args.command { |
| 23 | + CliCommand::Clippy(args) => run_clippy(args), |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Parser)] |
| 28 | +struct ClippyArgs { |
| 29 | + /// Whether to deny warnings (`clippy --deny warnings`). |
| 30 | + #[arg(long)] |
| 31 | + deny_warnings: bool, |
| 32 | +} |
| 33 | + |
| 34 | +fn run_clippy(args: ClippyArgs) -> Result<()> { |
| 35 | + let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string()); |
| 36 | + |
| 37 | + let mut clippy_command = Command::new(&cargo); |
| 38 | + clippy_command |
| 39 | + .arg("clippy") |
| 40 | + .arg("--workspace") |
| 41 | + .arg("--release") |
| 42 | + .arg("--all-targets") |
| 43 | + .arg("--all-features"); |
| 44 | + |
| 45 | + clippy_command.arg("--"); |
| 46 | + |
| 47 | + if args.deny_warnings { |
| 48 | + clippy_command.args(["--deny", "warnings"]); |
| 49 | + } |
| 50 | + |
| 51 | + // Allow all Clippy lints by default, as we have a lot of violations at the moment. |
| 52 | + // We can tighten things up once we have a better handle on them. |
| 53 | + clippy_command.args(["--allow", "clippy::all"]); |
| 54 | + |
| 55 | + // Deny `dbg!` and `todo!`s. |
| 56 | + clippy_command |
| 57 | + .args(["--deny", "clippy::dbg_macro"]) |
| 58 | + .args(["--deny", "clippy::todo"]); |
| 59 | + |
| 60 | + eprintln!( |
| 61 | + "running: {cargo} {}", |
| 62 | + clippy_command |
| 63 | + .get_args() |
| 64 | + .map(|arg| format!("{}", arg.to_str().unwrap())) |
| 65 | + .collect::<Vec<_>>() |
| 66 | + .join(" ") |
| 67 | + ); |
| 68 | + |
| 69 | + let exit_status = clippy_command |
| 70 | + .spawn() |
| 71 | + .context("failed to spawn child process")? |
| 72 | + .wait() |
| 73 | + .context("failed to wait for child process")?; |
| 74 | + |
| 75 | + if !exit_status.success() { |
| 76 | + bail!("clippy failed: {}", exit_status); |
| 77 | + } |
| 78 | + |
| 79 | + Ok(()) |
| 80 | +} |
0 commit comments