proto_gen.rs 4.0 KB

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