convert proc-macro for generating Context trait to a build script in hopes of getting...
[vector-math.git] / build.rs
diff --git a/build.rs b/build.rs
new file mode 100644 (file)
index 0000000..33d01bc
--- /dev/null
+++ b/build.rs
@@ -0,0 +1,36 @@
+use std::{
+    env, fs,
+    io::{self, Read, Write},
+    path::Path,
+    process::{Child, Command, Stdio},
+    thread,
+};
+use vector_math_build_helpers::make_context_types;
+
+fn format_source(source: String) -> String {
+    let rustfmt_path = which::which("rustfmt").unwrap();
+    let mut command = Command::new(rustfmt_path)
+        .stdin(Stdio::piped())
+        .stdout(Stdio::piped())
+        .spawn()
+        .unwrap();
+    let stdin = command.stdin.take().unwrap();
+    let reader_thread = thread::spawn(move || -> io::Result<(String, Child)> {
+        let mut output = String::new();
+        command.stdout.take().unwrap().read_to_string(&mut output)?;
+        Ok((output, command))
+    });
+    { stdin }.write_all(source.as_bytes()).unwrap();
+    let (output, mut command) = reader_thread.join().unwrap().unwrap();
+    let exit_status = command.wait().unwrap();
+    assert!(exit_status.success());
+    output
+}
+
+fn main() {
+    fs::write(
+        Path::new(&env::var_os("OUT_DIR").unwrap()).join("context_trait.rs"),
+        format_source(make_context_types().to_string()),
+    )
+    .unwrap();
+}