瀏覽代碼

create crate protobuf mod file automatically

appflowy 3 年之前
父節點
當前提交
447b5aa128

+ 3 - 3
rust-lib/flowy-derive/src/proto_buf/deserialize.rs

@@ -25,7 +25,7 @@ pub fn make_de_token_steam(ctxt: &Ctxt, ast: &ASTContainer) -> Option<TokenStrea
         impl std::convert::TryFrom<&Vec<u8>> for #struct_ident {
             type Error = String;
             fn try_from(bytes: &Vec<u8>) -> Result<Self, Self::Error> {
-                let result: ::protobuf::ProtobufResult<flowy_protobuf::#pb_ty> = ::protobuf::Message::parse_from_bytes(bytes);
+                let result: ::protobuf::ProtobufResult<crate::protobuf::#pb_ty> = ::protobuf::Message::parse_from_bytes(bytes);
                 match result {
                     Ok(mut pb) => {
                         #struct_ident::try_from(&mut pb)
@@ -35,9 +35,9 @@ pub fn make_de_token_steam(ctxt: &Ctxt, ast: &ASTContainer) -> Option<TokenStrea
             }
         }
 
-        impl std::convert::TryFrom<&mut flowy_protobuf::#pb_ty> for #struct_ident {
+        impl std::convert::TryFrom<&mut crate::protobuf::#pb_ty> for #struct_ident {
             type Error = String;
-            fn try_from(pb: &mut flowy_protobuf::#pb_ty) -> Result<Self, Self::Error> {
+            fn try_from(pb: &mut crate::protobuf::#pb_ty) -> Result<Self, Self::Error> {
                 let mut o = Self::default();
                 #(#build_take_fields)*
                 Ok(o)

+ 0 - 1
rust-lib/flowy-user/Cargo.toml

@@ -10,7 +10,6 @@ derive_more = {version = "0.99", features = ["display"]}
 flowy-sys = { path = "../flowy-sys" }
 flowy-log = { path = "../flowy-log" }
 flowy-derive = { path = "../flowy-derive" }
-flowy-protobuf = { path = "../flowy-protobuf" }
 tracing = { version = "0.1", features = ["log"] }
 bytes = "1.0"
 serde = { version = "1.0", features = ["derive"] }

+ 1 - 0
rust-lib/flowy-user/src/lib.rs

@@ -3,6 +3,7 @@ mod error;
 pub mod event;
 mod handlers;
 pub mod module;
+mod protobuf;
 
 pub mod prelude {
     pub use crate::{

+ 4 - 0
rust-lib/flowy-user/src/protobuf/mod.rs

@@ -0,0 +1,4 @@
+
+mod model;
+pub use model::*;
+        

+ 27 - 1
scripts/flowy-tool/src/proto/helper.rs

@@ -1,3 +1,5 @@
+use std::fs::OpenOptions;
+use std::io::Write;
 use walkdir::WalkDir;
 
 #[derive(Clone)]
@@ -29,7 +31,7 @@ impl CrateInfo {
         dir
     }
 
-    pub fn crate_mod_file(&self) -> String {
+    pub fn proto_model_mod_file(&self) -> String {
         format!("{}/mod.rs", self.proto_struct_output_dir())
     }
 
@@ -44,6 +46,30 @@ impl CrateProtoInfo {
     pub fn from_crate_info(inner: CrateInfo, files: Vec<FileProtoInfo>) -> Self {
         Self { files, inner }
     }
+
+    pub fn create_crate_mod_file(&self) {
+        // mod model;
+        // pub use model::*;
+        let mod_file_path = format!("{}/mod.rs", self.inner.protobuf_crate_name());
+        let content = r#"
+mod model;
+pub use model::*;
+        "#;
+        match OpenOptions::new()
+            .create(true)
+            .write(true)
+            .append(false)
+            .truncate(true)
+            .open(&mod_file_path)
+        {
+            Ok(ref mut file) => {
+                file.write_all(content.as_bytes()).unwrap();
+            }
+            Err(err) => {
+                panic!("Failed to open protobuf mod file: {}", err);
+            }
+        }
+    }
 }
 
 #[derive(Debug)]

+ 14 - 6
scripts/flowy-tool/src/proto/proto_gen.rs

@@ -23,6 +23,8 @@ impl ProtoGen {
 
         run_protoc(&crate_proto_infos);
 
+        write_protobuf_crate_mod_file(&crate_proto_infos);
+
         write_derive_meta(&crate_proto_infos, self.derive_meta_dir.as_ref());
 
         write_rust_crate_protobuf(&crate_proto_infos);
@@ -45,7 +47,7 @@ fn write_proto_files(crate_infos: &Vec<CrateProtoInfo>) {
 
 fn write_rust_crate_protobuf(crate_infos: &Vec<CrateProtoInfo>) {
     for crate_info in crate_infos {
-        let mod_path = crate_info.inner.crate_mod_file();
+        let mod_path = crate_info.inner.proto_model_mod_file();
         match OpenOptions::new()
             .create(true)
             .write(true)
@@ -79,10 +81,6 @@ fn write_rust_crate_protobuf(crate_infos: &Vec<CrateProtoInfo>) {
 }
 
 fn run_protoc(crate_infos: &Vec<CrateProtoInfo>) {
-    // protoc --rust_out=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/flowy-protobuf/src/model \
-    // --proto_path=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/flowy-protobuf/define \
-    // ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/flowy-protobuf/define/*.proto
-
     for crate_info in crate_infos {
         let rust_out = crate_info.inner.proto_struct_output_dir();
         let proto_path = crate_info.inner.proto_file_output_dir();
@@ -93,9 +91,19 @@ fn run_protoc(crate_infos: &Vec<CrateProtoInfo>) {
             .filter(|e| is_proto_file(e))
             .map(|e| e.path().to_str().unwrap().to_string())
         {
-            cmd_lib::run_cmd! {
+            if cmd_lib::run_cmd! {
                 protoc --rust_out=${rust_out} --proto_path=${proto_path} ${proto_file}
+            }
+            .is_err()
+            {
+                panic!("Create protobuf rust struct fail")
             };
         }
     }
 }
+
+fn write_protobuf_crate_mod_file(crate_infos: &Vec<CrateProtoInfo>) {
+    for crate_info in crate_infos {
+        crate_info.create_crate_mod_file();
+    }
+}