proto_info.rs 4.3 KB

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