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">
<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="workingDirectory" value="file://$PROJECT_DIR$" />
<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$/frontend" />
<option name="channel" value="DEFAULT" />
<option name="allFeatures" value="false" />
<option name="emulateTerminal" value="false" />
<option name="backtrace" value="SHORT" />
<envs>
<env name="rust_source" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/" />
<env name="build_cache" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/flowy-derive/src/auto_gen_file/category_from_str.rs" />
<env name="proto_file_output" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/flowy-protobuf/define" />
<env name="rust_mod_dir" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/flowy-protobuf/src/" />
<env name="flutter_mod_dir" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/flutter-lib/packages/flowy_protobuf/lib/" />
<env name="flowy_tool" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/scripts/flowy-tool/Cargo.toml" />
<env name="rust_lib" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/" />
<env name="shared_lib" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/../shared_lib" />
<env name="flutter_lib" value="${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/app_flowy/packages" />
<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>
<option name="isRedirectInput" value="false" />
<option name="redirectInputPath" value="" />

View File

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

View File

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

View File

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

View File

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

View File

@ -5,14 +5,14 @@ use std::path::Path;
use std::{fs::OpenOptions, io::Write};
pub struct ProtoGen {
pub(crate) rust_source_dir: String,
pub(crate) rust_source_dirs: Vec<String>,
pub(crate) flutter_package_lib: String,
pub(crate) derive_meta_dir: String,
}
impl ProtoGen {
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);
// FIXME: ignore unchanged file to reduce time cost

View File

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

View File

@ -19,8 +19,7 @@ script_runner = "@duckscript"
script = [
"""
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
flutter_lib=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/app_flowy/packages
@ -29,7 +28,7 @@ script = [
cargo run \
--manifest-path ${flowy_tool} pb-gen \
--rust_source=${rust_source} \
--rust_sources ${rust_lib},${shared_lib} \
--derive_meta=${derive_meta} \
--flutter_package_lib=${flutter_package_lib}
""",
@ -37,6 +36,7 @@ script = [
script_runner = "@shell"
[tasks.gen_pb_file.windows]
script = [
"""
@ -70,7 +70,7 @@ script = [
cargo run \
--manifest-path ${flowy_tool} dart-event \
--rust_source=${rust_source} \
--rust_sources=${rust_source} \
--output=${output}
""",
]

View File

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