move protobuf generate into bin crate

Signed-off-by: junxiang Mu <1948535941@qq.com>
This commit is contained in:
junxiang Mu
2025-04-21 03:09:54 +00:00
committed by houseme
parent 70e94fd018
commit 177dec627c
3 changed files with 36 additions and 22 deletions
+4
View File
@@ -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 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: Or use Docker:
```yml ```yml
+4 -2
View File
@@ -6,6 +6,10 @@ edition.workspace = true
[lints] [lints]
workspace = true workspace = true
[[bin]]
name = "gproto"
path = "src/main.rs"
[dependencies] [dependencies]
#async-backtrace = { workspace = true, optional = true } #async-backtrace = { workspace = true, optional = true }
common.workspace = true common.workspace = true
@@ -15,7 +19,5 @@ protobuf = { workspace = true }
tokio = { workspace = true } tokio = { workspace = true }
tonic = { workspace = true, features = ["transport"] } tonic = { workspace = true, features = ["transport"] }
tower = { workspace = true } tower = { workspace = true }
[build-dependencies]
prost-build = { workspace = true } prost-build = { workspace = true }
tonic-build = { workspace = true } tonic-build = { workspace = true }
@@ -1,13 +1,7 @@
use std::{ use std::{cmp, env, fs, io::Write, path::Path, process::Command};
cmp, env, fs,
io::Write,
path::{Path, PathBuf},
process::Command,
};
type AnyError = Box<dyn std::error::Error>; type AnyError = Box<dyn std::error::Error>;
const ENV_OUT_DIR: &str = "OUT_DIR";
const VERSION_PROTOBUF: Version = Version(30, 2, 0); // 30.2.0 const VERSION_PROTOBUF: Version = Version(30, 2, 0); // 30.2.0
const VERSION_FLATBUFFERS: Version = Version(24, 3, 25); // 24.3.25 const VERSION_FLATBUFFERS: Version = Version(24, 3, 25); // 24.3.25
/// Build protos if the major version of `flatc` or `protoc` is greater /// Build protos if the major version of `flatc` or `protoc` is greater
@@ -37,16 +31,16 @@ fn main() -> Result<(), AnyError> {
} }
// path of proto file // path of proto file
let project_root_dir = env::current_dir()?; let project_root_dir = env::current_dir()?.join("common/protos/src");
let proto_dir = project_root_dir.join("src"); let proto_dir = project_root_dir.clone();
let proto_files = &["node.proto"]; let proto_files = &["node.proto"];
let proto_out_dir = project_root_dir.join("src").join("generated").join("proto_gen"); let proto_out_dir = project_root_dir.join("generated").join("proto_gen");
let flatbuffer_out_dir = project_root_dir.join("src").join("generated").join("flatbuffers_generated"); 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"); // let descriptor_set_path = PathBuf::from(env::var(ENV_OUT_DIR).unwrap()).join("proto-descriptor.bin");
tonic_build::configure() tonic_build::configure()
.out_dir(proto_out_dir) .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") .protoc_arg("--experimental_allow_proto3_optional")
.compile_well_known_types(true) .compile_well_known_types(true)
.emit_rerun_if_changed(false) .emit_rerun_if_changed(false)
@@ -54,17 +48,13 @@ fn main() -> Result<(), AnyError> {
.map_err(|e| format!("Failed to generate protobuf file: {e}."))?; .map_err(|e| format!("Failed to generate protobuf file: {e}."))?;
// protos/gen/mod.rs // protos/gen/mod.rs
let generated_mod_rs_path = project_root_dir let generated_mod_rs_path = project_root_dir.join("generated").join("proto_gen").join("mod.rs");
.join("src")
.join("generated")
.join("proto_gen")
.join("mod.rs");
let mut generated_mod_rs = fs::File::create(generated_mod_rs_path)?; let mut generated_mod_rs = fs::File::create(generated_mod_rs_path)?;
writeln!(&mut generated_mod_rs, "pub mod node_service;")?; writeln!(&mut generated_mod_rs, "pub mod node_service;")?;
generated_mod_rs.flush()?; 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)?; let mut generated_mod_rs = fs::File::create(generated_mod_rs_path)?;
writeln!(&mut generated_mod_rs, "#![allow(unused_imports)]")?; writeln!(&mut generated_mod_rs, "#![allow(unused_imports)]")?;
@@ -80,7 +70,6 @@ fn main() -> Result<(), AnyError> {
Err(_) => "flatc".to_string(), Err(_) => "flatc".to_string(),
}; };
// build src/protos/*.fbs files to src/protos/gen/
compile_flatbuffers_models( compile_flatbuffers_models(
&mut generated_mod_rs, &mut generated_mod_rs,
&flatc_path, &flatc_path,
@@ -88,6 +77,8 @@ fn main() -> Result<(), AnyError> {
flatbuffer_out_dir.clone(), flatbuffer_out_dir.clone(),
vec!["models"], vec!["models"],
)?; )?;
fmt();
Ok(()) Ok(())
} }
@@ -260,3 +251,20 @@ fn protobuf_compiler_version() -> Result<Version, String> {
} }
}) })
} }
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);
}
}
}