-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
73 lines (61 loc) · 2.31 KB
/
build.rs
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
use std::{
process::Command,
env,
path::Path,
fs,
io::ErrorKind,
};
use regex::Regex;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=external/junk/index.js");
println!("cargo:rerun-if-changed=external/junk/package.json");
check_node_command();
let js_code = r#"
import { junkRegex } from './external/junk/index.js';
console.log(junkRegex.toString().replace(/^\/|\/$/g, ''));
"#;
let output = Command::new("node")
.arg("--input-type=module")
.arg("-e")
.arg(js_code)
.output()
.expect("Failed to execute node.js");
if !output.status.success() {
eprintln!("Node.js execution failed with error: {}", String::from_utf8_lossy(&output.stderr));
panic!("Node.js command execution failed.");
}
let regex_string = String::from_utf8(output.stdout).expect("Failed to convert Node.js output to string");
let regex_string = regex_string.trim_end();
if let Err(err) = Regex::new(regex_string) {
eprintln!("Regex parsing from the Node.js output failed with error: {}", err);
panic!("Regex parsing failed.");
}
let out_dir = env::var("OUT_DIR").expect("Failed to get the OUT_DIR environment variable");
let dest_path = Path::new(&out_dir).join("generated.rs");
fs::write(&dest_path, format!("pub static JUNK_REGEX_STR: &str = \"{}\";", escape_string_for_rust_string(regex_string)))
.expect("Could not write to the generated.rs file");
}
fn check_node_command() {
match Command::new("node").arg("--version").output() {
Ok(output) => {
if !output.status.success() {
panic!("Failed to retrieve Node.js version. Please ensure Node.js is properly installed and accessible.");
}
},
Err(e) => {
if e.kind() == ErrorKind::NotFound {
panic!("Node.js is not found on the system. Please install Node.js to proceed.");
} else {
panic!("Failed to run the 'node' command due to error: {}", e);
}
}
}
}
fn escape_string_for_rust_string(target: impl AsRef<str>) -> String {
target.as_ref()
.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r")
}