protoc.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/bin/bash
  2. DEFAULT_PROTOC_GEN="gogofast"
  3. DEFAULT_PROTOC="protoc"
  4. GO_COMMON_DIR_NAME="go-common"
  5. USR_INCLUDE_DIR="/usr/local/include"
  6. function _install_protoc() {
  7. osname=$(uname -s)
  8. echo "install protoc ..."
  9. case $osname in
  10. "Darwin" )
  11. brew install protobuf
  12. ;;
  13. *)
  14. echo "unknown operating system, need install protobuf manual see: https://developers.google.com/protocol-buffers"
  15. exit 1
  16. ;;
  17. esac
  18. }
  19. function _install_protoc_gen() {
  20. local protoc_gen=$1
  21. case $protoc_gen in
  22. "gofast" )
  23. echo "install gofast from github.com/gogo/protobuf/protoc-gen-gofast"
  24. go get github.com/gogo/protobuf/protoc-gen-gofast
  25. ;;
  26. "gogofast" )
  27. echo "install gogofast from github.com/gogo/protobuf/protoc-gen-gogofast"
  28. go get github.com/gogo/protobuf/protoc-gen-gogofast
  29. ;;
  30. "gogo" )
  31. echo "install gogo from github.com/gogo/protobuf/protoc-gen-gogo"
  32. go get github.com/gogo/protobuf/protoc-gen-gogo
  33. ;;
  34. "go" )
  35. echo "install protoc-gen-go from github.com/golang/protobuf"
  36. go get github.com/golang/protobuf/{proto,protoc-gen-go}
  37. ;;
  38. *)
  39. echo "can't install protoc-gen-${protoc_gen} automatic !"
  40. exit 1;
  41. ;;
  42. esac
  43. }
  44. function _find_go_common_dir() {
  45. local go_common_dir_name=$1
  46. local current_dir=$(pwd)
  47. while [[ "$(basename $current_dir)" != "$go_common_dir_name" ]]; do
  48. current_dir=$(dirname $current_dir)
  49. if [[ "$current_dir" == "/" || "$current_dir" == "." || -z "$current_dir" ]]; then
  50. return 1
  51. fi
  52. done
  53. echo $current_dir
  54. }
  55. function _fix_pb_file() {
  56. local target_dir=$1
  57. echo "fix pb file"
  58. local pb_files=$(find $target_dir -name "*.pb.go" -type f)
  59. local pkg_name_esc=$(echo "$target_dir" | sed 's_/_\\/_g')
  60. for file in $pb_files; do
  61. echo "fix pb file $file"
  62. if [[ $(uname -s) == 'Darwin' ]]; then
  63. sed -i "" -e "s/^import \(.*\) \"app\/\(.*\)\"/import \1 \"go-common\/app\/\2\"/g" $file
  64. else
  65. sed -i"" -E "s/^import\s*(.*)\s*\"app\/(.*)\"/import\1\"go-common\/app\/\2\"/g" $file
  66. fi
  67. done
  68. }
  69. function _esc_string() {
  70. echo $(echo "$1" | sed 's_/_\\/_g')
  71. }
  72. function _run_protoc() {
  73. local proto_dir=$1
  74. local proto_files=$(find $proto_dir -maxdepth 1 -name "*.proto")
  75. if [[ -z $proto_files ]]; then
  76. return
  77. fi
  78. local protoc_cmd="$PROTOC -I$PROTO_PATH --${PROTOC_GEN}_out=plugins=grpc:. ${proto_files}"
  79. echo $protoc_cmd
  80. $protoc_cmd
  81. }
  82. if [[ -z $PROTOC ]]; then
  83. PROTOC=${DEFAULT_PROTOC}
  84. which $PROTOC
  85. if [[ "$?" -ne "0" ]]; then
  86. _install_protoc
  87. fi
  88. fi
  89. if [[ -z $PROTOC_GEN ]]; then
  90. PROTOC_GEN=${DEFAULT_PROTOC_GEN}
  91. which protoc-gen-$PROTOC_GEN
  92. if [[ "$?" -ne "0" ]]; then
  93. _install_protoc_gen $PROTOC_GEN
  94. fi
  95. fi
  96. GO_COMMON_DIR=$(_find_go_common_dir $GO_COMMON_DIR_NAME)
  97. if [[ "$?" != "0" ]]; then
  98. echo "can't find go-common directoy"
  99. exit 1
  100. fi
  101. if [[ -z $PROTO_PATH ]]; then
  102. PROTO_PATH=$GO_COMMON_DIR:$GO_COMMON_DIR/vendor:$USR_INCLUDE_DIR
  103. else
  104. PROTO_PATH=$PROTO_PATH:$GO_COMMON_DIR:$GO_COMMON_DIR/vendor:$USR_INCLUDE_DIR
  105. fi
  106. if [[ ! -z $1 ]]; then
  107. cd $1
  108. fi
  109. TARGET_DIR=$(pwd)
  110. GO_COMMON_DIR_ESC=$(_esc_string "$GO_COMMON_DIR/")
  111. TARGET_DIR=${TARGET_DIR//$GO_COMMON_DIR_ESC/}
  112. # switch to go_common
  113. cd $GO_COMMON_DIR
  114. DIRS=$(find $TARGET_DIR -type d)
  115. for dir in $DIRS; do
  116. echo "run protoc in $dir"
  117. _run_protoc $dir
  118. done
  119. _fix_pb_file $TARGET_DIR