123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- extern crate protobuf_codegen;
- extern crate protobuf_codegen_pure;
- use std::path::Path;
- use std::fs::{read_to_string, write};
- use protobuf_codegen_pure::Customize;
- use protobuf_codegen_pure::parse_and_typecheck;
- fn main() {
- let customizations = Customize { ..Default::default() };
- let lib_str = read_to_string("src/lib.rs").unwrap();
-
- for line in lib_str.lines() {
- if !line.starts_with("pub mod ") {
- continue;
- }
- let len = line.len();
- let name = &line[8..len-1];
-
- let src = &format!("proto/{}.proto", name);
- let dest = &format!("src/{}.rs", name);
-
- let mut existing = "".to_string();
- if Path::new(dest).exists() {
-
- existing = read_to_string(dest).unwrap().replace("\r\n", "\n");
- }
- println!("Regenerating {} from {}", dest, src);
-
- let p = parse_and_typecheck(&["proto"], &[src]).expect("protoc");
-
-
- let result = protobuf_codegen::gen(
- &p.file_descriptors,
- &p.relative_paths,
- &customizations,
- );
-
- let new = &result.first().unwrap().content;
-
- let new = std::str::from_utf8(&new).unwrap();
-
- if new != existing {
- write(dest, &new).unwrap();
- }
- }
- }
|