github.com/kaydxh/golang@v0.0.131/script/go_proto_gen.sh (about) 1 # Created by kayxhding on 2020-10-11 12:40:37 2 #!/usr/bin/env bash 3 4 # exit by command return non-zero exit code 5 set -o errexit 6 # Indicate an error when it encounters an undefined variable 7 set -o nounset 8 # Fail on any error. 9 set -o pipefail 10 #set -o xtrace 11 12 # example, generate golang proto files 13 # bash go_proto_gen.sh -I . --proto_file_path pkg/webserver/webserver.proto --with-go 14 15 # if script called by source, $0 is the name of father script, not the name of source run script 16 SCRIPT_PATH=$(cd `dirname "${BASH_SOURCE[0]}"`;pwd) 17 18 <<'COMMENT' 19 SCRIPT=$(readlink -f "${BASH_SOURCE[0]}") 20 SCRIPT_PATH=$(dirname "$SCRIPT") 21 echo ${SCRIPT_PATH} 22 COMMENT 23 24 PROTOC_FILE_DIR= 25 PROTO_HEADERS= 26 # THIRD_PARTY_DIR=$(realpath "${2:-${SCRIPT_PATH}/../../third_party}") 27 THIRD_PARTY_DIR="${SCRIPT_PATH}/third_party" 28 WITH_DOC= 29 WITH_CPP= 30 WITH_GO= 31 32 function die() { 33 echo 1>&2 "$*" 34 exit 1 35 } 36 37 function getopts() { 38 local -a protodirs 39 while test $# -ne 0 40 do 41 case "$1" in 42 -I|--proto_path) 43 protodirs+=( 44 "-I $(realpath "$2")" 45 ) 46 shift 47 ;; 48 --third_party_path) 49 THIRD_PARTY_DIR=$(realpath "$2") 50 shift 51 ;; 52 --with-doc) 53 WITH_DOC=1 54 ;; 55 --proto_file_path) 56 PROTOC_FILE_DIR=$(realpath "$2") 57 shift 58 ;; 59 --with-cpp) 60 WITH_CPP=1 61 ;; 62 --with-go) 63 WITH_GO=1 64 ;; 65 esac 66 shift 67 done 68 69 PROTO_HEADERS="${protodirs[*]}" 70 # echo "${protodirs[*]}" 71 } 72 73 <<'COMMENT' 74 # This will place three binaries in your $GOBIN 75 # Make sure that your $GOBIN is in your $PATH 76 # install protoc-gen-doc on mac=> https: 77 # github.com/pseudomuto/protoc-gen-doc/issues/20 (make build, cp bin/protoc-gen-doc ${GOBIN}) 78 go install \ 79 github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \ 80 github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \ 81 google.golang.org/protobuf/cmd/protoc-gen-go \ 82 google.golang.org/grpc/cmd/protoc-gen-go-grpc \ 83 github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc \ 84 github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger 85 COMMENT 86 87 echo `pwd` 88 89 getopts $@ 90 91 echo "==> Checking tools..." 92 #GEN_PROTO_TOOLS=(protoc protoc-gen-go protoc-gen-grpc-gateway protoc-gen-govalidators) 93 GEN_PROTO_TOOLS=(protoc protoc-gen-go protoc-gen-grpc-gateway) 94 for tool in ${GEN_PROTO_TOOLS[@]}; do 95 q=$(command -v ${tool}) || die "didn't find ${tool}" 96 echo 1>&2 "${tool}: ${q}" 97 done 98 99 100 echo "==> Generating proto..." 101 #proto_headers="-I ${SCRIPT_PATH}/../../third_party" 102 #proto_headers="-I .. -I ${THIRD_PARTY_DIR}" 103 # "-I ." need behind PROTO_HEADERS, or remove it 104 proto_headers="${PROTO_HEADERS} -I `pwd`" 105 proto_headers="${proto_headers} -I ${THIRD_PARTY_DIR}/github.com/grpc-ecosystem/grpc-gateway" 106 source_relative_option="paths=source_relative:." 107 go_opt_option="" 108 go_out_option="" 109 go_tag_option="" 110 go_grpc_option="" 111 doc_option="" 112 doc_out_option="" 113 cpp_option="" 114 cpp_out_option="" 115 cpp_grpc_option="" 116 grpc_gateway_option="" 117 grpc_gateway_out_option="--grpc-gateway_out=logtostderr=true" 118 grpc_gateway_delete_option="--grpc-gateway_opt=allow_delete_body=true" 119 120 for proto in $(find ${PROTOC_FILE_DIR} -type f -name '*.proto' -print0 | xargs -0); do 121 echo "Generating ${proto}" 122 proto_base_name="$(basename ${proto} .proto)" 123 api_conf_yaml_base_name="${proto_base_name}.yaml" 124 api_conf_yaml_dir="$(dirname ${proto})" 125 api_conf_yaml="${api_conf_yaml_dir}/$api_conf_yaml_base_name" 126 grpc_api_yaml_option="" 127 grpc_gateway_option="" 128 129 if [[ -f "${api_conf_yaml}" ]];then 130 grpc_api_yaml_option="grpc_api_configuration=${api_conf_yaml},${source_relative_option}" 131 grpc_gateway_option="${grpc_gateway_out_option},${grpc_api_yaml_option} ${grpc_gateway_delete_option}" 132 fi 133 134 if [[ "${WITH_DOC}" -eq 1 ]]; then 135 # output file name 136 doc_option="--doc_opt=markdown,${proto_base_name}.md" 137 doc_out_option="--doc_out=${SCRIPT_PATH}/../doc" 138 fi 139 140 if [[ "${WITH_CPP}" -eq 1 ]]; then 141 cpp_option="--cpp_out=." 142 cpp_out_option="--grpc_out=." 143 cpp_grpc_option="--plugin=protoc-gen-grpc=`which grpc_cpp_plugin`" 144 fi 145 146 if [[ "${WITH_GO}" -eq 1 ]]; then 147 # go_tag_option="--go-tag_out=${source_relative_option}" 148 go_out_option="--go_out=." 149 go_opt_option="--go_opt=paths=source_relative" 150 go_grpc_option="--go-grpc_out=${source_relative_option}" 151 fi 152 153 protoc ${proto_headers} ${go_out_option} ${go_tag_option} ${go_opt_option} ${go_grpc_option} ${grpc_gateway_option} ${cpp_out_option} ${cpp_option} ${doc_option} ${doc_out_option} ${cpp_grpc_option} "${proto}" 154 #protoc -I . ${proto_headers} --go-tag_out=paths=source_relative:. --go-grpc_out=paths=source_relative:. --grpc-gateway_out=logtostderr=true,grpc_api_configuration=${api_conf_yaml},paths=source_relative:. --grpc-gateway_opt=allow_delete_body=true ${f} 155 done