proto_info.rs 3.9 KB

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