feat: calling user event from web (#4535)

* refactor: user manager

* refactor: user manager

* refactor: session location

* refactor: user manager

* chore: gen ts files

* feat: implement indexeddb persistence

* chore: integrate user manager

* chore: update

* chore: run on web thread

* chore: run on web thread

* chore: fix test

* chore: add test

* chore: add test

* chore: add user & sign in with password

* chore: fix test

* chore: update docs

* chore: fix warnings

* chore: gen files

* chore: add user

* chore: add files

* chore: update config

* chore: update scirpt

* chore: update scirpt

* fix: build

* chore: update command

* fix: ci

* ci: fix

* fix: compile

* fix: compile

* fix: ci

* fix: compile

* fix: tauri build

* chore: fix test

* chore: fix test
This commit is contained in:
Nathan.fooo
2024-01-30 05:36:27 +08:00
committed by GitHub
parent 86a0569d84
commit 55c97b56a3
164 changed files with 9334 additions and 2885 deletions

View File

@ -19,3 +19,55 @@ pub struct ProtoCache {
pub structs: Vec<String>,
pub enums: Vec<String>,
}
pub enum Project {
Tauri,
Web { relative_path: String },
Native,
}
impl Project {
pub fn dst(&self) -> String {
match self {
Project::Tauri => "appflowy_tauri/src/services/backend".to_string(),
Project::Web { .. } => "appflowy_web/src/services/backend".to_string(),
Project::Native => panic!("Native project is not supported yet."),
}
}
pub fn event_root(&self) -> String {
match self {
Project::Tauri => "../../".to_string(),
Project::Web { relative_path } => relative_path.to_string(),
Project::Native => panic!("Native project is not supported yet."),
}
}
pub fn model_root(&self) -> String {
match self {
Project::Tauri => "../../".to_string(),
Project::Web { relative_path } => relative_path.to_string(),
Project::Native => panic!("Native project is not supported yet."),
}
}
pub fn event_imports(&self) -> String {
match self {
Project::Tauri => r#"
/// Auto generate. Do not edit
import { Ok, Err, Result } from "ts-results";
import { invoke } from "@tauri-apps/api/tauri";
import * as pb from "../..";
"#
.to_string(),
Project::Web { .. } => r#"
/// Auto generate. Do not edit
import { Ok, Err, Result } from "ts-results";
import { invoke } from "@/application/app.ts";
import * as pb from "../..";
"#
.to_string(),
Project::Native => panic!("Native project is not supported yet."),
}
}
}

View File

@ -7,6 +7,7 @@ mod proto_info;
mod template;
use crate::util::path_string_with_component;
use crate::Project;
use itertools::Itertools;
use log::info;
pub use proto_gen::*;
@ -17,16 +18,10 @@ use std::path::{Path, PathBuf};
use std::process::Command;
use walkdir::WalkDir;
pub fn gen(crate_name: &str) {
let crate_path = std::fs::canonicalize(".")
.unwrap()
.as_path()
.display()
.to_string();
pub fn dart_gen(crate_name: &str) {
// 1. generate the proto files to proto_file_dir
#[cfg(feature = "proto_gen")]
let proto_crates = gen_proto_files(crate_name, &crate_path);
let proto_crates = gen_proto_files(crate_name);
for proto_crate in proto_crates {
let mut proto_file_paths = vec![];
@ -70,6 +65,55 @@ pub fn gen(crate_name: &str) {
&protoc_bin_path,
);
// 3. generate the protobuf files(Rust)
generate_rust_protobuf_files(
&protoc_bin_path,
&proto_file_paths,
&proto_file_output_path,
&protobuf_output_path,
);
}
}
#[allow(unused_variables)]
pub fn ts_gen(crate_name: &str, project: Project) {
// 1. generate the proto files to proto_file_dir
#[cfg(feature = "proto_gen")]
let proto_crates = gen_proto_files(crate_name);
for proto_crate in proto_crates {
let mut proto_file_paths = vec![];
let mut file_names = vec![];
let proto_file_output_path = proto_crate
.proto_output_path()
.to_str()
.unwrap()
.to_string();
let protobuf_output_path = proto_crate
.protobuf_crate_path()
.to_str()
.unwrap()
.to_string();
for (path, file_name) in WalkDir::new(&proto_file_output_path)
.into_iter()
.filter_map(|e| e.ok())
.map(|e| {
let path = e.path().to_str().unwrap().to_string();
let file_name = e.path().file_stem().unwrap().to_str().unwrap().to_string();
(path, file_name)
})
{
if path.ends_with(".proto") {
// https://stackoverflow.com/questions/49077147/how-can-i-force-build-rs-to-run-again-without-cleaning-my-whole-project
println!("cargo:rerun-if-changed={}", path);
proto_file_paths.push(path);
file_names.push(file_name);
}
}
let protoc_bin_path = protoc_bin_vendored::protoc_bin_path().unwrap();
// 2. generate the protobuf files(Dart)
#[cfg(feature = "ts")]
generate_ts_protobuf_files(
crate_name,
@ -77,6 +121,7 @@ pub fn gen(crate_name: &str) {
&proto_file_paths,
&file_names,
&protoc_bin_path,
&project,
);
// 3. generate the protobuf files(Rust)
@ -111,14 +156,14 @@ fn generate_ts_protobuf_files(
paths: &[String],
file_names: &Vec<String>,
protoc_bin_path: &Path,
project: &Project,
) {
let root = std::env::var("CARGO_MAKE_WORKING_DIRECTORY").unwrap_or("../../".to_string());
let tauri_backend_service_path = std::env::var("TAURI_BACKEND_SERVICE_PATH")
.unwrap_or("appflowy_tauri/src/services/backend".to_string());
let root = project.model_root();
let backend_service_path = project.dst();
let mut output = PathBuf::new();
output.push(root);
output.push(tauri_backend_service_path);
output.push(backend_service_path);
output.push("models");
output.push(name);
@ -136,6 +181,7 @@ fn generate_ts_protobuf_files(
// panic!("Generate ts pb file failed: {}, {:?}", path, err);
// }
println!("cargo:rerun-if-changed={}", output.to_str().unwrap());
let result = cmd_lib::run_cmd! {
${protoc_bin_path} --ts_out=${output} --proto_path=${proto_file_output_path} ${path}
};
@ -211,7 +257,7 @@ fn generate_dart_protobuf_files(
});
let protobuf_dart = path_string_with_component(&output, vec!["protobuf.dart"]);
println!("cargo:rerun-if-changed={}", protobuf_dart);
match std::fs::OpenOptions::new()
.create(true)
.write(true)
@ -282,8 +328,14 @@ pub fn check_pb_dart_plugin() {
}
#[cfg(feature = "proto_gen")]
fn gen_proto_files(crate_name: &str, crate_path: &str) -> Vec<ProtobufCrate> {
let crate_context = ProtoGenerator::gen(crate_name, crate_path);
pub fn gen_proto_files(crate_name: &str) -> Vec<ProtobufCrate> {
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

@ -4,6 +4,7 @@ use crate::ast::EventASTContext;
use crate::flowy_toml::{parse_crate_config_from, CrateConfig};
use crate::ts_event::event_template::{EventRenderContext, EventTemplate};
use crate::util::{is_crate_dir, is_hidden, path_string_with_component, read_file};
use crate::Project;
use flowy_ast::ASTResult;
use std::collections::HashSet;
use std::fs::File;
@ -12,10 +13,9 @@ use std::path::PathBuf;
use syn::Item;
use walkdir::WalkDir;
pub fn gen(crate_name: &str) {
let root = std::env::var("CARGO_MAKE_WORKING_DIRECTORY").unwrap_or("../../".to_string());
let tauri_backend_service_path = std::env::var("TAURI_BACKEND_SERVICE_PATH")
.unwrap_or("appflowy_tauri/src/services/backend".to_string());
pub fn gen(crate_name: &str, project: Project) {
let root = project.event_root();
let backend_service_path = project.dst();
let crate_path = std::fs::canonicalize(".")
.unwrap()
@ -29,7 +29,8 @@ pub fn gen(crate_name: &str) {
.collect::<Vec<_>>();
let event_render_ctx = ast_to_event_render_ctx(event_ast.as_ref());
let mut render_result = TS_HEADER.to_string();
let mut render_result = project.event_imports();
for (index, render_ctx) in event_render_ctx.into_iter().enumerate() {
let mut event_template = EventTemplate::new();
@ -39,7 +40,7 @@ pub fn gen(crate_name: &str) {
}
render_result.push_str(TS_FOOTER);
let ts_event_folder: PathBuf = [&root, &tauri_backend_service_path, "events", crate_name]
let ts_event_folder: PathBuf = [&root, &backend_service_path, "events", crate_name]
.iter()
.collect();
if !ts_event_folder.as_path().exists() {
@ -196,12 +197,5 @@ pub fn ast_to_event_render_ctx(ast: &[EventASTContext]) -> Vec<EventRenderContex
.collect::<Vec<EventRenderContext>>()
}
const TS_HEADER: &str = r#"
/// Auto generate. Do not edit
import { Ok, Err, Result } from "ts-results";
import { invoke } from "@tauri-apps/api/tauri";
import * as pb from "../..";
"#;
const TS_FOOTER: &str = r#"
"#;