fix flowy_tool issues

This commit is contained in:
appflowy 2021-11-29 20:07:38 +08:00
parent 180b0cd568
commit f9bfe94a6b
9 changed files with 91 additions and 66 deletions

View File

@ -1,17 +1,18 @@
<component name="ProjectRunConfigurationManager"> <component name="ProjectRunConfigurationManager">
<configuration default="false" name="ProtoBuf_Gen" type="CargoCommandRunConfiguration" factoryName="Cargo Command"> <configuration default="false" name="ProtoBuf_Gen" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="command" value="run --manifest-path $PROJECT_DIR$/scripts/flowy-tool/Cargo.toml -- pb-gen --rust_source=$PROJECT_DIR$/rust-lib/ --derive_meta=$PROJECT_DIR$/rust-lib/flowy-derive/src/derive_cache/derive_cache.rs --flutter_package_lib=$PROJECT_DIR$/app_flowy/packages/flowy_sdk/lib" /> <option name="command" value="run --manifest-path ${flowy_tool} -- pb-gen --rust_sources=${rust_lib},${shared_lib} --derive_meta=${derive_meta} --flutter_package_lib=${flutter_package_lib}" />
<option name="workingDirectory" value="file://$PROJECT_DIR$" /> <option name="workingDirectory" value="file://$PROJECT_DIR$/frontend" />
<option name="channel" value="DEFAULT" /> <option name="channel" value="DEFAULT" />
<option name="allFeatures" value="false" /> <option name="allFeatures" value="false" />
<option name="emulateTerminal" value="false" /> <option name="emulateTerminal" value="false" />
<option name="backtrace" value="SHORT" /> <option name="backtrace" value="SHORT" />
<envs> <envs>
<env name="rust_source" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/" /> <env name="flowy_tool" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/scripts/flowy-tool/Cargo.toml" />
<env name="build_cache" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/flowy-derive/src/auto_gen_file/category_from_str.rs" /> <env name="rust_lib" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/" />
<env name="proto_file_output" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/flowy-protobuf/define" /> <env name="shared_lib" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/../shared_lib" />
<env name="rust_mod_dir" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/flowy-protobuf/src/" /> <env name="flutter_lib" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/app_flowy/packages" />
<env name="flutter_mod_dir" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/flutter-lib/packages/flowy_protobuf/lib/" /> <env name="derive_meta" value="${shared_lib}/flowy-derive/src/derive_cache/derive_cache.rs" />
<env name="flutter_package_lib" value="${flutter_lib}/flowy_sdk/lib" />
</envs> </envs>
<option name="isRedirectInput" value="false" /> <option name="isRedirectInput" value="false" />
<option name="redirectInputPath" value="" /> <option name="redirectInputPath" value="" />

View File

@ -6,13 +6,13 @@ use syn::Item;
use walkdir::WalkDir; use walkdir::WalkDir;
pub struct DartEventCodeGen { pub struct DartEventCodeGen {
pub rust_source: String, pub rust_sources: Vec<String>,
pub output_dir: String, pub output_dir: String,
} }
impl DartEventCodeGen { impl DartEventCodeGen {
pub fn gen(&self) { pub fn gen(&self) {
let event_crates = parse_dart_event_files(self.rust_source.as_ref()); let event_crates = parse_dart_event_files(self.rust_sources.clone());
let event_ast = event_crates let event_ast = event_crates
.iter() .iter()
.map(|event_crate| parse_event_crate(event_crate)) .map(|event_crate| parse_event_crate(event_crate))
@ -55,15 +55,20 @@ impl DartEventCrate {
} }
} }
pub fn parse_dart_event_files(root: &str) -> Vec<DartEventCrate> { pub fn parse_dart_event_files(roots: Vec<String>) -> Vec<DartEventCrate> {
WalkDir::new(root) let mut dart_event_crates: Vec<DartEventCrate> = vec![];
.into_iter() roots.iter().for_each(|root| {
.filter_entry(|e| !is_hidden(e)) let crates = WalkDir::new(root)
.filter_map(|e| e.ok()) .into_iter()
.filter(|e| is_crate_dir(e)) .filter_entry(|e| !is_hidden(e))
.flat_map(|e| parse_crate_config_from(&e)) .filter_map(|e| e.ok())
.map(|crate_config| DartEventCrate::from_config(&crate_config)) .filter(|e| is_crate_dir(e))
.collect::<Vec<DartEventCrate>>() .flat_map(|e| parse_crate_config_from(&e))
.map(|crate_config| DartEventCrate::from_config(&crate_config))
.collect::<Vec<DartEventCrate>>();
dart_event_crates.extend(crates);
});
dart_event_crates
} }
pub fn parse_event_crate(event_crate: &DartEventCrate) -> Vec<EventASTContext> { pub fn parse_event_crate(event_crate: &DartEventCrate) -> Vec<EventASTContext> {

View File

@ -11,12 +11,16 @@ fn main() {
let matches = app().get_matches(); let matches = app().get_matches();
if let Some(ref matches) = matches.subcommand_matches("pb-gen") { if let Some(ref matches) = matches.subcommand_matches("pb-gen") {
let rust_source = matches.value_of("rust_source").unwrap(); let rust_sources: Vec<String> = matches
.values_of("rust_sources")
.unwrap()
.map(|value| value.to_owned())
.collect();
let derive_meta = matches.value_of("derive_meta").unwrap(); let derive_meta = matches.value_of("derive_meta").unwrap();
let flutter_package_lib = matches.value_of("flutter_package_lib").unwrap(); let flutter_package_lib = matches.value_of("flutter_package_lib").unwrap();
proto::ProtoGenBuilder::new() proto::ProtoGenBuilder::new()
.set_rust_source_dir(rust_source) .set_rust_source_dirs(rust_sources)
.set_derive_meta_dir(derive_meta) .set_derive_meta_dir(derive_meta)
.set_flutter_package_lib(flutter_package_lib) .set_flutter_package_lib(flutter_package_lib)
.build() .build()
@ -24,11 +28,15 @@ fn main() {
} }
if let Some(ref matches) = matches.subcommand_matches("dart-event") { if let Some(ref matches) = matches.subcommand_matches("dart-event") {
let rust_source = matches.value_of("rust_source").unwrap().to_string(); let rust_sources: Vec<String> = matches
.values_of("rust_sources")
.unwrap()
.map(|value| value.to_owned())
.collect();
let output_dir = matches.value_of("output").unwrap().to_string(); let output_dir = matches.value_of("output").unwrap().to_string();
let code_gen = dart_event::DartEventCodeGen { let code_gen = dart_event::DartEventCodeGen {
rust_source, rust_sources,
output_dir, output_dir,
}; };
code_gen.gen(); code_gen.gen();
@ -44,10 +52,13 @@ pub fn app<'a, 'b>() -> App<'a, 'b> {
App::new("pb-gen") App::new("pb-gen")
.about("Generate proto file from rust code") .about("Generate proto file from rust code")
.arg( .arg(
Arg::with_name("rust_source") Arg::with_name("rust_sources")
.long("rust_source") .long("rust_sources")
.multiple(true)
.required(true)
.min_values(1)
.value_name("DIRECTORY") .value_name("DIRECTORY")
.help("Directory of the cargo workspace"), .help("Directories of the cargo workspace"),
) )
.arg( .arg(
Arg::with_name("derive_meta") Arg::with_name("derive_meta")
@ -65,10 +76,13 @@ pub fn app<'a, 'b>() -> App<'a, 'b> {
App::new("dart-event") App::new("dart-event")
.about("Generate the codes that sending events from rust ast") .about("Generate the codes that sending events from rust ast")
.arg( .arg(
Arg::with_name("rust_source") Arg::with_name("rust_sources")
.long("rust_source") .long("rust_sources")
.multiple(true)
.required(true)
.min_values(1)
.value_name("DIRECTORY") .value_name("DIRECTORY")
.help("Directory of the cargo workspace"), .help("Directories of the cargo workspace"),
) )
.arg( .arg(
Arg::with_name("output") Arg::with_name("output")

View File

@ -8,8 +8,8 @@ use std::{fs::File, io::Read, path::Path};
use syn::Item; use syn::Item;
use walkdir::WalkDir; use walkdir::WalkDir;
pub fn parse_crate_protobuf(root: &str) -> Vec<CrateProtoInfo> { pub fn parse_crate_protobuf(roots: Vec<String>) -> Vec<CrateProtoInfo> {
let crate_infos = parse_crate_info_from_path(root); let crate_infos = parse_crate_info_from_path(roots);
crate_infos crate_infos
.into_iter() .into_iter()
.map(|crate_info| { .map(|crate_info| {

View File

@ -2,7 +2,7 @@ use crate::proto::ProtoGen;
#[allow(dead_code)] #[allow(dead_code)]
pub struct ProtoGenBuilder { pub struct ProtoGenBuilder {
rust_source_dir: Option<String>, rust_source_dirs: Option<Vec<String>>,
flutter_package_lib: Option<String>, flutter_package_lib: Option<String>,
derive_meta_dir: Option<String>, derive_meta_dir: Option<String>,
} }
@ -10,14 +10,14 @@ pub struct ProtoGenBuilder {
impl ProtoGenBuilder { impl ProtoGenBuilder {
pub fn new() -> Self { pub fn new() -> Self {
ProtoGenBuilder { ProtoGenBuilder {
rust_source_dir: None, rust_source_dirs: None,
flutter_package_lib: None, flutter_package_lib: None,
derive_meta_dir: None, derive_meta_dir: None,
} }
} }
pub fn set_rust_source_dir(mut self, dir: &str) -> Self { pub fn set_rust_source_dirs(mut self, dirs: Vec<String>) -> Self {
self.rust_source_dir = Some(dir.to_string()); self.rust_source_dirs = Some(dirs);
self self
} }
@ -33,7 +33,7 @@ impl ProtoGenBuilder {
pub fn build(self) -> ProtoGen { pub fn build(self) -> ProtoGen {
ProtoGen { ProtoGen {
rust_source_dir: self.rust_source_dir.unwrap(), rust_source_dirs: self.rust_source_dirs.unwrap(),
flutter_package_lib: self.flutter_package_lib.unwrap(), flutter_package_lib: self.flutter_package_lib.unwrap(),
derive_meta_dir: self.derive_meta_dir.unwrap(), derive_meta_dir: self.derive_meta_dir.unwrap(),
} }

View File

@ -5,14 +5,14 @@ use std::path::Path;
use std::{fs::OpenOptions, io::Write}; use std::{fs::OpenOptions, io::Write};
pub struct ProtoGen { pub struct ProtoGen {
pub(crate) rust_source_dir: String, pub(crate) rust_source_dirs: Vec<String>,
pub(crate) flutter_package_lib: String, pub(crate) flutter_package_lib: String,
pub(crate) derive_meta_dir: String, pub(crate) derive_meta_dir: String,
} }
impl ProtoGen { impl ProtoGen {
pub fn gen(&self) { pub fn gen(&self) {
let crate_proto_infos = parse_crate_protobuf(self.rust_source_dir.as_ref()); let crate_proto_infos = parse_crate_protobuf(self.rust_source_dirs.clone());
write_proto_files(&crate_proto_infos); write_proto_files(&crate_proto_infos);
// FIXME: ignore unchanged file to reduce time cost // FIXME: ignore unchanged file to reduce time cost

View File

@ -95,15 +95,20 @@ pub struct ProtoFile {
pub generated_content: String, pub generated_content: String,
} }
pub fn parse_crate_info_from_path(root: &str) -> Vec<ProtobufCrate> { pub fn parse_crate_info_from_path(roots: Vec<String>) -> Vec<ProtobufCrate> {
WalkDir::new(root) let mut protobuf_crates: Vec<ProtobufCrate> = vec![];
.into_iter() roots.iter().for_each(|root| {
.filter_entry(|e| !is_hidden(e)) let crates = WalkDir::new(root)
.filter_map(|e| e.ok()) .into_iter()
.filter(|e| is_crate_dir(e)) .filter_entry(|e| !is_hidden(e))
.flat_map(|e| parse_crate_config_from(&e)) .filter_map(|e| e.ok())
.map(ProtobufCrate::from_config) .filter(|e| is_crate_dir(e))
.collect::<Vec<ProtobufCrate>>() .flat_map(|e| parse_crate_config_from(&e))
.map(ProtobufCrate::from_config)
.collect::<Vec<ProtobufCrate>>();
protobuf_crates.extend(crates);
});
protobuf_crates
} }
pub struct FlutterProtobufInfo { pub struct FlutterProtobufInfo {

View File

@ -19,8 +19,7 @@ script_runner = "@duckscript"
script = [ script = [
""" """
flowy_tool=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/scripts/flowy-tool/Cargo.toml flowy_tool=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/scripts/flowy-tool/Cargo.toml
rust_source=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/ rust_lib=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/
# rust_lib=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib
shared_lib=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/../shared-lib shared_lib=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/../shared-lib
flutter_lib=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/app_flowy/packages flutter_lib=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/app_flowy/packages
@ -29,7 +28,7 @@ script = [
cargo run \ cargo run \
--manifest-path ${flowy_tool} pb-gen \ --manifest-path ${flowy_tool} pb-gen \
--rust_source=${rust_source} \ --rust_sources ${rust_lib},${shared_lib} \
--derive_meta=${derive_meta} \ --derive_meta=${derive_meta} \
--flutter_package_lib=${flutter_package_lib} --flutter_package_lib=${flutter_package_lib}
""", """,
@ -37,6 +36,7 @@ script = [
script_runner = "@shell" script_runner = "@shell"
[tasks.gen_pb_file.windows] [tasks.gen_pb_file.windows]
script = [ script = [
""" """
@ -70,7 +70,7 @@ script = [
cargo run \ cargo run \
--manifest-path ${flowy_tool} dart-event \ --manifest-path ${flowy_tool} dart-event \
--rust_source=${rust_source} \ --rust_sources=${rust_source} \
--output=${output} --output=${output}
""", """,
] ]

View File

@ -15,7 +15,14 @@ pub fn category_from_str(type_str: &str) -> TypeCategory {
"HashMap" => TypeCategory::Map, "HashMap" => TypeCategory::Map,
"u8" => TypeCategory::Bytes, "u8" => TypeCategory::Bytes,
"String" => TypeCategory::Str, "String" => TypeCategory::Str,
"QueryAppRequest" "KeyValue"
| "WorkspaceError"
| "DocError"
| "FFIRequest"
| "FFIResponse"
| "SubscribeObject"
| "UserError"
| "QueryAppRequest"
| "AppIdentifier" | "AppIdentifier"
| "CreateAppRequest" | "CreateAppRequest"
| "ColorStyle" | "ColorStyle"
@ -58,8 +65,6 @@ pub fn category_from_str(type_str: &str) -> TypeCategory {
| "Revision" | "Revision"
| "RevisionRange" | "RevisionRange"
| "WsDocumentData" | "WsDocumentData"
| "KeyValue"
| "WorkspaceError"
| "WsError" | "WsError"
| "WsMessage" | "WsMessage"
| "SignInRequest" | "SignInRequest"
@ -72,25 +77,20 @@ pub fn category_from_str(type_str: &str) -> TypeCategory {
| "UserProfile" | "UserProfile"
| "UpdateUserRequest" | "UpdateUserRequest"
| "UpdateUserParams" | "UpdateUserParams"
| "DocError"
| "FFIRequest"
| "FFIResponse"
| "SubscribeObject"
| "UserError"
=> TypeCategory::Protobuf, => TypeCategory::Protobuf,
"TrashType" "WorkspaceEvent"
| "ViewType"
| "ExportType"
| "ErrorCode"
| "RevType"
| "WsDataType"
| "WorkspaceEvent"
| "WorkspaceNotification" | "WorkspaceNotification"
| "WsModule" | "ErrorCode"
| "DocObservable" | "DocObservable"
| "FFIStatusCode" | "FFIStatusCode"
| "UserEvent" | "UserEvent"
| "UserNotification" | "UserNotification"
| "TrashType"
| "ViewType"
| "ExportType"
| "RevType"
| "WsDataType"
| "WsModule"
=> TypeCategory::Enum, => TypeCategory::Enum,
"Option" => TypeCategory::Opt, "Option" => TypeCategory::Opt,