Skip to content

Commit

Permalink
chore: Move codegen to separate crate (#1467)
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto authored Aug 25, 2023
1 parent 5fd635a commit 44aa46d
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 104 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ jobs:
- uses: actions/checkout@v3
- uses: EmbarkStudios/cargo-deny-action@v1

codegen:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: hecrj/setup-rust-action@v1
- name: Install protoc
uses: taiki-e/install-action@v2
with:
tool: protoc@${{ env.PROTOC_VERSION }}
- uses: Swatinem/rust-cache@v2
- run: cargo run --package codegen
- run: git diff --exit-code

udeps:
name: Check unused dependencies
runs-on: ${{ matrix.os }}
Expand Down
13 changes: 3 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,11 @@ example would explicitly use `Timeout::new`. For example:

When making changes to `tonic-build` that affects the generated code you will
need to ensure that each of the sub crates gets updated as well. Each of the sub
crates like, for example `tonic-health`, generate their gRPC code via a
`bootstrap.rs` test.

The bootstrap tests work by generating the code and then checking git if there
is any uncommitted generated code (there is a difference between the proto files
and the committed generated code). At this point the test will fail telling you
to commit the new code. When the new code is committed, running the test suite
again will cause it to pass as the generated code doesn't create a diff for git
and thus its up to date.
crates like, for example `tonic-health`, generate their gRPC code via `codegen`
crate.

```
cargo test --all
cargo run --package codegen
```

### Commits
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"tonic-reflection",
"tonic-web", # Non-published crates
"examples",
"codegen",
"interop", # Tests
"tests/disable_comments",
"tests/included_service",
Expand Down
10 changes: 10 additions & 0 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "codegen"
authors = ["Lucio Franco <[email protected]>"]
license = "MIT"
edition = "2021"
publish = false
version = "0.1.0"

[dependencies]
tonic-build = {path = "../tonic-build", default-features = false, features = ["prost", "cleanup-markdown"]}
75 changes: 75 additions & 0 deletions codegen/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::path::PathBuf;

fn main() {
// tonic-health
codegen(
&PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("tonic-health"),
&["proto/health.proto"],
&["proto"],
&PathBuf::from("src/generated"),
&PathBuf::from("src/generated/grpc_health_v1.bin"),
true,
true,
);

// tonic-reflection
codegen(
&PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("tonic-reflection"),
&["proto/reflection.proto"],
&["proto"],
&PathBuf::from("src/generated"),
&PathBuf::from("src/generated/reflection_v1alpha1.bin"),
true,
true,
);

// tonic-types
codegen(
&PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("tonic-types"),
&["proto/status.proto", "proto/error_details.proto"],
&["proto"],
&PathBuf::from("src/generated"),
&PathBuf::from("src/generated/types.bin"),
false,
false,
);
}

fn codegen(
root_dir: &PathBuf,
iface_files: &[&str],
include_dirs: &[&str],
out_dir: &PathBuf,
file_descriptor_set_path: &PathBuf,
build_client: bool,
build_server: bool,
) {
let iface_files: Vec<PathBuf> = iface_files
.into_iter()
.map(|&path| root_dir.join(path))
.collect();

let include_dirs: Vec<PathBuf> = include_dirs
.into_iter()
.map(|&path| root_dir.join(path))
.collect();
let out_dir = root_dir.join(out_dir);
let file_descriptor_set_path = root_dir.join(file_descriptor_set_path);

tonic_build::configure()
.build_client(build_client)
.build_server(build_server)
.out_dir(&out_dir)
.file_descriptor_set_path(file_descriptor_set_path)
.compile(&iface_files, &include_dirs)
.unwrap();
}
1 change: 0 additions & 1 deletion tonic-health/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ tonic = { version = "0.9", path = "../tonic", default-features = false, features
[dev-dependencies]
tokio = {version = "1.0", features = ["rt-multi-thread", "macros"]}
tokio-stream = "0.1"
tonic-build = { version = "0.9", path = "../tonic-build", default-features = false, features = ["prost"] }
prost-types = "0.11"
30 changes: 0 additions & 30 deletions tonic-health/tests/bootstrap.rs

This file was deleted.

1 change: 0 additions & 1 deletion tonic-reflection/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ tonic = { version = "0.9", path = "../tonic", default-features = false, features

[dev-dependencies]
tonic = { version = "0.9", path = "../tonic", default-features = false, features = ["transport"] }
tonic-build = { version = "0.9", path = "../tonic-build", default-features = false, features = ["prost", "cleanup-markdown"] }
30 changes: 0 additions & 30 deletions tonic-reflection/tests/bootstrap.rs

This file was deleted.

3 changes: 0 additions & 3 deletions tonic-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,3 @@ version = "0.9.2"
prost = "0.11"
prost-types = "0.11"
tonic = {version = "0.9", path = "../tonic", default-features = false}

[dev-dependencies]
tonic-build = {version = "0.9", path = "../tonic-build", default-features = false, features = ["prost", "cleanup-markdown"]}
29 changes: 0 additions & 29 deletions tonic-types/tests/bootstrap.rs

This file was deleted.

0 comments on commit 44aa46d

Please sign in to comment.