Merge pull request #563 from AppFlowy-IO/feat/default_flowy_config

Feat/default flowy config
This commit is contained in:
Nathan.fooo 2022-06-17 12:24:48 +08:00 committed by GitHub
commit 40bcb63627
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 77 additions and 88 deletions

View File

@ -1,4 +1,2 @@
proto_rust_file_input_dir = ["src/model"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/model"]

View File

@ -1,5 +1,2 @@
proto_rust_file_input_dir = ["src/entities"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/entities"]

View File

@ -1,5 +1,3 @@
proto_rust_file_input_dir = ["src/errors.rs",]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/errors.rs",]

View File

@ -1,5 +1,3 @@
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"
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/entities", "src/event_map.rs", "src/dart_notification.rs"]
event_files = ["src/event_map.rs"]

View File

@ -1,9 +1,8 @@
proto_rust_file_input_dir = [
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = [
"src/event_map.rs",
"src/services/field/type_options",
"src/entities",
"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,4 +1,3 @@
proto_rust_file_input_dir = ["src/event_map.rs", "src/entities"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/event_map.rs", "src/entities"]
event_files = ["src/event_map.rs"]

View File

@ -1,5 +1,3 @@
proto_rust_file_input_dir = ["src/event_map.rs", "src/entities.rs"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/event_map.rs", "src/entities.rs"]
event_files = ["src/event_map.rs"]

View File

@ -1,5 +1,3 @@
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"
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/event_map.rs", "src/dart_notification.rs"]
event_files = ["src/event_map.rs"]

View File

@ -1,5 +1,2 @@
proto_rust_file_input_dir = ["src/code.rs"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/code.rs"]

View File

@ -1,5 +1,2 @@
proto_rust_file_input_dir = ["src/entities",]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/entities",]

View File

@ -1,5 +1,2 @@
proto_rust_file_input_dir = ["src/entities/",]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/entities/",]

View File

@ -1,5 +1,2 @@
proto_rust_file_input_dir = ["src/entities"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/entities"]

View File

@ -1,5 +1,2 @@
proto_rust_file_input_dir = ["src/entities", "src/errors.rs"]
proto_file_output_dir = "resources/proto"
protobuf_crate_output_dir = "src/protobuf"
event_files = []
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/entities", "src/errors.rs"]

View File

@ -3,10 +3,28 @@ use std::path::{Path, PathBuf};
#[derive(serde::Deserialize, Clone, Debug)]
pub struct FlowyConfig {
#[serde(default)]
pub event_files: Vec<String>,
pub proto_rust_file_input_dir: Vec<String>,
pub proto_file_output_dir: String,
pub protobuf_crate_output_dir: String,
// Collect AST from the file or directory specified by proto_input to generate the proto files.
#[serde(default)]
pub proto_input: Vec<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 {

View File

@ -18,11 +18,11 @@ pub fn parse_protobuf_context_from(crate_paths: Vec<String>) -> Vec<ProtobufCrat
let contexts = crate_infos
.into_iter()
.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
.proto_rust_file_paths()
.proto_input_paths()
.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()
.collect::<Vec<ProtoFile>>();
@ -33,7 +33,7 @@ pub fn parse_protobuf_context_from(crate_paths: Vec<String>) -> Vec<ProtobufCrat
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![];
// 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)
@ -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));
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 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());
structs.iter().for_each(|s| {
@ -153,7 +153,7 @@ lazy_static! {
fn find_proto_syntax(path: &str) -> String {
if !Path::new(path).exists() {
return String::from("syntax = \"proto3\";\\n");
return String::from("syntax = \"proto3\";\n\n");
}
let mut result = String::new();

View File

@ -27,10 +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_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();
let proto_file_output_path = proto_crate.proto_output_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()
.filter_map(|e| e.ok())
.map(|e| {
@ -52,7 +52,7 @@ pub fn gen(crate_name: &str) {
#[cfg(feature = "dart")]
generate_dart_protobuf_files(
crate_name,
&proto_file_output_dir,
&proto_file_output_path,
&proto_file_paths,
&file_names,
&protoc_bin_path,
@ -62,8 +62,8 @@ pub fn gen(crate_name: &str) {
generate_rust_protobuf_files(
&protoc_bin_path,
&proto_file_paths,
&proto_file_output_dir,
&protobuf_output_dir,
&proto_file_output_path,
&protobuf_output_path,
);
}
}
@ -71,14 +71,14 @@ pub fn gen(crate_name: &str) {
fn generate_rust_protobuf_files(
protoc_bin_path: &Path,
proto_file_paths: &[String],
proto_file_output_dir: &str,
protobuf_output_dir: &str,
proto_file_output_path: &str,
protobuf_output_path: &str,
) {
protoc_rust::Codegen::new()
.out_dir(protobuf_output_dir)
.out_dir(protobuf_output_path)
.protoc_path(protoc_bin_path)
.inputs(proto_file_paths)
.include(proto_file_output_dir)
.include(proto_file_output_path)
.run()
.expect("Running rust protoc failed.");
}
@ -86,7 +86,7 @@ fn generate_rust_protobuf_files(
#[cfg(feature = "dart")]
fn generate_dart_protobuf_files(
name: &str,
proto_file_output_dir: &str,
proto_file_output_path: &str,
paths: &Vec<String>,
file_names: &Vec<String>,
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();
paths.iter().for_each(|path| {
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()
{

View File

@ -50,7 +50,7 @@ impl ProtoGenerator {
fn write_proto_files(crate_contexts: &[ProtobufCrateContext]) {
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| {
let proto_file = format!("{}.proto", &info.file_name);
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("// Auto-generated, do not edit\n");
walk_dir(
context.protobuf_crate.proto_file_output_dir(),
context.protobuf_crate.proto_output_path(),
|e| !e.file_type().is_dir(),
|_, 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
.proto_rust_file_input_dir
.proto_input
.iter()
.map(|name| path_buf_with_component(&self.crate_path, vec![name]))
.collect::<Vec<PathBuf>>()
}
// The protobuf_crate_path is used to store the generated protobuf Rust structures.
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);
crate_path
}
pub fn proto_file_output_dir(&self) -> PathBuf {
let output_dir = PathBuf::from(&self.flowy_config.proto_file_output_dir);
// The proto_output_path is used to store the proto files
pub fn proto_output_path(&self) -> PathBuf {
let output_dir = PathBuf::from(&self.flowy_config.proto_output);
create_dir_if_not_exist(&output_dir);
output_dir
}

View File

@ -1,4 +1,2 @@
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 = []
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/errors.rs", "src/msg.rs"]