-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.rs
51 lines (39 loc) · 1.29 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
extern crate protoc_grpcio;
extern crate glob;
use std::fs;
use std::io::Write;
use std::path::Path;
fn main() {
let proto_root = "protos";
let out = "src/proto";
match fs::create_dir(&out) {
Ok(_) => println!("Created"),
Err(e) => println!("{}", e)
};
protoc_grpcio::compile_grpc_protos(
&["protos/example.proto", "protos/google/protobuf/empty.proto","protos/pubsub.proto"],
&[proto_root],
&out,
None
).expect("Failed to compile gRPC definitions!");
gen_mod_rs_in_dir(out)
}
pub fn gen_mod_rs_in_dir(dir: &str) {
let mut mod_rs = fs::File::create(&format!("{}/mod.rs", dir)).expect("create");
writeln!(mod_rs, "// generated by {}", module_path!()).expect("write");
writeln!(mod_rs, "").expect("write");
let rs_files = glob::glob(&format!("{}/*.rs", dir));
for rs in rs_files.unwrap().filter_map(Result::ok) {
let file_name = Path::new(&rs)
.file_name()
.expect("file_name")
.to_str()
.expect("file_name");
if file_name == "mod.rs" {
continue;
}
let mod_name = &file_name[..file_name.len() - ".rs".len()];
writeln!(mod_rs, "pub mod {};", mod_name).expect("write");
}
mod_rs.flush().expect("flush");
}