proto_info.rs 3.7 KB

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