Skip to content

Commit

Permalink
make proto-gen and proto-swagger-gen to use docker
Browse files Browse the repository at this point in the history
it makes proto stub generation more deterministic
  • Loading branch information
hallazzang committed Apr 7, 2021
1 parent 5c05566 commit 2415f99
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 280 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,13 @@ format:
### Protobuf ###
###############################################################################

proto-all: proto-tools proto-gen proto-swagger-gen
proto-all: proto-gen proto-swagger-gen

proto-gen:
@./scripts/protocgen.sh
docker run --rm -v $(CURDIR):/workspace --workdir /workspace bharvest/liquidity-proto-gen sh ./scripts/protocgen.sh
go mod tidy

proto-swagger-gen:
@./scripts/protoc-swagger-gen.sh
docker run --rm -v $(CURDIR):/workspace --workdir /workspace bharvest/liquidity-proto-gen sh ./scripts/protoc-swagger-gen.sh

.PHONY: proto-all proto-gen proto-swagger-gen
29 changes: 29 additions & 0 deletions contrib/devtools/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM bufbuild/buf:latest AS builder

FROM golang:alpine

RUN apk add --update-cache \
nodejs \
npm \
&& rm -rf /var/cache/apk/*

WORKDIR /workspace

ENV GOLANG_PROTOBUF_VERSION=1.3.5 \
GOGO_PROTOBUF_VERSION=1.3.2 \
GRPC_GATEWAY_VERSION=1.16.0

RUN GO111MODULE=on go get \
github.com/golang/protobuf/protoc-gen-go@v${GOLANG_PROTOBUF_VERSION} \
github.com/gogo/protobuf/protoc-gen-gogo@v${GOGO_PROTOBUF_VERSION} \
github.com/gogo/protobuf/protoc-gen-gogofast@v${GOGO_PROTOBUF_VERSION} \
github.com/gogo/protobuf/protoc-gen-gogofaster@v${GOGO_PROTOBUF_VERSION} \
github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway@v${GRPC_GATEWAY_VERSION} \
github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger@v${GRPC_GATEWAY_VERSION} \
github.com/regen-network/cosmos-proto/protoc-gen-gocosmos@latest

RUN GO111MODULE=on go get -u github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc

RUN npm i -g swagger-combine

COPY --from=builder /usr/local/bin /usr/local/bin
10 changes: 0 additions & 10 deletions contrib/devtools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ UNAME_M ?= $(shell uname -m)
GOPATH ?= $(shell $(GO) env GOPATH)
GITHUBDIR := $(GOPATH)$(FS)src$(FS)github.com

BUF_VERSION ?= 0.11.0

TOOLS_DESTDIR ?= $(GOPATH)/bin
STATIK = $(TOOLS_DESTDIR)/statik
RUNSIM = $(TOOLS_DESTDIR)/runsim
Expand All @@ -57,14 +55,6 @@ tools-stamp: statik runsim
# in a row.
touch $@

proto-tools: proto-tools-stamp
proto-tools-stamp:
bash contrib/devtools/proto-tools-installer.sh
# Create dummy file to satisfy dependency and avoid
# rebuilding when this Makefile target is hit twice
# in a row.
touch $@

# Install the runsim binary with a temporary workaround of entering an outside
# directory as the "go get" command ignores the -mod option and will polute the
# go.{mod, sum} files.
Expand Down
128 changes: 0 additions & 128 deletions contrib/devtools/proto-tools-installer.sh

This file was deleted.

25 changes: 7 additions & 18 deletions scripts/protoc-swagger-gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,15 @@ proto_dirs=$(find ./proto -path -prune -o -name '*.proto' -print0 | xargs -0 -n1
for dir in $proto_dirs; do

# generate swagger files (filter query files)
query_file=$(find "${dir}" -maxdepth 1 -name 'query.proto')
query_file=$(find "${dir}" -maxdepth 1 \( -name 'query.proto' -o -name 'msg.proto' \))
if [[ ! -z "$query_file" ]]; then
protoc \
-I "proto" \
-I "third_party/proto" \
"$query_file" \
--swagger_out ./tmp-swagger-gen \
--swagger_opt logtostderr=true --swagger_opt fqn_for_swagger_name=true --swagger_opt simple_operation_ids=true
buf protoc \
-I "proto" \
-I "third_party/proto" \
"$query_file" \
--swagger_out=./tmp-swagger-gen \
--swagger_opt=logtostderr=true --swagger_opt=fqn_for_swagger_name=true --swagger_opt=simple_operation_ids=true
fi
# generate swagger files (filter query files)
tx_file=$(find "${dir}" -maxdepth 1 -name 'msg.proto')
if [[ ! -z "$tx_file" ]]; then
protoc \
-I "proto" \
-I "third_party/proto" \
"$tx_file" \
--swagger_out ./tmp-swagger-gen \
--swagger_opt logtostderr=true --swagger_opt fqn_for_swagger_name=true --swagger_opt simple_operation_ids=true
fi

done

# combine swagger files
Expand Down
21 changes: 16 additions & 5 deletions scripts/protocgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@

set -eo pipefail

protoc_gen_gocosmos() {
if ! grep "github.com/gogo/protobuf => github.com/regen-network/protobuf" go.mod &>/dev/null ; then
echo -e "\tPlease run this command from somewhere inside the cosmos-sdk folder."
return 1
fi

go get github.com/regen-network/cosmos-proto/protoc-gen-gocosmos@latest 2>/dev/null
}

protoc_gen_gocosmos

proto_dirs=$(find ./proto -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq)
for dir in $proto_dirs; do
protoc \
-I "proto" \
-I "third_party/proto" \
--gocosmos_out=plugins=interfacetype+grpc,\
buf protoc \
-I "proto" \
-I "third_party/proto" \
--gocosmos_out=plugins=interfacetype+grpc,\
Mgoogle/protobuf/any.proto=github.com/cosmos/cosmos-sdk/codec/types:. \
--grpc-gateway_out=logtostderr=true:. \
--grpc-gateway_out=logtostderr=true,allow_colon_final_segments=true:. \
$(find "${dir}" -maxdepth 1 -name '*.proto')

done
Expand Down
Loading

0 comments on commit 2415f99

Please sign in to comment.