-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.rs
59 lines (51 loc) · 1.66 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
use std::{env, fs, path::Path};
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("proto");
fs::create_dir_all(&dest_path).unwrap();
let proto_src_files = glob_simple("./proto/*.proto");
protoc_rust_grpc::run(protoc_rust_grpc::Args {
out_dir: dest_path.to_str().unwrap(),
input: &proto_src_files
.iter()
.map(|proto_file| proto_file.as_ref())
.collect::<Vec<&str>>(),
includes: &["./proto"],
rust_protobuf: true, // also generate protobuf messages, not just services
..Default::default()
})
.expect("protoc");
let mod_file_content = proto_src_files
.iter()
.map(|proto_file| {
let proto_path = Path::new(proto_file);
let mut mods = vec![format!(
"pub mod {};",
proto_path.file_stem().unwrap().to_str().unwrap()
)];
if proto_file.ends_with("Service.proto") {
mods.push(format!(
"pub mod {}_grpc;",
proto_path.file_stem().unwrap().to_str().unwrap()
))
}
mods
})
.flatten()
.collect::<Vec<_>>()
.join("\n");
fs::write(dest_path.join("mod.rs"), mod_file_content.as_bytes())
.expect("failed to write mod.rs");
}
fn glob_simple(pattern: &str) -> Vec<String> {
glob::glob(pattern)
.expect("glob")
.map(|g| {
g.expect("item")
.as_path()
.to_str()
.expect("utf-8")
.to_owned()
})
.collect()
}