proto_gen.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. use crate::proto::ast::*;
  2. use crate::proto::proto_info::*;
  3. use crate::{proto::template::*, util::*};
  4. use std::{fs::OpenOptions, io::Write};
  5. pub struct ProtoGen {
  6. pub(crate) rust_source_dir: String,
  7. pub(crate) flutter_package_lib: String,
  8. pub(crate) derive_meta_dir: String,
  9. }
  10. impl ProtoGen {
  11. pub fn gen(&self) {
  12. let crate_proto_infos = parse_crate_protobuf(self.rust_source_dir.as_ref());
  13. write_proto_files(&crate_proto_infos);
  14. // FIXME: ignore unchanged file to reduce time cost
  15. run_rust_protoc(&crate_proto_infos);
  16. write_rust_crate_mod_file(&crate_proto_infos);
  17. write_derive_meta(&crate_proto_infos, self.derive_meta_dir.as_ref());
  18. // FIXME: ignore unchanged file to reduce time cost
  19. let flutter_package = FlutterProtobufInfo::new(self.flutter_package_lib.as_ref());
  20. run_flutter_protoc(&crate_proto_infos, &flutter_package);
  21. write_flutter_protobuf_package_mod_file(&crate_proto_infos, &flutter_package);
  22. }
  23. }
  24. fn write_proto_files(crate_infos: &Vec<CrateProtoInfo>) {
  25. for crate_info in crate_infos {
  26. let dir = crate_info.inner.proto_file_output_dir();
  27. remove_everything_in_dir(dir.as_str());
  28. crate_info.files.iter().for_each(|info| {
  29. let proto_file_path = format!("{}/{}.proto", dir, &info.file_name);
  30. save_content_to_file_with_diff_prompt(
  31. &info.generated_content,
  32. proto_file_path.as_ref(),
  33. false,
  34. );
  35. });
  36. }
  37. }
  38. fn write_rust_crate_mod_file(crate_infos: &Vec<CrateProtoInfo>) {
  39. for crate_info in crate_infos {
  40. let mod_path = crate_info.inner.proto_model_mod_file();
  41. match OpenOptions::new()
  42. .create(true)
  43. .write(true)
  44. .append(false)
  45. .truncate(true)
  46. .open(&mod_path)
  47. {
  48. Ok(ref mut file) => {
  49. let mut mod_file_content = String::new();
  50. mod_file_content.push_str("// Auto-generated, do not edit \n");
  51. walk_dir(
  52. crate_info.inner.proto_file_output_dir().as_ref(),
  53. |e| e.file_type().is_dir() == false,
  54. |_, name| {
  55. let c = format!("\nmod {}; \npub use {}::*; \n", &name, &name);
  56. mod_file_content.push_str(c.as_ref());
  57. },
  58. );
  59. file.write_all(mod_file_content.as_bytes()).unwrap();
  60. }
  61. Err(err) => {
  62. panic!("Failed to open file: {}", err);
  63. }
  64. }
  65. }
  66. }
  67. fn write_flutter_protobuf_package_mod_file(
  68. crate_infos: &Vec<CrateProtoInfo>,
  69. package_info: &FlutterProtobufInfo,
  70. ) {
  71. let mod_path = package_info.mod_file_path();
  72. let _model_dir = package_info.model_dir();
  73. match OpenOptions::new()
  74. .create(true)
  75. .write(true)
  76. .append(false)
  77. .truncate(true)
  78. .open(&mod_path)
  79. {
  80. Ok(ref mut file) => {
  81. let mut mod_file_content = String::new();
  82. mod_file_content.push_str("// Auto-generated, do not edit \n");
  83. for crate_info in crate_infos {
  84. let _mod_path = crate_info.inner.proto_model_mod_file();
  85. walk_dir(
  86. crate_info.inner.proto_file_output_dir().as_ref(),
  87. |e| e.file_type().is_dir() == false,
  88. |_, name| {
  89. let c = format!("export 'protobuf/{}.pb.dart';\n", &name);
  90. mod_file_content.push_str(c.as_ref());
  91. },
  92. );
  93. }
  94. file.write_all(mod_file_content.as_bytes()).unwrap();
  95. file.flush().unwrap();
  96. }
  97. Err(err) => {
  98. panic!("Failed to open file: {}", err);
  99. }
  100. }
  101. }
  102. fn run_rust_protoc(crate_infos: &Vec<CrateProtoInfo>) {
  103. for crate_info in crate_infos {
  104. let rust_out = crate_info.inner.proto_struct_output_dir();
  105. let proto_path = crate_info.inner.proto_file_output_dir();
  106. walk_dir(
  107. proto_path.as_ref(),
  108. |e| is_proto_file(e),
  109. |proto_file, _| {
  110. if cmd_lib::run_cmd! {
  111. protoc --rust_out=${rust_out} --proto_path=${proto_path} ${proto_file}
  112. }
  113. .is_err()
  114. {
  115. panic!("Run flutter protoc fail")
  116. };
  117. },
  118. );
  119. crate_info.create_crate_mod_file();
  120. }
  121. }
  122. use std::path::Path;
  123. use std::process::Command;
  124. fn run_flutter_protoc(crate_infos: &Vec<CrateProtoInfo>, package_info: &FlutterProtobufInfo) {
  125. let model_dir = package_info.model_dir();
  126. let removed_dir = format!("{}/", model_dir);
  127. remove_everything_in_dir(removed_dir.as_str());
  128. for crate_info in crate_infos {
  129. let proto_path = crate_info.inner.proto_file_output_dir();
  130. walk_dir(
  131. proto_path.as_ref(),
  132. |e| is_proto_file(e),
  133. |proto_file, _| {
  134. if cmd_lib::run_cmd! {
  135. protoc --dart_out=${model_dir} --proto_path=${proto_path} ${proto_file}
  136. }
  137. .is_err()
  138. {
  139. panic!("Run flutter protoc fail")
  140. };
  141. },
  142. );
  143. }
  144. }
  145. fn remove_everything_in_dir(dir: &str) {
  146. if !Path::new(dir).exists() {
  147. if cmd_lib::run_cmd! {
  148. rm -rf ${dir}
  149. mkdir ${dir}
  150. }
  151. .is_err()
  152. {
  153. panic!("Reset protobuf directory failed")
  154. };
  155. } else {
  156. std::fs::create_dir_all(dir).unwrap();
  157. }
  158. }