From 177dec627cfafe8bcdbf783e107dbcd9ca9ad57b Mon Sep 17 00:00:00 2001 From: junxiang Mu <1948535941@qq.com> Date: Mon, 21 Apr 2025 03:09:54 +0000 Subject: [PATCH] move protobuf generate into bin crate Signed-off-by: junxiang Mu <1948535941@qq.com> --- README.md | 4 +++ common/protos/Cargo.toml | 6 ++-- common/protos/{build.rs => src/main.rs} | 48 ++++++++++++++----------- 3 files changed, 36 insertions(+), 22 deletions(-) rename common/protos/{build.rs => src/main.rs} (88%) diff --git a/README.md b/README.md index 3e1de2cb..511f6736 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,10 @@ https://github.com/google/flatbuffers/releases/download/v25.2.10/Linux.flatc.bin https://github.com/protocolbuffers/protobuf/releases/download/v30.2/protoc-30.2-linux-x86_64.zip +generate protobuf code: + +```cargo run --bin gproto``` + Or use Docker: ```yml diff --git a/common/protos/Cargo.toml b/common/protos/Cargo.toml index 581fb2d0..2041375c 100644 --- a/common/protos/Cargo.toml +++ b/common/protos/Cargo.toml @@ -6,6 +6,10 @@ edition.workspace = true [lints] workspace = true +[[bin]] +name = "gproto" +path = "src/main.rs" + [dependencies] #async-backtrace = { workspace = true, optional = true } common.workspace = true @@ -15,7 +19,5 @@ protobuf = { workspace = true } tokio = { workspace = true } tonic = { workspace = true, features = ["transport"] } tower = { workspace = true } - -[build-dependencies] prost-build = { workspace = true } tonic-build = { workspace = true } diff --git a/common/protos/build.rs b/common/protos/src/main.rs similarity index 88% rename from common/protos/build.rs rename to common/protos/src/main.rs index fc55ec7a..44fe20a1 100644 --- a/common/protos/build.rs +++ b/common/protos/src/main.rs @@ -1,13 +1,7 @@ -use std::{ - cmp, env, fs, - io::Write, - path::{Path, PathBuf}, - process::Command, -}; +use std::{cmp, env, fs, io::Write, path::Path, process::Command}; type AnyError = Box; -const ENV_OUT_DIR: &str = "OUT_DIR"; const VERSION_PROTOBUF: Version = Version(30, 2, 0); // 30.2.0 const VERSION_FLATBUFFERS: Version = Version(24, 3, 25); // 24.3.25 /// Build protos if the major version of `flatc` or `protoc` is greater @@ -37,16 +31,16 @@ fn main() -> Result<(), AnyError> { } // path of proto file - let project_root_dir = env::current_dir()?; - let proto_dir = project_root_dir.join("src"); + let project_root_dir = env::current_dir()?.join("common/protos/src"); + let proto_dir = project_root_dir.clone(); let proto_files = &["node.proto"]; - let proto_out_dir = project_root_dir.join("src").join("generated").join("proto_gen"); - let flatbuffer_out_dir = project_root_dir.join("src").join("generated").join("flatbuffers_generated"); - let descriptor_set_path = PathBuf::from(env::var(ENV_OUT_DIR).unwrap()).join("proto-descriptor.bin"); + let proto_out_dir = project_root_dir.join("generated").join("proto_gen"); + let flatbuffer_out_dir = project_root_dir.join("generated").join("flatbuffers_generated"); + // let descriptor_set_path = PathBuf::from(env::var(ENV_OUT_DIR).unwrap()).join("proto-descriptor.bin"); tonic_build::configure() .out_dir(proto_out_dir) - .file_descriptor_set_path(descriptor_set_path) + // .file_descriptor_set_path(descriptor_set_path) .protoc_arg("--experimental_allow_proto3_optional") .compile_well_known_types(true) .emit_rerun_if_changed(false) @@ -54,17 +48,13 @@ fn main() -> Result<(), AnyError> { .map_err(|e| format!("Failed to generate protobuf file: {e}."))?; // protos/gen/mod.rs - let generated_mod_rs_path = project_root_dir - .join("src") - .join("generated") - .join("proto_gen") - .join("mod.rs"); + let generated_mod_rs_path = project_root_dir.join("generated").join("proto_gen").join("mod.rs"); let mut generated_mod_rs = fs::File::create(generated_mod_rs_path)?; writeln!(&mut generated_mod_rs, "pub mod node_service;")?; generated_mod_rs.flush()?; - let generated_mod_rs_path = project_root_dir.join("src").join("generated").join("mod.rs"); + let generated_mod_rs_path = project_root_dir.join("generated").join("mod.rs"); let mut generated_mod_rs = fs::File::create(generated_mod_rs_path)?; writeln!(&mut generated_mod_rs, "#![allow(unused_imports)]")?; @@ -80,7 +70,6 @@ fn main() -> Result<(), AnyError> { Err(_) => "flatc".to_string(), }; - // build src/protos/*.fbs files to src/protos/gen/ compile_flatbuffers_models( &mut generated_mod_rs, &flatc_path, @@ -88,6 +77,8 @@ fn main() -> Result<(), AnyError> { flatbuffer_out_dir.clone(), vec!["models"], )?; + + fmt(); Ok(()) } @@ -260,3 +251,20 @@ fn protobuf_compiler_version() -> Result { } }) } + +fn fmt() { + let output = Command::new("cargo").arg("fmt").arg("-p").arg("protos").status(); + + match output { + Ok(status) => { + if status.success() { + println!("cargo fmt executed successfully."); + } else { + eprintln!("cargo fmt failed with status: {:?}", status); + } + } + Err(e) => { + eprintln!("Failed to execute cargo fmt: {}", e); + } + } +}