diff --git a/frontend/scripts/flowy-tool/src/dart_event/dart_event.rs b/frontend/scripts/flowy-tool/src/dart_event/dart_event.rs index e4288033af..167663a6b2 100644 --- a/frontend/scripts/flowy-tool/src/dart_event/dart_event.rs +++ b/frontend/scripts/flowy-tool/src/dart_event/dart_event.rs @@ -15,7 +15,7 @@ impl DartEventCodeGen { let event_crates = parse_dart_event_files(self.rust_sources.clone()); let event_ast = event_crates .iter() - .map(|event_crate| parse_event_crate(event_crate)) + .map(parse_event_crate) .flatten() .collect::>(); @@ -62,7 +62,7 @@ pub fn parse_dart_event_files(roots: Vec) -> Vec { .into_iter() .filter_entry(|e| !is_hidden(e)) .filter_map(|e| e.ok()) - .filter(|e| is_crate_dir(e)) + .filter(is_crate_dir) .flat_map(|e| parse_crate_config_from(&e)) .map(|crate_config| DartEventCrate::from_config(&crate_config)) .collect::>(); diff --git a/frontend/scripts/flowy-tool/src/main.rs b/frontend/scripts/flowy-tool/src/main.rs index c25b0041b5..7b23f2d00a 100644 --- a/frontend/scripts/flowy-tool/src/main.rs +++ b/frontend/scripts/flowy-tool/src/main.rs @@ -10,7 +10,7 @@ fn main() { let matches = app().get_matches(); - if let Some(ref matches) = matches.subcommand_matches("pb-gen") { + if let Some(matches) = matches.subcommand_matches("pb-gen") { let rust_sources: Vec = matches .values_of("rust_sources") .unwrap() @@ -27,7 +27,7 @@ fn main() { .gen(); } - if let Some(ref matches) = matches.subcommand_matches("dart-event") { + if let Some(matches) = matches.subcommand_matches("dart-event") { let rust_sources: Vec = matches .values_of("rust_sources") .unwrap() diff --git a/frontend/scripts/flowy-tool/src/proto/ast.rs b/frontend/scripts/flowy-tool/src/proto/ast.rs index e03fd9a3a2..ba02480685 100644 --- a/frontend/scripts/flowy-tool/src/proto/ast.rs +++ b/frontend/scripts/flowy-tool/src/proto/ast.rs @@ -59,7 +59,7 @@ fn parse_files_protobuf(proto_crate_path: &str, proto_output_dir: &str) -> Vec

Vec

, file_names: &Vec) { - let output = format!( - "{}/{}/{}", - env!("CARGO_MAKE_WORKING_DIRECTORY"), - env!("FLUTTER_FLOWY_SDK_PATH"), - name - ); + if std::env::var("CARGO_MAKE_WORKING_DIRECTORY").is_err() { + log::warn!("CARGO_MAKE_WORKING_DIRECTORY was not set, skip generate dart pb"); + return; + } + + if std::env::var("FLUTTER_FLOWY_SDK_PATH").is_err() { + log::warn!("FLUTTER_FLOWY_SDK_PATH was not set, skip generate dart pb"); + return; + } + + 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); if !std::path::Path::new(&output).exists() { std::fs::create_dir_all(&output).unwrap(); } diff --git a/shared-lib/lib-infra/src/proto_gen/ast.rs b/shared-lib/lib-infra/src/proto_gen/ast.rs index 2c313c0dea..8da1f64f62 100644 --- a/shared-lib/lib-infra/src/proto_gen/ast.rs +++ b/shared-lib/lib-infra/src/proto_gen/ast.rs @@ -56,7 +56,7 @@ fn parse_files_protobuf(proto_crate_path: &str, proto_output_dir: &str) -> Vec

Vec

{ + Err(_err) => { panic!("Failed to open file: {}", protobuf_cache_path); } } @@ -99,7 +99,7 @@ impl ProtoCache { fn from_crate_contexts(crate_contexts: &[ProtobufCrateContext]) -> Self { let proto_files = crate_contexts .iter() - .map(|ref crate_info| &crate_info.files) + .map(|crate_info| &crate_info.files) .flatten() .collect::>(); diff --git a/shared-lib/lib-infra/src/proto_gen/proto_info.rs b/shared-lib/lib-infra/src/proto_gen/proto_info.rs index 4f1ed49aa1..32b05e106c 100644 --- a/shared-lib/lib-infra/src/proto_gen/proto_info.rs +++ b/shared-lib/lib-infra/src/proto_gen/proto_info.rs @@ -109,7 +109,7 @@ pub fn parse_crate_info_from_path(roots: Vec) -> Vec { .into_iter() .filter_entry(|e| !is_hidden(e)) .filter_map(|e| e.ok()) - .filter(|e| is_crate_dir(e)) + .filter(is_crate_dir) .flat_map(|e| parse_crate_config_from(&e)) .map(ProtobufCrate::from_config) .collect::>(); diff --git a/shared-lib/lib-infra/src/proto_gen/template/derive_meta/derive_meta.rs b/shared-lib/lib-infra/src/proto_gen/template/derive_meta/derive_meta.rs index f84ebef5f7..336e5c201c 100644 --- a/shared-lib/lib-infra/src/proto_gen/template/derive_meta/derive_meta.rs +++ b/shared-lib/lib-infra/src/proto_gen/template/derive_meta/derive_meta.rs @@ -1,8 +1,5 @@ -use crate::proto_gen::proto_info::{ProtoFile, ProtobufCrateContext}; -use crate::proto_gen::template::{get_tera, read_file}; +use crate::proto_gen::template::get_tera; use itertools::Itertools; -use std::fs::OpenOptions; -use std::io::Write; use tera::Context; pub struct ProtobufDeriveMeta { @@ -36,44 +33,3 @@ impl ProtobufDeriveMeta { } } } - -pub fn write_derive_meta(crate_infos: &[ProtobufCrateContext], derive_meta_dir: &str) { - let file_proto_infos = crate_infos - .iter() - .map(|ref crate_info| &crate_info.files) - .flatten() - .collect::>(); - - let structs: Vec = file_proto_infos - .iter() - .map(|info| info.structs.clone()) - .flatten() - .collect(); - let enums: Vec = file_proto_infos - .iter() - .map(|info| info.enums.clone()) - .flatten() - .collect(); - - let mut derive_template = ProtobufDeriveMeta::new(structs, enums); - let new_content = derive_template.render().unwrap(); - let old_content = read_file(derive_meta_dir).unwrap(); - if new_content == old_content { - return; - } - // println!("{}", diff_lines(&old_content, &new_content)); - match OpenOptions::new() - .create(true) - .write(true) - .append(false) - .truncate(true) - .open(derive_meta_dir) - { - Ok(ref mut file) => { - file.write_all(new_content.as_bytes()).unwrap(); - } - Err(err) => { - panic!("Failed to open log file: {}", err); - } - } -} diff --git a/shared-lib/lib-infra/src/proto_gen/template/mod.rs b/shared-lib/lib-infra/src/proto_gen/template/mod.rs index 27624e66a2..e610bc373b 100644 --- a/shared-lib/lib-infra/src/proto_gen/template/mod.rs +++ b/shared-lib/lib-infra/src/proto_gen/template/mod.rs @@ -11,7 +11,13 @@ pub fn get_tera(directory: &str) -> Tera { let mut root = format!("{}/../", file!()); root.push_str(directory); - let root_absolute_path = std::fs::canonicalize(root).unwrap().as_path().display().to_string(); + let root_absolute_path = match std::fs::canonicalize(root) { + Ok(p) => p.as_path().display().to_string(), + Err(e) => { + panic!("canonicalize {} failed {:?}", root, e); + } + }; + let mut template_path = format!("{}/**/*.tera", root_absolute_path); if cfg!(windows) { // remove "\\?\" prefix on windows @@ -27,6 +33,7 @@ pub fn get_tera(directory: &str) -> Tera { } } +#[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();