From ea44e44950238f26e2d32b0531b91faa49d2f76d Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Mon, 27 May 2024 22:05:04 +0000 Subject: [PATCH] ci: add pre-commit hook --- --- a/.envrc +++ b/.envrc @@ -1 +1,8 @@ +RED='\033[0;31m' +RESET='\033[0m' + use flake + +if ! diff .github/hooks/pre-commit .git/hooks/pre-commit >/dev/null; then +echo -e "${RED}Hooks are updated, read .github/hooks/pre-commit, and then install it with cp .github/hooks/pre-commit .git/hooks/pre-commit${RESET}" +fi --- /dev/null +++ b/.github/hooks/pre-commit @@ -0,0 +1 @@ +cargo xtask lint --- a/flake.nix +++ b/flake.nix @@ -116,7 +116,7 @@ }; }; devShells.default = craneLib.devShell { - nativeBuildInputs = with pkgs; [ + packages = with pkgs; [ alejandra cargo-edit cargo-asm @@ -126,6 +126,9 @@ lld hyperfine graphviz + ] ++ lib.optionals (!stdenv.isDarwin) [ + valgrind + kcachegrind ]; }; } --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -15,3 +15,4 @@ quote.workspace = true ungrammar.workspace = true xshell.workspace = true +clap = {workspace = true, features = ["derive"]} --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,7 +1,77 @@ use anyhow::Result; +use clap::Parser; +use xshell::{cmd, Shell}; mod sourcegen; +#[derive(Parser)] +enum Opts { + /// Generate files for rowan parser + Sourcegen, + /// Profile file execution + Profile { + #[arg(long, default_value = "true")] + hyperfine: bool, + #[arg(long)] + callgrind: bool, + #[arg(long)] + cachegrind: bool, + #[arg(long, default_value = "x86_64-unknown-linux-gnu")] + target: String, + args: Vec, + }, + /// Run all lints enforced by this repo + Lint { + /// Also fix found issues when possible. + #[arg(long)] + fix: bool, + }, +} + fn main() -> Result<()> { - sourcegen::generate_ungrammar() + let sh = Shell::new()?; + match Opts::parse() { + Opts::Sourcegen => sourcegen::generate_ungrammar(), + Opts::Profile { + hyperfine, + callgrind, + cachegrind, + args, + target, + } => { + let out = sh.create_temp_dir()?; + + // build-std + cmd!( + sh, + "cargo build -Zbuild-std --target={target} --profile releasedebug" + ) + .run()?; + let built = format!("./target/{target}/releasedebug/jrsonnet"); + let bench_cmd = format!("{built} {}", args.join(" ")); + if hyperfine { + cmd!(sh, "hyperfine {bench_cmd}").run()?; + } + if callgrind { + let args = args.clone(); + let mut callgrind_out = out.path().to_owned(); + callgrind_out.push("callgrind.out.1"); + cmd!(sh, "valgrind --tool=callgrind --dump-instr=yes --collect-jumps=yes --callgrind-out-file={callgrind_out} {built} {args...}").run()?; + cmd!(sh, "kcachegrind {callgrind_out}").run()?; + } + if cachegrind { + let mut cachegrind_out = out.path().to_owned(); + cachegrind_out.push("cachegrind.out.1"); + cmd!(sh, "valgrind --tool=cachegrind --cachegrind-out-file={cachegrind_out} {built} {args...}").run()?; + cmd!(sh, "kcachegrind {cachegrind_out}").run()?; + } + + Ok(()) + } + Opts::Lint { fix } => { + let fmt_check = if fix { None } else { Some("--check") }; + cmd!(sh, "cargo fmt {fmt_check...}").run()?; + Ok(()) + } + } } -- gitstuff