forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
134 lines (116 loc) · 4.25 KB
/
Copy pathbuild.rs
File metadata and controls
134 lines (116 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! Build script for fe-web: rebuilds tree-sitter-fe.wasm when grammar inputs change.
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
const HASH_PREFIX: &str = "fnv1a64-relpath-v1";
const FNV1A64_OFFSET_BASIS: u64 = 0xcbf29ce484222325;
const FNV1A64_PRIME: u64 = 0x100000001b3;
fn main() {
let grammar_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../tree-sitter-fe");
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let vendor_wasm = manifest_dir.join("vendor/tree-sitter-fe.wasm");
let stamp_file = manifest_dir.join("vendor/tree-sitter-fe.wasm.inputs");
let inputs = tree_sitter_inputs(&grammar_dir);
for (_, input) in &inputs {
println!("cargo:rerun-if-changed={}", input.display());
}
println!("cargo:rerun-if-changed={}", vendor_wasm.display());
println!("cargo:rerun-if-changed={}", stamp_file.display());
if !inputs.iter().all(|(_, input)| input.exists()) {
if vendor_wasm.exists() {
println!(
"cargo:warning=tree-sitter grammar inputs are unavailable; using vendored {}",
vendor_wasm.display()
);
return;
}
panic!(
"tree-sitter grammar inputs are unavailable and {} does not exist",
vendor_wasm.display()
);
}
let current_hash = input_hash(&inputs);
let expected_stamp = format!("{HASH_PREFIX}:{current_hash:016x}");
if vendor_wasm.exists() && stamp_matches(&stamp_file, &expected_stamp) {
return;
}
rebuild_tree_sitter_wasm(&grammar_dir, &vendor_wasm);
fs::write(&stamp_file, format!("{expected_stamp}\n"))
.unwrap_or_else(|err| panic!("failed to write {}: {err}", stamp_file.display()));
}
fn stamp_matches(stamp_file: &Path, expected_stamp: &str) -> bool {
fs::read_to_string(stamp_file)
.map(|stamp| stamp.trim_end_matches(['\r', '\n']) == expected_stamp)
.unwrap_or(false)
}
fn tree_sitter_inputs(grammar_dir: &Path) -> Vec<(&'static str, PathBuf)> {
[
"grammar.js",
"src/scanner.c",
"tree-sitter.json",
"package.json",
"package-lock.json",
]
.into_iter()
.map(|path| (path, grammar_dir.join(path)))
.collect()
}
fn input_hash(inputs: &[(&str, PathBuf)]) -> u64 {
let mut hash = FNV1A64_OFFSET_BASIS;
for (relative_path, input) in inputs {
hash_bytes(&mut hash, relative_path.as_bytes());
hash_bytes(&mut hash, &[0]);
let contents = fs::read(input)
.unwrap_or_else(|err| panic!("failed to read {}: {err}", input.display()));
hash_text_bytes(&mut hash, &contents);
hash_bytes(&mut hash, &[0xff]);
}
hash
}
fn hash_text_bytes(hash: &mut u64, bytes: &[u8]) {
let mut bytes = bytes.iter().copied().peekable();
while let Some(byte) = bytes.next() {
if byte == b'\r' && bytes.peek() == Some(&b'\n') {
continue;
}
hash_byte(hash, byte);
}
}
fn hash_bytes(hash: &mut u64, bytes: &[u8]) {
for byte in bytes {
hash_byte(hash, *byte);
}
}
fn hash_byte(hash: &mut u64, byte: u8) {
*hash ^= u64::from(byte);
*hash = hash.wrapping_mul(FNV1A64_PRIME);
}
fn rebuild_tree_sitter_wasm(grammar_dir: &Path, vendor_wasm: &Path) {
let tree_sitter = grammar_dir.join("node_modules/.bin/tree-sitter");
if !tree_sitter.exists() {
panic!(
"{} is stale, but {} does not exist. Run `npm ci` in {} to install the pinned tree-sitter CLI.",
vendor_wasm.display(),
tree_sitter.display(),
grammar_dir.display()
);
}
let output_wasm = grammar_dir.join("tree-sitter-fe.wasm");
let output = Command::new(&tree_sitter)
.arg("build")
.arg("--wasm")
.current_dir(grammar_dir)
.output()
.unwrap_or_else(|err| panic!("failed to run {}: {err}", tree_sitter.display()));
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
panic!("{} build --wasm failed:\n{stderr}", tree_sitter.display());
}
fs::copy(&output_wasm, vendor_wasm).unwrap_or_else(|err| {
panic!(
"failed to copy {} to {}: {err}",
output_wasm.display(),
vendor_wasm.display()
)
});
}