feat(build): Add disable_comments option (#1127)

This commit is contained in:
bouzuya
2022-11-08 11:08:19 -05:00
committed by GitHub
parent c9cadd4b23
commit e188521149
10 changed files with 151 additions and 6 deletions
+17
View File
@@ -0,0 +1,17 @@
[package]
authors = ["bouzuya <[email protected]>"]
edition = "2018"
license = "MIT"
name = "disable-comments"
publish = false
version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
prost = "0.11"
tonic = { path = "../../tonic" }
[build-dependencies]
prost-build = "0.11"
tonic-build = { path = "../../tonic-build" }
+11
View File
@@ -0,0 +1,11 @@
fn main() {
let mut config = prost_build::Config::default();
config.disable_comments(&["test.Input1", "test.Output1"]);
tonic_build::configure()
.disable_comments("test.Service1")
.disable_comments("test.Service1.Rpc1")
.build_client(true)
.build_server(true)
.compile_with_config(config, &["proto/test.proto"], &["proto"])
.unwrap();
}
+29
View File
@@ -0,0 +1,29 @@
syntax = "proto3";
package test;
// This comment will be removed.
service Service1 {
// This comment will be removed.
rpc Rpc1(Input1) returns (Output1);
// This comment will not be removed.
rpc Rpc2(Input2) returns (Output2);
}
// This comment will not be removed.
service Service2 {
// This comment will not be removed.
rpc Rpc(Input1) returns (Output1);
}
// This comment will be removed.
message Input1 {}
// This comment will not be removed.
message Input2 {}
// This comment will be removed.
message Output1 {}
// This comment will not be removed.
message Output2 {}
+3
View File
@@ -0,0 +1,3 @@
pub mod pb {
tonic::include_proto!("test");
}
@@ -0,0 +1,15 @@
use std::{fs, path::PathBuf};
#[test]
fn test() {
let path = PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("test.rs");
let s = fs::read_to_string(path).unwrap();
assert!(!s.contains("This comment will be removed."));
let mut count = 0_usize;
let mut index = 0_usize;
while let Some(found) = s[index..].find("This comment will not be removed.") {
index += found + 1;
count += 1;
}
assert_eq!(count, 2 + 3 + 3); // message: 2, client: 3, server: 3
}