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::path::Path;
|
|
|
|
use std::fs::{read_to_string, write};
|
2015-05-09 10:07:24 +00:00
|
|
|
|
2019-09-08 12:07:45 +00:00
|
|
|
use protobuf_codegen_pure::Customize;
|
|
|
|
use protobuf_codegen_pure::parse_and_typecheck;
|
2016-11-21 02:48:17 +00:00
|
|
|
|
2019-09-08 12:07:45 +00:00
|
|
|
fn main() {
|
|
|
|
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() {
|
|
|
|
if !line.starts_with("pub mod ") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let len = line.len();
|
|
|
|
let name = &line[8..len-1]; // Remove keywords and semi-colon
|
|
|
|
|
|
|
|
// Build the paths to relevant files.
|
|
|
|
let src = &format!("proto/{}.proto", name);
|
|
|
|
let dest = &format!("src/{}.rs", name);
|
|
|
|
|
|
|
|
// Get the contents of the existing generated file.
|
|
|
|
let mut existing = "".to_string();
|
|
|
|
if Path::new(dest).exists() {
|
|
|
|
// Removing CRLF line endings if present.
|
|
|
|
existing = read_to_string(dest).unwrap().replace("\r\n", "\n");
|
|
|
|
}
|
2016-11-21 02:48:17 +00:00
|
|
|
|
2019-09-08 12:07:45 +00:00
|
|
|
println!("Regenerating {} from {}", dest, src);
|
|
|
|
|
|
|
|
// Parse the proto files as the protobuf-codegen-pure crate does.
|
|
|
|
let p = parse_and_typecheck(&["proto"], &[src]).expect("protoc");
|
|
|
|
// But generate them with the protobuf-codegen crate directly.
|
|
|
|
// Then we can keep the result in-memory.
|
|
|
|
let result = protobuf_codegen::gen(
|
|
|
|
&p.file_descriptors,
|
|
|
|
&p.relative_paths,
|
|
|
|
&customizations,
|
|
|
|
);
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|