proto_info.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #![allow(clippy::all)]
  2. #![allow(dead_code)]
  3. #![allow(unused_imports)]
  4. use crate::util::*;
  5. use std::fs::OpenOptions;
  6. use std::io::Write;
  7. use walkdir::WalkDir;
  8. pub struct CrateProtoInfo {
  9. pub files: Vec<ProtoFile>,
  10. pub inner: ProtobufCrate,
  11. }
  12. impl CrateProtoInfo {
  13. pub fn from_crate_info(inner: ProtobufCrate, files: Vec<ProtoFile>) -> Self {
  14. Self { files, inner }
  15. }
  16. pub fn create_crate_mod_file(&self) {
  17. // mod model;
  18. // pub use model::*;
  19. let mod_file_path = format!("{}/mod.rs", self.inner.protobuf_crate_name());
  20. let mut content = "#![cfg_attr(rustfmt, rustfmt::skip)]\n".to_owned();
  21. content.push_str("// Auto-generated, do not edit\n");
  22. content.push_str("mod model;\npub use model::*;");
  23. match OpenOptions::new()
  24. .create(true)
  25. .write(true)
  26. .append(false)
  27. .truncate(true)
  28. .open(&mod_file_path)
  29. {
  30. Ok(ref mut file) => {
  31. file.write_all(content.as_bytes()).unwrap();
  32. }
  33. Err(err) => {
  34. panic!("Failed to open protobuf mod file: {}", err);
  35. }
  36. }
  37. }
  38. pub fn flutter_mod_dir(&self, root: &str) -> String {
  39. let crate_module_dir = format!("{}/{}", root, self.inner.folder_name);
  40. crate_module_dir
  41. }
  42. pub fn flutter_mod_file(&self, root: &str) -> String {
  43. let crate_module_dir = format!("{}/{}/protobuf.dart", root, self.inner.folder_name);
  44. crate_module_dir
  45. }
  46. }
  47. #[derive(Clone, Debug)]
  48. pub struct ProtobufCrate {
  49. pub folder_name: String,
  50. pub proto_paths: Vec<String>,
  51. pub crate_path: String,
  52. }
  53. impl ProtobufCrate {
  54. pub fn from_config(config: CrateConfig) -> Self {
  55. let proto_paths = config.proto_paths();
  56. ProtobufCrate {
  57. folder_name: config.folder_name,
  58. proto_paths,
  59. crate_path: config.crate_path,
  60. }
  61. }
  62. fn protobuf_crate_name(&self) -> String {
  63. format!("{}/src/protobuf", self.crate_path)
  64. }
  65. pub fn proto_file_output_dir(&self) -> String {
  66. let dir = format!("{}/proto", self.protobuf_crate_name());
  67. create_dir_if_not_exist(dir.as_ref());
  68. dir
  69. }
  70. pub fn proto_struct_output_dir(&self) -> String {
  71. let dir = format!("{}/model", self.protobuf_crate_name());
  72. create_dir_if_not_exist(dir.as_ref());
  73. dir
  74. }
  75. pub fn proto_model_mod_file(&self) -> String {
  76. format!("{}/mod.rs", self.proto_struct_output_dir())
  77. }
  78. }
  79. #[derive(Debug)]
  80. pub struct ProtoFile {
  81. pub file_path: String,
  82. pub file_name: String,
  83. pub structs: Vec<String>,
  84. pub enums: Vec<String>,
  85. pub generated_content: String,
  86. }
  87. pub fn parse_crate_info_from_path(roots: Vec<String>) -> Vec<ProtobufCrate> {
  88. let mut protobuf_crates: Vec<ProtobufCrate> = vec![];
  89. roots.iter().for_each(|root| {
  90. let crates = WalkDir::new(root)
  91. .into_iter()
  92. .filter_entry(|e| !is_hidden(e))
  93. .filter_map(|e| e.ok())
  94. .filter(|e| is_crate_dir(e))
  95. .flat_map(|e| parse_crate_config_from(&e))
  96. .map(ProtobufCrate::from_config)
  97. .collect::<Vec<ProtobufCrate>>();
  98. protobuf_crates.extend(crates);
  99. });
  100. protobuf_crates
  101. }
  102. pub struct FlutterProtobufInfo {
  103. package_path: String,
  104. }
  105. impl FlutterProtobufInfo {
  106. pub fn new(root: &str) -> Self {
  107. FlutterProtobufInfo {
  108. package_path: root.to_owned(),
  109. }
  110. }
  111. pub fn model_dir(&self) -> String {
  112. let model_dir = format!("{}/protobuf", self.package_path);
  113. create_dir_if_not_exist(model_dir.as_ref());
  114. model_dir
  115. }
  116. #[allow(dead_code)]
  117. pub fn mod_file_path(&self) -> String {
  118. let mod_file_path = format!("{}/protobuf.dart", self.package_path);
  119. mod_file_path
  120. }
  121. }