forked from awestlake87/sc2-proto-rs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.rs
68 lines (60 loc) · 1.76 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
60
61
62
63
64
65
66
67
68
#[cfg(feature = "generate")]
use std::{env, ffi::OsStr, fs, io::prelude::*, path::Path};
#[cfg(feature = "generate")]
use protobuf_codegen::{Codegen,Customize};
#[cfg(feature = "generate")]
use protoc_bin_vendored::protoc_bin_path;
#[cfg(feature = "generate")]
fn proto_modules(proto_dir: &Path) -> Vec<String> {
fs::read_dir(proto_dir)
.expect("Could not read protobuf directory")
.filter_map(|entry| {
let path = entry.ok()?.path();
if path.is_file() && path.extension() == Some(OsStr::new("proto")) {
path.file_stem().and_then(|n| n.to_os_string().into_string().ok())
} else {
None
}
})
.collect()
}
#[cfg(feature = "generate")]
fn main() {
let in_dir = "./s2client-proto/s2clientprotocol";
let out_dir = &env::var("OUT_DIR").unwrap();
// Read list of all input protobuf files
let input_mods = proto_modules(Path::new(in_dir));
let input_files: Vec<String> = input_mods
.iter()
.map(|s| format!("{}/{}.proto", in_dir, s))
.collect();
// Compile protocol buffers
Codegen::new()
.out_dir(out_dir)
.protoc_path(&protoc_bin_path().unwrap())
.include("./s2client-proto/")
.inputs(input_files)
.run_from_script();
println!("protobufs were generated successfully");
// Generate the lib.rs source code
let mut buffer = fs::File::create(format!("{}/{}", out_dir, "lib.rs")).unwrap();
buffer
.write_all(
input_mods
.iter()
.map(|s| format!("pub mod {};", s))
.collect::<Vec<_>>()
.join("\n")
.as_bytes(),
)
.unwrap();
// Copy generated *.rs files to "src"
fs::read_dir(out_dir).unwrap().for_each(|f| {
let f = f.unwrap();
fs::copy(f.path(), format!("src/{}", f.file_name().to_str().unwrap())).unwrap();
});
}
#[cfg(not(feature = "generate"))]
fn main() {
println!("using pre-generated *.rs files in 'src/'");
}