proto_info.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #![allow(dead_code)]
  2. use crate::flowy_toml::{parse_crate_config_from, CrateConfig, FlowyConfig};
  3. use crate::util::*;
  4. use std::fs::OpenOptions;
  5. use std::io::Write;
  6. use std::path::PathBuf;
  7. use std::str::FromStr;
  8. use walkdir::WalkDir;
  9. #[derive(Debug)]
  10. pub struct ProtobufCrateContext {
  11. pub files: Vec<ProtoFile>,
  12. pub protobuf_crate: ProtobufCrate,
  13. }
  14. impl ProtobufCrateContext {
  15. pub fn from_crate_info(inner: ProtobufCrate, files: Vec<ProtoFile>) -> Self {
  16. Self {
  17. files,
  18. protobuf_crate: inner,
  19. }
  20. }
  21. pub fn create_crate_mod_file(&self) {
  22. // mod model;
  23. // pub use model::*;
  24. let mod_file_path =
  25. path_string_with_component(&self.protobuf_crate.protobuf_crate_path(), vec!["mod.rs"]);
  26. let mut content = "#![cfg_attr(rustfmt, rustfmt::skip)]\n".to_owned();
  27. content.push_str(" #![allow(ambiguous_glob_reexports)]\n");
  28. content.push_str("// Auto-generated, do not edit\n");
  29. content.push_str("mod model;\npub use model::*;");
  30. match OpenOptions::new()
  31. .create(true)
  32. .write(true)
  33. .append(false)
  34. .truncate(true)
  35. .open(&mod_file_path)
  36. {
  37. Ok(ref mut file) => {
  38. file.write_all(content.as_bytes()).unwrap();
  39. },
  40. Err(err) => {
  41. panic!("Failed to open protobuf mod file: {}", err);
  42. },
  43. }
  44. }
  45. #[allow(dead_code)]
  46. pub fn flutter_mod_dir(&self, root: &str) -> String {
  47. let crate_module_dir = format!("{}/{}", root, self.protobuf_crate.crate_folder);
  48. crate_module_dir
  49. }
  50. #[allow(dead_code)]
  51. pub fn flutter_mod_file(&self, root: &str) -> String {
  52. let crate_module_dir = format!(
  53. "{}/{}/protobuf.dart",
  54. root, self.protobuf_crate.crate_folder
  55. );
  56. crate_module_dir
  57. }
  58. }
  59. #[derive(Clone, Debug)]
  60. pub struct ProtobufCrate {
  61. pub crate_folder: String,
  62. pub crate_path: PathBuf,
  63. flowy_config: FlowyConfig,
  64. }
  65. impl ProtobufCrate {
  66. pub fn from_config(config: CrateConfig) -> Self {
  67. ProtobufCrate {
  68. crate_path: config.crate_path,
  69. crate_folder: config.crate_folder,
  70. flowy_config: config.flowy_config,
  71. }
  72. }
  73. // Return the file paths for each rust file that used to generate the proto file.
  74. pub fn proto_input_paths(&self) -> Vec<PathBuf> {
  75. self
  76. .flowy_config
  77. .proto_input
  78. .iter()
  79. .map(|name| path_buf_with_component(&self.crate_path, vec![name]))
  80. .collect::<Vec<PathBuf>>()
  81. }
  82. // The protobuf_crate_path is used to store the generated protobuf Rust structures.
  83. pub fn protobuf_crate_path(&self) -> PathBuf {
  84. let crate_path = PathBuf::from(&self.flowy_config.protobuf_crate_path);
  85. create_dir_if_not_exist(&crate_path);
  86. crate_path
  87. }
  88. // The proto_output_path is used to store the proto files
  89. pub fn proto_output_path(&self) -> PathBuf {
  90. let output_dir = PathBuf::from(&self.flowy_config.proto_output);
  91. create_dir_if_not_exist(&output_dir);
  92. output_dir
  93. }
  94. pub fn proto_model_mod_file(&self) -> String {
  95. path_string_with_component(&self.protobuf_crate_path(), vec!["mod.rs"])
  96. }
  97. }
  98. #[derive(Debug)]
  99. pub struct ProtoFile {
  100. pub file_path: String,
  101. pub file_name: String,
  102. pub structs: Vec<String>,
  103. // store the type of current file using
  104. pub ref_types: Vec<String>,
  105. pub enums: Vec<String>,
  106. // proto syntax. "proto3" or "proto2"
  107. pub syntax: String,
  108. // proto message content
  109. pub content: String,
  110. }
  111. impl ProtoFile {
  112. pub fn symbols(&self) -> Vec<String> {
  113. let mut symbols = self.structs.clone();
  114. let mut enum_symbols = self.enums.clone();
  115. symbols.append(&mut enum_symbols);
  116. symbols
  117. }
  118. }
  119. pub fn parse_crate_info_from_path(roots: Vec<String>) -> Vec<ProtobufCrate> {
  120. let mut protobuf_crates: Vec<ProtobufCrate> = vec![];
  121. roots.iter().for_each(|root| {
  122. let crates = WalkDir::new(root)
  123. .into_iter()
  124. .filter_entry(|e| !is_hidden(e))
  125. .filter_map(|e| e.ok())
  126. .filter(is_crate_dir)
  127. .flat_map(|e| parse_crate_config_from(&e))
  128. .map(ProtobufCrate::from_config)
  129. .collect::<Vec<ProtobufCrate>>();
  130. protobuf_crates.extend(crates);
  131. });
  132. protobuf_crates
  133. }