2019-09-08 12:07:45 +00:00
|
|
|
extern crate protobuf_codegen; // Does the business
|
|
|
|
extern crate protobuf_codegen_pure; // Helper function
|
2015-05-09 10:07:24 +00:00
|
|
|
|
2019-09-08 12:07:45 +00:00
|
|
|
use std::fs::{read_to_string, write};
|
2019-10-08 09:31:18 +00:00
|
|
|
use std::path::Path;
|
2015-05-09 10:07:24 +00:00
|
|
|
|
2019-09-08 12:07:45 +00:00
|
|
|
use protobuf_codegen_pure::parse_and_typecheck;
|
2019-10-08 09:31:18 +00:00
|
|
|
use protobuf_codegen_pure::Customize;
|
2016-11-21 02:48:17 +00:00
|
|
|
|
2019-09-08 12:07:45 +00:00
|
|
|
fn main() {
|
2019-10-08 09:31:18 +00:00
|
|
|
let customizations = Customize {
|
|
|
|
..Default::default()
|
|
|
|
};
|
2016-11-21 02:48:17 +00:00
|
|
|
|
2019-09-08 12:07:45 +00:00
|
|
|
let lib_str = read_to_string("src/lib.rs").unwrap();
|
2016-11-21 02:48:17 +00:00
|
|
|
|
2019-09-08 12:07:45 +00:00
|
|
|
// Iterate over the desired module names.
|
|
|
|
for line in lib_str.lines() {
|
2019-12-16 11:47:52 +00:00
|
|
|
if !line.starts_with("pub mod ") && !line.starts_with("mod ") {
|
2019-09-08 12:07:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let len = line.len();
|
2019-12-16 11:47:52 +00:00
|
|
|
|
|
|
|
let name;
|
|
|
|
if line.starts_with("pub mod ") {
|
2020-01-17 17:11:07 +00:00
|
|
|
name = &line[8..len - 1]; // Remove keywords and semi-colon
|
|
|
|
} else {
|
|
|
|
name = &line[4..len - 1]; // Remove keywords and semi-colon
|
2019-12-16 11:47:52 +00:00
|
|
|
}
|
2019-09-08 12:07:45 +00:00
|
|
|
|
|
|
|
// Build the paths to relevant files.
|
2020-07-22 10:47:25 +00:00
|
|
|
let src_fname = &format!("proto/{}.proto", name);
|
|
|
|
let dest_fname = &format!("src/{}.rs", name);
|
|
|
|
let src = Path::new(src_fname);
|
|
|
|
let dest = Path::new(dest_fname);
|
2019-09-08 12:07:45 +00:00
|
|
|
// Get the contents of the existing generated file.
|
|
|
|
let mut existing = "".to_string();
|
2020-07-22 10:47:25 +00:00
|
|
|
if dest.exists() {
|
2019-09-08 12:07:45 +00:00
|
|
|
// Removing CRLF line endings if present.
|
|
|
|
existing = read_to_string(dest).unwrap().replace("\r\n", "\n");
|
|
|
|
}
|
2016-11-21 02:48:17 +00:00
|
|
|
|
2020-07-22 10:47:25 +00:00
|
|
|
println!("Regenerating {} from {}", dest.display(), src.display());
|
2019-09-08 12:07:45 +00:00
|
|
|
|
|
|
|
// Parse the proto files as the protobuf-codegen-pure crate does.
|
2020-07-22 10:47:25 +00:00
|
|
|
let p = parse_and_typecheck(&[&Path::new("proto")], &[src]).expect("protoc");
|
2019-09-08 12:07:45 +00:00
|
|
|
// But generate them with the protobuf-codegen crate directly.
|
|
|
|
// Then we can keep the result in-memory.
|
2019-10-08 09:31:18 +00:00
|
|
|
let result = protobuf_codegen::gen(&p.file_descriptors, &p.relative_paths, &customizations);
|
2019-09-08 12:07:45 +00:00
|
|
|
// Protoc result as a byte array.
|
|
|
|
let new = &result.first().unwrap().content;
|
|
|
|
// Convert to utf8 to compare with existing.
|
|
|
|
let new = std::str::from_utf8(&new).unwrap();
|
|
|
|
// Save newly generated file if changed.
|
|
|
|
if new != existing {
|
|
|
|
write(dest, &new).unwrap();
|
|
|
|
}
|
2016-11-21 02:48:17 +00:00
|
|
|
}
|
|
|
|
}
|