Skip to content

Commit e188521

Browse files
authoredNov 8, 2022
feat(build): Add disable_comments option (#1127)
1 parent c9cadd4 commit e188521

File tree

10 files changed

+151
-6
lines changed

10 files changed

+151
-6
lines changed
 

‎Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ members = [
88
"tonic-web", # Non-published crates
99
"examples",
1010
"interop", # Tests
11+
"tests/disable_comments",
1112
"tests/included_service",
1213
"tests/same_name",
1314
"tests/service_named_service",

‎tests/disable_comments/Cargo.toml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
authors = ["bouzuya <m@bouzuya.net>"]
3+
edition = "2018"
4+
license = "MIT"
5+
name = "disable-comments"
6+
publish = false
7+
version = "0.1.0"
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[dependencies]
12+
prost = "0.11"
13+
tonic = { path = "../../tonic" }
14+
15+
[build-dependencies]
16+
prost-build = "0.11"
17+
tonic-build = { path = "../../tonic-build" }

‎tests/disable_comments/build.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {
2+
let mut config = prost_build::Config::default();
3+
config.disable_comments(&["test.Input1", "test.Output1"]);
4+
tonic_build::configure()
5+
.disable_comments("test.Service1")
6+
.disable_comments("test.Service1.Rpc1")
7+
.build_client(true)
8+
.build_server(true)
9+
.compile_with_config(config, &["proto/test.proto"], &["proto"])
10+
.unwrap();
11+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
syntax = "proto3";
2+
3+
package test;
4+
5+
// This comment will be removed.
6+
service Service1 {
7+
// This comment will be removed.
8+
rpc Rpc1(Input1) returns (Output1);
9+
// This comment will not be removed.
10+
rpc Rpc2(Input2) returns (Output2);
11+
}
12+
13+
// This comment will not be removed.
14+
service Service2 {
15+
// This comment will not be removed.
16+
rpc Rpc(Input1) returns (Output1);
17+
}
18+
19+
// This comment will be removed.
20+
message Input1 {}
21+
22+
// This comment will not be removed.
23+
message Input2 {}
24+
25+
// This comment will be removed.
26+
message Output1 {}
27+
28+
// This comment will not be removed.
29+
message Output2 {}

‎tests/disable_comments/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod pb {
2+
tonic::include_proto!("test");
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::{fs, path::PathBuf};
2+
3+
#[test]
4+
fn test() {
5+
let path = PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("test.rs");
6+
let s = fs::read_to_string(path).unwrap();
7+
assert!(!s.contains("This comment will be removed."));
8+
let mut count = 0_usize;
9+
let mut index = 0_usize;
10+
while let Some(found) = s[index..].find("This comment will not be removed.") {
11+
index += found + 1;
12+
count += 1;
13+
}
14+
assert_eq!(count, 2 + 3 + 3); // message: 2, client: 3, server: 3
15+
}

‎tonic-build/src/client.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::HashSet;
2+
13
use super::{Attributes, Method, Service};
24
use crate::{generate_doc_comments, naive_snake_case};
35
use proc_macro2::TokenStream;
@@ -14,13 +16,19 @@ pub fn generate<T: Service>(
1416
compile_well_known_types: bool,
1517
build_transport: bool,
1618
attributes: &Attributes,
19+
disable_comments: &HashSet<String>,
1720
) -> TokenStream {
1821
let service_ident = quote::format_ident!("{}Client", service.name());
1922
let client_mod = quote::format_ident!("{}_client", naive_snake_case(service.name()));
20-
let methods = generate_methods(service, emit_package, proto_path, compile_well_known_types);
23+
let methods = generate_methods(
24+
service,
25+
emit_package,
26+
proto_path,
27+
compile_well_known_types,
28+
disable_comments,
29+
);
2130

2231
let connect = generate_connect(&service_ident, build_transport);
23-
let service_doc = generate_doc_comments(service.comment());
2432

2533
let package = if emit_package { service.package() } else { "" };
2634
let path = format!(
@@ -30,6 +38,12 @@ pub fn generate<T: Service>(
3038
service.identifier()
3139
);
3240

41+
let service_doc = if disable_comments.contains(&path) {
42+
TokenStream::new()
43+
} else {
44+
generate_doc_comments(service.comment())
45+
};
46+
3347
let mod_attributes = attributes.for_mod(package);
3448
let struct_attributes = attributes.for_struct(&path);
3549

@@ -142,6 +156,7 @@ fn generate_methods<T: Service>(
142156
emit_package: bool,
143157
proto_path: &str,
144158
compile_well_known_types: bool,
159+
disable_comments: &HashSet<String>,
145160
) -> TokenStream {
146161
let mut stream = TokenStream::new();
147162
let package = if emit_package { service.package() } else { "" };
@@ -155,7 +170,15 @@ fn generate_methods<T: Service>(
155170
method.identifier()
156171
);
157172

158-
stream.extend(generate_doc_comments(method.comment()));
173+
if !disable_comments.contains(&format!(
174+
"{}{}{}.{}",
175+
package,
176+
if package.is_empty() { "" } else { "." },
177+
service.identifier(),
178+
method.identifier()
179+
)) {
180+
stream.extend(generate_doc_comments(method.comment()));
181+
}
159182

160183
let method = match (method.client_streaming(), method.server_streaming()) {
161184
(false, false) => generate_unary(method, proto_path, compile_well_known_types, path),

‎tonic-build/src/manual.rs

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use super::{client, server, Attributes};
3232
use proc_macro2::TokenStream;
3333
use quote::ToTokens;
3434
use std::{
35+
collections::HashSet,
3536
fs,
3637
path::{Path, PathBuf},
3738
};
@@ -357,6 +358,7 @@ impl ServiceGenerator {
357358
"", // proto_path, -- not used
358359
false, // compile_well_known_types -- not used
359360
&Attributes::default(),
361+
&HashSet::default(),
360362
);
361363
self.servers.extend(server);
362364
}
@@ -369,6 +371,7 @@ impl ServiceGenerator {
369371
false, // compile_well_known_types, -- not used
370372
self.builder.build_transport,
371373
&Attributes::default(),
374+
&HashSet::default(),
372375
);
373376
self.clients.extend(client);
374377
}

‎tonic-build/src/prost.rs

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use proc_macro2::TokenStream;
33
use prost_build::{Config, Method, Service};
44
use quote::ToTokens;
55
use std::{
6+
collections::HashSet,
67
ffi::OsString,
78
io,
89
path::{Path, PathBuf},
@@ -29,6 +30,7 @@ pub fn configure() -> Builder {
2930
protoc_args: Vec::new(),
3031
include_file: None,
3132
emit_rerun_if_changed: std::env::var_os("CARGO").is_some(),
33+
disable_comments: HashSet::default(),
3234
}
3335
}
3436

@@ -163,6 +165,7 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
163165
&self.builder.proto_path,
164166
self.builder.compile_well_known_types,
165167
&self.builder.server_attributes,
168+
&self.builder.disable_comments,
166169
);
167170
self.servers.extend(server);
168171
}
@@ -175,6 +178,7 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
175178
self.builder.compile_well_known_types,
176179
self.builder.build_transport,
177180
&self.builder.client_attributes,
181+
&self.builder.disable_comments,
178182
);
179183
self.clients.extend(client);
180184
}
@@ -229,6 +233,7 @@ pub struct Builder {
229233
pub(crate) protoc_args: Vec<OsString>,
230234
pub(crate) include_file: Option<PathBuf>,
231235
pub(crate) emit_rerun_if_changed: bool,
236+
pub(crate) disable_comments: HashSet<String>,
232237

233238
out_dir: Option<PathBuf>,
234239
}
@@ -354,6 +359,12 @@ impl Builder {
354359
self
355360
}
356361

362+
/// Disable service and rpc comments emission.
363+
pub fn disable_comments(mut self, path: impl AsRef<str>) -> Self {
364+
self.disable_comments.insert(path.as_ref().to_string());
365+
self
366+
}
367+
357368
/// Emits GRPC endpoints with no attached package. Effectively ignores protofile package declaration from grpc context.
358369
///
359370
/// This effectively sets prost's exported package to an empty string.

‎tonic-build/src/server.rs

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::HashSet;
2+
13
use super::{Attributes, Method, Service};
24
use crate::{generate_doc_comment, generate_doc_comments, naive_snake_case};
35
use proc_macro2::{Span, TokenStream};
@@ -14,6 +16,7 @@ pub fn generate<T: Service>(
1416
proto_path: &str,
1517
compile_well_known_types: bool,
1618
attributes: &Attributes,
19+
disable_comments: &HashSet<String>,
1720
) -> TokenStream {
1821
let methods = generate_methods(service, proto_path, compile_well_known_types);
1922

@@ -22,11 +25,12 @@ pub fn generate<T: Service>(
2225
let server_mod = quote::format_ident!("{}_server", naive_snake_case(service.name()));
2326
let generated_trait = generate_trait(
2427
service,
28+
emit_package,
2529
proto_path,
2630
compile_well_known_types,
2731
server_trait.clone(),
32+
disable_comments,
2833
);
29-
let service_doc = generate_doc_comments(service.comment());
3034
let package = if emit_package { service.package() } else { "" };
3135
// Transport based implementations
3236
let path = format!(
@@ -35,6 +39,13 @@ pub fn generate<T: Service>(
3539
if package.is_empty() { "" } else { "." },
3640
service.identifier()
3741
);
42+
43+
let service_doc = if disable_comments.contains(&path) {
44+
TokenStream::new()
45+
} else {
46+
generate_doc_comments(service.comment())
47+
};
48+
3849
let named = generate_named(&server_service, &server_trait, &path);
3950
let mod_attributes = attributes.for_mod(package);
4051
let struct_attributes = attributes.for_struct(&path);
@@ -167,11 +178,19 @@ pub fn generate<T: Service>(
167178

168179
fn generate_trait<T: Service>(
169180
service: &T,
181+
emit_package: bool,
170182
proto_path: &str,
171183
compile_well_known_types: bool,
172184
server_trait: Ident,
185+
disable_comments: &HashSet<String>,
173186
) -> TokenStream {
174-
let methods = generate_trait_methods(service, proto_path, compile_well_known_types);
187+
let methods = generate_trait_methods(
188+
service,
189+
emit_package,
190+
proto_path,
191+
compile_well_known_types,
192+
disable_comments,
193+
);
175194
let trait_doc = generate_doc_comment(&format!(
176195
" Generated trait containing gRPC methods that should be implemented for use with {}Server.",
177196
service.name()
@@ -188,18 +207,31 @@ fn generate_trait<T: Service>(
188207

189208
fn generate_trait_methods<T: Service>(
190209
service: &T,
210+
emit_package: bool,
191211
proto_path: &str,
192212
compile_well_known_types: bool,
213+
disable_comments: &HashSet<String>,
193214
) -> TokenStream {
194215
let mut stream = TokenStream::new();
195216

217+
let package = if emit_package { service.package() } else { "" };
196218
for method in service.methods() {
197219
let name = quote::format_ident!("{}", method.name());
198220

199221
let (req_message, res_message) =
200222
method.request_response_name(proto_path, compile_well_known_types);
201223

202-
let method_doc = generate_doc_comments(method.comment());
224+
let method_doc = if disable_comments.contains(&format!(
225+
"{}{}{}.{}",
226+
package,
227+
if package.is_empty() { "" } else { "." },
228+
service.identifier(),
229+
method.identifier()
230+
)) {
231+
TokenStream::new()
232+
} else {
233+
generate_doc_comments(method.comment())
234+
};
203235

204236
let method = match (method.client_streaming(), method.server_streaming()) {
205237
(false, false) => {

0 commit comments

Comments
 (0)
Please sign in to comment.