chore: add description for FlowyConfig

This commit is contained in:
appflowy 2022-06-17 11:19:49 +08:00
parent 233a9a0692
commit 16e1387a8e
19 changed files with 88 additions and 70 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,9 +4,24 @@ use std::path::{Path, PathBuf};
#[derive(serde::Deserialize, Clone, Debug)] #[derive(serde::Deserialize, Clone, Debug)]
pub struct FlowyConfig { pub struct FlowyConfig {
pub event_files: Vec<String>, pub event_files: Vec<String>,
pub proto_rust_file_input_dir: Vec<String>, // Collect AST from the file or directory specified by proto_input to generate the proto files.
pub proto_file_output_dir: String, pub proto_input: Vec<String>,
pub protobuf_crate_output_dir: String,
// Output path for the generated proto files. The default value is default_proto_output()
#[serde(default = "default_proto_output")]
pub proto_output: String,
// Create a crate that stores the generated protobuf Rust structures. The default value is default_protobuf_crate()
#[serde(default = "default_protobuf_crate")]
pub protobuf_crate_path: String,
}
fn default_proto_output() -> String {
"resources/proto".to_owned()
}
fn default_protobuf_crate() -> String {
"src/protobuf".to_owned()
} }
impl FlowyConfig { impl FlowyConfig {

View File

@ -18,11 +18,11 @@ pub fn parse_protobuf_context_from(crate_paths: Vec<String>) -> Vec<ProtobufCrat
let contexts = crate_infos let contexts = crate_infos
.into_iter() .into_iter()
.map(|crate_info| { .map(|crate_info| {
let proto_output_dir = crate_info.proto_file_output_dir(); let proto_output_path = crate_info.proto_output_path();
let files = crate_info let files = crate_info
.proto_rust_file_paths() .proto_input_paths()
.iter() .iter()
.map(|proto_crate_path| parse_files_protobuf(proto_crate_path, &proto_output_dir)) .map(|proto_crate_path| parse_files_protobuf(proto_crate_path, &proto_output_path))
.flatten() .flatten()
.collect::<Vec<ProtoFile>>(); .collect::<Vec<ProtoFile>>();
@ -33,7 +33,7 @@ pub fn parse_protobuf_context_from(crate_paths: Vec<String>) -> Vec<ProtobufCrat
contexts contexts
} }
fn parse_files_protobuf(proto_crate_path: &Path, proto_output_dir: &Path) -> Vec<ProtoFile> { fn parse_files_protobuf(proto_crate_path: &Path, proto_output_path: &Path) -> Vec<ProtoFile> {
let mut gen_proto_vec: Vec<ProtoFile> = vec![]; let mut gen_proto_vec: Vec<ProtoFile> = vec![];
// file_stem https://doc.rust-lang.org/std/path/struct.Path.html#method.file_stem // file_stem https://doc.rust-lang.org/std/path/struct.Path.html#method.file_stem
for (path, file_name) in WalkDir::new(proto_crate_path) for (path, file_name) in WalkDir::new(proto_crate_path)
@ -56,7 +56,7 @@ fn parse_files_protobuf(proto_crate_path: &Path, proto_output_dir: &Path) -> Vec
.unwrap_or_else(|_| panic!("Unable to parse file at {}", path)); .unwrap_or_else(|_| panic!("Unable to parse file at {}", path));
let structs = get_ast_structs(&ast); let structs = get_ast_structs(&ast);
let proto_file = format!("{}.proto", &file_name); let proto_file = format!("{}.proto", &file_name);
let proto_file_path = path_string_with_component(proto_output_dir, vec![&proto_file]); let proto_file_path = path_string_with_component(proto_output_path, vec![&proto_file]);
let mut proto_file_content = find_proto_syntax(proto_file_path.as_ref()); let mut proto_file_content = find_proto_syntax(proto_file_path.as_ref());
structs.iter().for_each(|s| { structs.iter().for_each(|s| {
@ -153,7 +153,7 @@ lazy_static! {
fn find_proto_syntax(path: &str) -> String { fn find_proto_syntax(path: &str) -> String {
if !Path::new(path).exists() { if !Path::new(path).exists() {
return String::from("syntax = \"proto3\";\\n"); return String::from("syntax = \"proto3\";\n\n");
} }
let mut result = String::new(); let mut result = String::new();

View File

@ -27,10 +27,10 @@ pub fn gen(crate_name: &str) {
for proto_crate in proto_crates { for proto_crate in proto_crates {
let mut proto_file_paths = vec![]; let mut proto_file_paths = vec![];
let mut file_names = vec![]; let mut file_names = vec![];
let proto_file_output_dir = proto_crate.proto_file_output_dir().to_str().unwrap().to_string(); let proto_file_output_path = proto_crate.proto_output_path().to_str().unwrap().to_string();
let protobuf_output_dir = proto_crate.protobuf_crate_path().to_str().unwrap().to_string(); let protobuf_output_path = proto_crate.protobuf_crate_path().to_str().unwrap().to_string();
for (path, file_name) in WalkDir::new(&proto_file_output_dir) for (path, file_name) in WalkDir::new(&proto_file_output_path)
.into_iter() .into_iter()
.filter_map(|e| e.ok()) .filter_map(|e| e.ok())
.map(|e| { .map(|e| {
@ -52,7 +52,7 @@ pub fn gen(crate_name: &str) {
#[cfg(feature = "dart")] #[cfg(feature = "dart")]
generate_dart_protobuf_files( generate_dart_protobuf_files(
crate_name, crate_name,
&proto_file_output_dir, &proto_file_output_path,
&proto_file_paths, &proto_file_paths,
&file_names, &file_names,
&protoc_bin_path, &protoc_bin_path,
@ -62,8 +62,8 @@ pub fn gen(crate_name: &str) {
generate_rust_protobuf_files( generate_rust_protobuf_files(
&protoc_bin_path, &protoc_bin_path,
&proto_file_paths, &proto_file_paths,
&proto_file_output_dir, &proto_file_output_path,
&protobuf_output_dir, &protobuf_output_path,
); );
} }
} }
@ -71,14 +71,14 @@ pub fn gen(crate_name: &str) {
fn generate_rust_protobuf_files( fn generate_rust_protobuf_files(
protoc_bin_path: &Path, protoc_bin_path: &Path,
proto_file_paths: &[String], proto_file_paths: &[String],
proto_file_output_dir: &str, proto_file_output_path: &str,
protobuf_output_dir: &str, protobuf_output_path: &str,
) { ) {
protoc_rust::Codegen::new() protoc_rust::Codegen::new()
.out_dir(protobuf_output_dir) .out_dir(protobuf_output_path)
.protoc_path(protoc_bin_path) .protoc_path(protoc_bin_path)
.inputs(proto_file_paths) .inputs(proto_file_paths)
.include(proto_file_output_dir) .include(proto_file_output_path)
.run() .run()
.expect("Running rust protoc failed."); .expect("Running rust protoc failed.");
} }
@ -86,7 +86,7 @@ fn generate_rust_protobuf_files(
#[cfg(feature = "dart")] #[cfg(feature = "dart")]
fn generate_dart_protobuf_files( fn generate_dart_protobuf_files(
name: &str, name: &str,
proto_file_output_dir: &str, proto_file_output_path: &str,
paths: &Vec<String>, paths: &Vec<String>,
file_names: &Vec<String>, file_names: &Vec<String>,
protoc_bin_path: &PathBuf, protoc_bin_path: &PathBuf,
@ -115,7 +115,7 @@ fn generate_dart_protobuf_files(
let protoc_bin_path = protoc_bin_path.to_str().unwrap().to_owned(); let protoc_bin_path = protoc_bin_path.to_str().unwrap().to_owned();
paths.iter().for_each(|path| { paths.iter().for_each(|path| {
if cmd_lib::run_cmd! { if cmd_lib::run_cmd! {
${protoc_bin_path} --dart_out=${output} --proto_path=${proto_file_output_dir} ${path} ${protoc_bin_path} --dart_out=${output} --proto_path=${proto_file_output_path} ${path}
} }
.is_err() .is_err()
{ {

View File

@ -50,7 +50,7 @@ impl ProtoGenerator {
fn write_proto_files(crate_contexts: &[ProtobufCrateContext]) { fn write_proto_files(crate_contexts: &[ProtobufCrateContext]) {
for context in crate_contexts { for context in crate_contexts {
let dir = context.protobuf_crate.proto_file_output_dir(); let dir = context.protobuf_crate.proto_output_path();
context.files.iter().for_each(|info| { context.files.iter().for_each(|info| {
let proto_file = format!("{}.proto", &info.file_name); let proto_file = format!("{}.proto", &info.file_name);
let proto_file_path = path_string_with_component(&dir, vec![&proto_file]); let proto_file_path = path_string_with_component(&dir, vec![&proto_file]);
@ -75,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("#![cfg_attr(rustfmt, rustfmt::skip)]\n");
mod_file_content.push_str("// Auto-generated, do not edit\n"); mod_file_content.push_str("// Auto-generated, do not edit\n");
walk_dir( walk_dir(
context.protobuf_crate.proto_file_output_dir(), context.protobuf_crate.proto_output_path(),
|e| !e.file_type().is_dir(), |e| !e.file_type().is_dir(),
|_, name| { |_, name| {
let c = format!("\nmod {};\npub use {}::*;\n", &name, &name); let c = format!("\nmod {};\npub use {}::*;\n", &name, &name);

View File

@ -73,22 +73,25 @@ impl ProtobufCrate {
} }
} }
pub fn proto_rust_file_paths(&self) -> Vec<PathBuf> { // Return the file paths for each rust file that used to generate the proto file.
pub fn proto_input_paths(&self) -> Vec<PathBuf> {
self.flowy_config self.flowy_config
.proto_rust_file_input_dir .proto_input
.iter() .iter()
.map(|name| path_buf_with_component(&self.crate_path, vec![name])) .map(|name| path_buf_with_component(&self.crate_path, vec![name]))
.collect::<Vec<PathBuf>>() .collect::<Vec<PathBuf>>()
} }
// The protobuf_crate_path is used to store the generated protobuf Rust structures.
pub fn protobuf_crate_path(&self) -> PathBuf { pub fn protobuf_crate_path(&self) -> PathBuf {
let crate_path = PathBuf::from(&self.flowy_config.protobuf_crate_output_dir); let crate_path = PathBuf::from(&self.flowy_config.protobuf_crate_path);
create_dir_if_not_exist(&crate_path); create_dir_if_not_exist(&crate_path);
crate_path crate_path
} }
pub fn proto_file_output_dir(&self) -> PathBuf { // The proto_output_path is used to store the proto files
let output_dir = PathBuf::from(&self.flowy_config.proto_file_output_dir); pub fn proto_output_path(&self) -> PathBuf {
let output_dir = PathBuf::from(&self.flowy_config.proto_output);
create_dir_if_not_exist(&output_dir); create_dir_if_not_exist(&output_dir);
output_dir output_dir
} }

View File

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