Skip to content

Commit

Permalink
initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
moray95 committed Aug 16, 2023
1 parent 364c31b commit 89da80a
Show file tree
Hide file tree
Showing 22 changed files with 587 additions and 1 deletion.
26 changes: 26 additions & 0 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Check

on:
push:
branches:
- "master"
pull_request:
branches:
- "master"

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install clippy
run: rustup component add clippy
- name: Run clippy
run: cargo clippy --verbose

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
40 changes: 40 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Release
on:
push:
tags:
- '*'

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install clippy
run: rustup component add clippy
- name: Run clippy
run: cargo clippy --verbose

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build

publish:
needs: [lint, build, test]
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable

- name: Publish to crates.io
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: cargo publish --token ${CARGO_REGISTRY_TOKEN}
72 changes: 72 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Created by https://www.toptal.com/developers/gitignore/api/rust,visualstudiocode,macos
# Edit at https://www.toptal.com/developers/gitignore?templates=rust,visualstudiocode,macos

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### macOS Patch ###
# iCloud generated files
*.icloud

### Rust ###
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix

### VisualStudioCode Patch ###
# Ignore all local history of files
.history
.ionide

# End of https://www.toptal.com/developers/gitignore/api/rust,visualstudiocode,macos
17 changes: 17 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "tonic-buf-build"
version = "0.1.0"
edition = "2021"
description = "A build helper that integrates buf.build to tonic-build."
license = "MIT"
repository = "https://github.com/Valensas/tonic-buf-build"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tonic-build = "0.9.2"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9.22"
uuid = { version = "1.2.2", features = ["v4", "fast-rng"] }
prost-build = "*"
scopeguard = "1.2.0"
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Valensas

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
# tonic-buf-build
A build helper that integrates buf.build to tonic-build

A build helper that allows you to integrate [buf.build](https://buf.build) with [tonic-build](https://github.com/hyperium/tonic/tree/master/tonic-build).
Using buf.build and tonic, you can easily manage third party dependencies for proto files and generate code for your proto files in Rust.
Works with both [buf.yaml](https://buf.build/docs/configuration/v1/buf-yaml) and [buf.work.yaml](https://buf.build/docs/configuration/v1/buf-work-yaml).

## Usage

Add the following to your Cargo.toml:

```toml
tonic_buf_build = {path = "../../"}
tonic-build = "*"
```

Then, in your build.rs:

```rust
fn main() -> Result<(), tonic_buf_build::error::TonicBufBuildError> {
tonic_buf_build::compile_from_buf(tonic_build::configure(), None)?;
Ok(())
}
```

To use buf workspaces, simply call `tonic_buf_build::compile_from_buf_workspace` instead.

For complete and working examples, take a look at the examples folder.
15 changes: 15 additions & 0 deletions examples/buf-work-yaml/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "buf-work-yaml"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tonic = {version = "0.9.2", features = ["tls"]}
tokio = {version = "1.28.2", features = ["full"]}
prost = "0.11.9"

[build-dependencies]
tonic_buf_build = {path = "../../"}
tonic-build = "*"
8 changes: 8 additions & 0 deletions examples/buf-work-yaml/buf-module/buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Generated by buf. DO NOT EDIT.
version: v1
deps:
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 711e289f6a384c4caeebaff7c6931ade
digest: shake256:e08fb55dad7469f69df00304eed31427d2d1576e9aab31e6bf86642688e04caaf0372f15fe6974cf79432779a635b3ea401ca69c943976dc42749524e4c25d94
9 changes: 9 additions & 0 deletions examples/buf-work-yaml/buf-module/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: v1
deps:
- buf.build/googleapis/googleapis
lint:
except:
- PACKAGE_VERSION_SUFFIX
- PACKAGE_DIRECTORY_MATCH
rpc_allow_google_protobuf_empty_requests: true
rpc_allow_google_protobuf_empty_responses: true
15 changes: 15 additions & 0 deletions examples/buf-work-yaml/buf-module/sample.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";
package tonic_buf_build_sample;

import "google/api/annotations.proto";
import "google/protobuf/empty.proto";

service HelloService {
rpc SayHello(google.protobuf.Empty) returns (SayHelloResponse) {
option (google.api.http) = {get: "/hello"};
}
}

message SayHelloResponse {
string value = 1;
}
3 changes: 3 additions & 0 deletions examples/buf-work-yaml/buf.work.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: v1
directories:
- buf-module
4 changes: 4 additions & 0 deletions examples/buf-work-yaml/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() -> Result<(), tonic_buf_build::error::TonicBufBuildError> {
tonic_buf_build::compile_from_buf_workspace(tonic_build::configure(), None)?;
Ok(())
}
31 changes: 31 additions & 0 deletions examples/buf-work-yaml/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::result::Result;

use proto::{hello_service_server::HelloServiceServer, SayHelloResponse};
use tonic::{transport::Server, Request, Response, Status};

pub mod proto {
tonic::include_proto!("tonic_buf_build_sample");
}

#[derive(Clone)]
struct HelloServer {}

#[tonic::async_trait]
impl proto::hello_service_server::HelloService for HelloServer {
async fn say_hello(&self, _: Request<()>) -> Result<Response<SayHelloResponse>, Status> {
Ok(Response::new(SayHelloResponse {
value: "Hello world!".into(),
}))
}
}

#[tokio::main]
async fn main() {
let hello_server = HelloServer {};

Server::builder()
.add_service(HelloServiceServer::new(hello_server))
.serve("127.0.0.1:10000".parse().unwrap())
.await
.unwrap();
}
13 changes: 13 additions & 0 deletions examples/buf-yaml/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "buf-yaml"
version = "0.1.0"
edition = "2021"

[dependencies]
tonic = {version = "0.9.2", features = ["tls"]}
tokio = {version = "1.28.2", features = ["full"]}
prost = "0.11.9"

[build-dependencies]
tonic_buf_build = {path = "../../"}
tonic-build = "*"
8 changes: 8 additions & 0 deletions examples/buf-yaml/buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Generated by buf. DO NOT EDIT.
version: v1
deps:
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 711e289f6a384c4caeebaff7c6931ade
digest: shake256:e08fb55dad7469f69df00304eed31427d2d1576e9aab31e6bf86642688e04caaf0372f15fe6974cf79432779a635b3ea401ca69c943976dc42749524e4c25d94
9 changes: 9 additions & 0 deletions examples/buf-yaml/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: v1
deps:
- buf.build/googleapis/googleapis
lint:
except:
- PACKAGE_VERSION_SUFFIX
- PACKAGE_DIRECTORY_MATCH
rpc_allow_google_protobuf_empty_requests: true
rpc_allow_google_protobuf_empty_responses: true
4 changes: 4 additions & 0 deletions examples/buf-yaml/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() -> Result<(), tonic_buf_build::error::TonicBufBuildError> {
tonic_buf_build::compile_from_buf(tonic_build::configure(), None)?;
Ok(())
}
15 changes: 15 additions & 0 deletions examples/buf-yaml/sample.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";
package tonic_buf_build_sample;

import "google/api/annotations.proto";
import "google/protobuf/empty.proto";

service HelloService {
rpc SayHello(google.protobuf.Empty) returns (SayHelloResponse) {
option (google.api.http) = {get: "/hello"};
}
}

message SayHelloResponse {
string value = 1;
}
31 changes: 31 additions & 0 deletions examples/buf-yaml/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::result::Result;

use proto::{hello_service_server::HelloServiceServer, SayHelloResponse};
use tonic::{transport::Server, Request, Response, Status};

pub mod proto {
tonic::include_proto!("tonic_buf_build_sample");
}

#[derive(Clone)]
struct HelloServer {}

#[tonic::async_trait]
impl proto::hello_service_server::HelloService for HelloServer {
async fn say_hello(&self, _: Request<()>) -> Result<Response<SayHelloResponse>, Status> {
Ok(Response::new(SayHelloResponse {
value: "Hello world!".into(),
}))
}
}

#[tokio::main]
async fn main() {
let hello_server = HelloServer {};

Server::builder()
.add_service(HelloServiceServer::new(hello_server))
.serve("127.0.0.1:10000".parse().unwrap())
.await
.unwrap();
}
Loading

0 comments on commit 89da80a

Please sign in to comment.