mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
generate the dart side code from the rust side event
This commit is contained in:
@ -100,6 +100,7 @@ pub fn parse_event_crate(event_crate: &DartEventCrate) -> Vec<EventASTContext> {
|
||||
|
||||
pub fn ast_to_event_render_ctx(ast: &Vec<EventASTContext>) -> Vec<EventRenderContext> {
|
||||
ast.iter()
|
||||
.filter(|event_ast| event_ast.event_input.is_some() && event_ast.event_output.is_some())
|
||||
.map(|event_ast| EventRenderContext {
|
||||
input_deserializer: event_ast
|
||||
.event_input
|
||||
|
@ -1,3 +1,4 @@
|
||||
use crate::util::get_tera;
|
||||
use tera::Context;
|
||||
|
||||
pub struct EventTemplate {
|
||||
@ -6,7 +7,7 @@ pub struct EventTemplate {
|
||||
|
||||
pub const DART_IMPORTED: &'static str = r#"
|
||||
/// Auto gen code from rust ast, do not edit
|
||||
part of 'cqrs.dart';
|
||||
part of 'dispatch.dart';
|
||||
"#;
|
||||
|
||||
pub struct EventRenderContext {
|
||||
@ -24,69 +25,30 @@ impl EventTemplate {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn render(&mut self, _render_context: EventRenderContext, _index: usize) -> Option<String> {
|
||||
None
|
||||
// if index == 0 {
|
||||
// self.tera_context
|
||||
// .insert("imported_dart_files", DART_IMPORTED)
|
||||
// }
|
||||
// self.tera_context.insert("index", &index);
|
||||
//
|
||||
//
|
||||
//
|
||||
// self.tera_context.insert(
|
||||
// "command_request_struct_ident",
|
||||
// &render_context.command_request_struct_ident,
|
||||
// );
|
||||
//
|
||||
// self.tera_context
|
||||
// .insert("request_deserializer", &render_context.request_deserializer);
|
||||
//
|
||||
// if render_context.request_deserializer.is_empty() {
|
||||
// self.tera_context.insert("has_request_deserializer", &false);
|
||||
// } else {
|
||||
// self.tera_context.insert("has_request_deserializer", &true);
|
||||
// }
|
||||
// self.tera_context
|
||||
// .insert("command_ident", &render_context.event);
|
||||
//
|
||||
// if render_context.response_deserializer.is_empty() {
|
||||
// self.tera_context
|
||||
// .insert("has_response_deserializer", &false);
|
||||
// self.tera_context
|
||||
// .insert("response_deserializer", "ResponsePacket");
|
||||
// } else {
|
||||
// self.tera_context.insert("has_response_deserializer", &true);
|
||||
// self.tera_context.insert(
|
||||
// "response_deserializer",
|
||||
// &render_context.response_deserializer,
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// self.tera_context
|
||||
// .insert("async_cqrs_type", &render_context.async_cqrs_type);
|
||||
// let repo_absolute_path =
|
||||
// std::fs::canonicalize("./flowy-scripts/rust-tool/src/flutter/cqrs")
|
||||
// .unwrap()
|
||||
// .as_path()
|
||||
// .display()
|
||||
// .to_string();
|
||||
//
|
||||
// let template_path = format!("{}/**/*.tera", repo_absolute_path);
|
||||
// let tera = match Tera::new(&template_path) {
|
||||
// Ok(t) => t,
|
||||
// Err(e) => {
|
||||
// log::error!("Parsing error(s): {}", e);
|
||||
// ::std::process::exit(1);
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// match tera.render("command_request_template.tera", &self.tera_context) {
|
||||
// Ok(r) => Some(r),
|
||||
// Err(e) => {
|
||||
// log::error!("{:?}", e);
|
||||
// None
|
||||
// }
|
||||
// }
|
||||
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);
|
||||
|
||||
self.tera_context.insert("event_class", &dart_class_name);
|
||||
self.tera_context.insert("event", &event);
|
||||
self.tera_context
|
||||
.insert("input_deserializer", &ctx.input_deserializer);
|
||||
self.tera_context
|
||||
.insert("output_deserializer", &ctx.output_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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,71 +2,25 @@
|
||||
{{ imported_dart_files }}
|
||||
{%- endif -%}
|
||||
|
||||
class {{ command_request_struct_ident }} {
|
||||
{%- if has_request_deserializer %}
|
||||
{{ request_deserializer }} body;
|
||||
{%- else %}
|
||||
Uint8List? body;
|
||||
{%- endif %}
|
||||
class {{ event_class }} {
|
||||
{{ input_deserializer }} params;
|
||||
{{ event_class }}(this.params);
|
||||
|
||||
{%- if has_request_deserializer %}
|
||||
{{ command_request_struct_ident }}(this.body);
|
||||
{%- else %}
|
||||
{{ command_request_struct_ident }}();
|
||||
{%- endif %}
|
||||
Future<Either<{{ response_deserializer }}, FlowyError>> send() {
|
||||
final command = Command.{{ command_ident }};
|
||||
var request = RequestPacket.create()
|
||||
..command = command
|
||||
..id = uuid();
|
||||
Future<Either<{{ output_deserializer }}, FlowyError>> send() {
|
||||
return paramsToBytes(params).fold(
|
||||
(bytes) {
|
||||
final request = FFIRequest.create()
|
||||
..event = {{ event }}.toString()
|
||||
..payload = bytes;
|
||||
|
||||
{%- if has_request_deserializer %}
|
||||
return protobufToBytes(body).fold(
|
||||
(req_bytes) {
|
||||
request.body = req_bytes;
|
||||
return {{ async_cqrs_type }}(request).then((response) {
|
||||
{%- if has_response_deserializer %}
|
||||
try {
|
||||
if (response.hasErr()) {
|
||||
return right(FlowyError.from(response));
|
||||
} else {
|
||||
final pb = {{ response_deserializer }}.fromBuffer(response.body);
|
||||
return left(pb);
|
||||
}
|
||||
|
||||
} catch (e, s) {
|
||||
final error = FlowyError.fromError('error: ${e.runtimeType}. Stack trace: $s', StatusCode.ProtobufDeserializeError);
|
||||
return right(error);
|
||||
}
|
||||
{%- else %}
|
||||
return left(response);
|
||||
{%- endif %}
|
||||
});
|
||||
return Dispatch.asyncRequest(request)
|
||||
.then((bytesResult) => bytesResult.fold(
|
||||
(bytes) => left({{ output_deserializer }}.fromBuffer(bytes)),
|
||||
(error) => right(error),
|
||||
));
|
||||
},
|
||||
(err) => Future(() {
|
||||
final error = FlowyError.fromError(err, StatusCode.ProtobufSerializeError);
|
||||
return right(error);
|
||||
}),
|
||||
);
|
||||
{%- else %}
|
||||
return {{ async_cqrs_type }}(request).then((response) {
|
||||
{%- if has_response_deserializer %}
|
||||
try {
|
||||
if (response.hasErr()) {
|
||||
return right(FlowyError.from(response));
|
||||
} else {
|
||||
final pb = {{ response_deserializer }}.fromBuffer(response.body);
|
||||
return left(pb);
|
||||
}
|
||||
} catch (e, s) {
|
||||
final error = FlowyError.fromError('error: ${e.runtimeType}. Stack trace: $s', StatusCode.ProtobufDeserializeError);
|
||||
return right(error);
|
||||
}
|
||||
{%- else %}
|
||||
return left(response);
|
||||
{%- endif %}
|
||||
|
||||
});
|
||||
{%- endif %}
|
||||
(err) => Future(() => right(err)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,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("proto/template/derive_meta");
|
||||
match tera.render("derive_meta.tera", &self.context) {
|
||||
Ok(r) => Some(r),
|
||||
Err(e) => {
|
||||
|
@ -26,7 +26,7 @@ impl EnumTemplate {
|
||||
|
||||
pub fn render(&mut self) -> Option<String> {
|
||||
self.context.insert("items", &self.items);
|
||||
let tera = get_tera("proto_file");
|
||||
let tera = get_tera("proto/template/proto_file");
|
||||
match tera.render("enum.tera", &self.context) {
|
||||
Ok(r) => Some(r),
|
||||
Err(e) => {
|
||||
|
@ -84,7 +84,7 @@ impl StructTemplate {
|
||||
|
||||
pub fn render(&mut self) -> Option<String> {
|
||||
self.context.insert("fields", &self.fields);
|
||||
let tera = get_tera("proto_file");
|
||||
let tera = get_tera("proto/template/proto_file");
|
||||
match tera.render("struct.tera", &self.context) {
|
||||
Ok(r) => Some(r),
|
||||
Err(e) => {
|
||||
|
@ -89,7 +89,7 @@ pub fn print_diff(old_content: String, new_content: String) {
|
||||
}
|
||||
|
||||
pub fn get_tera(directory: &str) -> Tera {
|
||||
let mut root = "./scripts/flowy-tool/src/proto/template/".to_owned();
|
||||
let mut root = "./scripts/flowy-tool/src/".to_owned();
|
||||
root.push_str(directory);
|
||||
|
||||
let root_absolute_path = std::fs::canonicalize(root)
|
||||
|
@ -5,7 +5,7 @@ dependencies = ["gen_pb_file"]
|
||||
[tasks.gen_pb_file]
|
||||
script = [
|
||||
"""
|
||||
pb_gen_bin=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/scripts/flowy-tool/Cargo.toml
|
||||
flowy_tool=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/scripts/flowy-tool/Cargo.toml
|
||||
rust_source=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/
|
||||
rust_lib=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib
|
||||
flutter_lib=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/app_flowy/packages
|
||||
@ -14,10 +14,28 @@ script = [
|
||||
flutter_package_lib=${flutter_lib}/flowy_sdk/lib
|
||||
|
||||
cargo run \
|
||||
--manifest-path ${pb_gen_bin} pb-gen \
|
||||
--manifest-path ${flowy_tool} pb-gen \
|
||||
--rust_source=${rust_source} \
|
||||
--derive_meta=${derive_meta} \
|
||||
--flutter_package_lib=${flutter_package_lib}
|
||||
""",
|
||||
]
|
||||
script_runner = "@shell"
|
||||
|
||||
|
||||
[tasks.gen_dart_event]
|
||||
script = [
|
||||
"""
|
||||
flowy_tool=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/scripts/flowy-tool/Cargo.toml
|
||||
flutter_lib=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/app_flowy/packages
|
||||
|
||||
rust_source=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/
|
||||
output=${flutter_lib}/flowy_sdk/lib/dispatch/code_gen.dart
|
||||
|
||||
cargo run \
|
||||
--manifest-path ${flowy_tool} dart-event \
|
||||
--rust_source=${rust_source} \
|
||||
--output=${output}
|
||||
""",
|
||||
]
|
||||
script_runner = "@shell"
|
||||
|
Reference in New Issue
Block a user