git.delta.rocks / jrsonnet / refs/commits / ea44e4495023

difftreelog

ci add pre-commit hook

Yaroslav Bolyukin2024-06-18parent: #8d39c73.patch.diff
in: master

5 files changed

modified.envrcdiffbeforeafterboth
--- 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
added.github/hooks/pre-commitdiffbeforeafterboth
--- /dev/null
+++ b/.github/hooks/pre-commit
@@ -0,0 +1 @@
+cargo xtask lint
modifiedflake.nixdiffbeforeafterboth
--- 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
           ];
         };
       }
modifiedxtask/Cargo.tomldiffbeforeafterboth
15quote.workspace = true15quote.workspace = true
16ungrammar.workspace = true16ungrammar.workspace = true
17xshell.workspace = true17xshell.workspace = true
18clap = {workspace = true, features = ["derive"]}
1819
modifiedxtask/src/main.rsdiffbeforeafterboth
--- 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<String>,
+	},
+	/// 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(())
+		}
+	}
 }