Skip to content

Commit

Permalink
Add js-out and grpc-web-out options
Browse files Browse the repository at this point in the history
Add options to allow modifying the output options for node and web code
generation. Add web generation tests.
  • Loading branch information
artificial-aidan committed Jan 5, 2021
1 parent 78ef090 commit 174e001
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

This repository contains support for various Docker images that wrap `protoc`,
`prototool`, `grpc_cli` commands with [gRPC](https://github.com/grpc/grpc) support
in a variety of languages removing the need to install and manage these commands locally.
in a variety of languages removing the need to install and manage these commands locally.
It relies on setting a simple volume to the docker container,
usually mapping the current directory to `/defs`, and specifying the file and
language you want to generate.
Expand Down Expand Up @@ -92,6 +92,12 @@ $ docker run ... namely/protoc-all -f protorepo/catalog/catalog.proto -l go

`--with-rbi` to generate Ruby Sorbet type definition .rbi files

### node/web specific options

`--js-out <string>` to modify the `js_out=` options for node and web code generation

`--grpc-web-out <string>` to modify the `grpc-web_out=` options for web code generation

## gRPC Gateway (Experimental)

This repo also provides a docker images `namely/gen-grpc-gateway` that
Expand Down Expand Up @@ -133,7 +139,7 @@ listens on port `80` for HTTP traffic.

The gateway is configured using [spf13/viper](https://github.com/spf13/viper), see [gwy/templates/config.yaml.tmpl](https://github.com/namely/docker-protoc/blob/master/gwy/templates/config.yaml.tmpl) for configuration options.

To configure your gateway to run under a prefix, set proxy.api-prefix to that prefix. For example, if you have `(google.api.http) = '/foo/bar'`, and set `proxy.api-prefix` to `/api/'`, your gateway will listen to requests on `'/api/foo/bar'`. This can also be set with the environment variable `<SERVICE>_PROXY_API-PREFIX` where `<SERVICE>` is the name of the service generating the gateway.
To configure your gateway to run under a prefix, set proxy.api-prefix to that prefix. For example, if you have `(google.api.http) = '/foo/bar'`, and set `proxy.api-prefix` to `/api/'`, your gateway will listen to requests on `'/api/foo/bar'`. This can also be set with the environment variable `<SERVICE>_PROXY_API-PREFIX` where `<SERVICE>` is the name of the service generating the gateway.

See [gwy/test.sh](https://github.com/namely/docker-protoc/blob/master/gwy/test.sh) for an example of how to set the prefix with an environment variable.

Expand Down Expand Up @@ -172,8 +178,8 @@ There are four values:

### Other Response Headers

You can configure additional headers to be sent in the HTTP response.
Set environment variable with prefix `<SERVICE>_RESPONSE-HEADERS_` (e.g `SOMESERVICE_RESPONSE-HEADERS_SOME-HEADER-KEY`).
You can configure additional headers to be sent in the HTTP response.
Set environment variable with prefix `<SERVICE>_RESPONSE-HEADERS_` (e.g `SOMESERVICE_RESPONSE-HEADERS_SOME-HEADER-KEY`).
You can also set headers in the your configuration file (e.g `response-headers.some-header-key`)

### Marshalling options
Expand Down
18 changes: 16 additions & 2 deletions all/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ printUsage() {
echo " --with-swagger-json-names Use with --with-gateway flag. Generated swagger file will use JSON names instead of protobuf names.
(deprecated. Please use --with-openapi-json-names)"
echo " --with-openapi-json-names Use with --with-gateway flag. Generated OpenAPI file will use JSON names instead of protobuf names."
echo " --js-out This option overrides the 'js_out=' argument in the grpc-node and grpc-web code generation. Defaults to 'import_style=commonjs'."
echo " --grpc-web-out This option overrides the 'grpc-web_out=' argument in the grpc-web code generation. Defaults to 'import_style=typescript'."
}

GEN_GATEWAY=false
Expand All @@ -60,6 +62,8 @@ DESCR_FILENAME="descriptor_set.pb"
CSHARP_OPT=""
SCALA_OPT=""
OPENAPI_JSON=false
JS_OUT="import_style=commonjs"
WEB_OUT="import_style=typescript"

while test $# -gt 0; do
case "$1" in
Expand Down Expand Up @@ -196,6 +200,16 @@ while test $# -gt 0; do
OPENAPI_JSON=true
shift
;;
--js-out)
shift
JS_OUT=$1
shift
;;
--grpc-web-out)
shift
WEB_OUT=$1
shift
;;
*)
break
;;
Expand Down Expand Up @@ -313,10 +327,10 @@ plugins=grpc+embedded\
GEN_STRING="--scala_out=$SCALA_OUT --plugin=`which protoc-gen-scala`"
;;
"node")
GEN_STRING="--grpc_out=$OUT_DIR --js_out=import_style=commonjs,binary:$OUT_DIR --plugin=protoc-gen-grpc=`which grpc_${PLUGIN_LANG}_plugin`"
GEN_STRING="--grpc_out=$OUT_DIR --js_out=$JS_OUT,binary:$OUT_DIR --plugin=protoc-gen-grpc=`which grpc_${PLUGIN_LANG}_plugin`"
;;
"web")
GEN_STRING="--grpc-web_out=import_style=typescript,mode=grpcwebtext:$OUT_DIR --js_out=import_style=commonjs:$OUT_DIR --plugin=protoc-gen-grpc-web=`which grpc_${PLUGIN_LANG}_plugin`"
GEN_STRING="--grpc-web_out=$WEB_OUT,mode=grpcwebtext:$OUT_DIR --js_out=$JS_OUT:$OUT_DIR --plugin=protoc-gen-grpc-web=`which grpc_${PLUGIN_LANG}_plugin`"
;;
"descriptor_set")
GEN_STRING="--descriptor_set_out=$OUT_DIR/$DESCR_FILENAME"
Expand Down
50 changes: 49 additions & 1 deletion all/test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash -e

LANGS=("go" "ruby" "csharp" "java" "python" "objc" "node" "gogo" "php" "cpp" "descriptor_set")
LANGS=("go" "ruby" "csharp" "java" "python" "objc" "node" "gogo" "php" "cpp" "descriptor_set" "web")

CONTAINER=${CONTAINER}

Expand Down Expand Up @@ -122,6 +122,47 @@ testGeneration() {
fi
fi

if [[ "$extra_args" == *"--js-out library=testlib"* ]]; then
# Test that we have generated the testlib.js file
testlib_count=$(find $expected_output_dir -type f -name "testlib.js" | wc -l)
if [ $testlib_count -ne 1 ]; then
echo "testlib.js file was not generated in $expected_output_dir"
exit 1
fi
fi

if [[ "$extra_args" == *"--grpc-web-out import_style=commonjs+dts"* ]]; then
# Test that we have generated the .d.ts files and .js files
ts_file_count=$(find $expected_output_dir -type f -name "*.d.ts" | wc -l)
if [ $ts_file_count -ne 2 ]; then
echo ".d.ts files were not generated in $expected_output_dir"
exit 1
fi
js_file_count=$(find $expected_output_dir -type f -name "*.js" | wc -l)
if [ $js_file_count -ne 2 ]; then
echo ".js files were not generated in $expected_output_dir"
exit 1
fi
fi
if [[ "$extra_args" == *"--grpc-web-out import_style=typescript"* ]]; then
# Test that we have generated the .d.ts files, .ts files and .js files
d_ts_file_count=$(find $expected_output_dir -type f -name "*.d.ts" | wc -l)
if [ $d_ts_file_count -ne 1 ]; then
echo ".d.ts files were not generated in $expected_output_dir"
exit 1
fi
ts_file_count=$(find $expected_output_dir -type f -name "*Pb.ts" | wc -l)
if [ $ts_file_count -ne 1 ]; then
echo ".ts files were not generated in $expected_output_dir"
exit 1
fi
js_file_count=$(find $expected_output_dir -type f -name "*.js" | wc -l)
if [ $js_file_count -ne 1 ]; then
echo "More than 1 .js file was generated in $expected_output_dir"
exit 1
fi
fi

rm -rf `echo $expected_output_dir | cut -d '/' -f1`
echo "Generating for $lang passed!"
}
Expand Down Expand Up @@ -153,6 +194,13 @@ testGeneration ruby "gen/pb-ruby" --with-rbi
# Test TypeScript declaration file generation (only valid for Node)
testGeneration node "gen/pb-node" --with-typescript

# Test node alternative import style (only valid for node and web)
testGeneration node "gen/pb-node" --js-out library=testlib

# Test grpc web alternative import style (only valid for web)
testGeneration web "gen/pb-web" --grpc-web-out import_style=typescript
testGeneration web "gen/pb-web" --grpc-web-out import_style=commonjs+dts

# Generate proto files
for lang in ${LANGS[@]}; do
expected_output_dir=""
Expand Down

0 comments on commit 174e001

Please sign in to comment.