From 0933935071ca64b95976399ef4ad41ed36d2b714 Mon Sep 17 00:00:00 2001 From: appflowy Date: Tue, 15 Feb 2022 21:27:00 +0800 Subject: [PATCH 1/9] refactor: add code_gen crate --- frontend/rust-lib/dart-ffi/build.rs | 4 +- frontend/rust-lib/dart-notify/build.rs | 4 +- frontend/rust-lib/flowy-error/build.rs | 4 +- frontend/rust-lib/flowy-folder/build.rs | 4 +- frontend/rust-lib/flowy-net/build.rs | 4 +- frontend/rust-lib/flowy-user/build.rs | 4 +- shared-lib/flowy-collaboration/build.rs | 4 +- shared-lib/flowy-derive/src/proto_buf/util.rs | 2 +- shared-lib/flowy-error-code/build.rs | 4 +- shared-lib/flowy-folder-data-model/build.rs | 4 +- shared-lib/flowy-user-data-model/build.rs | 4 +- shared-lib/lib-infra/Cargo.toml | 1 + .../src/code_gen/dart_event/dart_event.rs | 120 ++++++++++++++++++ .../src/code_gen/dart_event/event_template.rs | 69 ++++++++++ .../code_gen/dart_event/event_template.tera | 49 +++++++ .../lib-infra/src/code_gen/dart_event/mod.rs | 5 + .../src/{proto_gen => code_gen}/flowy_toml.rs | 6 +- shared-lib/lib-infra/src/code_gen/mod.rs | 11 ++ .../protobuf_file}/ast.rs | 10 +- .../{pb.rs => code_gen/protobuf_file/mod.rs} | 10 +- .../protobuf_file}/proto_gen.rs | 12 +- .../protobuf_file}/proto_info.rs | 4 +- .../template/derive_meta/derive_meta.rs | 4 +- .../template/derive_meta/derive_meta.tera | 0 .../template/derive_meta/mod.rs | 0 .../code_gen/protobuf_file/template/mod.rs | 5 + .../template/proto_file/enum.tera | 0 .../template/proto_file/enum_template.rs | 6 +- .../protobuf_file}/template/proto_file/mod.rs | 0 .../template/proto_file/struct.tera | 0 .../template/proto_file/struct_template.rs | 5 +- .../src/{proto_gen => code_gen}/util.rs | 30 ++++- shared-lib/lib-infra/src/lib.rs | 7 +- shared-lib/lib-infra/src/proto_gen/mod.rs | 10 -- .../lib-infra/src/proto_gen/template/mod.rs | 47 ------- shared-lib/lib-ws/build.rs | 4 +- 36 files changed, 343 insertions(+), 114 deletions(-) create mode 100644 shared-lib/lib-infra/src/code_gen/dart_event/dart_event.rs create mode 100644 shared-lib/lib-infra/src/code_gen/dart_event/event_template.rs create mode 100644 shared-lib/lib-infra/src/code_gen/dart_event/event_template.tera create mode 100644 shared-lib/lib-infra/src/code_gen/dart_event/mod.rs rename shared-lib/lib-infra/src/{proto_gen => code_gen}/flowy_toml.rs (91%) create mode 100644 shared-lib/lib-infra/src/code_gen/mod.rs rename shared-lib/lib-infra/src/{proto_gen => code_gen/protobuf_file}/ast.rs (94%) rename shared-lib/lib-infra/src/{pb.rs => code_gen/protobuf_file/mod.rs} (97%) rename shared-lib/lib-infra/src/{proto_gen => code_gen/protobuf_file}/proto_gen.rs (91%) rename shared-lib/lib-infra/src/{proto_gen => code_gen/protobuf_file}/proto_info.rs (97%) rename shared-lib/lib-infra/src/{proto_gen => code_gen/protobuf_file}/template/derive_meta/derive_meta.rs (88%) rename shared-lib/lib-infra/src/{proto_gen => code_gen/protobuf_file}/template/derive_meta/derive_meta.tera (100%) rename shared-lib/lib-infra/src/{proto_gen => code_gen/protobuf_file}/template/derive_meta/mod.rs (100%) create mode 100644 shared-lib/lib-infra/src/code_gen/protobuf_file/template/mod.rs rename shared-lib/lib-infra/src/{proto_gen => code_gen/protobuf_file}/template/proto_file/enum.tera (100%) rename shared-lib/lib-infra/src/{proto_gen => code_gen/protobuf_file}/template/proto_file/enum_template.rs (84%) rename shared-lib/lib-infra/src/{proto_gen => code_gen/protobuf_file}/template/proto_file/mod.rs (100%) rename shared-lib/lib-infra/src/{proto_gen => code_gen/protobuf_file}/template/proto_file/struct.tera (100%) rename shared-lib/lib-infra/src/{proto_gen => code_gen/protobuf_file}/template/proto_file/struct_template.rs (97%) rename shared-lib/lib-infra/src/{proto_gen => code_gen}/util.rs (81%) delete mode 100644 shared-lib/lib-infra/src/proto_gen/mod.rs delete mode 100644 shared-lib/lib-infra/src/proto_gen/template/mod.rs diff --git a/frontend/rust-lib/dart-ffi/build.rs b/frontend/rust-lib/dart-ffi/build.rs index ef2b12f93d..7f06ab48c8 100644 --- a/frontend/rust-lib/dart-ffi/build.rs +++ b/frontend/rust-lib/dart-ffi/build.rs @@ -1,5 +1,5 @@ -use lib_infra::pb; +use lib_infra::code_gen; fn main() { - pb::gen_files(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); + code_gen::protobuf_file::gen(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); } diff --git a/frontend/rust-lib/dart-notify/build.rs b/frontend/rust-lib/dart-notify/build.rs index ef2b12f93d..7f06ab48c8 100644 --- a/frontend/rust-lib/dart-notify/build.rs +++ b/frontend/rust-lib/dart-notify/build.rs @@ -1,5 +1,5 @@ -use lib_infra::pb; +use lib_infra::code_gen; fn main() { - pb::gen_files(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); + code_gen::protobuf_file::gen(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); } diff --git a/frontend/rust-lib/flowy-error/build.rs b/frontend/rust-lib/flowy-error/build.rs index ca23d51872..1556b7749f 100644 --- a/frontend/rust-lib/flowy-error/build.rs +++ b/frontend/rust-lib/flowy-error/build.rs @@ -1,5 +1,5 @@ -use lib_infra::pb; +use lib_infra::code_gen; fn main() { - pb::gen_files("flowy-error", "./src/protobuf/proto"); + code_gen::protobuf_file::gen("flowy-error", "./src/protobuf/proto"); } diff --git a/frontend/rust-lib/flowy-folder/build.rs b/frontend/rust-lib/flowy-folder/build.rs index ef2b12f93d..7f06ab48c8 100644 --- a/frontend/rust-lib/flowy-folder/build.rs +++ b/frontend/rust-lib/flowy-folder/build.rs @@ -1,5 +1,5 @@ -use lib_infra::pb; +use lib_infra::code_gen; fn main() { - pb::gen_files(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); + code_gen::protobuf_file::gen(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); } diff --git a/frontend/rust-lib/flowy-net/build.rs b/frontend/rust-lib/flowy-net/build.rs index ef2b12f93d..7f06ab48c8 100644 --- a/frontend/rust-lib/flowy-net/build.rs +++ b/frontend/rust-lib/flowy-net/build.rs @@ -1,5 +1,5 @@ -use lib_infra::pb; +use lib_infra::code_gen; fn main() { - pb::gen_files(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); + code_gen::protobuf_file::gen(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); } diff --git a/frontend/rust-lib/flowy-user/build.rs b/frontend/rust-lib/flowy-user/build.rs index ef2b12f93d..7f06ab48c8 100644 --- a/frontend/rust-lib/flowy-user/build.rs +++ b/frontend/rust-lib/flowy-user/build.rs @@ -1,5 +1,5 @@ -use lib_infra::pb; +use lib_infra::code_gen; fn main() { - pb::gen_files(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); + code_gen::protobuf_file::gen(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); } diff --git a/shared-lib/flowy-collaboration/build.rs b/shared-lib/flowy-collaboration/build.rs index ef2b12f93d..7f06ab48c8 100644 --- a/shared-lib/flowy-collaboration/build.rs +++ b/shared-lib/flowy-collaboration/build.rs @@ -1,5 +1,5 @@ -use lib_infra::pb; +use lib_infra::code_gen; fn main() { - pb::gen_files(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); + code_gen::protobuf_file::gen(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); } diff --git a/shared-lib/flowy-derive/src/proto_buf/util.rs b/shared-lib/flowy-derive/src/proto_buf/util.rs index 7260d97844..e214de0f40 100644 --- a/shared-lib/flowy-derive/src/proto_buf/util.rs +++ b/shared-lib/flowy-derive/src/proto_buf/util.rs @@ -1,7 +1,7 @@ use dashmap::{DashMap, DashSet}; use flowy_ast::{Ctxt, TyInfo}; use lazy_static::lazy_static; -use lib_infra::proto_gen::ProtoCache; +use lib_infra::code_gen::protobuf_file::ProtoCache; use std::fs::File; use std::io::Read; use std::sync::atomic::{AtomicBool, Ordering}; diff --git a/shared-lib/flowy-error-code/build.rs b/shared-lib/flowy-error-code/build.rs index ef2b12f93d..7f06ab48c8 100644 --- a/shared-lib/flowy-error-code/build.rs +++ b/shared-lib/flowy-error-code/build.rs @@ -1,5 +1,5 @@ -use lib_infra::pb; +use lib_infra::code_gen; fn main() { - pb::gen_files(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); + code_gen::protobuf_file::gen(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); } diff --git a/shared-lib/flowy-folder-data-model/build.rs b/shared-lib/flowy-folder-data-model/build.rs index ef2b12f93d..7f06ab48c8 100644 --- a/shared-lib/flowy-folder-data-model/build.rs +++ b/shared-lib/flowy-folder-data-model/build.rs @@ -1,5 +1,5 @@ -use lib_infra::pb; +use lib_infra::code_gen; fn main() { - pb::gen_files(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); + code_gen::protobuf_file::gen(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); } diff --git a/shared-lib/flowy-user-data-model/build.rs b/shared-lib/flowy-user-data-model/build.rs index ef2b12f93d..7f06ab48c8 100644 --- a/shared-lib/flowy-user-data-model/build.rs +++ b/shared-lib/flowy-user-data-model/build.rs @@ -1,5 +1,5 @@ -use lib_infra::pb; +use lib_infra::code_gen; fn main() { - pb::gen_files(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); + code_gen::protobuf_file::gen(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); } diff --git a/shared-lib/lib-infra/Cargo.toml b/shared-lib/lib-infra/Cargo.toml index 8d667bfc3e..47e9d77fdf 100644 --- a/shared-lib/lib-infra/Cargo.toml +++ b/shared-lib/lib-infra/Cargo.toml @@ -48,4 +48,5 @@ proto_gen = [ "toml" ] pb_gen = ["cmd_lib", "protoc-rust", "walkdir", "protoc-bin-vendored",] +dart_event = ["walkdir", "flowy-ast", "tera", "syn"] dart = ["proto_gen"] \ No newline at end of file diff --git a/shared-lib/lib-infra/src/code_gen/dart_event/dart_event.rs b/shared-lib/lib-infra/src/code_gen/dart_event/dart_event.rs new file mode 100644 index 0000000000..a9ba2ddcdc --- /dev/null +++ b/shared-lib/lib-infra/src/code_gen/dart_event/dart_event.rs @@ -0,0 +1,120 @@ +use super::event_template::*; +use crate::code_gen::flowy_toml::{parse_crate_config_from, CrateConfig}; +use crate::code_gen::util::{is_crate_dir, is_hidden, read_file, save_content_to_file_with_diff_prompt}; +use flowy_ast::{event_ast::*, *}; +use syn::Item; +use walkdir::WalkDir; + +pub struct DartEventCodeGen(); + +impl DartEventCodeGen { + pub fn gen(crate_name: &str, crate_path: &str) { + let event_crates = parse_dart_event_files(vec![crate_path.to_owned()]); + let event_ast = event_crates.iter().map(parse_event_crate).flatten().collect::>(); + + let event_render_ctx = ast_to_event_render_ctx(event_ast.as_ref()); + let mut render_result = String::new(); + for (index, render_ctx) in event_render_ctx.into_iter().enumerate() { + let mut event_template = EventTemplate::new(); + + if let Some(content) = event_template.render(render_ctx, index) { + render_result.push_str(content.as_ref()) + } + } + + save_content_to_file_with_diff_prompt(render_result.as_ref(), "."); + } +} + +pub struct DartEventCrate { + crate_path: String, + #[allow(dead_code)] + crate_name: String, + event_files: Vec, +} + +impl DartEventCrate { + pub fn from_config(config: &CrateConfig) -> Self { + DartEventCrate { + crate_path: config.crate_path.clone(), + crate_name: config.folder_name.clone(), + event_files: config.flowy_config.event_files.clone(), + } + } +} + +pub fn parse_dart_event_files(crate_paths: Vec) -> Vec { + let mut dart_event_crates: Vec = vec![]; + crate_paths.iter().for_each(|root| { + let crates = WalkDir::new(root) + .into_iter() + .filter_entry(|e| !is_hidden(e)) + .filter_map(|e| e.ok()) + .filter(is_crate_dir) + .flat_map(|e| parse_crate_config_from(&e)) + .map(|crate_config| DartEventCrate::from_config(&crate_config)) + .collect::>(); + dart_event_crates.extend(crates); + }); + dart_event_crates +} + +pub fn parse_event_crate(event_crate: &DartEventCrate) -> Vec { + event_crate + .event_files + .iter() + .map(|event_file| { + let file_path = format!("{}/{}", event_crate.crate_path, event_file); + let file_content = read_file(file_path.as_ref()).unwrap(); + let ast = syn::parse_file(file_content.as_ref()).expect("Unable to parse file"); + ast.items + .iter() + .map(|item| match item { + Item::Enum(item_enum) => { + let ctxt = Ctxt::new(); + let attrs = + flowy_ast::enum_from_ast(&ctxt, &item_enum.ident, &item_enum.variants, &item_enum.attrs); + ctxt.check().unwrap(); + attrs + .iter() + .filter(|attr| !attr.attrs.event_attrs.ignore) + .enumerate() + .map(|(_index, attr)| EventASTContext::from(&attr.attrs)) + .collect::>() + } + _ => vec![], + }) + .flatten() + .collect::>() + }) + .flatten() + .collect::>() +} + +pub fn ast_to_event_render_ctx(ast: &[EventASTContext]) -> Vec { + ast.iter() + .map(|event_ast| { + let input_deserializer = event_ast + .event_input + .as_ref() + .map(|event_input| event_input.get_ident().unwrap().to_string()); + + let output_deserializer = event_ast + .event_output + .as_ref() + .map(|event_output| event_output.get_ident().unwrap().to_string()); + // eprintln!( + // "😁 {:?} / {:?}", + // event_ast.event_input, event_ast.event_output + // ); + + EventRenderContext { + input_deserializer, + output_deserializer, + error_deserializer: event_ast.event_error.clone(), + event: event_ast.event.to_string(), + event_ty: event_ast.event_ty.to_string(), + } + }) + .collect::>() +} diff --git a/shared-lib/lib-infra/src/code_gen/dart_event/event_template.rs b/shared-lib/lib-infra/src/code_gen/dart_event/event_template.rs new file mode 100644 index 0000000000..cc880e74bf --- /dev/null +++ b/shared-lib/lib-infra/src/code_gen/dart_event/event_template.rs @@ -0,0 +1,69 @@ +use crate::code_gen::util::get_tera; +use tera::Context; + +pub struct EventTemplate { + tera_context: Context, +} + +pub const DART_IMPORTED: &str = r#" +/// Auto gen code from rust ast, do not edit +part of 'dispatch.dart'; +"#; + +pub struct EventRenderContext { + pub input_deserializer: Option, + pub output_deserializer: Option, + pub error_deserializer: String, + pub event: String, + pub event_ty: String, +} + +#[allow(dead_code)] +impl EventTemplate { + pub fn new() -> Self { + EventTemplate { + tera_context: Context::new(), + } + } + + pub fn render(&mut self, ctx: EventRenderContext, index: usize) -> Option { + if index == 0 { + self.tera_context.insert("imported_dart_files", DART_IMPORTED) + } + self.tera_context.insert("index", &index); + let dart_class_name = format!("{}{}", ctx.event_ty, ctx.event); + let event = format!("{}.{}", ctx.event_ty, ctx.event); + self.tera_context.insert("event_class", &dart_class_name); + self.tera_context.insert("event", &event); + + self.tera_context.insert("has_input", &ctx.input_deserializer.is_some()); + match ctx.input_deserializer { + None => self.tera_context.insert("input_deserializer", "Unit"), + Some(ref input) => self.tera_context.insert("input_deserializer", input), + } + + // eprintln!( + // "😁 {:?} / {:?}", + // &ctx.input_deserializer, &ctx.output_deserializer + // ); + + let has_output = ctx.output_deserializer.is_some(); + self.tera_context.insert("has_output", &has_output); + + match ctx.output_deserializer { + None => self.tera_context.insert("output_deserializer", "Unit"), + Some(ref output) => self.tera_context.insert("output_deserializer", output), + } + + self.tera_context.insert("error_deserializer", &ctx.error_deserializer); + + let tera = get_tera("dart_event"); + match tera.render("event_template.tera", &self.tera_context) { + Ok(r) => Some(r), + Err(e) => { + log::error!("{:?}", e); + None + } + } + } +} diff --git a/shared-lib/lib-infra/src/code_gen/dart_event/event_template.tera b/shared-lib/lib-infra/src/code_gen/dart_event/event_template.tera new file mode 100644 index 0000000000..aac3012fb6 --- /dev/null +++ b/shared-lib/lib-infra/src/code_gen/dart_event/event_template.tera @@ -0,0 +1,49 @@ +{%- if index == 0 %} +{{ imported_dart_files }} +{%- endif -%} + +class {{ event_class }} { +{%- if has_input %} + {{ input_deserializer }} request; + {{ event_class }}(this.request); +{%- else %} + {{ event_class }}(); +{%- endif %} + + Future> send() { + +{%- if has_input %} + final request = FFIRequest.create() + ..event = {{ event }}.toString() + ..payload = requestToBytes(this.request); + + return Dispatch.asyncRequest(request) + .then((bytesResult) => bytesResult.fold( + + {%- if has_output %} + (okBytes) => left({{ output_deserializer }}.fromBuffer(okBytes)), + {%- else %} + (bytes) => left(unit), + {%- endif %} + (errBytes) => right({{ error_deserializer }}.fromBuffer(errBytes)), + )); + +{%- else %} + final request = FFIRequest.create() + ..event = {{ event }}.toString(); + {%- if has_input %} + ..payload = bytes; + {%- endif %} + + return Dispatch.asyncRequest(request).then((bytesResult) => bytesResult.fold( + {%- if has_output %} + (okBytes) => left({{ output_deserializer }}.fromBuffer(okBytes)), + {%- else %} + (bytes) => left(unit), + {%- endif %} + (errBytes) => right({{ error_deserializer }}.fromBuffer(errBytes)), + )); +{%- endif %} + } +} + diff --git a/shared-lib/lib-infra/src/code_gen/dart_event/mod.rs b/shared-lib/lib-infra/src/code_gen/dart_event/mod.rs new file mode 100644 index 0000000000..2b6423c2bc --- /dev/null +++ b/shared-lib/lib-infra/src/code_gen/dart_event/mod.rs @@ -0,0 +1,5 @@ +#![allow(clippy::module_inception)] +mod dart_event; +mod event_template; + +pub use dart_event::*; diff --git a/shared-lib/lib-infra/src/proto_gen/flowy_toml.rs b/shared-lib/lib-infra/src/code_gen/flowy_toml.rs similarity index 91% rename from shared-lib/lib-infra/src/proto_gen/flowy_toml.rs rename to shared-lib/lib-infra/src/code_gen/flowy_toml.rs index 0c38166fcb..954731ee02 100644 --- a/shared-lib/lib-infra/src/proto_gen/flowy_toml.rs +++ b/shared-lib/lib-infra/src/code_gen/flowy_toml.rs @@ -15,9 +15,9 @@ impl FlowyConfig { } pub struct CrateConfig { - pub(crate) crate_path: String, - pub(crate) folder_name: String, - pub(crate) flowy_config: FlowyConfig, + pub crate_path: String, + pub folder_name: String, + pub flowy_config: FlowyConfig, } impl CrateConfig { diff --git a/shared-lib/lib-infra/src/code_gen/mod.rs b/shared-lib/lib-infra/src/code_gen/mod.rs new file mode 100644 index 0000000000..32570614cb --- /dev/null +++ b/shared-lib/lib-infra/src/code_gen/mod.rs @@ -0,0 +1,11 @@ +#[cfg(feature = "pb_gen")] +pub mod protobuf_file; + +#[cfg(feature = "dart_event")] +pub mod dart_event; + +#[cfg(any(feature = "pb_gen", feature = "dart_event"))] +mod flowy_toml; + +#[cfg(any(feature = "pb_gen", feature = "dart_event"))] +mod util; diff --git a/shared-lib/lib-infra/src/proto_gen/ast.rs b/shared-lib/lib-infra/src/code_gen/protobuf_file/ast.rs similarity index 94% rename from shared-lib/lib-infra/src/proto_gen/ast.rs rename to shared-lib/lib-infra/src/code_gen/protobuf_file/ast.rs index d61d454240..8f7f1e052b 100644 --- a/shared-lib/lib-infra/src/proto_gen/ast.rs +++ b/shared-lib/lib-infra/src/code_gen/protobuf_file/ast.rs @@ -2,9 +2,9 @@ #![allow(dead_code)] #![allow(unused_imports)] #![allow(unused_results)] -use crate::proto_gen::template::{EnumTemplate, StructTemplate}; -use crate::proto_gen::util::*; -use crate::proto_gen::{parse_crate_info_from_path, ProtoFile, ProtobufCrateContext}; +use crate::code_gen::protobuf_file::template::{EnumTemplate, StructTemplate}; +use crate::code_gen::protobuf_file::{parse_crate_info_from_path, ProtoFile, ProtobufCrateContext}; +use crate::code_gen::util::*; use fancy_regex::Regex; use flowy_ast::*; use lazy_static::lazy_static; @@ -12,8 +12,8 @@ use std::{fs::File, io::Read, path::Path}; use syn::Item; use walkdir::WalkDir; -pub fn parse_crate_protobuf(roots: Vec) -> Vec { - let crate_infos = parse_crate_info_from_path(roots); +pub fn parse_crate_protobuf(crate_paths: Vec) -> Vec { + let crate_infos = parse_crate_info_from_path(crate_paths); crate_infos .into_iter() .map(|crate_info| { diff --git a/shared-lib/lib-infra/src/pb.rs b/shared-lib/lib-infra/src/code_gen/protobuf_file/mod.rs similarity index 97% rename from shared-lib/lib-infra/src/pb.rs rename to shared-lib/lib-infra/src/code_gen/protobuf_file/mod.rs index e9f64349d2..e7946ac311 100644 --- a/shared-lib/lib-infra/src/pb.rs +++ b/shared-lib/lib-infra/src/code_gen/protobuf_file/mod.rs @@ -1,9 +1,15 @@ #![allow(unused_imports)] #![allow(unused_attributes)] #![allow(dead_code)] +mod ast; +mod proto_gen; +mod proto_info; +mod template; + +pub use proto_gen::*; +pub use proto_info::*; #[cfg(feature = "proto_gen")] -use crate::proto_gen::*; use log::info; use std::fs::File; use std::io::Write; @@ -11,7 +17,7 @@ use std::path::PathBuf; use std::process::Command; use walkdir::WalkDir; -pub fn gen_files(crate_name: &str, proto_file_dir: &str) { +pub fn gen(crate_name: &str, proto_file_dir: &str) { // 1. generate the proto files to proto_file_dir #[cfg(feature = "proto_gen")] let _ = gen_protos(crate_name); diff --git a/shared-lib/lib-infra/src/proto_gen/proto_gen.rs b/shared-lib/lib-infra/src/code_gen/protobuf_file/proto_gen.rs similarity index 91% rename from shared-lib/lib-infra/src/proto_gen/proto_gen.rs rename to shared-lib/lib-infra/src/code_gen/protobuf_file/proto_gen.rs index bbeea67299..19618c6df4 100644 --- a/shared-lib/lib-infra/src/proto_gen/proto_gen.rs +++ b/shared-lib/lib-infra/src/code_gen/protobuf_file/proto_gen.rs @@ -2,17 +2,17 @@ #![allow(dead_code)] #![allow(unused_imports)] #![allow(unused_results)] -use crate::proto_gen::ast::parse_crate_protobuf; -use crate::proto_gen::proto_info::ProtobufCrateContext; -use crate::proto_gen::util::*; -use crate::proto_gen::ProtoFile; +use crate::code_gen::protobuf_file::ast::parse_crate_protobuf; +use crate::code_gen::protobuf_file::proto_info::ProtobufCrateContext; +use crate::code_gen::protobuf_file::ProtoFile; +use crate::code_gen::util::*; use std::fs::File; use std::path::Path; use std::{fs::OpenOptions, io::Write}; -pub(crate) struct ProtoGenerator(); +pub struct ProtoGenerator(); impl ProtoGenerator { - pub(crate) fn gen(crate_name: &str, crate_path: &str, cache_path: &str) -> Vec { + pub fn gen(crate_name: &str, crate_path: &str, cache_path: &str) -> Vec { let crate_contexts = parse_crate_protobuf(vec![crate_path.to_owned()]); write_proto_files(&crate_contexts); write_rust_crate_mod_file(&crate_contexts); diff --git a/shared-lib/lib-infra/src/proto_gen/proto_info.rs b/shared-lib/lib-infra/src/code_gen/protobuf_file/proto_info.rs similarity index 97% rename from shared-lib/lib-infra/src/proto_gen/proto_info.rs rename to shared-lib/lib-infra/src/code_gen/protobuf_file/proto_info.rs index 32b05e106c..2471ded9ff 100644 --- a/shared-lib/lib-infra/src/proto_gen/proto_info.rs +++ b/shared-lib/lib-infra/src/code_gen/protobuf_file/proto_info.rs @@ -1,6 +1,6 @@ #![allow(dead_code)] -use crate::proto_gen::flowy_toml::{parse_crate_config_from, CrateConfig}; -use crate::proto_gen::util::*; +use crate::code_gen::flowy_toml::{parse_crate_config_from, CrateConfig}; +use crate::code_gen::util::*; use std::fs::OpenOptions; use std::io::Write; use walkdir::WalkDir; diff --git a/shared-lib/lib-infra/src/proto_gen/template/derive_meta/derive_meta.rs b/shared-lib/lib-infra/src/code_gen/protobuf_file/template/derive_meta/derive_meta.rs similarity index 88% rename from shared-lib/lib-infra/src/proto_gen/template/derive_meta/derive_meta.rs rename to shared-lib/lib-infra/src/code_gen/protobuf_file/template/derive_meta/derive_meta.rs index 336e5c201c..c38e301aa9 100644 --- a/shared-lib/lib-infra/src/proto_gen/template/derive_meta/derive_meta.rs +++ b/shared-lib/lib-infra/src/code_gen/protobuf_file/template/derive_meta/derive_meta.rs @@ -1,4 +1,4 @@ -use crate::proto_gen::template::get_tera; +use crate::code_gen::util::get_tera; use itertools::Itertools; use tera::Context; @@ -23,7 +23,7 @@ impl ProtobufDeriveMeta { self.context.insert("names", &self.structs); self.context.insert("enums", &self.enums); - let tera = get_tera("derive_meta"); + let tera = get_tera("protobuf_file/template/derive_meta"); match tera.render("derive_meta.tera", &self.context) { Ok(r) => Some(r), Err(e) => { diff --git a/shared-lib/lib-infra/src/proto_gen/template/derive_meta/derive_meta.tera b/shared-lib/lib-infra/src/code_gen/protobuf_file/template/derive_meta/derive_meta.tera similarity index 100% rename from shared-lib/lib-infra/src/proto_gen/template/derive_meta/derive_meta.tera rename to shared-lib/lib-infra/src/code_gen/protobuf_file/template/derive_meta/derive_meta.tera diff --git a/shared-lib/lib-infra/src/proto_gen/template/derive_meta/mod.rs b/shared-lib/lib-infra/src/code_gen/protobuf_file/template/derive_meta/mod.rs similarity index 100% rename from shared-lib/lib-infra/src/proto_gen/template/derive_meta/mod.rs rename to shared-lib/lib-infra/src/code_gen/protobuf_file/template/derive_meta/mod.rs diff --git a/shared-lib/lib-infra/src/code_gen/protobuf_file/template/mod.rs b/shared-lib/lib-infra/src/code_gen/protobuf_file/template/mod.rs new file mode 100644 index 0000000000..2ef3646ff7 --- /dev/null +++ b/shared-lib/lib-infra/src/code_gen/protobuf_file/template/mod.rs @@ -0,0 +1,5 @@ +mod derive_meta; +mod proto_file; + +pub use derive_meta::*; +pub use proto_file::*; diff --git a/shared-lib/lib-infra/src/proto_gen/template/proto_file/enum.tera b/shared-lib/lib-infra/src/code_gen/protobuf_file/template/proto_file/enum.tera similarity index 100% rename from shared-lib/lib-infra/src/proto_gen/template/proto_file/enum.tera rename to shared-lib/lib-infra/src/code_gen/protobuf_file/template/proto_file/enum.tera diff --git a/shared-lib/lib-infra/src/proto_gen/template/proto_file/enum_template.rs b/shared-lib/lib-infra/src/code_gen/protobuf_file/template/proto_file/enum_template.rs similarity index 84% rename from shared-lib/lib-infra/src/proto_gen/template/proto_file/enum_template.rs rename to shared-lib/lib-infra/src/code_gen/protobuf_file/template/proto_file/enum_template.rs index 404b3eb88b..28162bf0bb 100644 --- a/shared-lib/lib-infra/src/proto_gen/template/proto_file/enum_template.rs +++ b/shared-lib/lib-infra/src/code_gen/protobuf_file/template/proto_file/enum_template.rs @@ -1,5 +1,5 @@ -use crate::proto_gen::ast::FlowyEnum; -use crate::proto_gen::template::get_tera; +use crate::code_gen::protobuf_file::ast::FlowyEnum; +use crate::code_gen::util::get_tera; use tera::Context; pub struct EnumTemplate { @@ -26,7 +26,7 @@ impl EnumTemplate { pub fn render(&mut self) -> Option { self.context.insert("items", &self.items); - let tera = get_tera("proto_file"); + let tera = get_tera("protobuf_file/template/proto_file"); match tera.render("enum.tera", &self.context) { Ok(r) => Some(r), Err(e) => { diff --git a/shared-lib/lib-infra/src/proto_gen/template/proto_file/mod.rs b/shared-lib/lib-infra/src/code_gen/protobuf_file/template/proto_file/mod.rs similarity index 100% rename from shared-lib/lib-infra/src/proto_gen/template/proto_file/mod.rs rename to shared-lib/lib-infra/src/code_gen/protobuf_file/template/proto_file/mod.rs diff --git a/shared-lib/lib-infra/src/proto_gen/template/proto_file/struct.tera b/shared-lib/lib-infra/src/code_gen/protobuf_file/template/proto_file/struct.tera similarity index 100% rename from shared-lib/lib-infra/src/proto_gen/template/proto_file/struct.tera rename to shared-lib/lib-infra/src/code_gen/protobuf_file/template/proto_file/struct.tera diff --git a/shared-lib/lib-infra/src/proto_gen/template/proto_file/struct_template.rs b/shared-lib/lib-infra/src/code_gen/protobuf_file/template/proto_file/struct_template.rs similarity index 97% rename from shared-lib/lib-infra/src/proto_gen/template/proto_file/struct_template.rs rename to shared-lib/lib-infra/src/code_gen/protobuf_file/template/proto_file/struct_template.rs index d7c0568561..2f3e9d462d 100644 --- a/shared-lib/lib-infra/src/proto_gen/template/proto_file/struct_template.rs +++ b/shared-lib/lib-infra/src/code_gen/protobuf_file/template/proto_file/struct_template.rs @@ -1,7 +1,6 @@ +use crate::code_gen::util::get_tera; use flowy_ast::*; use phf::phf_map; - -use crate::proto_gen::template::get_tera; use tera::Context; // Protobuf data type : https://developers.google.com/protocol-buffers/docs/proto3 @@ -95,7 +94,7 @@ impl StructTemplate { pub fn render(&mut self) -> Option { self.context.insert("fields", &self.fields); - let tera = get_tera("proto_file"); + let tera = get_tera("protobuf_file/template/proto_file"); match tera.render("struct.tera", &self.context) { Ok(r) => Some(r), Err(e) => { diff --git a/shared-lib/lib-infra/src/proto_gen/util.rs b/shared-lib/lib-infra/src/code_gen/util.rs similarity index 81% rename from shared-lib/lib-infra/src/proto_gen/util.rs rename to shared-lib/lib-infra/src/code_gen/util.rs index 8c8f8282f1..5f9a9d1be5 100644 --- a/shared-lib/lib-infra/src/proto_gen/util.rs +++ b/shared-lib/lib-infra/src/code_gen/util.rs @@ -1,11 +1,11 @@ use console::Style; - use similar::{ChangeTag, TextDiff}; use std::{ fs::{File, OpenOptions}, io::{Read, Write}, path::Path, }; +use tera::Tera; use walkdir::WalkDir; pub fn read_file(path: &str) -> Option { @@ -100,7 +100,7 @@ pub fn create_dir_if_not_exist(dir: &str) { } #[allow(dead_code)] -pub(crate) fn walk_dir(dir: &str, filter: F2, mut path_and_name: F1) +pub fn walk_dir(dir: &str, filter: F2, mut path_and_name: F1) where F1: FnMut(String, String), F2: Fn(&walkdir::DirEntry) -> bool, @@ -126,3 +126,29 @@ pub fn suffix_relative_to_path(path: &str, base: &str) -> String { let path = Path::new(path); path.strip_prefix(base).unwrap().to_str().unwrap().to_owned() } + +pub fn get_tera(directory: &str) -> Tera { + let mut root = format!("{}/src/code_gen/", env!("CARGO_MANIFEST_DIR")); + root.push_str(directory); + + let root_absolute_path = match std::fs::canonicalize(&root) { + Ok(p) => p.as_path().display().to_string(), + Err(e) => { + panic!("❌ Canonicalize file path {} failed {:?}", root, e); + } + }; + + let mut template_path = format!("{}/**/*.tera", root_absolute_path); + if cfg!(windows) { + // remove "\\?\" prefix on windows + template_path = format!("{}/**/*.tera", &root_absolute_path[4..]); + } + + match Tera::new(template_path.as_ref()) { + Ok(t) => t, + Err(e) => { + log::error!("Parsing error(s): {}", e); + ::std::process::exit(1); + } + } +} diff --git a/shared-lib/lib-infra/src/lib.rs b/shared-lib/lib-infra/src/lib.rs index fbe9b11486..ba8b17c7b5 100644 --- a/shared-lib/lib-infra/src/lib.rs +++ b/shared-lib/lib-infra/src/lib.rs @@ -1,12 +1,7 @@ +pub mod code_gen; pub mod future; pub mod retry; -#[cfg(feature = "pb_gen")] -pub mod pb; - -#[cfg(feature = "proto_gen")] -pub mod proto_gen; - #[allow(dead_code)] pub fn uuid_string() -> String { uuid::Uuid::new_v4().to_string() diff --git a/shared-lib/lib-infra/src/proto_gen/mod.rs b/shared-lib/lib-infra/src/proto_gen/mod.rs deleted file mode 100644 index 4d2f7c902b..0000000000 --- a/shared-lib/lib-infra/src/proto_gen/mod.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![allow(clippy::module_inception)] -mod ast; -mod flowy_toml; -mod proto_gen; -mod proto_info; -mod template; -pub mod util; - -pub use proto_gen::*; -pub use proto_info::*; diff --git a/shared-lib/lib-infra/src/proto_gen/template/mod.rs b/shared-lib/lib-infra/src/proto_gen/template/mod.rs deleted file mode 100644 index a2ef4da1d5..0000000000 --- a/shared-lib/lib-infra/src/proto_gen/template/mod.rs +++ /dev/null @@ -1,47 +0,0 @@ -mod derive_meta; -mod proto_file; - -pub use derive_meta::*; -pub use proto_file::*; -use std::fs::File; -use std::io::Read; -use tera::Tera; - -pub fn get_tera(directory: &str) -> Tera { - let mut root = format!("{}/src/proto_gen/template/", env!("CARGO_MANIFEST_DIR")); - root.push_str(directory); - - let root_absolute_path = match std::fs::canonicalize(&root) { - Ok(p) => p.as_path().display().to_string(), - Err(e) => { - panic!("❌ Canonicalize file path {} failed {:?}", root, e); - } - }; - - let mut template_path = format!("{}/**/*.tera", root_absolute_path); - if cfg!(windows) { - // remove "\\?\" prefix on windows - template_path = format!("{}/**/*.tera", &root_absolute_path[4..]); - } - - match Tera::new(template_path.as_ref()) { - Ok(t) => t, - Err(e) => { - log::error!("Parsing error(s): {}", e); - ::std::process::exit(1); - } - } -} - -#[allow(dead_code)] -pub fn read_file(path: &str) -> Option { - let mut file = File::open(path).unwrap_or_else(|_| panic!("Unable to open file at {}", path)); - let mut content = String::new(); - match file.read_to_string(&mut content) { - Ok(_) => Some(content), - Err(e) => { - log::error!("{}, with error: {:?}", path, e); - Some("".to_string()) - } - } -} diff --git a/shared-lib/lib-ws/build.rs b/shared-lib/lib-ws/build.rs index ef2b12f93d..7f06ab48c8 100644 --- a/shared-lib/lib-ws/build.rs +++ b/shared-lib/lib-ws/build.rs @@ -1,5 +1,5 @@ -use lib_infra::pb; +use lib_infra::code_gen; fn main() { - pb::gen_files(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); + code_gen::protobuf_file::gen(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); } From bf3a0b6e5b5832116a23fd77c545d33fed3e7577 Mon Sep 17 00:00:00 2001 From: appflowy Date: Tue, 15 Feb 2022 22:35:52 +0800 Subject: [PATCH 2/9] feat: add flowy_clean command to remove the build.rs cache --- frontend/Makefile | 6 ++- frontend/app_flowy/.vscode/launch.json | 7 +++ frontend/app_flowy/.vscode/tasks.json | 21 ++++++++ frontend/rust-lib/flowy-folder/build.rs | 4 +- frontend/scripts/clean.cmd | 7 +++ frontend/scripts/clean.sh | 10 ++++ shared-lib/flowy-derive/src/proto_buf/mod.rs | 1 + .../src/code_gen/dart_event/dart_event.rs | 48 ++++++++++++------- .../src/code_gen/protobuf_file/mod.rs | 5 +- .../src/code_gen/protobuf_file/proto_gen.rs | 4 +- shared-lib/lib-infra/src/code_gen/util.rs | 4 ++ 11 files changed, 93 insertions(+), 24 deletions(-) create mode 100644 frontend/scripts/clean.cmd create mode 100644 frontend/scripts/clean.sh diff --git a/frontend/Makefile b/frontend/Makefile index d765683f94..c393bec0e0 100644 --- a/frontend/Makefile +++ b/frontend/Makefile @@ -1,4 +1,4 @@ -.PHONY: flowy_dev_install +.PHONY: flowy_dev_install flowy_clean flowy_dev_install: brew bundle @@ -8,3 +8,7 @@ flowy_dev_install: cargo make flowy_dev +flowy_clean: + sh ./scripts/clean.sh + + diff --git a/frontend/app_flowy/.vscode/launch.json b/frontend/app_flowy/.vscode/launch.json index c7236062fd..e0d00d7407 100644 --- a/frontend/app_flowy/.vscode/launch.json +++ b/frontend/app_flowy/.vscode/launch.json @@ -40,5 +40,12 @@ "preLaunchTask": "Generate Language Files", "cwd": "${workspaceRoot}" }, + { + "name": "Clean", + "request": "launch", + "type": "dart", + "preLaunchTask": "Clean", + "cwd": "${workspaceRoot}" + } ] } \ No newline at end of file diff --git a/frontend/app_flowy/.vscode/tasks.json b/frontend/app_flowy/.vscode/tasks.json index 7aa58281ab..8d012c40e5 100644 --- a/frontend/app_flowy/.vscode/tasks.json +++ b/frontend/app_flowy/.vscode/tasks.json @@ -70,6 +70,27 @@ "options": { "cwd": "${workspaceFolder}/../" }, + }, + { + "label": "Clean FlowySDK", + "type": "shell", + "command": "sh ./scripts/clean.sh", + "windows": { + "options": { + "shell": { + "executable": "cmd.exe", + "args": [ + "/d", + "/c", + ".\\scripts\\clean.cmd" + ] + } + } + }, + "group": "build", + "options": { + "cwd": "${workspaceFolder}/../" + }, } ] } \ No newline at end of file diff --git a/frontend/rust-lib/flowy-folder/build.rs b/frontend/rust-lib/flowy-folder/build.rs index 7f06ab48c8..5d30db316f 100644 --- a/frontend/rust-lib/flowy-folder/build.rs +++ b/frontend/rust-lib/flowy-folder/build.rs @@ -1,5 +1,7 @@ use lib_infra::code_gen; fn main() { - code_gen::protobuf_file::gen(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); + let crate_name = env!("CARGO_PKG_NAME"); + code_gen::protobuf_file::gen(crate_name, "./src/protobuf/proto"); + // dart_event::gen(crate_name); } diff --git a/frontend/scripts/clean.cmd b/frontend/scripts/clean.cmd new file mode 100644 index 0000000000..b503f675b9 --- /dev/null +++ b/frontend/scripts/clean.cmd @@ -0,0 +1,7 @@ +cd rust-lib +cargo clean + +cd ../../shared-lib +cargo clean + +rmdir /s/q lib-infra/.cache \ No newline at end of file diff --git a/frontend/scripts/clean.sh b/frontend/scripts/clean.sh new file mode 100644 index 0000000000..b276f25250 --- /dev/null +++ b/frontend/scripts/clean.sh @@ -0,0 +1,10 @@ +#!/bin/sh +#!/usr/bin/env fish + +cd rust-lib +cargo clean + +cd ../../shared-lib +cargo clean + +rm -rf lib-infra/.cache \ No newline at end of file diff --git a/shared-lib/flowy-derive/src/proto_buf/mod.rs b/shared-lib/flowy-derive/src/proto_buf/mod.rs index 78ea151f72..adde54a31a 100644 --- a/shared-lib/flowy-derive/src/proto_buf/mod.rs +++ b/shared-lib/flowy-derive/src/proto_buf/mod.rs @@ -8,6 +8,7 @@ use crate::proto_buf::{ }; use flowy_ast::*; use proc_macro2::TokenStream; +use std::default::Default; pub fn expand_derive(input: &syn::DeriveInput) -> Result> { let ctxt = Ctxt::new(); diff --git a/shared-lib/lib-infra/src/code_gen/dart_event/dart_event.rs b/shared-lib/lib-infra/src/code_gen/dart_event/dart_event.rs index a9ba2ddcdc..0e64e29147 100644 --- a/shared-lib/lib-infra/src/code_gen/dart_event/dart_event.rs +++ b/shared-lib/lib-infra/src/code_gen/dart_event/dart_event.rs @@ -1,28 +1,42 @@ use super::event_template::*; use crate::code_gen::flowy_toml::{parse_crate_config_from, CrateConfig}; -use crate::code_gen::util::{is_crate_dir, is_hidden, read_file, save_content_to_file_with_diff_prompt}; +use crate::code_gen::util::{cache_dir, is_crate_dir, is_hidden, read_file, save_content_to_file_with_diff_prompt}; use flowy_ast::{event_ast::*, *}; +use std::fs::File; +use std::io::Write; use syn::Item; use walkdir::WalkDir; -pub struct DartEventCodeGen(); +pub fn gen(crate_name: &str) { + let event_crates = parse_dart_event_files(vec![".".to_owned()]); + let event_ast = event_crates.iter().map(parse_event_crate).flatten().collect::>(); -impl DartEventCodeGen { - pub fn gen(crate_name: &str, crate_path: &str) { - let event_crates = parse_dart_event_files(vec![crate_path.to_owned()]); - let event_ast = event_crates.iter().map(parse_event_crate).flatten().collect::>(); + let event_render_ctx = ast_to_event_render_ctx(event_ast.as_ref()); + let mut render_result = String::new(); + for (index, render_ctx) in event_render_ctx.into_iter().enumerate() { + let mut event_template = EventTemplate::new(); - let event_render_ctx = ast_to_event_render_ctx(event_ast.as_ref()); - let mut render_result = String::new(); - for (index, render_ctx) in event_render_ctx.into_iter().enumerate() { - let mut event_template = EventTemplate::new(); - - if let Some(content) = event_template.render(render_ctx, index) { - render_result.push_str(content.as_ref()) - } + if let Some(content) = event_template.render(render_ctx, index) { + render_result.push_str(content.as_ref()) } + } - save_content_to_file_with_diff_prompt(render_result.as_ref(), "."); + let cache_dir = format!("{}/{}", cache_dir(), crate_name); + let dart_event_file_path = format!("{}/dart_event.dart", cache_dir); + match std::fs::OpenOptions::new() + .create(true) + .write(true) + .append(false) + .truncate(true) + .open(&dart_event_file_path) + { + Ok(ref mut file) => { + file.write_all(render_result.as_bytes()).unwrap(); + File::flush(file).unwrap(); + } + Err(_err) => { + panic!("Failed to open file: {}", dart_event_file_path); + } } } @@ -45,8 +59,8 @@ impl DartEventCrate { pub fn parse_dart_event_files(crate_paths: Vec) -> Vec { let mut dart_event_crates: Vec = vec![]; - crate_paths.iter().for_each(|root| { - let crates = WalkDir::new(root) + crate_paths.iter().for_each(|path| { + let crates = WalkDir::new(path) .into_iter() .filter_entry(|e| !is_hidden(e)) .filter_map(|e| e.ok()) diff --git a/shared-lib/lib-infra/src/code_gen/protobuf_file/mod.rs b/shared-lib/lib-infra/src/code_gen/protobuf_file/mod.rs index e7946ac311..083bea4949 100644 --- a/shared-lib/lib-infra/src/code_gen/protobuf_file/mod.rs +++ b/shared-lib/lib-infra/src/code_gen/protobuf_file/mod.rs @@ -153,9 +153,8 @@ fn run_command(cmd: &str) -> bool { #[cfg(feature = "proto_gen")] fn gen_protos(crate_name: &str) -> Vec { - let cache_path = env!("CARGO_MANIFEST_DIR"); - let root = std::fs::canonicalize(".").unwrap().as_path().display().to_string(); - let crate_context = ProtoGenerator::gen(crate_name, &root, cache_path); + let crate_path = std::fs::canonicalize(".").unwrap().as_path().display().to_string(); + let crate_context = ProtoGenerator::gen(crate_name, &crate_path); let proto_crates = crate_context .iter() .map(|info| info.protobuf_crate.clone()) diff --git a/shared-lib/lib-infra/src/code_gen/protobuf_file/proto_gen.rs b/shared-lib/lib-infra/src/code_gen/protobuf_file/proto_gen.rs index 19618c6df4..c4aa5170aa 100644 --- a/shared-lib/lib-infra/src/code_gen/protobuf_file/proto_gen.rs +++ b/shared-lib/lib-infra/src/code_gen/protobuf_file/proto_gen.rs @@ -12,7 +12,7 @@ use std::{fs::OpenOptions, io::Write}; pub struct ProtoGenerator(); impl ProtoGenerator { - pub fn gen(crate_name: &str, crate_path: &str, cache_path: &str) -> Vec { + pub fn gen(crate_name: &str, crate_path: &str) -> Vec { let crate_contexts = parse_crate_protobuf(vec![crate_path.to_owned()]); write_proto_files(&crate_contexts); write_rust_crate_mod_file(&crate_contexts); @@ -24,7 +24,7 @@ impl ProtoGenerator { let cache = ProtoCache::from_crate_contexts(&crate_contexts); let cache_str = serde_json::to_string(&cache).unwrap(); - let cache_dir = format!("{}/.cache/{}", cache_path, crate_name); + let cache_dir = format!("{}/{}", cache_dir(), crate_name); if !Path::new(&cache_dir).exists() { std::fs::create_dir_all(&cache_dir).unwrap(); } diff --git a/shared-lib/lib-infra/src/code_gen/util.rs b/shared-lib/lib-infra/src/code_gen/util.rs index 5f9a9d1be5..b25c8b8bb8 100644 --- a/shared-lib/lib-infra/src/code_gen/util.rs +++ b/shared-lib/lib-infra/src/code_gen/util.rs @@ -152,3 +152,7 @@ pub fn get_tera(directory: &str) -> Tera { } } } + +pub fn cache_dir() -> String { + format!("{}/.cache", env!("CARGO_MANIFEST_DIR")) +} From cd013529d4c76b80b57210450f1996f4977ddf1f Mon Sep 17 00:00:00 2001 From: appflowy Date: Wed, 16 Feb 2022 10:00:31 +0800 Subject: [PATCH 3/9] chore: write dart_event file --- frontend/Makefile.toml | 6 +-- .../flowy_sdk/lib/dispatch/code_gen.dart | 1 - .../lib/protobuf/flowy-net/event_map.pb.dart | 11 ++++ .../protobuf/flowy-net/event_map.pbenum.dart | 24 +++++++++ .../protobuf/flowy-net/event_map.pbjson.dart | 20 +++++++ .../flowy-net/event_map.pbserver.dart | 9 ++++ .../lib/protobuf/flowy-net/protobuf.dart | 2 +- frontend/rust-lib/dart-ffi/Cargo.toml | 4 +- frontend/rust-lib/dart-ffi/build.rs | 11 ++++ frontend/rust-lib/flowy-folder/build.rs | 2 +- frontend/rust-lib/flowy-net/Flowy.toml | 4 +- frontend/rust-lib/flowy-net/build.rs | 4 +- frontend/rust-lib/flowy-net/src/event.rs | 9 ---- frontend/rust-lib/flowy-net/src/event_map.rs | 19 +++++++ frontend/rust-lib/flowy-net/src/lib.rs | 3 +- frontend/rust-lib/flowy-net/src/module.rs | 10 ---- .../protobuf/model/{event.rs => event_map.rs} | 6 +-- .../flowy-net/src/protobuf/model/mod.rs | 4 +- .../proto/{event.proto => event_map.proto} | 0 frontend/rust-lib/flowy-sdk/src/module.rs | 2 +- frontend/rust-lib/flowy-user/build.rs | 4 +- frontend/rust-lib/flowy-user/src/event.rs | 27 ---------- shared-lib/flowy-derive/src/proto_buf/util.rs | 1 + shared-lib/lib-infra/Cargo.toml | 2 +- .../src/code_gen/dart_event/dart_event.rs | 53 +++++++++++++++---- .../src/code_gen/dart_event/event_template.rs | 8 --- .../code_gen/dart_event/event_template.tera | 4 -- shared-lib/lib-infra/src/code_gen/mod.rs | 2 +- .../src/code_gen/protobuf_file/mod.rs | 27 ++++++---- 29 files changed, 177 insertions(+), 102 deletions(-) create mode 100644 frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pb.dart create mode 100644 frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pbenum.dart create mode 100644 frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pbjson.dart create mode 100644 frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pbserver.dart delete mode 100644 frontend/rust-lib/flowy-net/src/event.rs create mode 100644 frontend/rust-lib/flowy-net/src/event_map.rs delete mode 100644 frontend/rust-lib/flowy-net/src/module.rs rename frontend/rust-lib/flowy-net/src/protobuf/model/{event.rs => event_map.rs} (94%) rename frontend/rust-lib/flowy-net/src/protobuf/proto/{event.proto => event_map.proto} (100%) delete mode 100644 frontend/rust-lib/flowy-user/src/event.rs diff --git a/frontend/Makefile.toml b/frontend/Makefile.toml index 46f7279240..f7c47820cd 100644 --- a/frontend/Makefile.toml +++ b/frontend/Makefile.toml @@ -21,7 +21,7 @@ PRODUCT_NAME = "AppFlowy" CRATE_TYPE = "staticlib" SDK_EXT = "a" APP_ENVIRONMENT = "local" -FLUTTER_FLOWY_SDK_PATH="app_flowy/packages/flowy_sdk/lib/protobuf" +FLUTTER_FLOWY_SDK_PATH="app_flowy/packages/flowy_sdk" PROTOBUF_DERIVE_CACHE="../shared-lib/flowy-derive/src/derive_cache/derive_cache.rs" [env.development-mac] @@ -158,8 +158,8 @@ script_runner = "@duckscript" condition = { env_set = [ "FLUTTER_FLOWY_SDK_PATH"] } script = [ """ - cd ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/../shared-lib/error-code - cargo build -vv + cd ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/flowy-net + cargo build -vv --features=dart """, ] script_runner = "@shell" diff --git a/frontend/app_flowy/packages/flowy_sdk/lib/dispatch/code_gen.dart b/frontend/app_flowy/packages/flowy_sdk/lib/dispatch/code_gen.dart index 1029bbfd78..40ccbc8065 100644 --- a/frontend/app_flowy/packages/flowy_sdk/lib/dispatch/code_gen.dart +++ b/frontend/app_flowy/packages/flowy_sdk/lib/dispatch/code_gen.dart @@ -1,5 +1,4 @@ - /// Auto gen code from rust ast, do not edit part of 'dispatch.dart'; class FolderEventCreateWorkspace { diff --git a/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pb.dart b/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pb.dart new file mode 100644 index 0000000000..5bfad20674 --- /dev/null +++ b/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pb.dart @@ -0,0 +1,11 @@ +/// +// Generated code. Do not modify. +// source: event_map.proto +// +// @dart = 2.12 +// ignore_for_file: annotate_overrides,camel_case_types,unnecessary_const,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type,unnecessary_this,prefer_final_fields + +import 'dart:core' as $core; + +export 'event_map.pbenum.dart'; + diff --git a/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pbenum.dart b/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pbenum.dart new file mode 100644 index 0000000000..1fa669c16f --- /dev/null +++ b/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pbenum.dart @@ -0,0 +1,24 @@ +/// +// Generated code. Do not modify. +// source: event_map.proto +// +// @dart = 2.12 +// ignore_for_file: annotate_overrides,camel_case_types,unnecessary_const,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type,unnecessary_this,prefer_final_fields + +// ignore_for_file: UNDEFINED_SHOWN_NAME +import 'dart:core' as $core; +import 'package:protobuf/protobuf.dart' as $pb; + +class NetworkEvent extends $pb.ProtobufEnum { + static const NetworkEvent UpdateNetworkType = NetworkEvent._(0, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'UpdateNetworkType'); + + static const $core.List values = [ + UpdateNetworkType, + ]; + + static final $core.Map<$core.int, NetworkEvent> _byValue = $pb.ProtobufEnum.initByValue(values); + static NetworkEvent? valueOf($core.int value) => _byValue[value]; + + const NetworkEvent._($core.int v, $core.String n) : super(v, n); +} + diff --git a/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pbjson.dart b/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pbjson.dart new file mode 100644 index 0000000000..39d0bbde12 --- /dev/null +++ b/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pbjson.dart @@ -0,0 +1,20 @@ +/// +// Generated code. Do not modify. +// source: event_map.proto +// +// @dart = 2.12 +// ignore_for_file: annotate_overrides,camel_case_types,unnecessary_const,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type,unnecessary_this,prefer_final_fields,deprecated_member_use_from_same_package + +import 'dart:core' as $core; +import 'dart:convert' as $convert; +import 'dart:typed_data' as $typed_data; +@$core.Deprecated('Use networkEventDescriptor instead') +const NetworkEvent$json = const { + '1': 'NetworkEvent', + '2': const [ + const {'1': 'UpdateNetworkType', '2': 0}, + ], +}; + +/// Descriptor for `NetworkEvent`. Decode as a `google.protobuf.EnumDescriptorProto`. +final $typed_data.Uint8List networkEventDescriptor = $convert.base64Decode('CgxOZXR3b3JrRXZlbnQSFQoRVXBkYXRlTmV0d29ya1R5cGUQAA=='); diff --git a/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pbserver.dart b/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pbserver.dart new file mode 100644 index 0000000000..e359d1146c --- /dev/null +++ b/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/event_map.pbserver.dart @@ -0,0 +1,9 @@ +/// +// Generated code. Do not modify. +// source: event_map.proto +// +// @dart = 2.12 +// ignore_for_file: annotate_overrides,camel_case_types,unnecessary_const,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type,unnecessary_this,prefer_final_fields,deprecated_member_use_from_same_package + +export 'event_map.pb.dart'; + diff --git a/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/protobuf.dart b/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/protobuf.dart index 8d2a575e3c..20c0bc012d 100644 --- a/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/protobuf.dart +++ b/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-net/protobuf.dart @@ -1,3 +1,3 @@ // Auto-generated, do not edit export './network_state.pb.dart'; -export './event.pb.dart'; +export './event_map.pb.dart'; diff --git a/frontend/rust-lib/dart-ffi/Cargo.toml b/frontend/rust-lib/dart-ffi/Cargo.toml index 7bbd53f824..b84944b88f 100644 --- a/frontend/rust-lib/dart-ffi/Cargo.toml +++ b/frontend/rust-lib/dart-ffi/Cargo.toml @@ -29,8 +29,8 @@ dart-notify = {path = "../dart-notify" } flowy-derive = {path = "../../../shared-lib/flowy-derive" } [features] -default = ["flowy-sdk/dart"] -flutter = ["dart-notify/dart"] +default = ["flowy-sdk/dart", "dart-notify/dart", "flutter"] +flutter = [] http_server = ["flowy-sdk/http_server", "flowy-sdk/use_bunyan"] #use_serde = ["bincode"] #use_protobuf= ["protobuf"] diff --git a/frontend/rust-lib/dart-ffi/build.rs b/frontend/rust-lib/dart-ffi/build.rs index 7f06ab48c8..01f337bce8 100644 --- a/frontend/rust-lib/dart-ffi/build.rs +++ b/frontend/rust-lib/dart-ffi/build.rs @@ -1,5 +1,16 @@ use lib_infra::code_gen; +use lib_infra::code_gen::dart_event; fn main() { code_gen::protobuf_file::gen(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); + #[cfg(feature = "flutter")] + copy_dart_event_files(); +} + +#[cfg(feature = "flutter")] +fn copy_dart_event_files() { + let workspace_dir = std::env::var("CARGO_MAKE_WORKING_DIRECTORY").unwrap(); + let flutter_sdk_path = std::env::var("FLUTTER_FLOWY_SDK_PATH").unwrap(); + let output_file = format!("{}/{}/lib/dispatch/code_gen.dart", workspace_dir, flutter_sdk_path); + dart_event::write_dart_event_file(&output_file); } diff --git a/frontend/rust-lib/flowy-folder/build.rs b/frontend/rust-lib/flowy-folder/build.rs index 5d30db316f..4ab7b9e23d 100644 --- a/frontend/rust-lib/flowy-folder/build.rs +++ b/frontend/rust-lib/flowy-folder/build.rs @@ -3,5 +3,5 @@ use lib_infra::code_gen; fn main() { let crate_name = env!("CARGO_PKG_NAME"); code_gen::protobuf_file::gen(crate_name, "./src/protobuf/proto"); - // dart_event::gen(crate_name); + code_gen::dart_event::gen(crate_name); } diff --git a/frontend/rust-lib/flowy-net/Flowy.toml b/frontend/rust-lib/flowy-net/Flowy.toml index e25305c348..055287f500 100644 --- a/frontend/rust-lib/flowy-net/Flowy.toml +++ b/frontend/rust-lib/flowy-net/Flowy.toml @@ -1,2 +1,2 @@ -proto_crates = ["src/event.rs", "src/entities"] -event_files = ["src/event.rs"] \ No newline at end of file +proto_crates = ["src/event_map.rs", "src/entities"] +event_files = ["src/event_map.rs"] \ No newline at end of file diff --git a/frontend/rust-lib/flowy-net/build.rs b/frontend/rust-lib/flowy-net/build.rs index 7f06ab48c8..4ab7b9e23d 100644 --- a/frontend/rust-lib/flowy-net/build.rs +++ b/frontend/rust-lib/flowy-net/build.rs @@ -1,5 +1,7 @@ use lib_infra::code_gen; fn main() { - code_gen::protobuf_file::gen(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); + let crate_name = env!("CARGO_PKG_NAME"); + code_gen::protobuf_file::gen(crate_name, "./src/protobuf/proto"); + code_gen::dart_event::gen(crate_name); } diff --git a/frontend/rust-lib/flowy-net/src/event.rs b/frontend/rust-lib/flowy-net/src/event.rs deleted file mode 100644 index 81b4060f68..0000000000 --- a/frontend/rust-lib/flowy-net/src/event.rs +++ /dev/null @@ -1,9 +0,0 @@ -use flowy_derive::{Flowy_Event, ProtoBuf_Enum}; -use strum_macros::Display; - -#[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)] -#[event_err = "FlowyError"] -pub enum NetworkEvent { - #[event(input = "NetworkState")] - UpdateNetworkType = 0, -} diff --git a/frontend/rust-lib/flowy-net/src/event_map.rs b/frontend/rust-lib/flowy-net/src/event_map.rs new file mode 100644 index 0000000000..4fce155360 --- /dev/null +++ b/frontend/rust-lib/flowy-net/src/event_map.rs @@ -0,0 +1,19 @@ +use crate::{handlers::*, ws::connection::FlowyWebSocketConnect}; +use flowy_derive::{Flowy_Event, ProtoBuf_Enum}; +use lib_dispatch::prelude::*; +use std::sync::Arc; +use strum_macros::Display; + +pub fn create(ws_conn: Arc) -> Module { + Module::new() + .name("Flowy-Network") + .data(ws_conn) + .event(NetworkEvent::UpdateNetworkType, update_network_ty) +} + +#[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)] +#[event_err = "FlowyError"] +pub enum NetworkEvent { + #[event(input = "NetworkState")] + UpdateNetworkType = 0, +} diff --git a/frontend/rust-lib/flowy-net/src/lib.rs b/frontend/rust-lib/flowy-net/src/lib.rs index 78f8a28f02..6347e3f459 100644 --- a/frontend/rust-lib/flowy-net/src/lib.rs +++ b/frontend/rust-lib/flowy-net/src/lib.rs @@ -1,10 +1,9 @@ mod configuration; pub mod entities; -mod event; +pub mod event_map; mod handlers; pub mod http_server; pub mod local_server; -pub mod module; pub mod protobuf; mod request; pub mod ws; diff --git a/frontend/rust-lib/flowy-net/src/module.rs b/frontend/rust-lib/flowy-net/src/module.rs deleted file mode 100644 index 26de3769f3..0000000000 --- a/frontend/rust-lib/flowy-net/src/module.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::{event::NetworkEvent, handlers::*, ws::connection::FlowyWebSocketConnect}; -use lib_dispatch::prelude::*; -use std::sync::Arc; - -pub fn create(ws_conn: Arc) -> Module { - Module::new() - .name("Flowy-Network") - .data(ws_conn) - .event(NetworkEvent::UpdateNetworkType, update_network_ty) -} diff --git a/frontend/rust-lib/flowy-net/src/protobuf/model/event.rs b/frontend/rust-lib/flowy-net/src/protobuf/model/event_map.rs similarity index 94% rename from frontend/rust-lib/flowy-net/src/protobuf/model/event.rs rename to frontend/rust-lib/flowy-net/src/protobuf/model/event_map.rs index 765d45a594..5eb3ed93d9 100644 --- a/frontend/rust-lib/flowy-net/src/protobuf/model/event.rs +++ b/frontend/rust-lib/flowy-net/src/protobuf/model/event_map.rs @@ -17,7 +17,7 @@ #![allow(trivial_casts)] #![allow(unused_imports)] #![allow(unused_results)] -//! Generated file from `event.proto` +//! Generated file from `event_map.proto` /// Generated files are compatible only with the same version /// of protobuf runtime. @@ -71,8 +71,8 @@ impl ::protobuf::reflect::ProtobufValue for NetworkEvent { } static file_descriptor_proto_data: &'static [u8] = b"\ - \n\x0bevent.proto*%\n\x0cNetworkEvent\x12\x15\n\x11UpdateNetworkType\x10\ - \0b\x06proto3\ + \n\x0fevent_map.proto*%\n\x0cNetworkEvent\x12\x15\n\x11UpdateNetworkType\ + \x10\0b\x06proto3\ "; static file_descriptor_proto_lazy: ::protobuf::rt::LazyV2<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::rt::LazyV2::INIT; diff --git a/frontend/rust-lib/flowy-net/src/protobuf/model/mod.rs b/frontend/rust-lib/flowy-net/src/protobuf/model/mod.rs index 105757dc0c..dd4f933626 100644 --- a/frontend/rust-lib/flowy-net/src/protobuf/model/mod.rs +++ b/frontend/rust-lib/flowy-net/src/protobuf/model/mod.rs @@ -4,5 +4,5 @@ mod network_state; pub use network_state::*; -mod event; -pub use event::*; +mod event_map; +pub use event_map::*; diff --git a/frontend/rust-lib/flowy-net/src/protobuf/proto/event.proto b/frontend/rust-lib/flowy-net/src/protobuf/proto/event_map.proto similarity index 100% rename from frontend/rust-lib/flowy-net/src/protobuf/proto/event.proto rename to frontend/rust-lib/flowy-net/src/protobuf/proto/event_map.proto diff --git a/frontend/rust-lib/flowy-sdk/src/module.rs b/frontend/rust-lib/flowy-sdk/src/module.rs index 00fb88cec4..b88300d529 100644 --- a/frontend/rust-lib/flowy-sdk/src/module.rs +++ b/frontend/rust-lib/flowy-sdk/src/module.rs @@ -24,5 +24,5 @@ fn mk_folder_module(core: Arc) -> Module { } fn mk_network_module(ws_conn: Arc) -> Module { - flowy_net::module::create(ws_conn) + flowy_net::event_map::create(ws_conn) } diff --git a/frontend/rust-lib/flowy-user/build.rs b/frontend/rust-lib/flowy-user/build.rs index 7f06ab48c8..4ab7b9e23d 100644 --- a/frontend/rust-lib/flowy-user/build.rs +++ b/frontend/rust-lib/flowy-user/build.rs @@ -1,5 +1,7 @@ use lib_infra::code_gen; fn main() { - code_gen::protobuf_file::gen(env!("CARGO_PKG_NAME"), "./src/protobuf/proto"); + let crate_name = env!("CARGO_PKG_NAME"); + code_gen::protobuf_file::gen(crate_name, "./src/protobuf/proto"); + code_gen::dart_event::gen(crate_name); } diff --git a/frontend/rust-lib/flowy-user/src/event.rs b/frontend/rust-lib/flowy-user/src/event.rs deleted file mode 100644 index 617fd252eb..0000000000 --- a/frontend/rust-lib/flowy-user/src/event.rs +++ /dev/null @@ -1,27 +0,0 @@ -use flowy_derive::{Flowy_Event, ProtoBuf_Enum}; -use strum_macros::Display; - -#[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)] -#[event_err = "FlowyError"] -pub enum UserEvent { - #[event()] - InitUser = 0, - - #[event(input = "SignInRequest", output = "UserProfile")] - SignIn = 1, - - #[event(input = "SignUpRequest", output = "UserProfile")] - SignUp = 2, - - #[event(passthrough)] - SignOut = 3, - - #[event(input = "UpdateUserRequest")] - UpdateUser = 4, - - #[event(output = "UserProfile")] - GetUserProfile = 5, - - #[event(output = "UserProfile")] - CheckUser = 6, -} diff --git a/shared-lib/flowy-derive/src/proto_buf/util.rs b/shared-lib/flowy-derive/src/proto_buf/util.rs index e214de0f40..f88a1e453a 100644 --- a/shared-lib/flowy-derive/src/proto_buf/util.rs +++ b/shared-lib/flowy-derive/src/proto_buf/util.rs @@ -54,6 +54,7 @@ pub fn category_from_str(type_str: String) -> TypeCategory { for path in WalkDir::new(cache_dir) .into_iter() .filter_map(|e| e.ok()) + .filter(|e| e.path().file_stem().unwrap().to_str().unwrap() == "proto_cache") .map(|e| e.path().to_str().unwrap().to_string()) { match read_file(&path) { diff --git a/shared-lib/lib-infra/Cargo.toml b/shared-lib/lib-infra/Cargo.toml index 47e9d77fdf..fc2338ec0d 100644 --- a/shared-lib/lib-infra/Cargo.toml +++ b/shared-lib/lib-infra/Cargo.toml @@ -49,4 +49,4 @@ proto_gen = [ ] pb_gen = ["cmd_lib", "protoc-rust", "walkdir", "protoc-bin-vendored",] dart_event = ["walkdir", "flowy-ast", "tera", "syn"] -dart = ["proto_gen"] \ No newline at end of file +dart = ["proto_gen", "dart_event"] \ No newline at end of file diff --git a/shared-lib/lib-infra/src/code_gen/dart_event/dart_event.rs b/shared-lib/lib-infra/src/code_gen/dart_event/dart_event.rs index 0e64e29147..697fd8b0a1 100644 --- a/shared-lib/lib-infra/src/code_gen/dart_event/dart_event.rs +++ b/shared-lib/lib-infra/src/code_gen/dart_event/dart_event.rs @@ -1,6 +1,6 @@ use super::event_template::*; use crate::code_gen::flowy_toml::{parse_crate_config_from, CrateConfig}; -use crate::code_gen::util::{cache_dir, is_crate_dir, is_hidden, read_file, save_content_to_file_with_diff_prompt}; +use crate::code_gen::util::{cache_dir, is_crate_dir, is_hidden, read_file}; use flowy_ast::{event_ast::*, *}; use std::fs::File; use std::io::Write; @@ -8,7 +8,8 @@ use syn::Item; use walkdir::WalkDir; pub fn gen(crate_name: &str) { - let event_crates = parse_dart_event_files(vec![".".to_owned()]); + let crate_path = std::fs::canonicalize(".").unwrap().as_path().display().to_string(); + let event_crates = parse_dart_event_files(vec![crate_path]); let event_ast = event_crates.iter().map(parse_event_crate).flatten().collect::>(); let event_render_ctx = ast_to_event_render_ctx(event_ast.as_ref()); @@ -23,6 +24,7 @@ pub fn gen(crate_name: &str) { let cache_dir = format!("{}/{}", cache_dir(), crate_name); let dart_event_file_path = format!("{}/dart_event.dart", cache_dir); + match std::fs::OpenOptions::new() .create(true) .write(true) @@ -34,16 +36,50 @@ pub fn gen(crate_name: &str) { file.write_all(render_result.as_bytes()).unwrap(); File::flush(file).unwrap(); } - Err(_err) => { - panic!("Failed to open file: {}", dart_event_file_path); + Err(err) => { + panic!("Failed to open file: {}, {:?}", dart_event_file_path, err); } } } +const DART_IMPORTED: &str = r#" +/// Auto gen code from rust ast, do not edit +part of 'dispatch.dart'; +"#; + +pub fn write_dart_event_file(file_path: &str) { + let cache_dir = cache_dir(); + let mut content = DART_IMPORTED.to_owned(); + for path in WalkDir::new(cache_dir) + .into_iter() + .filter_map(|e| e.ok()) + .filter(|e| e.path().file_stem().unwrap().to_str().unwrap() == "dart_event") + .map(|e| e.path().to_str().unwrap().to_string()) + { + let file_content = read_file(path.as_ref()).unwrap(); + content.push_str(&file_content); + } + + match std::fs::OpenOptions::new() + .create(true) + .write(true) + .append(false) + .truncate(true) + .open(&file_path) + { + Ok(ref mut file) => { + file.write_all(content.as_bytes()).unwrap(); + File::flush(file).unwrap(); + } + Err(err) => { + panic!("Failed to write dart event file: {}", err); + } + } +} + +#[derive(Debug)] pub struct DartEventCrate { crate_path: String, - #[allow(dead_code)] - crate_name: String, event_files: Vec, } @@ -51,7 +87,6 @@ impl DartEventCrate { pub fn from_config(config: &CrateConfig) -> Self { DartEventCrate { crate_path: config.crate_path.clone(), - crate_name: config.folder_name.clone(), event_files: config.flowy_config.event_files.clone(), } } @@ -117,10 +152,6 @@ pub fn ast_to_event_render_ctx(ast: &[EventASTContext]) -> Vec, pub output_deserializer: Option, @@ -27,9 +22,6 @@ impl EventTemplate { } pub fn render(&mut self, ctx: EventRenderContext, index: usize) -> Option { - if index == 0 { - self.tera_context.insert("imported_dart_files", DART_IMPORTED) - } self.tera_context.insert("index", &index); let dart_class_name = format!("{}{}", ctx.event_ty, ctx.event); let event = format!("{}.{}", ctx.event_ty, ctx.event); diff --git a/shared-lib/lib-infra/src/code_gen/dart_event/event_template.tera b/shared-lib/lib-infra/src/code_gen/dart_event/event_template.tera index aac3012fb6..445807ea33 100644 --- a/shared-lib/lib-infra/src/code_gen/dart_event/event_template.tera +++ b/shared-lib/lib-infra/src/code_gen/dart_event/event_template.tera @@ -1,7 +1,3 @@ -{%- if index == 0 %} -{{ imported_dart_files }} -{%- endif -%} - class {{ event_class }} { {%- if has_input %} {{ input_deserializer }} request; diff --git a/shared-lib/lib-infra/src/code_gen/mod.rs b/shared-lib/lib-infra/src/code_gen/mod.rs index 32570614cb..2e8f026fde 100644 --- a/shared-lib/lib-infra/src/code_gen/mod.rs +++ b/shared-lib/lib-infra/src/code_gen/mod.rs @@ -8,4 +8,4 @@ pub mod dart_event; mod flowy_toml; #[cfg(any(feature = "pb_gen", feature = "dart_event"))] -mod util; +pub mod util; diff --git a/shared-lib/lib-infra/src/code_gen/protobuf_file/mod.rs b/shared-lib/lib-infra/src/code_gen/protobuf_file/mod.rs index 083bea4949..25115c81bc 100644 --- a/shared-lib/lib-infra/src/code_gen/protobuf_file/mod.rs +++ b/shared-lib/lib-infra/src/code_gen/protobuf_file/mod.rs @@ -22,7 +22,7 @@ pub fn gen(crate_name: &str, proto_file_dir: &str) { #[cfg(feature = "proto_gen")] let _ = gen_protos(crate_name); - let mut paths = vec![]; + let mut proto_file_paths = vec![]; let mut file_names = vec![]; for (path, file_name) in WalkDir::new(proto_file_dir) @@ -37,26 +37,31 @@ pub fn gen(crate_name: &str, proto_file_dir: &str) { if path.ends_with(".proto") { // https://stackoverflow.com/questions/49077147/how-can-i-force-build-rs-to-run-again-without-cleaning-my-whole-project println!("cargo:rerun-if-changed={}", path); - paths.push(path); + proto_file_paths.push(path); file_names.push(file_name); } } - println!("cargo:rerun-if-changed=build.rs"); let protoc_bin_path = protoc_bin_vendored::protoc_bin_path().unwrap(); // 2. generate the protobuf files(Dart) #[cfg(feature = "dart")] - generate_dart_protobuf_files(crate_name, proto_file_dir, &paths, &file_names, &protoc_bin_path); + generate_dart_protobuf_files( + crate_name, + proto_file_dir, + &proto_file_paths, + &file_names, + &protoc_bin_path, + ); // 3. generate the protobuf files(Rust) - generate_rust_protobuf_files(&protoc_bin_path, &paths, proto_file_dir); + generate_rust_protobuf_files(&protoc_bin_path, &proto_file_paths, proto_file_dir); } -fn generate_rust_protobuf_files(protoc_bin_path: &PathBuf, input_paths: &Vec, proto_file_dir: &str) { +fn generate_rust_protobuf_files(protoc_bin_path: &PathBuf, proto_file_paths: &Vec, proto_file_dir: &str) { protoc_rust::Codegen::new() .out_dir("./src/protobuf/model") .protoc_path(protoc_bin_path) - .inputs(input_paths) + .inputs(proto_file_paths) .include(proto_file_dir) .run() .expect("Running protoc failed."); @@ -68,7 +73,7 @@ fn generate_dart_protobuf_files( root: &str, paths: &Vec, file_names: &Vec, - proto_path: &PathBuf, + protoc_bin_path: &PathBuf, ) { if std::env::var("CARGO_MAKE_WORKING_DIRECTORY").is_err() { log::warn!("CARGO_MAKE_WORKING_DIRECTORY was not set, skip generate dart pb"); @@ -82,15 +87,15 @@ fn generate_dart_protobuf_files( let workspace_dir = std::env::var("CARGO_MAKE_WORKING_DIRECTORY").unwrap(); let flutter_sdk_path = std::env::var("FLUTTER_FLOWY_SDK_PATH").unwrap(); - let output = format!("{}/{}/{}", workspace_dir, flutter_sdk_path, name); + let output = format!("{}/{}/lib/protobuf/{}", workspace_dir, flutter_sdk_path, name); if !std::path::Path::new(&output).exists() { std::fs::create_dir_all(&output).unwrap(); } check_pb_dart_plugin(); - let proto_path = proto_path.to_str().unwrap().to_owned(); + let protoc_bin_path = protoc_bin_path.to_str().unwrap().to_owned(); paths.iter().for_each(|path| { if cmd_lib::run_cmd! { - ${proto_path} --dart_out=${output} --proto_path=${root} ${path} + ${protoc_bin_path} --dart_out=${output} --proto_path=${root} ${path} } .is_err() { From afd8335e9573b96abcf6b969d1e69461c1ff22c9 Mon Sep 17 00:00:00 2001 From: appflowy Date: Wed, 16 Feb 2022 11:13:02 +0800 Subject: [PATCH 4/9] chore: cache dart_event.dart file --- .run/dart-event.run.xml | 2 +- .../flowy_sdk/lib/dispatch/{code_gen.dart => dart_event.dart} | 0 .../app_flowy/packages/flowy_sdk/lib/dispatch/dispatch.dart | 2 +- frontend/rust-lib/dart-ffi/build.rs | 3 ++- frontend/scripts/makefile/protobuf.toml | 4 ++-- 5 files changed, 6 insertions(+), 5 deletions(-) rename frontend/app_flowy/packages/flowy_sdk/lib/dispatch/{code_gen.dart => dart_event.dart} (100%) diff --git a/.run/dart-event.run.xml b/.run/dart-event.run.xml index 71ac27e8e7..911316db88 100644 --- a/.run/dart-event.run.xml +++ b/.run/dart-event.run.xml @@ -1,6 +1,6 @@ -