Skip to content

Commit

Permalink
Add xtask for (crudely) checking license headers
Browse files Browse the repository at this point in the history
  • Loading branch information
pfmooney committed Jul 21, 2023
1 parent 4145083 commit e625b66
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ header:
file, You can obtain one at https://mozilla.org/MPL/2.0/.
paths:
- '**/*.rs'
paths-ignore:
- 'target/**/*.rs'

comment: on-failure
67 changes: 59 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ edition = "2021"
[dependencies]
anyhow.workspace = true
clap = { workspace = true, features = ["derive"] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
serde_yaml = { version = "0.9" }
glob = { version = "0.3.1" }
globset = { version = "0.4.11" }
71 changes: 9 additions & 62 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// 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, Stdio};

use anyhow::{bail, Result};
use anyhow::Result;
use clap::{Parser, Subcommand};
use serde_json::{Map, Value};

mod task_clippy;
mod task_license;
mod util;

#[derive(Parser)]
#[command(name = "cargo xtask", about = "Builder tasks for Propolis")]
Expand All @@ -23,67 +24,13 @@ enum Cmds {
#[arg(short, long)]
strict: bool,
},
}

fn workspace_root() -> Result<String> {
let mut cmd = Command::new("cargo");
cmd.args(["metadata", "--format-version=1"])
.stdin(Stdio::null())
.stderr(Stdio::inherit());

let output = cmd.output()?;
if !output.status.success() {
bail!("failed to query cargo metadata");
}
let metadata: Map<String, Value> = serde_json::from_slice(&output.stdout)?;

if let Some(Value::String(root)) = metadata.get("workspace_root") {
Ok(root.clone())
} else {
bail!("could not location workspace root")
}
}

fn cmd_clippy(strict: 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 strict {
cmd.args(["--", "-Dwarnings"]);
}

let status = cmd.spawn()?.wait()?;
Ok(!status.success())
};

let mut failed = false;

// Everything in the workspace (including tests, etc)
failed |= run_clippy(&["--workspace", "--all-targets"])?;

// Check the server as it is built for production
failed |=
run_clippy(&["-p", "propolis-server", "--features", "omicron-build"])?;

// Check the Falcon bits
failed |= run_clippy(&["-p", "propolis-server", "--features", "falcon"])?;

// Check the mock server
failed |=
run_clippy(&["-p", "propolis-server", "--features", "mock-only"])?;

if failed {
bail!("Clippy failures detected")
}

Ok(())
/// (Crudely) Check for appropriate license headers
License,
}

fn main() -> Result<()> {
match Args::parse().cmd {
Cmds::Clippy { strict } => cmd_clippy(strict),
Cmds::Clippy { strict } => task_clippy::cmd_clippy(strict),
Cmds::License => task_license::cmd_license(),
}
}
47 changes: 47 additions & 0 deletions xtask/src/task_clippy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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_clippy(strict: 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 strict {
cmd.args(["--", "-Dwarnings"]);
}

let status = cmd.spawn()?.wait()?;
Ok(!status.success())
};

let mut failed = false;

// Everything in the workspace (including tests, etc)
failed |= run_clippy(&["--workspace", "--all-targets"])?;

// Check the server as it is built for production
failed |=
run_clippy(&["-p", "propolis-server", "--features", "omicron-build"])?;

// Check the Falcon bits
failed |= run_clippy(&["-p", "propolis-server", "--features", "falcon"])?;

// Check the mock server
failed |=
run_clippy(&["-p", "propolis-server", "--features", "mock-only"])?;

if failed {
bail!("Clippy failures detected")
}

Ok(())
}
Loading

0 comments on commit e625b66

Please sign in to comment.