diff --git a/grpc/scripts/gen.sh b/grpc/scripts/gen.sh index 7491f07..50b34a8 100755 --- a/grpc/scripts/gen.sh +++ b/grpc/scripts/gen.sh @@ -1,17 +1,20 @@ #!/usr/bin/env bash # check pb dir -pb_dir="$(cd "$(dirname $0)";pwd)/../pb" +pb_dir="$( + cd "$(dirname $0)" + pwd +)/../pb" if [ ! -d $pb_dir ]; then - echo "pb dir $pb_dir does not exist in grpc folder" - exit + echo "pb dir $pb_dir does not exist in grpc folder" + exit fi cd $pb_dir # check protoc -which protoc > /dev/null -if [ $? -ne 0 ];then - echo "missing protoc" && exit +which protoc >/dev/null +if [ $? -ne 0 ]; then + echo "missing protoc" && exit fi gen_dir="$pb_dir/../gen" @@ -22,99 +25,98 @@ cmd=protoc async_tag="" # check all arguments -for aug in $@ -do - if [ $aug == "-a" ]; then - async_tag="&" +for aug in $@; do + if [ $aug == "-a" ]; then + async_tag="&" + fi + if [ $aug == "--async" ]; then + async_tag="&" + fi + if [ $aug == "go" ]; then + plugin=protoc-gen-go + which $plugin >/dev/null + if [ $? -ne 0 ]; then + echo "missing plugin: $plugin" && exit fi - if [ $aug == "--async" ]; then - async_tag="&" + vplugin=protoc-gen-govalidators + which $vplugin >/dev/null + if [ $? -ne 0 ]; then + echo "missing plugin: $vplugin" && exit fi - if [ $aug == "go" ]; then - plugin=protoc-gen-go - which $plugin > /dev/null - if [ $? -ne 0 ];then - echo "missing plugin: $plugin" && exit - fi - vplugin=protoc-gen-govalidators - which $vplugin > /dev/null - if [ $? -ne 0 ];then - echo "missing plugin: $vplugin" && exit - fi - out="$gen_dir/go" - if [ ! -d $out ]; then - mkdir -p $out - fi - echo "Generating Go code" - $cmd \ - --go_out=$out \ - --go-grpc_out=$out \ - $pb_files \ - $async_tag + out="$gen_dir/go" + if [ ! -d $out ]; then + mkdir -p $out fi - if [ $aug == "gohttp" ]; then - plugin=protoc-gen-grpc-gateway - which $plugin > /dev/null - if [ $? -ne 0 ];then - echo "missing plugin: $plugin" && exit - fi - out="$gen_dir/go" - if [ ! -d $out ]; then - mkdir -p $out - fi - echo "Generating Go HTTP code" - $cmd \ - --grpc-gateway_out $out \ - --grpc-gateway_opt grpc_api_configuration=bdware/bdledger/api/grpc-gateway.yml \ - --grpc-gateway_opt logtostderr=true \ - --openapiv2_out ../../docs/openapiv2 \ - --openapiv2_opt grpc_api_configuration=bdware/bdledger/api/grpc-gateway.yml \ - --openapiv2_opt logtostderr=true \ - $pb_files \ - $async_tag + echo "Generating Go code" + $cmd \ + --go_out=$out \ + --go-grpc_out=$out \ + $pb_files \ + $async_tag + fi + if [ $aug == "gohttp" ]; then + plugin=protoc-gen-grpc-gateway + which $plugin >/dev/null + if [ $? -ne 0 ]; then + echo "missing plugin: $plugin" && exit fi - if [ $aug == "nodejs" ]; then - npmBinPath=$(npm bin) - jsCmd=$npmBinPath/grpc_tools_node_protoc - pluginPath=$npmBinPath/protoc-gen-ts - out="$gen_dir/nodejs" - if [ ! -d $out ]; then - mkdir -p $out - fi - echo "Generating Node.js code & TypeScript definitions" - $jsCmd \ - --plugin=protoc-gen-ts=$pluginPath \ - --js_out=import_style=commonjs,binary:$out \ - --grpc_out=grpc_js:$out \ - --ts_out=grpc_js:$out \ - $pb_files $emptypb_file \ - $async_tag + out="$gen_dir/go" + if [ ! -d $out ]; then + mkdir -p $out fi - if [ $aug == "docs" ]; then - plugin=protoc-gen-ts - plugin_path=$(which $plugin) - if [ $? -ne 0 ];then - echo "missing plugin: $plugin" && exit - fi - out="$pb_dir/../../docs" - if [ ! -d $out ]; then - mkdir -p $out - fi - echo "Generating documentations" - $cmd \ - --doc_out=$out \ - --doc_opt=html,apis.html \ - $pb_files $emptypb_file \ - $async_tag - $cmd \ - --doc_out=$out \ - --doc_opt=markdown,apis.md \ - $pb_files $emptypb_file \ - $async_tag - $cmd \ - --doc_out=$out \ - --doc_opt=json,apis.json \ - $pb_files $emptypb_file \ - $async_tag + echo "Generating Go HTTP code" + $cmd \ + --grpc-gateway_out $out \ + --grpc-gateway_opt grpc_api_configuration=bdware/bdledger/api/grpc-gateway.yml \ + --grpc-gateway_opt logtostderr=true \ + --openapiv2_out ../../docs/openapiv2 \ + --openapiv2_opt grpc_api_configuration=bdware/bdledger/api/grpc-gateway.yml \ + --openapiv2_opt logtostderr=true \ + $pb_files \ + $async_tag + fi + if [ $aug == "nodejs" ]; then + npmBinPath=$(npm bin) + jsCmd=$npmBinPath/grpc_tools_node_protoc + pluginPath=$npmBinPath/protoc-gen-ts + out="$gen_dir/nodejs" + if [ ! -d $out ]; then + mkdir -p $out fi + echo "Generating Node.js code & TypeScript definitions" + $jsCmd \ + --plugin=protoc-gen-ts=$pluginPath \ + --js_out=import_style=commonjs,binary:$out \ + --grpc_out=grpc_js:$out \ + --ts_out=grpc_js:$out \ + $pb_files $emptypb_file \ + $async_tag + fi + if [ $aug == "docs" ]; then + plugin=protoc-gen-ts + plugin_path=$(which $plugin) + if [ $? -ne 0 ]; then + echo "missing plugin: $plugin" && exit + fi + out="$pb_dir/../../docs" + if [ ! -d $out ]; then + mkdir -p $out + fi + echo "Generating documentations" + $cmd \ + --doc_out=$out \ + --doc_opt=html,apis.html \ + $pb_files $emptypb_file \ + $async_tag + $cmd \ + --doc_out=$out \ + --doc_opt=markdown,apis.md \ + $pb_files $emptypb_file \ + $async_tag + $cmd \ + --doc_out=$out \ + --doc_opt=json,apis.json \ + $pb_files $emptypb_file \ + $async_tag + fi done