This repository has been archived by the owner on Jun 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
55 lines (47 loc) · 1.7 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
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use ttrpc_codegen::Codegen;
use ttrpc_codegen::{Customize, ProtobufCustomize};
fn main() {
let mut protos = fs::read_dir("protos")
.expect("Could not read 'protos' directory")
.map(|res| {
res.map(|e| e.path())
.expect("Could not get path of proto files")
})
.collect::<Vec<_>>();
// Sort entries
protos.sort();
// Create the lib.rs file and add all the auto-generated mods
let mut libf = File::create("src/lib.rs").expect("Could not open crate's lib file");
libf.write(b"// This is auto-generated by build.rs\n\n")
.expect("Could not write in crate's lib.rs");
for proto in protos.iter() {
// Re-run compilation every time the proto file changes
println!("cargo:rerun-if-changed={:?}", &proto);
// Strip path prefix and .proto suffix
let mod_name = proto
.strip_prefix("protos/")
.expect(&format!("Error stripping file {:?}", proto))
.with_extension("");
libf.write_all(&format!("pub mod {};\n", mod_name.to_str().unwrap()).as_bytes())
.expect("Could not write mod in crate's lib file");
}
// Add as well the ttrpc file
libf.write_all(b"pub mod agent_ttrpc;\n")
.expect("Could not write mod in crate's lib file");
Codegen::new()
.out_dir("src")
.inputs(&protos)
.include("protos")
.rust_protobuf()
.customize(Customize {
..Default::default()
})
.rust_protobuf_customize(ProtobufCustomize {
..Default::default()
})
.run()
.expect("Protocol generation failed.");
}