Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,7 @@ sync-stable-docs:
update-docsy:
rm -rf docs/themes/docsy
git clone --recurse-submodules --depth 1 https://github.com/google/docsy.git docs/themes/docsy

.PHONY: codegen
codegen:
docker run -v $PWD:/repo -it gcr.io/pipecd/codegen:0.1.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits, without container name, I think we should add --rm to remove the container after it exits

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! 💯

2 changes: 2 additions & 0 deletions dockers/codegen/DOCKER_BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version: 0.1.0
registry: gcr.io/pipecd/codegen
46 changes: 46 additions & 0 deletions dockers/codegen/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
FROM golang:1.17-alpine3.15

ENV PROTOC_VER=3.14.0
ENV PROTOC_GEN_GO_VER=1.27.1
ENV PROTOC_GEN_GRPC_WEB_VER=1.3.1
ENV PROTOC_GEN_GO_GRPC_VER=1.1
ENV PROTOC_GEN_VALIDATE_VER=0.6.6
ENV GLIBC_VERSION=2.33-r0

RUN apk --no-cache add wget bash \
&& wget -q https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub -O /etc/apk/keys/sgerrand.rsa.pub \
&& wget -q https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VERSION}/glibc-${GLIBC_VERSION}.apk -O glibc.apk \
&& apk add glibc.apk \
&& rm /etc/apk/keys/sgerrand.rsa.pub glibc.apk

# protoc
RUN wget -q https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VER}/protoc-${PROTOC_VER}-linux-x86_64.zip -O protoc.zip \
&& unzip protoc.zip -d /usr/local \
&& rm protoc.zip

# protoc-gen-go
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@v${PROTOC_GEN_GO_VER}

# protoc-gen-go-grpc
RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v${PROTOC_GEN_GO_GRPC_VER}

# protoc-gen-grpc-web
RUN wget https://github.com/grpc/grpc-web/releases/download/${PROTOC_GEN_GRPC_WEB_VER}/protoc-gen-grpc-web-${PROTOC_GEN_GRPC_WEB_VER}-linux-x86_64 \
&& mv protoc-gen-grpc-web-${PROTOC_GEN_GRPC_WEB_VER}-linux-x86_64 /usr/local/bin/protoc-gen-grpc-web \
&& chmod +x /usr/local/bin/protoc-gen-grpc-web

# protoc-gen-validate
RUN go install github.com/envoyproxy/protoc-gen-validate@v${PROTOC_GEN_VALIDATE_VER} \
&& wget -q https://github.com/envoyproxy/protoc-gen-validate/archive/refs/tags/v${PROTOC_GEN_VALIDATE_VER}.tar.gz -O protoc-gen-validate.tar.gz \
&& mkdir -p /go/src/github.com/envoyproxy \
&& tar xvfz protoc-gen-validate.tar.gz -C /go/src/github.com/envoyproxy \
&& rm protoc-gen-validate.tar.gz \
&& mv /go/src/github.com/envoyproxy/protoc-gen-validate-${PROTOC_GEN_VALIDATE_VER} /go/src/github.com/envoyproxy/protoc-gen-validate

VOLUME /repo
WORKDIR /repo

COPY ./codegen.sh /
RUN chmod +x /codegen.sh

ENTRYPOINT ["/codegen.sh"]
79 changes: 79 additions & 0 deletions dockers/codegen/codegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/bash

set -o errexit
set -o nounset
set -o pipefail

# Generate Go files from proto files.
goProtoDirs=(
"pkg/model"
"pkg/app/server/service/apiservice"
"pkg/app/server/service/pipedservice"
"pkg/app/server/service/webservice"
"pkg/app/helloworld/service"
)

for dir in ${goProtoDirs[*]}; do
echo ""
echo "- ${dir}"
echo "deleting previously generated Go files..."
find ${dir} -name "*.pb.go" -o -name "*.pb.validate.go" -type f -delete
echo "successfully deleted"

echo "generating new Go files..."
protoc \
-I . \
-I /go/src/github.com/envoyproxy/protoc-gen-validate \
--go_out=. \
--go_opt=paths=source_relative \
--go-grpc_out=. \
--go-grpc_opt=paths=source_relative \
--validate_out="lang=go:." \
--validate_opt=paths=source_relative \
${dir}/*.proto
echo "successfully generated"
done

# Generate JS files from proto files.
jsProtoDirs=(
"pkg/model"
"pkg/app/server/service/webservice"
)
jsOutDirs=(
"pkg/app/web/model"
"pkg/app/web/api_client"
)

i=0
while [ $i -lt ${#jsProtoDirs[*]} ]; do
inDir=${jsProtoDirs[$i]}
outDir=${jsOutDirs[$i]}
i=$(( $i + 1))

echo ""
echo "- ${inDir}"
echo "deleting previously generated JS files..."
rm -rf ${outDir}
mkdir -p ${outDir}
echo "successfully deleted"

echo "generating new JS files..."
protoc \
-I . \
-I /go/src/github.com/envoyproxy/protoc-gen-validate \
--js_out=import_style=commonjs:. \
--grpc-web_out=import_style=commonjs+dts,mode=grpcwebtext:. \
${inDir}/*.proto
mv ${inDir}/*.js ${outDir}
mv ${inDir}/*.d.ts ${outDir}

find ${outDir} -type f -exec sed -i 's:.*validate_pb.*::g' {} \;
find ${outDir} -type f -exec sed -i "s:'.*pkg:'pipecd\/pkg\/app\/web:g;" {} \;
find ${outDir} -type f -exec sed -i "s:'.*\/model\/:'pipecd\/pkg\/app\/web\/model\/:g;" {} \;
echo "successfully generated"

echo ""
echo "Successfully generated all code"
echo ""

done