Skip to content

Commit 7b29cfb

Browse files
Add Support for stephenh/ts-proto (#317)
* adding ts_proto * adding missing arguments, fixing entrypoint bugs, adding tests * update syntax to appease linter * add file body check for ts-proto optional arguments test case * Fix typescript filename test expects, file contents test expects * add ts-proto renovate configuration Co-authored-by: Ido David <36866853+ido-namely@users.noreply.github.com>
1 parent 0eb5fa3 commit 7b29cfb

6 files changed

Lines changed: 93 additions & 27 deletions

File tree

.github/renovate.json5

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@
123123
],
124124
depNameTemplate: "google.golang.org/grpc",
125125
datasourceTemplate: "go",
126+
},
127+
{
128+
fileMatch: [
129+
"variables.sh$"
130+
],
131+
matchStrings: [
132+
"TS_PROTO_VERSION=\\$\\{TS_PROTO_VERSION:-(?<currentValue>.*?)\\}\\n"
133+
],
134+
depNameTemplate: "ts-proto",
135+
datasourceTemplate: "npm",
126136
}
127137
],
128138
packageRules: [

Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ ARG node_version
99
ARG node_grpc_tools_node_protoc_ts_version
1010
ARG node_grpc_tools_version
1111
ARG node_protoc_gen_grpc_web_version
12+
ARG ts_proto_version
1213
ARG go_envoyproxy_pgv_version
1314
ARG go_mwitkow_gpv_version
1415

@@ -133,6 +134,7 @@ ARG node_version
133134
ARG node_grpc_tools_node_protoc_ts_version
134135
ARG node_grpc_tools_version
135136
ARG node_protoc_gen_grpc_web_version
137+
ARG ts_proto_version
136138

137139
ARG go_envoyproxy_pgv_version
138140
ARG go_mwitkow_gpv_version
@@ -153,10 +155,13 @@ RUN set -ex && apt-get update && apt-get install -y --no-install-recommends \
153155
RUN curl -fsSL "https://deb.nodesource.com/setup_${node_version}.x" | bash -
154156
RUN apt-get install -y nodejs
155157

156-
# Add TypeScript support
158+
# Add Node TypeScript support
157159
RUN npm config set unsafe-perm true
158160
RUN npm i -g grpc_tools_node_protoc_ts@$node_grpc_tools_node_protoc_ts_version grpc-tools@$node_grpc_tools_version protoc-gen-grpc-web@$node_protoc_gen_grpc_web_version
159161

162+
# Add TypeScript support
163+
RUN npm i -g ts-proto@$ts_proto_version
164+
160165
COPY --from=build /tmp/googleapis/google/ /opt/include/google
161166
COPY --from=build /tmp/api-common-protos/google/ /opt/include/google
162167

all/entrypoint.sh

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ printUsage() {
3939
echo " --js-out This option overrides the 'js_out=' argument in the grpc-node and grpc-web code generation. Defaults to 'import_style=commonjs'."
4040
echo " --grpc-out This option allows overriding the left-half of the 'grpc_out=' argument (before the colon) with grpc-node and grpc-web code generation. Options are: generate_package_definition, grpc_js or grpc(depricated from April 2021). Defaults to grpc_js."
4141
echo " --grpc-web-out This option overrides the 'grpc-web_out=' argument in the grpc-web code generation. Defaults to 'import_style=typescript'."
42+
echo " --ts_opt The options to pass to protoc to customize the typescript code generation. See https://github.com/stephenh/ts-proto#supported-options. --ts_opt useOptionals=messages will evaluate to --ts_proto_opt=useOptionals=messages"
4243
}
4344

4445
GEN_GATEWAY=false
@@ -50,7 +51,7 @@ GEN_RBI=false
5051
GEN_TYPESCRIPT=false
5152
LINT=false
5253
LINT_CHECKS=""
53-
SUPPORTED_LANGUAGES=("go" "ruby" "csharp" "java" "python" "objc" "gogo" "php" "node" "web" "cpp" "descriptor_set" "scala")
54+
SUPPORTED_LANGUAGES=("go" "ruby" "csharp" "java" "python" "objc" "gogo" "php" "node" "typescript" "web" "cpp" "descriptor_set" "scala")
5455
EXTRA_INCLUDES=""
5556
OUT_DIR=""
5657
GO_SOURCE_RELATIVE=""
@@ -69,6 +70,7 @@ GENERATE_UNBOUND_METHODS=false
6970
JS_OUT="import_style=commonjs"
7071
WEB_OUT="import_style=typescript"
7172
GRPC_OUT="grpc_js"
73+
TYPESCRIPT_OPT=""
7274
while test $# -gt 0; do
7375
case "$1" in
7476
-h|--help)
@@ -141,27 +143,27 @@ while test $# -gt 0; do
141143
LINT=true
142144
if [ "$#" -gt 1 ] && [[ $2 != -* ]]; then
143145
LINT_CHECKS=$2
144-
shift
146+
shift
145147
fi
146148
shift
147149
;;
148-
--validator-source-relative)
150+
--validator-source-relative)
149151
VALIDATOR_SOURCE_RELATIVE=",paths=source_relative"
150152
shift
151153
;;
152154
--go-source-relative)
153155
GO_SOURCE_RELATIVE="paths=source_relative,"
154156
shift
155157
;;
156-
--go-module-prefix)
158+
--go-module-prefix)
157159
shift
158160
GO_MODULE_PREFIX="module=$1,"
159161
shift
160162
;;
161163
--go-package-map)
162164
if [ "$#" -gt 1 ] && [[ $2 != -* ]]; then
163165
GO_PACKAGE_MAP=$2,
164-
shift
166+
shift
165167
fi
166168
shift
167169
;;
@@ -228,6 +230,11 @@ while test $# -gt 0; do
228230
GRPC_OUT=$1
229231
shift
230232
;;
233+
--ts_opt)
234+
shift
235+
TYPESCRIPT_OPT=$1
236+
shift
237+
;;
231238
*)
232239
break
233240
;;
@@ -339,7 +346,7 @@ plugins=grpc+embedded\
339346
:$OUT_DIR"
340347
;;
341348
"java")
342-
GEN_STRING="--grpc_out=$OUT_DIR --${GEN_LANG}_out=$OUT_DIR --plugin=protoc-gen-grpc=`which grpc_java_plugin`"
349+
GEN_STRING="--grpc_out=$OUT_DIR --${GEN_LANG}_out=$OUT_DIR --plugin=protoc-gen-grpc=$(which grpc_java_plugin)"
343350
;;
344351
"scala")
345352
SCALA_OUT=$OUT_DIR
@@ -348,16 +355,16 @@ plugins=grpc+embedded\
348355
SCALA_OUT="$SCALA_OPT:$OUT_DIR"
349356
fi
350357

351-
GEN_STRING="--scala_out=$SCALA_OUT --plugin=`which protoc-gen-scala`"
358+
GEN_STRING="--scala_out=$SCALA_OUT --plugin=$(which protoc-gen-scala)"
352359
;;
353360
"node")
354361
# add plugin
355-
GEN_STRING="--plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin`"
362+
GEN_STRING="--plugin=protoc-gen-grpc=$(which grpc_tools_node_protoc_plugin)"
356363
GEN_STRING="$GEN_STRING --js_out=$JS_OUT,binary:$OUT_DIR --grpc_out=$GRPC_OUT:$OUT_DIR"
357364
;;
358365
"web")
359366
# add plugins
360-
GEN_STRING=" --plugin=protoc-gen-grpc-web=`which protoc-gen-grpc-web`"
367+
GEN_STRING=" --plugin=protoc-gen-grpc-web=$(which protoc-gen-grpc-web)"
361368
GEN_STRING="$GEN_STRING --js_out=$JS_OUT,binary:$OUT_DIR --grpc-web_out=$WEB_OUT,mode=grpcwebtext:$OUT_DIR"
362369
;;
363370
"descriptor_set")
@@ -370,13 +377,21 @@ plugins=grpc+embedded\
370377
fi
371378
;;
372379
"csharp")
373-
GEN_STRING="--grpc_out=$OUT_DIR --csharp_out=$OUT_DIR --plugin=protoc-gen-grpc=`which grpc_csharp_plugin`"
380+
GEN_STRING="--grpc_out=$OUT_DIR --csharp_out=$OUT_DIR --plugin=protoc-gen-grpc=$(which grpc_csharp_plugin)"
374381
if [[ ! -z $CSHARP_OPT ]]; then
375382
GEN_STRING="$GEN_STRING --csharp_opt=$CSHARP_OPT"
376383
fi
377384
;;
385+
"typescript")
386+
# add plugin
387+
GEN_STRING="--plugin=$(which protoc-gen-ts_proto)"
388+
GEN_STRING="$GEN_STRING --ts_proto_out=$OUT_DIR"
389+
if [[ ! -z $TYPESCRIPT_OPT ]]; then
390+
GEN_STRING="$GEN_STRING --ts_proto_opt=$TYPESCRIPT_OPT"
391+
fi
392+
;;
378393
*)
379-
GEN_STRING="--grpc_out=$OUT_DIR --${GEN_LANG}_out=$OUT_DIR --plugin=protoc-gen-grpc=`which grpc_${PLUGIN_LANG}_plugin`"
394+
GEN_STRING="--grpc_out=$OUT_DIR --${GEN_LANG}_out=$OUT_DIR --plugin=protoc-gen-grpc=$(which grpc_${PLUGIN_LANG}_plugin)"
380395
;;
381396
esac
382397

@@ -410,7 +425,7 @@ if [[ $GEN_RBI == true ]]; then
410425
fi
411426

412427
if [[ $GEN_TYPESCRIPT == true ]]; then
413-
GEN_STRING="$GEN_STRING --plugin=protoc-gen-ts=`which protoc-gen-ts` --ts_out=$GRPC_OUT:$OUT_DIR"
428+
GEN_STRING="$GEN_STRING --plugin=protoc-gen-ts=$(which protoc-gen-ts) --ts_out=$GRPC_OUT:$OUT_DIR"
414429
fi
415430

416431
LINT_STRING=''

all/test/all_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,40 @@ func (s *TestSuite) TestAllCases() {
552552
},
553553
extraArgs: []string{"--with-validator"},
554554
},
555+
"typescript": {
556+
lang: "typescript",
557+
protofileName: "all/test/test.proto",
558+
expectedOutputDir: "gen/pb-typescript",
559+
fileExpectations: []FileExpectation{
560+
{fileName: "all/test/test.ts"},
561+
},
562+
},
563+
"typescript with alternative output dir": {
564+
lang: "typescript",
565+
protofileName: "all/test/test.proto",
566+
expectedOutputDir: "gen/foo/bar",
567+
fileExpectations: []FileExpectation{
568+
{fileName: "all/test/test.ts", assert: func(filePath, expectedValue string) {
569+
fileText := s.readFile(filePath)
570+
s.Assert().True(strings.Contains(fileText, expectedValue), "does not contain \"%s\"", expectedValue)
571+
// without useOptionals=messages uses `string[] | undefined` syntax. by default.
572+
}, expectedValue: "updateMask: string[] | undefined;"},
573+
},
574+
extraArgs: []string{"-o", "gen/foo/bar"},
575+
},
576+
"typescript with arguments": {
577+
lang: "typescript",
578+
protofileName: "all/test/test.proto",
579+
expectedOutputDir: "gen/pb-typescript",
580+
fileExpectations: []FileExpectation{
581+
{fileName: "all/test/test.ts", assert: func(filePath, expectedValue string) {
582+
fileText := s.readFile(filePath)
583+
s.Assert().True(strings.Contains(fileText, expectedValue), "does not contain \"%s\"", expectedValue)
584+
// useOptionals=messages changes `updateMask: string[] | undefined` to `updateMask?: string[]`
585+
}, expectedValue: "updateMask?: string[];"},
586+
},
587+
extraArgs: []string{"--ts_opt", "useOptionals=messages"},
588+
},
555589
}
556590
container := os.Getenv("CONTAINER")
557591
s.Require().NotEmpty(container, "CONTAINER env var must be set")

build.sh

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ for build in ${BUILDS[@]}; do
77
echo "building ${build} container with tag ${tag}"
88
docker build -t ${tag} \
99
-f Dockerfile \
10-
--build-arg grpc_version=${GRPC_VERSION} \
11-
--build-arg grpc_java_version=${GRPC_JAVA_VERSION} \
12-
--build-arg grpc_web_version=${GRPC_WEB_VERSION} \
13-
--build-arg grpc_gateway_version=${GRPC_GATEWAY_VERSION} \
14-
--build-arg go_version=${GO_VERSION} \
15-
--build-arg uber_prototool_version=${UBER_PROTOTOOL_VERSION} \
16-
--build-arg scala_pb_version=${SCALA_PB_VERSION} \
17-
--build-arg node_version=${NODE_VERSION} \
18-
--build-arg node_grpc_tools_node_protoc_ts_version=${NODE_GRPC_TOOLS_NODE_PROTOC_TS_VERSION} \
19-
--build-arg node_grpc_tools_version=${NODE_GRPC_TOOLS_VERSION} \
20-
--build-arg node_protoc_gen_grpc_web_version=${NODE_PROTOC_GEN_GRPC_WEB_VERSION} \
21-
--build-arg go_envoyproxy_pgv_version=${GO_ENVOYPROXY_PGV_VERSION} \
22-
--build-arg go_mwitkow_gpv_version=${GO_MWITKOW_GPV_VERSION} \
23-
--target ${build} \
10+
--build-arg grpc_version="${GRPC_VERSION}" \
11+
--build-arg grpc_java_version="${GRPC_JAVA_VERSION}" \
12+
--build-arg grpc_web_version="${GRPC_WEB_VERSION}" \
13+
--build-arg grpc_gateway_version="${GRPC_GATEWAY_VERSION}" \
14+
--build-arg go_version="${GO_VERSION}" \
15+
--build-arg uber_prototool_version="${UBER_PROTOTOOL_VERSION}" \
16+
--build-arg scala_pb_version="${SCALA_PB_VERSION}" \
17+
--build-arg node_version="${NODE_VERSION}" \
18+
--build-arg node_grpc_tools_node_protoc_ts_version="${NODE_GRPC_TOOLS_NODE_PROTOC_TS_VERSION}" \
19+
--build-arg node_grpc_tools_version="${NODE_GRPC_TOOLS_VERSION}" \
20+
--build-arg node_protoc_gen_grpc_web_version="${NODE_PROTOC_GEN_GRPC_WEB_VERSION}" \
21+
--build-arg go_envoyproxy_pgv_version="${GO_ENVOYPROXY_PGV_VERSION}" \
22+
--build-arg go_mwitkow_gpv_version="${GO_MWITKOW_GPV_VERSION}" \
23+
--build-arg ts_proto_version="${TS_PROTO_VERSION}" \
24+
--target "${build}" \
2425
.
2526

2627
if [ "${LATEST}" = true ]; then

variables.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ NODE_VERSION=${NODE_VERSION:-14}
1717
NODE_GRPC_TOOLS_NODE_PROTOC_TS_VERSION=${NODE_GRPC_TOOLS_NODE_PROTOC_TS_VERSION:-5.3.2}
1818
NODE_GRPC_TOOLS_VERSION=${NODE_GRPC_TOOLS_VERSION:-1.11.2}
1919
NODE_PROTOC_GEN_GRPC_WEB_VERSION=${NODE_PROTOC_GEN_GRPC_WEB_VERSION:-1.4.0}
20+
TS_PROTO_VERSION=${TS_PROTO_VERSION:1.117.0}
2021
GO_ENVOYPROXY_PGV_VERSION=${GO_ENVOYPROXY_PGV_VERSION:-0.6.7}
2122
GO_MWITKOW_GPV_VERSION=${GO_MWITKOW_GPV_VERSION:-0.3.2}
2223
BUILD_VERSION="${BUILD_VERSION:-local}"

0 commit comments

Comments
 (0)