build.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. extern crate protobuf_codegen; // Does the business
  2. extern crate protobuf_codegen_pure; // Helper function
  3. use std::path::Path;
  4. use std::fs::{read_to_string, write};
  5. use protobuf_codegen_pure::Customize;
  6. use protobuf_codegen_pure::parse_and_typecheck;
  7. fn main() {
  8. let customizations = Customize { ..Default::default() };
  9. let lib_str = read_to_string("src/lib.rs").unwrap();
  10. // Iterate over the desired module names.
  11. for line in lib_str.lines() {
  12. if !line.starts_with("pub mod ") && !line.starts_with("mod ") {
  13. continue;
  14. }
  15. let len = line.len();
  16. let name;
  17. if line.starts_with("pub mod ") {
  18. name = &line[8..len-1]; // Remove keywords and semi-colon
  19. }
  20. else {
  21. name = &line[4..len-1]; // Remove keywords and semi-colon
  22. }
  23. // Build the paths to relevant files.
  24. let src = &format!("proto/{}.proto", name);
  25. let dest = &format!("src/{}.rs", name);
  26. // Get the contents of the existing generated file.
  27. let mut existing = "".to_string();
  28. if Path::new(dest).exists() {
  29. // Removing CRLF line endings if present.
  30. existing = read_to_string(dest).unwrap().replace("\r\n", "\n");
  31. }
  32. println!("Regenerating {} from {}", dest, src);
  33. // Parse the proto files as the protobuf-codegen-pure crate does.
  34. let p = parse_and_typecheck(&["proto"], &[src]).expect("protoc");
  35. // But generate them with the protobuf-codegen crate directly.
  36. // Then we can keep the result in-memory.
  37. let result = protobuf_codegen::gen(
  38. &p.file_descriptors,
  39. &p.relative_paths,
  40. &customizations,
  41. );
  42. // Protoc result as a byte array.
  43. let new = &result.first().unwrap().content;
  44. // Convert to utf8 to compare with existing.
  45. let new = std::str::from_utf8(&new).unwrap();
  46. // Save newly generated file if changed.
  47. if new != existing {
  48. write(dest, &new).unwrap();
  49. }
  50. }
  51. }