1
0

build.rs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. use std::process::Command;
  2. fn main() {
  3. // If any TS changes, re-run the build script
  4. println!("cargo:rerun-if-changed=src/ts/*.ts");
  5. // Compute the hash of the ts files
  6. let hash = hash_ts_files();
  7. // If the hash matches the one on disk, we're good and don't need to update bindings
  8. let expected = include_str!("src/js/hash.txt").trim();
  9. if expected == hash.to_string() {
  10. return;
  11. }
  12. // Otherwise, generate the bindings and write the new hash to disk
  13. // Generate the bindings for both native and web
  14. gen_bindings("common", "common");
  15. gen_bindings("native", "native");
  16. gen_bindings("core", "core");
  17. std::fs::write("src/js/hash.txt", hash.to_string()).unwrap();
  18. }
  19. /// Hashes the contents of a directory
  20. fn hash_ts_files() -> u128 {
  21. let mut out = 0;
  22. let files = [
  23. include_str!("src/ts/common.ts"),
  24. include_str!("src/ts/native.ts"),
  25. include_str!("src/ts/core.ts"),
  26. ];
  27. // Let's make the dumbest hasher by summing the bytes of the files
  28. // The location is multiplied by the byte value to make sure that the order of the bytes matters
  29. let mut idx = 0;
  30. for file in files {
  31. // windows + git does a weird thing with line endings, so we need to normalize them
  32. for line in file.lines() {
  33. idx += 1;
  34. for byte in line.bytes() {
  35. idx += 1;
  36. out += (byte as u128) * (idx as u128);
  37. }
  38. }
  39. }
  40. out
  41. }
  42. // okay...... so tsc might fail if the user doesn't have it installed
  43. // we don't really want to fail if that's the case
  44. // but if you started *editing* the .ts files, you're gonna have a bad time
  45. // so.....
  46. // we need to hash each of the .ts files and add that hash to the JS files
  47. // if the hashes don't match, we need to fail the build
  48. // that way we also don't need
  49. fn gen_bindings(input_name: &str, output_name: &str) {
  50. // If the file is generated, and the hash is different, we need to generate it
  51. let status = Command::new("bun")
  52. .arg("build")
  53. .arg(format!("src/ts/{input_name}.ts"))
  54. .arg("--outfile")
  55. .arg(format!("src/js/{output_name}.js"))
  56. .arg("--minify-whitespace")
  57. .arg("--minify-syntax")
  58. .status()
  59. .unwrap();
  60. if !status.success() {
  61. panic!(
  62. "Failed to generate bindings for {}. Make sure you have tsc installed",
  63. input_name
  64. );
  65. }
  66. }