Skip to content

Commit

Permalink
chore: Make codegen independent from protoc (#1953)
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto authored Sep 23, 2024
1 parent f074f13 commit 6d93c1d
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 9 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: hecrj/setup-rust-action@v2
- name: Install protoc
uses: taiki-e/install-action@v2
with:
tool: [email protected]
- uses: Swatinem/rust-cache@v2
- run: cargo run --package codegen
- run: git diff --exit-code
Expand Down
3 changes: 1 addition & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ 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 `codegen`
crate. This requires `Protocol Buffers Compiler` of which version is same as the
one used in the GitHub Action (see [`codegen` job](./.github/workflows/CI.yml)).
crate.

```
cargo run --package codegen
Expand Down
1 change: 1 addition & 0 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ version = "0.1.0"

[dependencies]
tempfile = "3.8.0"
protox = "0.7"
tonic-build = {path = "../tonic-build", default-features = false, features = ["prost", "cleanup-markdown"]}
23 changes: 20 additions & 3 deletions codegen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
use std::path::{Path, PathBuf};
use std::{
fs::File,
io::{BufWriter, Write as _},
path::{Path, PathBuf},
};

use protox::prost::{bytes::BytesMut, Message as _};
use tonic_build::FileDescriptorSet;

fn main() {
// tonic-health
Expand Down Expand Up @@ -82,12 +89,15 @@ fn codegen(
let out_dir = root_dir.join(out_dir);
let file_descriptor_set_path = root_dir.join(file_descriptor_set_path);

let fds = protox::compile(&iface_files, &include_dirs).unwrap();

write_fds(&fds, &file_descriptor_set_path);

tonic_build::configure()
.build_client(build_client)
.build_server(build_server)
.out_dir(&tempdir)
.file_descriptor_set_path(file_descriptor_set_path)
.compile_protos(&iface_files, &include_dirs)
.compile_fds(fds)
.unwrap();

for path in std::fs::read_dir(tempdir.path()).unwrap() {
Expand All @@ -105,3 +115,10 @@ fn codegen(
std::fs::copy(&path, &to).unwrap();
}
}

fn write_fds(fds: &FileDescriptorSet, path: &Path) {
let mut writer = BufWriter::new(File::create(path).unwrap());
let mut buf = BytesMut::with_capacity(fds.encoded_len());
fds.encode(&mut buf).unwrap();
writer.write_all(&buf).unwrap();
}
Binary file modified tonic-health/src/generated/grpc_health_v1.bin
Binary file not shown.
Binary file modified tonic-reflection/src/generated/reflection_v1.bin
Binary file not shown.
Binary file modified tonic-reflection/src/generated/reflection_v1alpha1.bin
Binary file not shown.
Binary file modified tonic-types/src/generated/types.bin
Binary file not shown.

0 comments on commit 6d93c1d

Please sign in to comment.