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")) +}