15 releases (1 stable)

Uses new Rust 2021

1.0.0 Nov 25, 2021
1.0.0-pre.0 Nov 19, 2021
0.4.0 Oct 28, 2021
0.3.0 Oct 28, 2021
0.0.5 Jun 19, 2020

#37 in Development tools

Download history 1397/week @ 2021-08-13 1047/week @ 2021-08-20 913/week @ 2021-08-27 652/week @ 2021-09-03 1202/week @ 2021-09-10 896/week @ 2021-09-17 678/week @ 2021-09-24 954/week @ 2021-10-01 1736/week @ 2021-10-08 1483/week @ 2021-10-15 921/week @ 2021-10-22 1044/week @ 2021-10-29 459/week @ 2021-11-05 1218/week @ 2021-11-12 1406/week @ 2021-11-19 1657/week @ 2021-11-26

4,926 downloads per month

MIT license

15KB
258 lines

grpc-build

grpc-build provides an flexible way to manage protobuf files and generate the gRPC code required by tonic.

It is built on top of tonic_build and it extends its functionality by compiling all the protobuf files inside a directory.

If the protobuf content is valid (worth linting it), grpc-build will take care of the protobuf imports and it will also generate the mod.rs file to allow the compiler to find the generated code. This file will be placed inside the output directory.

It comes both as a library that can be used directly inside a project and as a binary that can be used in CI pipelines.

Documentation - Crates.io

Getting started

Using it as a binary

Get the latest binary release and use it inside your CI pipeline.

grpc-build build --in-dir="<protobuf directory>" --out-dir="<codegen>"

Depending on the requirements, you can generate the gRPC Client and/or Server by using the --build-client (-c) and --build-server (-s) flags.

To overwrite the contents of the output directory, use the --force (-f) flag.

// both client and server, overwriting the existing protogen
grpc-build build -c -s --in-dir="<protobuf directory>" --out-dir="<codegen>" -f

Using it as a library

The most convenient way of using grpc_build as a library is by taking advantage of Rust's build.rs file. Don't forget to add grpc_build to the build-dependencies list.

// build.rs
use grpc_build::build;

fn main() {
    build(
        "protos",       // protobuf files input dir
        "src/protogen", // output directory
        true,           // --build_server=true
        true,           // --build_client=true
        true,           // --force
    )
    .unwrap();
}

If you want to set advanced compilation options (like an additional #[derive] for the generated types), use the build_with_config function, which exposes the underlying tonic_build::Builder.

A more advanced usage is to use the get_protos and refactor functions yourself. The following example does the same as the example above

use grpc_build::base::{prepare_out_dir, get_protos, refactor};
use tonic_build::configure;

fn main() {
    let proto_src_dir = "protos";
    let proto_out_dir = "src/protogen";

    prepare_out_dir(proto_out_dir).unwrap();

    configure()
        .out_dir(proto_out_dir)
        .build_server(true)
        .build_client(true)
        .compile(
            &get_protos(proto_src_dir).collect::<Vec<_>>(),
            &["."]
        )
        .unwrap();

    refactor(proto_out_dir).unwrap();
}

License

This project is licensed under the MIT license.

Dependencies

~3.5MB
~58K SLoC