refactor: remove the protobuf's model folder

This commit is contained in:
appflowy 2022-06-16 08:21:31 +08:00
parent f49f1695b0
commit 736f21ef8a
21 changed files with 142 additions and 123 deletions

View File

@ -1,4 +1,4 @@
proto_crates = ["src/model"]
proto_output_dir = "resources/proto"
protobuf_crate_path = "src/protobuf"
proto_rust_file_input_dir = ["src/model"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []

View File

@ -1,5 +1,5 @@
proto_crates = ["src/entities"]
proto_output_dir = "resources/proto"
protobuf_crate_path = "src/protobuf"
proto_rust_file_input_dir = ["src/entities"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []

View File

@ -1,5 +1,5 @@
proto_crates = ["src/errors.rs",]
proto_output_dir = "resources/proto"
protobuf_crate_path = "src/protobuf"
proto_rust_file_input_dir = ["src/errors.rs",]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []

View File

@ -1,5 +1,5 @@
proto_crates = ["src/entities", "src/event_map.rs", "src/dart_notification.rs"]
proto_output_dir = "resources/proto"
protobuf_crate_path = "src/protobuf"
proto_rust_file_input_dir = ["src/entities", "src/event_map.rs", "src/dart_notification.rs"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = ["src/event_map.rs"]

View File

@ -1,9 +1,9 @@
proto_crates = [
proto_rust_file_input_dir = [
"src/event_map.rs",
"src/services/field/type_options",
"src/entities",
"src/dart_notification.rs"
]
proto_output_dir = "resources/proto"
protobuf_crate_path = "src/protobuf"
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = ["src/event_map.rs"]

View File

@ -1,4 +1,38 @@
#![cfg_attr(rustfmt, rustfmt::skip)]
// Auto-generated, do not edit
mod model;
pub use model::*;
mod field_entities;
pub use field_entities::*;
mod number_type_option;
pub use number_type_option::*;
mod dart_notification;
pub use dart_notification::*;
mod selection_type_option;
pub use selection_type_option::*;
mod row_entities;
pub use row_entities::*;
mod cell_entities;
pub use cell_entities::*;
mod url_type_option;
pub use url_type_option::*;
mod checkbox_type_option;
pub use checkbox_type_option::*;
mod format;
pub use format::*;
mod event_map;
pub use event_map::*;
mod text_type_option;
pub use text_type_option::*;
mod date_type_option;
pub use date_type_option::*;

View File

@ -1,4 +1,4 @@
proto_crates = ["src/event_map.rs", "src/entities"]
proto_output_dir = "resources/proto"
protobuf_crate_path = "src/protobuf"
proto_rust_file_input_dir = ["src/event_map.rs", "src/entities"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = ["src/event_map.rs"]

View File

@ -1,5 +1,5 @@
proto_crates = ["src/event_map.rs", "src/entities.rs"]
proto_output_dir = "resources/proto"
protobuf_crate_path = "src/protobuf"
proto_rust_file_input_dir = ["src/event_map.rs", "src/entities.rs"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = ["src/event_map.rs"]

View File

@ -1,5 +1,5 @@
proto_crates = ["src/event_map.rs", "src/dart_notification.rs"]
proto_output_dir = "resources/proto"
protobuf_crate_path = "src/protobuf"
proto_rust_file_input_dir = ["src/event_map.rs", "src/dart_notification.rs"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = ["src/event_map.rs"]

View File

@ -1,5 +1,5 @@
proto_crates = ["src/code.rs"]
proto_output_dir = "resources/proto"
protobuf_crate_path = "src/protobuf"
proto_rust_file_input_dir = ["src/code.rs"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []

View File

@ -1,5 +1,5 @@
proto_crates = ["src/entities",]
proto_output_dir = "resources/proto"
protobuf_crate_path = "src/protobuf"
proto_rust_file_input_dir = ["src/entities",]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []

View File

@ -1,5 +1,5 @@
proto_crates = ["src/entities/",]
proto_output_dir = "resources/proto"
protobuf_crate_path = "src/protobuf"
proto_rust_file_input_dir = ["src/entities/",]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []

View File

@ -1,5 +1,5 @@
proto_crates = ["src/entities"]
proto_output_dir = "resources/proto"
protobuf_crate_path = "src/protobuf"
proto_rust_file_input_dir = ["src/entities"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []

View File

@ -1,5 +1,5 @@
proto_crates = ["src/entities", "src/errors.rs"]
proto_output_dir = "resources/proto"
protobuf_crate_path = "src/protobuf"
proto_rust_file_input_dir = ["src/entities", "src/errors.rs"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []

View File

@ -1,13 +1,12 @@
use crate::code_gen::util::path_buf_with_component;
use std::fs;
use std::path::{Path, PathBuf};
#[derive(serde::Deserialize, Clone, Debug)]
pub struct FlowyConfig {
pub proto_crates: Vec<String>,
pub event_files: Vec<String>,
pub proto_output_dir: String,
pub protobuf_crate_path: String,
pub proto_rust_file_input_dir: Vec<String>,
pub proto_file_output_dir: String,
pub protobuf_crate_output_dir: String,
}
impl FlowyConfig {
@ -20,22 +19,10 @@ impl FlowyConfig {
pub struct CrateConfig {
pub crate_path: PathBuf,
pub folder_name: String,
pub crate_folder: String,
pub flowy_config: FlowyConfig,
}
impl CrateConfig {
pub fn proto_paths(&self) -> Vec<PathBuf> {
let proto_paths = self
.flowy_config
.proto_crates
.iter()
.map(|name| path_buf_with_component(&self.crate_path, vec![name]))
.collect::<Vec<PathBuf>>();
proto_paths
}
}
pub fn parse_crate_config_from(entry: &walkdir::DirEntry) -> Option<CrateConfig> {
let mut config_path = entry.path().parent().unwrap().to_path_buf();
config_path.push("Flowy.toml");
@ -44,11 +31,11 @@ pub fn parse_crate_config_from(entry: &walkdir::DirEntry) -> Option<CrateConfig>
}
let crate_path = entry.path().parent().unwrap().to_path_buf();
let flowy_config = FlowyConfig::from_toml_file(config_path.as_path());
let folder_name = crate_path.file_stem().unwrap().to_str().unwrap().to_string();
let crate_folder = crate_path.file_stem().unwrap().to_str().unwrap().to_string();
Some(CrateConfig {
crate_path,
folder_name,
crate_folder,
flowy_config,
})
}

View File

@ -13,14 +13,14 @@ use std::{fs::File, io::Read, path::Path};
use syn::Item;
use walkdir::WalkDir;
pub fn parse_crate_protobuf(crate_paths: Vec<String>) -> Vec<ProtobufCrateContext> {
pub fn parse_protobuf_context_from(crate_paths: Vec<String>) -> Vec<ProtobufCrateContext> {
let crate_infos = parse_crate_info_from_path(crate_paths);
crate_infos
let contexts = crate_infos
.into_iter()
.map(|crate_info| {
let proto_output_dir = crate_info.proto_output_dir();
let proto_output_dir = crate_info.proto_file_output_dir();
let files = crate_info
.proto_paths
.proto_rust_file_paths()
.iter()
.map(|proto_crate_path| parse_files_protobuf(proto_crate_path, &proto_output_dir))
.flatten()
@ -28,7 +28,9 @@ pub fn parse_crate_protobuf(crate_paths: Vec<String>) -> Vec<ProtobufCrateContex
ProtobufCrateContext::from_crate_info(crate_info, files)
})
.collect::<Vec<ProtobufCrateContext>>()
.collect::<Vec<ProtobufCrateContext>>();
contexts
}
fn parse_files_protobuf(proto_crate_path: &Path, proto_output_dir: &Path) -> Vec<ProtoFile> {
@ -55,7 +57,7 @@ fn parse_files_protobuf(proto_crate_path: &Path, proto_output_dir: &Path) -> Vec
let structs = get_ast_structs(&ast);
let proto_file = format!("{}.proto", &file_name);
let proto_file_path = path_string_with_component(proto_output_dir, vec![&proto_file]);
let mut proto_file_content = parse_or_init_proto_file(proto_file_path.as_ref());
let mut proto_file_content = find_proto_syntax(proto_file_path.as_ref());
structs.iter().for_each(|s| {
let mut struct_template = StructTemplate::new();
@ -94,14 +96,6 @@ fn parse_files_protobuf(proto_crate_path: &Path, proto_output_dir: &Path) -> Vec
gen_proto_vec
}
pub fn parse_or_init_proto_file(path: &str) -> String {
let mut proto_file_content = String::new();
let imported_content = find_proto_file_import(path);
proto_file_content.push_str(imported_content.as_ref());
proto_file_content.push('\n');
proto_file_content
}
pub fn get_ast_structs(ast: &syn::File) -> Vec<Struct> {
// let mut content = format!("{:#?}", &ast);
// let mut file = File::create("./foo.txt").unwrap();
@ -157,14 +151,12 @@ lazy_static! {
static ref IMPORT_REGEX: Regex = Regex::new("(import\\s).*;").unwrap();
}
fn find_proto_file_import(path: &str) -> String {
let mut result = String::new();
fn find_proto_syntax(path: &str) -> String {
if !Path::new(path).exists() {
// log::error!("{} not exist", path);
result = String::from("syntax = \"proto3\";");
return result;
return String::from("syntax = \"proto3\";\\n");
}
let mut result = String::new();
let mut file = File::open(path).unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
@ -182,5 +174,6 @@ fn find_proto_file_import(path: &str) -> String {
}
});
result.push('\n');
result
}

View File

@ -27,9 +27,10 @@ pub fn gen(crate_name: &str) {
for proto_crate in proto_crates {
let mut proto_file_paths = vec![];
let mut file_names = vec![];
let proto_file_dir = &proto_crate.flowy_config.proto_output_dir;
let proto_file_output_dir = proto_crate.proto_file_output_dir().to_str().unwrap().to_string();
let protobuf_output_dir = proto_crate.protobuf_crate_path().to_str().unwrap().to_string();
for (path, file_name) in WalkDir::new(proto_file_dir)
for (path, file_name) in WalkDir::new(&proto_file_output_dir)
.into_iter()
.filter_map(|e| e.ok())
.map(|e| {
@ -51,31 +52,41 @@ pub fn gen(crate_name: &str) {
#[cfg(feature = "dart")]
generate_dart_protobuf_files(
crate_name,
proto_file_dir,
&proto_file_output_dir,
&proto_file_paths,
&file_names,
&protoc_bin_path,
);
// 3. generate the protobuf files(Rust)
generate_rust_protobuf_files(&protoc_bin_path, &proto_file_paths, proto_file_dir);
generate_rust_protobuf_files(
&protoc_bin_path,
&proto_file_paths,
&proto_file_output_dir,
&protobuf_output_dir,
);
}
}
fn generate_rust_protobuf_files(protoc_bin_path: &Path, proto_file_paths: &[String], proto_file_dir: &str) {
fn generate_rust_protobuf_files(
protoc_bin_path: &Path,
proto_file_paths: &[String],
proto_file_output_dir: &str,
protobuf_output_dir: &str,
) {
protoc_rust::Codegen::new()
.out_dir("./src/protobuf/model")
.out_dir(protobuf_output_dir)
.protoc_path(protoc_bin_path)
.inputs(proto_file_paths)
.include(proto_file_dir)
.include(proto_file_output_dir)
.run()
.expect("Running protoc failed.");
.expect("Running rust protoc failed.");
}
#[cfg(feature = "dart")]
fn generate_dart_protobuf_files(
name: &str,
proto_path: &str,
proto_file_output_dir: &str,
paths: &Vec<String>,
file_names: &Vec<String>,
protoc_bin_path: &PathBuf,
@ -104,7 +115,7 @@ fn generate_dart_protobuf_files(
let protoc_bin_path = protoc_bin_path.to_str().unwrap().to_owned();
paths.iter().for_each(|path| {
if cmd_lib::run_cmd! {
${protoc_bin_path} --dart_out=${output} --proto_path=${proto_path} ${path}
${protoc_bin_path} --dart_out=${output} --proto_path=${proto_file_output_dir} ${path}
}
.is_err()
{

View File

@ -2,7 +2,7 @@
#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_results)]
use crate::code_gen::protobuf_file::ast::parse_crate_protobuf;
use crate::code_gen::protobuf_file::ast::parse_protobuf_context_from;
use crate::code_gen::protobuf_file::proto_info::ProtobufCrateContext;
use crate::code_gen::protobuf_file::ProtoFile;
use crate::code_gen::util::*;
@ -14,14 +14,9 @@ use std::{fs::OpenOptions, io::Write};
pub struct ProtoGenerator();
impl ProtoGenerator {
pub fn gen(crate_name: &str, crate_path: &str) -> Vec<ProtobufCrateContext> {
let crate_contexts = parse_crate_protobuf(vec![crate_path.to_owned()]);
let crate_contexts = parse_protobuf_context_from(vec![crate_path.to_owned()]);
write_proto_files(&crate_contexts);
write_rust_crate_mod_file(&crate_contexts);
for crate_info in &crate_contexts {
let _ = crate_info.protobuf_crate.create_output_dir();
let _ = crate_info.protobuf_crate.proto_output_dir();
crate_info.create_crate_mod_file();
}
let proto_cache = ProtoCache::from_crate_contexts(&crate_contexts);
let proto_cache_str = serde_json::to_string(&proto_cache).unwrap();
@ -55,7 +50,7 @@ impl ProtoGenerator {
fn write_proto_files(crate_contexts: &[ProtobufCrateContext]) {
for context in crate_contexts {
let dir = context.protobuf_crate.proto_output_dir();
let dir = context.protobuf_crate.proto_file_output_dir();
context.files.iter().for_each(|info| {
let proto_file = format!("{}.proto", &info.file_name);
let proto_file_path = path_string_with_component(&dir, vec![&proto_file]);
@ -80,7 +75,7 @@ fn write_rust_crate_mod_file(crate_contexts: &[ProtobufCrateContext]) {
mod_file_content.push_str("#![cfg_attr(rustfmt, rustfmt::skip)]\n");
mod_file_content.push_str("// Auto-generated, do not edit\n");
walk_dir(
context.protobuf_crate.proto_output_dir(),
context.protobuf_crate.proto_file_output_dir(),
|e| !e.file_type().is_dir(),
|_, name| {
let c = format!("\nmod {};\npub use {}::*;\n", &name, &name);

View File

@ -24,7 +24,7 @@ impl ProtobufCrateContext {
pub fn create_crate_mod_file(&self) {
// mod model;
// pub use model::*;
let mod_file_path = path_string_with_component(&self.protobuf_crate.protobuf_crate_name(), vec!["mod.rs"]);
let mod_file_path = path_string_with_component(&self.protobuf_crate.protobuf_crate_path(), vec!["mod.rs"]);
let mut content = "#![cfg_attr(rustfmt, rustfmt::skip)]\n".to_owned();
content.push_str("// Auto-generated, do not edit\n");
content.push_str("mod model;\npub use model::*;");
@ -46,57 +46,55 @@ impl ProtobufCrateContext {
#[allow(dead_code)]
pub fn flutter_mod_dir(&self, root: &str) -> String {
let crate_module_dir = format!("{}/{}", root, self.protobuf_crate.folder_name);
let crate_module_dir = format!("{}/{}", root, self.protobuf_crate.crate_folder);
crate_module_dir
}
#[allow(dead_code)]
pub fn flutter_mod_file(&self, root: &str) -> String {
let crate_module_dir = format!("{}/{}/protobuf.dart", root, self.protobuf_crate.folder_name);
let crate_module_dir = format!("{}/{}/protobuf.dart", root, self.protobuf_crate.crate_folder);
crate_module_dir
}
}
#[derive(Clone, Debug)]
pub struct ProtobufCrate {
pub folder_name: String,
pub proto_paths: Vec<PathBuf>,
pub crate_folder: String,
pub crate_path: PathBuf,
pub flowy_config: FlowyConfig,
flowy_config: FlowyConfig,
}
impl ProtobufCrate {
pub fn from_config(config: CrateConfig) -> Self {
let proto_paths = config.proto_paths();
ProtobufCrate {
folder_name: config.folder_name,
proto_paths,
crate_path: config.crate_path,
crate_folder: config.crate_folder,
flowy_config: config.flowy_config.clone(),
}
}
fn protobuf_crate_name(&self) -> PathBuf {
let crate_path = PathBuf::from(&self.flowy_config.protobuf_crate_path);
pub fn proto_rust_file_paths(&self) -> Vec<PathBuf> {
self.flowy_config
.proto_rust_file_input_dir
.iter()
.map(|name| path_buf_with_component(&self.crate_path, vec![name]))
.collect::<Vec<PathBuf>>()
}
pub fn protobuf_crate_path(&self) -> PathBuf {
let crate_path = PathBuf::from(&self.flowy_config.protobuf_crate_output_dir);
create_dir_if_not_exist(&crate_path);
crate_path
}
pub fn proto_output_dir(&self) -> PathBuf {
let output_dir = PathBuf::from(&self.flowy_config.proto_output_dir);
pub fn proto_file_output_dir(&self) -> PathBuf {
let output_dir = PathBuf::from(&self.flowy_config.proto_file_output_dir);
create_dir_if_not_exist(&output_dir);
output_dir
}
pub fn create_output_dir(&self) -> PathBuf {
let path = self.protobuf_crate_name();
let dir = path_buf_with_component(&path, vec!["model"]);
create_dir_if_not_exist(&dir);
dir
}
pub fn proto_model_mod_file(&self) -> String {
path_string_with_component(&self.create_output_dir(), vec!["mod.rs"])
path_string_with_component(&self.protobuf_crate_path(), vec!["mod.rs"])
}
}

View File

@ -104,6 +104,7 @@ pub fn path_string_with_component(path: &Path, components: Vec<&str>) -> String
path_buf_with_component(path, components).to_str().unwrap().to_string()
}
#[allow(dead_code)]
pub fn path_buf_with_component(path: &Path, components: Vec<&str>) -> PathBuf {
let mut path_buf = path.to_path_buf();
for component in components {

View File

@ -1,4 +1,4 @@
proto_crates = ["src/errors.rs", "src/msg.rs"]
proto_output_dir = "resources/proto"
protobuf_crate_path = "src/protobuf"
proto_rust_file_input_dir = ["src/errors.rs", "src/msg.rs"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []