chore: write dart_event file

This commit is contained in:
appflowy 2022-02-16 10:00:31 +08:00
parent bf3a0b6e5b
commit cd013529d4
29 changed files with 177 additions and 102 deletions

View File

@ -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"

View File

@ -1,5 +1,4 @@
/// Auto gen code from rust ast, do not edit
part of 'dispatch.dart';
class FolderEventCreateWorkspace {

View File

@ -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';

View File

@ -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<NetworkEvent> values = <NetworkEvent> [
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);
}

View File

@ -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==');

View File

@ -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';

View File

@ -1,3 +1,3 @@
// Auto-generated, do not edit
export './network_state.pb.dart';
export './event.pb.dart';
export './event_map.pb.dart';

View File

@ -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"]

View File

@ -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);
}

View File

@ -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);
}

View File

@ -1,2 +1,2 @@
proto_crates = ["src/event.rs", "src/entities"]
event_files = ["src/event.rs"]
proto_crates = ["src/event_map.rs", "src/entities"]
event_files = ["src/event_map.rs"]

View File

@ -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);
}

View File

@ -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,
}

View File

@ -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<FlowyWebSocketConnect>) -> 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,
}

View File

@ -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;

View File

@ -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<FlowyWebSocketConnect>) -> Module {
Module::new()
.name("Flowy-Network")
.data(ws_conn)
.event(NetworkEvent::UpdateNetworkType, update_network_ty)
}

View File

@ -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;

View File

@ -4,5 +4,5 @@
mod network_state;
pub use network_state::*;
mod event;
pub use event::*;
mod event_map;
pub use event_map::*;

View File

@ -24,5 +24,5 @@ fn mk_folder_module(core: Arc<FolderManager>) -> Module {
}
fn mk_network_module(ws_conn: Arc<FlowyWebSocketConnect>) -> Module {
flowy_net::module::create(ws_conn)
flowy_net::event_map::create(ws_conn)
}

View File

@ -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);
}

View File

@ -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,
}

View File

@ -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) {

View File

@ -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"]
dart = ["proto_gen", "dart_event"]

View File

@ -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::<Vec<_>>();
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<String>,
}
@ -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<EventRenderContex
.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,

View File

@ -5,11 +5,6 @@ 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<String>,
pub output_deserializer: Option<String>,
@ -27,9 +22,6 @@ impl EventTemplate {
}
pub fn render(&mut self, ctx: EventRenderContext, index: usize) -> Option<String> {
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);

View File

@ -1,7 +1,3 @@
{%- if index == 0 %}
{{ imported_dart_files }}
{%- endif -%}
class {{ event_class }} {
{%- if has_input %}
{{ input_deserializer }} request;

View File

@ -8,4 +8,4 @@ pub mod dart_event;
mod flowy_toml;
#[cfg(any(feature = "pb_gen", feature = "dart_event"))]
mod util;
pub mod util;

View File

@ -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<String>, proto_file_dir: &str) {
fn generate_rust_protobuf_files(protoc_bin_path: &PathBuf, proto_file_paths: &Vec<String>, 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<String>,
file_names: &Vec<String>,
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()
{