2024-04-26 15:55:48 +00:00
|
|
|
use std::collections::hash_map::DefaultHasher;
|
2024-07-02 03:50:36 +00:00
|
|
|
use std::path::PathBuf;
|
2024-04-26 15:55:48 +00:00
|
|
|
use std::{hash::Hasher, process::Command};
|
2024-02-24 02:52:21 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// If any TS changes, re-run the build script
|
2024-07-02 03:50:36 +00:00
|
|
|
let watching = std::fs::read_dir("./src/ts").unwrap();
|
|
|
|
let ts_paths: Vec<_> = watching
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
|
|
|
.map(|entry| entry.path())
|
|
|
|
.filter(|path| path.extension().map(|ext| ext == "ts").unwrap_or(false))
|
|
|
|
.collect();
|
|
|
|
for path in &ts_paths {
|
|
|
|
println!("cargo:rerun-if-changed={}", path.display());
|
|
|
|
}
|
2024-02-24 02:52:21 +00:00
|
|
|
|
2024-02-28 00:33:34 +00:00
|
|
|
// Compute the hash of the ts files
|
2024-07-02 03:50:36 +00:00
|
|
|
let hash = hash_ts_files(ts_paths);
|
2024-02-28 00:33:34 +00:00
|
|
|
|
|
|
|
// If the hash matches the one on disk, we're good and don't need to update bindings
|
2024-07-02 03:50:36 +00:00
|
|
|
let fs_hash_string = std::fs::read_to_string("src/js/hash.txt");
|
|
|
|
let expected = fs_hash_string
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| s.trim())
|
|
|
|
.unwrap_or_default();
|
2024-03-07 01:54:20 +00:00
|
|
|
if expected == hash.to_string() {
|
2024-03-07 01:05:03 +00:00
|
|
|
return;
|
2024-02-28 00:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, generate the bindings and write the new hash to disk
|
|
|
|
// Generate the bindings for both native and web
|
2024-03-05 19:16:34 +00:00
|
|
|
gen_bindings("common", "common");
|
|
|
|
gen_bindings("native", "native");
|
|
|
|
gen_bindings("core", "core");
|
2024-04-26 15:55:48 +00:00
|
|
|
gen_bindings("eval", "eval");
|
|
|
|
gen_bindings("native_eval", "native_eval");
|
2024-07-02 03:50:36 +00:00
|
|
|
gen_bindings("hydrate", "hydrate");
|
|
|
|
gen_bindings("initialize_streaming", "initialize_streaming");
|
2024-02-28 00:33:34 +00:00
|
|
|
|
2024-03-07 03:18:41 +00:00
|
|
|
std::fs::write("src/js/hash.txt", hash.to_string()).unwrap();
|
2024-02-28 00:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Hashes the contents of a directory
|
2024-07-02 03:50:36 +00:00
|
|
|
fn hash_ts_files(mut files: Vec<PathBuf>) -> u64 {
|
|
|
|
// Different systems will read the files in different orders, so we sort them to make sure the hash is consistent
|
|
|
|
files.sort();
|
2024-04-26 15:55:48 +00:00
|
|
|
let mut hash = DefaultHasher::new();
|
2024-03-07 01:43:43 +00:00
|
|
|
for file in files {
|
2024-07-02 03:50:36 +00:00
|
|
|
let contents = std::fs::read_to_string(file).unwrap();
|
2024-03-07 03:18:41 +00:00
|
|
|
// windows + git does a weird thing with line endings, so we need to normalize them
|
2024-07-02 03:50:36 +00:00
|
|
|
for line in contents.lines() {
|
2024-04-26 15:55:48 +00:00
|
|
|
hash.write(line.as_bytes());
|
2024-03-07 03:18:41 +00:00
|
|
|
}
|
2024-02-28 00:33:34 +00:00
|
|
|
}
|
2024-04-26 15:55:48 +00:00
|
|
|
hash.finish()
|
2024-02-24 02:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// okay...... so tsc might fail if the user doesn't have it installed
|
|
|
|
// we don't really want to fail if that's the case
|
|
|
|
// but if you started *editing* the .ts files, you're gonna have a bad time
|
|
|
|
// so.....
|
|
|
|
// we need to hash each of the .ts files and add that hash to the JS files
|
|
|
|
// if the hashes don't match, we need to fail the build
|
|
|
|
// that way we also don't need
|
2024-03-02 07:37:46 +00:00
|
|
|
fn gen_bindings(input_name: &str, output_name: &str) {
|
2024-02-24 02:52:21 +00:00
|
|
|
// If the file is generated, and the hash is different, we need to generate it
|
2024-03-02 07:37:46 +00:00
|
|
|
let status = Command::new("bun")
|
|
|
|
.arg("build")
|
|
|
|
.arg(format!("src/ts/{input_name}.ts"))
|
|
|
|
.arg("--outfile")
|
|
|
|
.arg(format!("src/js/{output_name}.js"))
|
2024-03-06 06:38:38 +00:00
|
|
|
.arg("--minify-whitespace")
|
|
|
|
.arg("--minify-syntax")
|
2024-02-24 02:52:21 +00:00
|
|
|
.status()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
if !status.success() {
|
|
|
|
panic!(
|
|
|
|
"Failed to generate bindings for {}. Make sure you have tsc installed",
|
2024-03-02 07:37:46 +00:00
|
|
|
input_name
|
2024-02-24 02:52:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|