Skip to content

Commit 118f407

Browse files
committed
ci: add pre-commit hook
Fixes: #123
1 parent 48c2404 commit 118f407

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

.envrc

+7
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1+
RED='\033[0;31m'
2+
RESET='\033[0m'
3+
14
use flake
5+
6+
if ! diff .github/hooks/pre-commit .git/hooks/pre-commit >/dev/null; then
7+
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}"
8+
fi

.github/hooks/pre-commit

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cargo xtask lint

flake.nix

+4-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
};
117117
};
118118
devShells.default = craneLib.devShell {
119-
nativeBuildInputs = with pkgs; [
119+
packages = with pkgs; [
120120
alejandra
121121
cargo-edit
122122
cargo-asm
@@ -126,6 +126,9 @@
126126
lld
127127
hyperfine
128128
graphviz
129+
] ++ lib.optionals (!stdenv.isDarwin) [
130+
valgrind
131+
kcachegrind
129132
];
130133
};
131134
}

xtask/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ proc-macro2.workspace = true
1515
quote.workspace = true
1616
ungrammar.workspace = true
1717
xshell.workspace = true
18+
clap = {workspace = true, features = ["derive"]}

xtask/src/main.rs

+71-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,77 @@
11
use anyhow::Result;
2+
use clap::Parser;
3+
use xshell::{cmd, Shell};
24

35
mod sourcegen;
46

7+
#[derive(Parser)]
8+
enum Opts {
9+
/// Generate files for rowan parser
10+
Sourcegen,
11+
/// Profile file execution
12+
Profile {
13+
#[arg(long, default_value = "true")]
14+
hyperfine: bool,
15+
#[arg(long)]
16+
callgrind: bool,
17+
#[arg(long)]
18+
cachegrind: bool,
19+
#[arg(long, default_value = "x86_64-unknown-linux-gnu")]
20+
target: String,
21+
args: Vec<String>,
22+
},
23+
/// Run all lints enforced by this repo
24+
Lint {
25+
/// Also fix found issues when possible.
26+
#[arg(long)]
27+
fix: bool,
28+
},
29+
}
30+
531
fn main() -> Result<()> {
6-
sourcegen::generate_ungrammar()
32+
let sh = Shell::new()?;
33+
match Opts::parse() {
34+
Opts::Sourcegen => sourcegen::generate_ungrammar(),
35+
Opts::Profile {
36+
hyperfine,
37+
callgrind,
38+
cachegrind,
39+
args,
40+
target,
41+
} => {
42+
let out = sh.create_temp_dir()?;
43+
44+
// build-std
45+
cmd!(
46+
sh,
47+
"cargo build -Zbuild-std --target={target} --profile releasedebug"
48+
)
49+
.run()?;
50+
let built = format!("./target/{target}/releasedebug/jrsonnet");
51+
let bench_cmd = format!("{built} {}", args.join(" "));
52+
if hyperfine {
53+
cmd!(sh, "hyperfine {bench_cmd}").run()?;
54+
}
55+
if callgrind {
56+
let args = args.clone();
57+
let mut callgrind_out = out.path().to_owned();
58+
callgrind_out.push("callgrind.out.1");
59+
cmd!(sh, "valgrind --tool=callgrind --dump-instr=yes --collect-jumps=yes --callgrind-out-file={callgrind_out} {built} {args...}").run()?;
60+
cmd!(sh, "kcachegrind {callgrind_out}").run()?;
61+
}
62+
if cachegrind {
63+
let mut cachegrind_out = out.path().to_owned();
64+
cachegrind_out.push("cachegrind.out.1");
65+
cmd!(sh, "valgrind --tool=cachegrind --cachegrind-out-file={cachegrind_out} {built} {args...}").run()?;
66+
cmd!(sh, "kcachegrind {cachegrind_out}").run()?;
67+
}
68+
69+
Ok(())
70+
}
71+
Opts::Lint { fix } => {
72+
let fmt_check = if fix { None } else { Some("--check") };
73+
cmd!(sh, "cargo fmt {fmt_check...}").run()?;
74+
Ok(())
75+
}
76+
}
777
}

0 commit comments

Comments
 (0)