feat: add flowy_clean command to remove the build.rs cache

This commit is contained in:
appflowy 2022-02-15 22:35:52 +08:00
parent 0933935071
commit bf3a0b6e5b
11 changed files with 93 additions and 24 deletions

View File

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

View File

@ -40,5 +40,12 @@
"preLaunchTask": "Generate Language Files",
"cwd": "${workspaceRoot}"
},
{
"name": "Clean",
"request": "launch",
"type": "dart",
"preLaunchTask": "Clean",
"cwd": "${workspaceRoot}"
}
]
}

View File

@ -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}/../"
},
}
]
}

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");
// dart_event::gen(crate_name);
}

View File

@ -0,0 +1,7 @@
cd rust-lib
cargo clean
cd ../../shared-lib
cargo clean
rmdir /s/q lib-infra/.cache

10
frontend/scripts/clean.sh Normal file
View File

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

View File

@ -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<TokenStream, Vec<syn::Error>> {
let ctxt = Ctxt::new();

View File

@ -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::<Vec<_>>();
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::<Vec<_>>();
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<String>) -> Vec<DartEventCrate> {
let mut dart_event_crates: Vec<DartEventCrate> = 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())

View File

@ -153,9 +153,8 @@ fn run_command(cmd: &str) -> bool {
#[cfg(feature = "proto_gen")]
fn gen_protos(crate_name: &str) -> Vec<ProtobufCrate> {
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())

View File

@ -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<ProtobufCrateContext> {
pub fn gen(crate_name: &str, crate_path: &str) -> Vec<ProtobufCrateContext> {
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();
}

View File

@ -152,3 +152,7 @@ pub fn get_tera(directory: &str) -> Tera {
}
}
}
pub fn cache_dir() -> String {
format!("{}/.cache", env!("CARGO_MANIFEST_DIR"))
}