proto_gen.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #![allow(unused_attributes)]
  2. #![allow(dead_code)]
  3. #![allow(unused_imports)]
  4. #![allow(unused_results)]
  5. use crate::code_gen::protobuf_file::ast::parse_crate_protobuf;
  6. use crate::code_gen::protobuf_file::proto_info::ProtobufCrateContext;
  7. use crate::code_gen::protobuf_file::ProtoFile;
  8. use crate::code_gen::util::*;
  9. use crate::code_gen::ProtoCache;
  10. use std::fs::File;
  11. use std::path::Path;
  12. use std::{fs::OpenOptions, io::Write};
  13. pub struct ProtoGenerator();
  14. impl ProtoGenerator {
  15. pub fn gen(crate_name: &str, crate_path: &str) -> Vec<ProtobufCrateContext> {
  16. let crate_contexts = parse_crate_protobuf(vec![crate_path.to_owned()]);
  17. write_proto_files(&crate_contexts);
  18. write_rust_crate_mod_file(&crate_contexts);
  19. for crate_info in &crate_contexts {
  20. let _ = crate_info.protobuf_crate.create_output_dir();
  21. let _ = crate_info.protobuf_crate.proto_output_dir();
  22. crate_info.create_crate_mod_file();
  23. }
  24. let proto_cache = ProtoCache::from_crate_contexts(&crate_contexts);
  25. let proto_cache_str = serde_json::to_string(&proto_cache).unwrap();
  26. let crate_cache_dir = path_buf_with_component(&cache_dir(), vec![crate_name]);
  27. if !crate_cache_dir.as_path().exists() {
  28. std::fs::create_dir_all(&crate_cache_dir).unwrap();
  29. }
  30. let protobuf_cache_path = path_string_with_component(&crate_cache_dir, vec!["proto_cache"]);
  31. match std::fs::OpenOptions::new()
  32. .create(true)
  33. .write(true)
  34. .append(false)
  35. .truncate(true)
  36. .open(&protobuf_cache_path)
  37. {
  38. Ok(ref mut file) => {
  39. file.write_all(proto_cache_str.as_bytes()).unwrap();
  40. File::flush(file).unwrap();
  41. }
  42. Err(_err) => {
  43. panic!("Failed to open file: {}", protobuf_cache_path);
  44. }
  45. }
  46. crate_contexts
  47. }
  48. }
  49. fn write_proto_files(crate_contexts: &[ProtobufCrateContext]) {
  50. for context in crate_contexts {
  51. let dir = context.protobuf_crate.proto_output_dir();
  52. context.files.iter().for_each(|info| {
  53. let proto_file = format!("{}.proto", &info.file_name);
  54. let proto_file_path = path_string_with_component(&dir, vec![&proto_file]);
  55. save_content_to_file_with_diff_prompt(&info.generated_content, proto_file_path.as_ref());
  56. });
  57. }
  58. }
  59. fn write_rust_crate_mod_file(crate_contexts: &[ProtobufCrateContext]) {
  60. for context in crate_contexts {
  61. let mod_path = context.protobuf_crate.proto_model_mod_file();
  62. match OpenOptions::new()
  63. .create(true)
  64. .write(true)
  65. .append(false)
  66. .truncate(true)
  67. .open(&mod_path)
  68. {
  69. Ok(ref mut file) => {
  70. let mut mod_file_content = String::new();
  71. mod_file_content.push_str("#![cfg_attr(rustfmt, rustfmt::skip)]\n");
  72. mod_file_content.push_str("// Auto-generated, do not edit\n");
  73. walk_dir(
  74. context.protobuf_crate.proto_output_dir(),
  75. |e| !e.file_type().is_dir(),
  76. |_, name| {
  77. let c = format!("\nmod {};\npub use {}::*;\n", &name, &name);
  78. mod_file_content.push_str(c.as_ref());
  79. },
  80. );
  81. file.write_all(mod_file_content.as_bytes()).unwrap();
  82. }
  83. Err(err) => {
  84. panic!("Failed to open file: {}", err);
  85. }
  86. }
  87. }
  88. }
  89. impl ProtoCache {
  90. fn from_crate_contexts(crate_contexts: &[ProtobufCrateContext]) -> Self {
  91. let proto_files = crate_contexts
  92. .iter()
  93. .map(|crate_info| &crate_info.files)
  94. .flatten()
  95. .collect::<Vec<&ProtoFile>>();
  96. let structs: Vec<String> = proto_files.iter().map(|info| info.structs.clone()).flatten().collect();
  97. let enums: Vec<String> = proto_files.iter().map(|info| info.enums.clone()).flatten().collect();
  98. Self { structs, enums }
  99. }
  100. }