-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
30 lines (29 loc) · 1.07 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
//! Here, we generate Rust code from a proto file before project compilation.
use std::env;
use std::fs::{read_to_string, File};
use std::io::{BufWriter, Write};
use std::path::Path;
fn main() {
let out_dir_env = env::var_os("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir_env);
protobuf_codegen_pure::Codegen::new()
.out_dir(out_dir)
.inputs(["proto/common-index-format-v1.proto"])
.include("proto")
.run()
.expect("Codegen failed.");
let path = out_dir.join("common_index_format_v1.rs");
let code = read_to_string(&path).expect("Failed to read generated file");
let mut writer = BufWriter::new(File::create(path).unwrap());
for line in code.lines() {
if !line.contains("//!") && !line.contains("#!") {
writer
.write_all(line.as_bytes())
.expect("Failed to write to generated file");
writer
.write_all(&[b'\n'])
.expect("Failed to write to generated file");
}
}
println!("cargo:rerun-if-changed=build.rs");
}