Skip to content

Commit 44aa46d

Browse files
authored
chore: Move codegen to separate crate (#1467)
1 parent 5fd635a commit 44aa46d

File tree

11 files changed

+102
-104
lines changed

11 files changed

+102
-104
lines changed

.github/workflows/CI.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ jobs:
2828
- uses: actions/checkout@v3
2929
- uses: EmbarkStudios/cargo-deny-action@v1
3030

31+
codegen:
32+
runs-on: ubuntu-latest
33+
steps:
34+
- uses: actions/checkout@v3
35+
- uses: hecrj/setup-rust-action@v1
36+
- name: Install protoc
37+
uses: taiki-e/install-action@v2
38+
with:
39+
tool: protoc@${{ env.PROTOC_VERSION }}
40+
- uses: Swatinem/rust-cache@v2
41+
- run: cargo run --package codegen
42+
- run: git diff --exit-code
43+
3144
udeps:
3245
name: Check unused dependencies
3346
runs-on: ${{ matrix.os }}

CONTRIBUTING.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,11 @@ example would explicitly use `Timeout::new`. For example:
210210

211211
When making changes to `tonic-build` that affects the generated code you will
212212
need to ensure that each of the sub crates gets updated as well. Each of the sub
213-
crates like, for example `tonic-health`, generate their gRPC code via a
214-
`bootstrap.rs` test.
215-
216-
The bootstrap tests work by generating the code and then checking git if there
217-
is any uncommitted generated code (there is a difference between the proto files
218-
and the committed generated code). At this point the test will fail telling you
219-
to commit the new code. When the new code is committed, running the test suite
220-
again will cause it to pass as the generated code doesn't create a diff for git
221-
and thus its up to date.
213+
crates like, for example `tonic-health`, generate their gRPC code via `codegen`
214+
crate.
222215

223216
```
224-
cargo test --all
217+
cargo run --package codegen
225218
```
226219

227220
### Commits

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ members = [
77
"tonic-reflection",
88
"tonic-web", # Non-published crates
99
"examples",
10+
"codegen",
1011
"interop", # Tests
1112
"tests/disable_comments",
1213
"tests/included_service",

codegen/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "codegen"
3+
authors = ["Lucio Franco <[email protected]>"]
4+
license = "MIT"
5+
edition = "2021"
6+
publish = false
7+
version = "0.1.0"
8+
9+
[dependencies]
10+
tonic-build = {path = "../tonic-build", default-features = false, features = ["prost", "cleanup-markdown"]}

codegen/src/main.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use std::path::PathBuf;
2+
3+
fn main() {
4+
// tonic-health
5+
codegen(
6+
&PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
7+
.parent()
8+
.unwrap()
9+
.join("tonic-health"),
10+
&["proto/health.proto"],
11+
&["proto"],
12+
&PathBuf::from("src/generated"),
13+
&PathBuf::from("src/generated/grpc_health_v1.bin"),
14+
true,
15+
true,
16+
);
17+
18+
// tonic-reflection
19+
codegen(
20+
&PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
21+
.parent()
22+
.unwrap()
23+
.join("tonic-reflection"),
24+
&["proto/reflection.proto"],
25+
&["proto"],
26+
&PathBuf::from("src/generated"),
27+
&PathBuf::from("src/generated/reflection_v1alpha1.bin"),
28+
true,
29+
true,
30+
);
31+
32+
// tonic-types
33+
codegen(
34+
&PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
35+
.parent()
36+
.unwrap()
37+
.join("tonic-types"),
38+
&["proto/status.proto", "proto/error_details.proto"],
39+
&["proto"],
40+
&PathBuf::from("src/generated"),
41+
&PathBuf::from("src/generated/types.bin"),
42+
false,
43+
false,
44+
);
45+
}
46+
47+
fn codegen(
48+
root_dir: &PathBuf,
49+
iface_files: &[&str],
50+
include_dirs: &[&str],
51+
out_dir: &PathBuf,
52+
file_descriptor_set_path: &PathBuf,
53+
build_client: bool,
54+
build_server: bool,
55+
) {
56+
let iface_files: Vec<PathBuf> = iface_files
57+
.into_iter()
58+
.map(|&path| root_dir.join(path))
59+
.collect();
60+
61+
let include_dirs: Vec<PathBuf> = include_dirs
62+
.into_iter()
63+
.map(|&path| root_dir.join(path))
64+
.collect();
65+
let out_dir = root_dir.join(out_dir);
66+
let file_descriptor_set_path = root_dir.join(file_descriptor_set_path);
67+
68+
tonic_build::configure()
69+
.build_client(build_client)
70+
.build_server(build_server)
71+
.out_dir(&out_dir)
72+
.file_descriptor_set_path(file_descriptor_set_path)
73+
.compile(&iface_files, &include_dirs)
74+
.unwrap();
75+
}

tonic-health/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ tonic = { version = "0.9", path = "../tonic", default-features = false, features
2828
[dev-dependencies]
2929
tokio = {version = "1.0", features = ["rt-multi-thread", "macros"]}
3030
tokio-stream = "0.1"
31-
tonic-build = { version = "0.9", path = "../tonic-build", default-features = false, features = ["prost"] }
3231
prost-types = "0.11"

tonic-health/tests/bootstrap.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.

tonic-reflection/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,3 @@ tonic = { version = "0.9", path = "../tonic", default-features = false, features
2626

2727
[dev-dependencies]
2828
tonic = { version = "0.9", path = "../tonic", default-features = false, features = ["transport"] }
29-
tonic-build = { version = "0.9", path = "../tonic-build", default-features = false, features = ["prost", "cleanup-markdown"] }

tonic-reflection/tests/bootstrap.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.

tonic-types/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,3 @@ version = "0.9.2"
2121
prost = "0.11"
2222
prost-types = "0.11"
2323
tonic = {version = "0.9", path = "../tonic", default-features = false}
24-
25-
[dev-dependencies]
26-
tonic-build = {version = "0.9", path = "../tonic-build", default-features = false, features = ["prost", "cleanup-markdown"]}

0 commit comments

Comments
 (0)