From 44aa46db64a14436b4cfefa67a7fa12024a1e1ac Mon Sep 17 00:00:00 2001 From: tottoto Date: Sat, 26 Aug 2023 04:01:20 +0900 Subject: [PATCH] chore: Move codegen to separate crate (#1467) --- .github/workflows/CI.yml | 13 +++++ CONTRIBUTING.md | 13 ++--- Cargo.toml | 1 + codegen/Cargo.toml | 10 ++++ codegen/src/main.rs | 75 +++++++++++++++++++++++++++++ tonic-health/Cargo.toml | 1 - tonic-health/tests/bootstrap.rs | 30 ------------ tonic-reflection/Cargo.toml | 1 - tonic-reflection/tests/bootstrap.rs | 30 ------------ tonic-types/Cargo.toml | 3 -- tonic-types/tests/bootstrap.rs | 29 ----------- 11 files changed, 102 insertions(+), 104 deletions(-) create mode 100644 codegen/Cargo.toml create mode 100644 codegen/src/main.rs delete mode 100644 tonic-health/tests/bootstrap.rs delete mode 100644 tonic-reflection/tests/bootstrap.rs delete mode 100644 tonic-types/tests/bootstrap.rs diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 36a8721b7..a3ab20a3f 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -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 }} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index aef749ec0..a1817f23f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 6ebf17872..670a0d9fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ members = [ "tonic-reflection", "tonic-web", # Non-published crates "examples", + "codegen", "interop", # Tests "tests/disable_comments", "tests/included_service", diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml new file mode 100644 index 000000000..a5032bc35 --- /dev/null +++ b/codegen/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "codegen" +authors = ["Lucio Franco "] +license = "MIT" +edition = "2021" +publish = false +version = "0.1.0" + +[dependencies] +tonic-build = {path = "../tonic-build", default-features = false, features = ["prost", "cleanup-markdown"]} diff --git a/codegen/src/main.rs b/codegen/src/main.rs new file mode 100644 index 000000000..557ec52cb --- /dev/null +++ b/codegen/src/main.rs @@ -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 = iface_files + .into_iter() + .map(|&path| root_dir.join(path)) + .collect(); + + let include_dirs: Vec = 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(); +} diff --git a/tonic-health/Cargo.toml b/tonic-health/Cargo.toml index f36135772..c6218aa08 100644 --- a/tonic-health/Cargo.toml +++ b/tonic-health/Cargo.toml @@ -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" diff --git a/tonic-health/tests/bootstrap.rs b/tonic-health/tests/bootstrap.rs deleted file mode 100644 index 8896de85b..000000000 --- a/tonic-health/tests/bootstrap.rs +++ /dev/null @@ -1,30 +0,0 @@ -use std::{path::PathBuf, process::Command}; - -#[test] -fn bootstrap() { - let iface_files = &["proto/health.proto"]; - let dirs = &["proto"]; - - let out_dir = PathBuf::from(std::env!("CARGO_MANIFEST_DIR")) - .join("src") - .join("generated"); - - tonic_build::configure() - .build_client(true) - .build_server(true) - .build_transport(false) - .out_dir(&out_dir) - .file_descriptor_set_path(out_dir.join("grpc_health_v1.bin")) - .compile(iface_files, dirs) - .unwrap(); - - let status = Command::new("git") - .arg("diff") - .arg("--exit-code") - .arg("--") - .arg(&out_dir) - .status() - .unwrap(); - - assert!(status.success(), "You should commit the protobuf files"); -} diff --git a/tonic-reflection/Cargo.toml b/tonic-reflection/Cargo.toml index efaa7839b..efe35ac9b 100644 --- a/tonic-reflection/Cargo.toml +++ b/tonic-reflection/Cargo.toml @@ -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"] } diff --git a/tonic-reflection/tests/bootstrap.rs b/tonic-reflection/tests/bootstrap.rs deleted file mode 100644 index 0ec200dbd..000000000 --- a/tonic-reflection/tests/bootstrap.rs +++ /dev/null @@ -1,30 +0,0 @@ -use std::{path::PathBuf, process::Command}; - -#[test] -fn bootstrap() { - let iface_files = &["proto/reflection.proto"]; - let dirs = &["proto"]; - - let out_dir = PathBuf::from(std::env!("CARGO_MANIFEST_DIR")) - .join("src") - .join("generated"); - - tonic_build::configure() - .build_client(true) - .build_server(true) - .build_transport(false) - .file_descriptor_set_path(out_dir.join("reflection_v1alpha1.bin")) - .out_dir(&out_dir) - .compile(iface_files, dirs) - .unwrap(); - - let status = Command::new("git") - .arg("diff") - .arg("--exit-code") - .arg("--") - .arg(&out_dir) - .status() - .unwrap(); - - assert!(status.success(), "You should commit the protobuf files"); -} diff --git a/tonic-types/Cargo.toml b/tonic-types/Cargo.toml index bdfa6a037..6cf46ee1e 100644 --- a/tonic-types/Cargo.toml +++ b/tonic-types/Cargo.toml @@ -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"]} diff --git a/tonic-types/tests/bootstrap.rs b/tonic-types/tests/bootstrap.rs deleted file mode 100644 index 2b15f178c..000000000 --- a/tonic-types/tests/bootstrap.rs +++ /dev/null @@ -1,29 +0,0 @@ -use std::{path::PathBuf, process::Command}; - -#[test] -fn bootstrap() { - let iface_files = &["proto/status.proto", "proto/error_details.proto"]; - let dirs = &["proto"]; - - let out_dir = PathBuf::from(std::env!("CARGO_MANIFEST_DIR")) - .join("src") - .join("generated"); - - tonic_build::configure() - .build_client(false) - .build_server(false) - .out_dir(&out_dir) - .file_descriptor_set_path(out_dir.join("types.bin")) - .compile(iface_files, dirs) - .unwrap(); - - let status = Command::new("git") - .arg("diff") - .arg("--exit-code") - .arg("--") - .arg(&out_dir) - .status() - .unwrap(); - - assert!(status.success(), "You should commit the protobuf files"); -}