-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
33 lines (28 loc) · 1.31 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
use pb_rs::{types::FileDescriptor, ConfigBuilder};
use std::path::{Path, PathBuf};
use walkdir::WalkDir;
fn main() {
let out_dir = std::env::var("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir).join("proto");
let in_dir = PathBuf::from(::std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("proto");
// Re-run this build.rs if the protos dir changes (i.e. a new file is added)
println!("cargo:rerun-if-changed={}", in_dir.to_str().unwrap());
// Find all *.proto files in the `in_dir` and add them to the list of files
let mut protos = Vec::new();
let proto_ext = Some(Path::new("proto").as_os_str());
for entry in WalkDir::new(&in_dir) {
let path = entry.unwrap().into_path();
if path.extension() == proto_ext {
// Re-run this build.rs if any of the files in the protos dir change
println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
protos.push(path);
}
}
// Delete all old generated files before re-generating new ones
if out_dir.exists() {
std::fs::remove_dir_all(&out_dir).unwrap();
}
std::fs::DirBuilder::new().create(&out_dir).unwrap();
let config_builder = ConfigBuilder::new(&protos, None, Some(&out_dir), &[in_dir]).unwrap();
FileDescriptor::run(&config_builder.build()).unwrap()
}