-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Rust protos, dependencies enhancements #2538
Changes from all commits
652c21d
c51aca0
be994e3
d119996
2ad8dd8
d51bcbb
a8348bf
3261d4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,28 +1,59 @@ | ||||||||||||||||
use prost_build::Config; | ||||||||||||||||
use regex::Regex; | ||||||||||||||||
use std::env; | ||||||||||||||||
use std::path::PathBuf; | ||||||||||||||||
|
||||||||||||||||
fn main() -> Result<(), tonic_buf_build::error::TonicBufBuildError> { | ||||||||||||||||
if std::env::var("V4_PROTO_REBUILD").is_ok() { | ||||||||||||||||
let mut config = Config::new(); | ||||||||||||||||
config.out_dir("src"); | ||||||||||||||||
config.include_file("_includes.rs"); | ||||||||||||||||
config.enable_type_names(); | ||||||||||||||||
let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").map_err(|e| { | ||||||||||||||||
tonic_buf_build::error::TonicBufBuildError { | ||||||||||||||||
message: format!("Failed to get CARGO_MANIFEST_DIR: {}", e), | ||||||||||||||||
cause: None, | ||||||||||||||||
} | ||||||||||||||||
})?); | ||||||||||||||||
path.pop(); | ||||||||||||||||
tonic_buf_build::compile_from_buf_workspace_with_config( | ||||||||||||||||
tonic_build::configure().build_server(false), | ||||||||||||||||
Some(config), | ||||||||||||||||
tonic_buf_build::TonicBufConfig { | ||||||||||||||||
buf_dir: Some(path), | ||||||||||||||||
}, | ||||||||||||||||
)?; | ||||||||||||||||
use std::fs; | ||||||||||||||||
use std::io; | ||||||||||||||||
use std::path::{Path, PathBuf}; | ||||||||||||||||
|
||||||||||||||||
const OUT_DIR: &str = "src"; | ||||||||||||||||
|
||||||||||||||||
fn features_patch(dir: impl AsRef<Path>) -> io::Result<()> { | ||||||||||||||||
let regex = "impl(.+)tonic::transport(.+)"; | ||||||||||||||||
let replacement = "#[cfg(feature = \"grpc-transport\")]\n \ | ||||||||||||||||
impl${1}tonic::transport${2}"; | ||||||||||||||||
|
||||||||||||||||
let paths = fs::read_dir(dir)?; | ||||||||||||||||
|
||||||||||||||||
for entry in paths { | ||||||||||||||||
let path = entry?.path(); | ||||||||||||||||
let mut contents = fs::read_to_string(&path)?; | ||||||||||||||||
|
||||||||||||||||
contents = Regex::new(regex) | ||||||||||||||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))? | ||||||||||||||||
.replace_all(&contents, replacement) | ||||||||||||||||
.to_string(); | ||||||||||||||||
|
||||||||||||||||
fs::write(&path, &contents)? | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
Ok(()) | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||||||||||||||||
if !std::env::var("V4_PROTO_REBUILD").is_ok() { | ||||||||||||||||
return Ok(()); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
let mut config = Config::new(); | ||||||||||||||||
config.out_dir(OUT_DIR); | ||||||||||||||||
config.include_file("_includes.rs"); | ||||||||||||||||
config.enable_type_names(); | ||||||||||||||||
let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").map_err(|e| { | ||||||||||||||||
tonic_buf_build::error::TonicBufBuildError { | ||||||||||||||||
message: format!("Failed to get CARGO_MANIFEST_DIR: {}", e), | ||||||||||||||||
cause: None, | ||||||||||||||||
} | ||||||||||||||||
})?); | ||||||||||||||||
Comment on lines
+41
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Simplify error handling when retrieving Since the Apply this diff to simplify error handling: -let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").map_err(|e| {
- tonic_buf_build::error::TonicBufBuildError {
- message: format!("Failed to get CARGO_MANIFEST_DIR: {}", e),
- cause: None,
- }
-})?);
+let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?); 📝 Committable suggestion
Suggested change
|
||||||||||||||||
path.pop(); | ||||||||||||||||
tonic_buf_build::compile_from_buf_workspace_with_config( | ||||||||||||||||
tonic_build::configure().build_server(false), | ||||||||||||||||
Some(config), | ||||||||||||||||
tonic_buf_build::TonicBufConfig { | ||||||||||||||||
buf_dir: Some(path), | ||||||||||||||||
}, | ||||||||||||||||
)?; | ||||||||||||||||
|
||||||||||||||||
features_patch(OUT_DIR)?; | ||||||||||||||||
|
||||||||||||||||
Ok(()) | ||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider using a Rust parser instead of regex for code modification
Using regular expressions to modify Rust code can be fragile and may not handle all edge cases correctly. Consider using a Rust parsing library like
syn
orquote
to parse and manipulate the code in a more robust way.