Skip to content

Instantly share code, notes, and snippets.

@VinGarcia
Last active August 4, 2022 19:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VinGarcia/43dfa71b412c16b9365c224d3760af5e to your computer and use it in GitHub Desktop.
Save VinGarcia/43dfa71b412c16b9365c224d3760af5e to your computer and use it in GitHub Desktop.
A script to wrap protoc & several dependencies in a portable way (meant to work with Golang)

This script was written to make it easy to compile protobuf files into Golang. It depends only on docker and bash which should make it very portable.

Usage

To run this script just copy it to your project (you can add it to version control) and then run it passing as argument the relative or absolute path of the directory containing your .proto files, e.g.:

bash protogen.sh my_proto_dir/

It will then generate the source files inside this same directory.

Updating/Changing the script

Adding extra dependencies is easy: Just git clone them inside the Dockerfile and add an extra -I option to the protoc command.

Adapting it to another language should not be too complicated either, just change the base image and install the required packages as you would on a local environment.

Making it less verbose

To hide the docker build output, you may replace the docker build line with:

docker build -t inline_protogen - <<EOF > /dev/null

Which might be useful if you plan on running this script from a Makefile and don't want the user of the Makefile to worry about what is going on internally.

#!/usr/bin/env bash
set -e
# (building an inline Dockerfile so it is easier to copy & paste this script)
docker build -t inline_protogen - <<EOF
FROM golang:1.15.6
WORKDIR /protogen
RUN go mod init protogen
RUN mkdir proto
RUN GO111MODULES=off go get -u \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \
google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc
RUN git clone https://github.com/grpc-ecosystem/grpc-gateway
RUN apt-get update
RUN apt-get -yy install unzip wget
# Download protoc (you might need to update this link from time to time):
RUN wget -O protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v3.14.0/protoc-3.14.0-linux-x86_64.zip
# Install protoc:
RUN unzip protoc.zip -d /usr/
CMD protoc -I . --go_out . \
-I grpc-gateway \
-I grpc-gateway/third_party/googleapis \
--experimental_allow_proto3_optional \
--go_opt paths=source_relative \
--go-grpc_out . \
--go-grpc_opt paths=source_relative \
--grpc-gateway_out . \
--grpc-gateway_opt logtostderr=true \
--grpc-gateway_opt paths=source_relative \
proto/*.proto
EOF
# Convert a relative path into a fullpath if necessary
# (this is necessary because the `docker -v` option only works with fullpaths)
PROTO_DIR=$(echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")")
echo
echo "Building the following proto files:"
echo
ls $PROTO_DIR/*.proto
echo
docker run -v "$PROTO_DIR:/protogen/proto" inline_protogen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment