style: gen.sh

This commit is contained in:
Nex Zhu 2021-07-15 14:41:32 +08:00
parent a0aa79ba13
commit 6b2c291f27
No known key found for this signature in database
GPG Key ID: 15C6254AD19362B4

View File

@ -1,17 +1,20 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# check pb dir # check pb dir
pb_dir="$(cd "$(dirname $0)";pwd)/../pb" pb_dir="$(
cd "$(dirname $0)"
pwd
)/../pb"
if [ ! -d $pb_dir ]; then if [ ! -d $pb_dir ]; then
echo "pb dir $pb_dir does not exist in grpc folder" echo "pb dir $pb_dir does not exist in grpc folder"
exit exit
fi fi
cd $pb_dir cd $pb_dir
# check protoc # check protoc
which protoc > /dev/null which protoc >/dev/null
if [ $? -ne 0 ];then if [ $? -ne 0 ]; then
echo "missing protoc" && exit echo "missing protoc" && exit
fi fi
gen_dir="$pb_dir/../gen" gen_dir="$pb_dir/../gen"
@ -22,99 +25,98 @@ cmd=protoc
async_tag="" async_tag=""
# check all arguments # check all arguments
for aug in $@ for aug in $@; do
do if [ $aug == "-a" ]; then
if [ $aug == "-a" ]; then async_tag="&"
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 fi
if [ $aug == "--async" ]; then vplugin=protoc-gen-govalidators
async_tag="&" which $vplugin >/dev/null
if [ $? -ne 0 ]; then
echo "missing plugin: $vplugin" && exit
fi fi
if [ $aug == "go" ]; then out="$gen_dir/go"
plugin=protoc-gen-go if [ ! -d $out ]; then
which $plugin > /dev/null mkdir -p $out
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
fi fi
if [ $aug == "gohttp" ]; then echo "Generating Go code"
plugin=protoc-gen-grpc-gateway $cmd \
which $plugin > /dev/null --go_out=$out \
if [ $? -ne 0 ];then --go-grpc_out=$out \
echo "missing plugin: $plugin" && exit $pb_files \
fi $async_tag
out="$gen_dir/go" fi
if [ ! -d $out ]; then if [ $aug == "gohttp" ]; then
mkdir -p $out plugin=protoc-gen-grpc-gateway
fi which $plugin >/dev/null
echo "Generating Go HTTP code" if [ $? -ne 0 ]; then
$cmd \ echo "missing plugin: $plugin" && exit
--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 fi
if [ $aug == "nodejs" ]; then out="$gen_dir/go"
npmBinPath=$(npm bin) if [ ! -d $out ]; then
jsCmd=$npmBinPath/grpc_tools_node_protoc mkdir -p $out
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 fi
if [ $aug == "docs" ]; then echo "Generating Go HTTP code"
plugin=protoc-gen-ts $cmd \
plugin_path=$(which $plugin) --grpc-gateway_out $out \
if [ $? -ne 0 ];then --grpc-gateway_opt grpc_api_configuration=bdware/bdledger/api/grpc-gateway.yml \
echo "missing plugin: $plugin" && exit --grpc-gateway_opt logtostderr=true \
fi --openapiv2_out ../../docs/openapiv2 \
out="$pb_dir/../../docs" --openapiv2_opt grpc_api_configuration=bdware/bdledger/api/grpc-gateway.yml \
if [ ! -d $out ]; then --openapiv2_opt logtostderr=true \
mkdir -p $out $pb_files \
fi $async_tag
echo "Generating documentations" fi
$cmd \ if [ $aug == "nodejs" ]; then
--doc_out=$out \ npmBinPath=$(npm bin)
--doc_opt=html,apis.html \ jsCmd=$npmBinPath/grpc_tools_node_protoc
$pb_files $emptypb_file \ pluginPath=$npmBinPath/protoc-gen-ts
$async_tag out="$gen_dir/nodejs"
$cmd \ if [ ! -d $out ]; then
--doc_out=$out \ mkdir -p $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 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 done