Skip to content

Commit 7916212

Browse files
committed
feat: Add proto header to generated code
1 parent 881dc87 commit 7916212

File tree

5 files changed

+169
-3
lines changed

5 files changed

+169
-3
lines changed

codegen/src/main.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,22 @@ fn codegen(
113113
fn write_fds(fds: &FileDescriptorSet, path: &Path) {
114114
const GENERATED_COMMENT: &str = "// This file is @generated by codegen.";
115115

116+
let mut file_header = String::new();
117+
116118
let mut fds = fds.clone();
119+
120+
for fd in fds.file.iter() {
121+
let Some(source_code_info) = &fd.source_code_info else {
122+
continue;
123+
};
124+
125+
for location in &source_code_info.location {
126+
for comment in &location.leading_detached_comments {
127+
file_header += comment;
128+
}
129+
}
130+
}
131+
117132
for fd in fds.file.iter_mut() {
118133
fd.source_code_info = None;
119134
}
@@ -126,8 +141,23 @@ fn write_fds(fds: &FileDescriptorSet, path: &Path) {
126141
let ast = syn::parse2(tokens).unwrap();
127142
let formatted = prettyplease::unparse(&ast);
128143

129-
let content = format!("{GENERATED_COMMENT}\n{formatted}");
130-
131144
let mut writer = BufWriter::new(File::create(path).unwrap());
132-
writer.write_all(content.as_bytes()).unwrap();
145+
146+
writer.write_all(GENERATED_COMMENT.as_bytes()).unwrap();
147+
writer.write_all(b"\n").unwrap();
148+
149+
if !file_header.is_empty() {
150+
let file_header = comment_out(&file_header);
151+
writer.write_all(file_header.as_bytes()).unwrap();
152+
writer.write_all(b"\n").unwrap();
153+
}
154+
155+
writer.write_all(formatted.as_bytes()).unwrap()
156+
}
157+
158+
fn comment_out(s: &str) -> String {
159+
s.split('\n')
160+
.map(|line| format!("// {line}"))
161+
.collect::<Vec<String>>()
162+
.join("\n")
133163
}

tonic-health/src/generated/grpc_health_v1_fds.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
// This file is @generated by codegen.
2+
// Copyright 2015 The gRPC Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// The canonical version of this proto can be found at
16+
// https://github.com/grpc/grpc-proto/blob/master/grpc/health/v1/health.proto
17+
//
218
/// Byte encoded FILE_DESCRIPTOR_SET.
319
pub const FILE_DESCRIPTOR_SET: &[u8] = &[
420
10u8, 158u8, 4u8, 10u8, 12u8, 104u8, 101u8, 97u8, 108u8, 116u8, 104u8, 46u8, 112u8,

tonic-reflection/src/generated/reflection_v1_fds.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
// This file is @generated by codegen.
2+
// Copyright 2016 The gRPC Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// Service exported by server reflection. A more complete description of how
16+
// server reflection works can be found at
17+
// https://github.com/grpc/grpc/blob/master/doc/server-reflection.md
18+
//
19+
// The canonical version of this proto can be found at
20+
// https://github.com/grpc/grpc-proto/blob/master/grpc/reflection/v1/reflection.proto
21+
//
222
/// Byte encoded FILE_DESCRIPTOR_SET.
323
pub const FILE_DESCRIPTOR_SET: &[u8] = &[
424
10u8, 192u8, 13u8, 10u8, 19u8, 114u8, 101u8, 102u8, 108u8, 101u8, 99u8, 116u8, 105u8,

tonic-reflection/src/generated/reflection_v1alpha1_fds.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
// This file is @generated by codegen.
2+
// Copyright 2016 gRPC authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// Service exported by server reflection
16+
//
217
/// Byte encoded FILE_DESCRIPTOR_SET.
318
pub const FILE_DESCRIPTOR_SET: &[u8] = &[
419
10u8, 143u8, 13u8, 10u8, 24u8, 114u8, 101u8, 102u8, 108u8, 101u8, 99u8, 116u8, 105u8,

tonic-types/src/generated/types_fds.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,89 @@
11
// This file is @generated by codegen.
2+
// Protocol Buffers - Google's data interchange format
3+
// Copyright 2008 Google Inc. All rights reserved.
4+
// https://developers.google.com/protocol-buffers/
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are
8+
// met:
9+
//
10+
// * Redistributions of source code must retain the above copyright
11+
// notice, this list of conditions and the following disclaimer.
12+
// * Redistributions in binary form must reproduce the above
13+
// copyright notice, this list of conditions and the following disclaimer
14+
// in the documentation and/or other materials provided with the
15+
// distribution.
16+
// * Neither the name of Google Inc. nor the names of its
17+
// contributors may be used to endorse or promote products derived from
18+
// this software without specific prior written permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
// Copyright 2020 Google LLC
32+
//
33+
// Licensed under the Apache License, Version 2.0 (the "License");
34+
// you may not use this file except in compliance with the License.
35+
// You may obtain a copy of the License at
36+
//
37+
// http://www.apache.org/licenses/LICENSE-2.0
38+
//
39+
// Unless required by applicable law or agreed to in writing, software
40+
// distributed under the License is distributed on an "AS IS" BASIS,
41+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42+
// See the License for the specific language governing permissions and
43+
// limitations under the License.
44+
// Protocol Buffers - Google's data interchange format
45+
// Copyright 2008 Google Inc. All rights reserved.
46+
// https://developers.google.com/protocol-buffers/
47+
//
48+
// Redistribution and use in source and binary forms, with or without
49+
// modification, are permitted provided that the following conditions are
50+
// met:
51+
//
52+
// * Redistributions of source code must retain the above copyright
53+
// notice, this list of conditions and the following disclaimer.
54+
// * Redistributions in binary form must reproduce the above
55+
// copyright notice, this list of conditions and the following disclaimer
56+
// in the documentation and/or other materials provided with the
57+
// distribution.
58+
// * Neither the name of Google Inc. nor the names of its
59+
// contributors may be used to endorse or promote products derived from
60+
// this software without specific prior written permission.
61+
//
62+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
63+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
64+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
65+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
66+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
67+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
68+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
69+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
70+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
71+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
72+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73+
// Copyright 2020 Google LLC
74+
//
75+
// Licensed under the Apache License, Version 2.0 (the "License");
76+
// you may not use this file except in compliance with the License.
77+
// You may obtain a copy of the License at
78+
//
79+
// http://www.apache.org/licenses/LICENSE-2.0
80+
//
81+
// Unless required by applicable law or agreed to in writing, software
82+
// distributed under the License is distributed on an "AS IS" BASIS,
83+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
84+
// See the License for the specific language governing permissions and
85+
// limitations under the License.
86+
//
287
/// Byte encoded FILE_DESCRIPTOR_SET.
388
pub const FILE_DESCRIPTOR_SET: &[u8] = &[
489
10u8, 228u8, 1u8, 10u8, 25u8, 103u8, 111u8, 111u8, 103u8, 108u8, 101u8, 47u8, 112u8,

0 commit comments

Comments
 (0)