schema.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/bin/bash
  2. set -e
  3. export PROTOBUF_ROOT=$HOME/.local
  4. export WORKSPACE=$PWD
  5. # -----------------------------------------------------------------------------
  6. function generate_grpc_by_lang() {
  7. local target=$WORKSPACE/sdk/$1
  8. echo "generate code for grpc-$1"
  9. if [ -d $target ]
  10. then
  11. rm -r $target
  12. fi
  13. mkdir -p $target
  14. $PROTOBUF_ROOT/bin/protoc -I $WORKSPACE/protocols \
  15. -I $PROTOBUF_ROOT/include/google/protobuf \
  16. --${1}_out=$target --grpc_out=$target \
  17. --plugin=protoc-gen-grpc=$PROTOBUF_ROOT/bin/grpc_${1}_plugin \
  18. $WORKSPACE/protocols/*.proto
  19. }
  20. function generate_flatbuffers(){
  21. echo "generate flatbuffers"
  22. flatc --rust -o $WORKSPACE/src/$2.rs $WORKSPACE/protocols/$1.fbs
  23. }
  24. # https://github.com/grpc/grpc-web#code-generator-plugin
  25. function generate_grpc_web() {
  26. echo "generate code for grpc-web"
  27. local target=$1/src/protocols
  28. if [ -d $target ]
  29. then
  30. rm -r $target
  31. fi
  32. mkdir -p $target
  33. $PROTOBUF_ROOT/bin/protoc -I $WORKSPACE/protocols \
  34. -I $PROTOBUF_ROOT/include/google/protobuf \
  35. --js_out=import_style=commonjs,binary:$target \
  36. --grpc-web_out=import_style=typescript,mode=grpcweb:$target \
  37. $WORKSPACE/protocols/*.proto
  38. }
  39. function generate_for_morus() {
  40. echo "generate code for morus project"
  41. local target=$WORKSPACE/morus/morus
  42. local -a folders=(
  43. "GPBMetadata"
  44. "Mint"
  45. "Palm"
  46. )
  47. for f in "${folders[@]}"
  48. do
  49. local t=$target/$f
  50. if [ -d $t ]
  51. then
  52. rm -r $t
  53. fi
  54. done
  55. $PROTOBUF_ROOT/bin/protoc -I $WORKSPACE/protocols \
  56. -I $PROTOBUF_ROOT/include/google/protobuf \
  57. --php_out=$target --grpc_out=generate_server:$target \
  58. --plugin=protoc-gen-grpc=$PROTOBUF_ROOT/bin/grpc_php_plugin \
  59. $WORKSPACE/protocols/morus.proto
  60. }
  61. function generate_for_lily() {
  62. echo "generate code for lily project"
  63. local target=$WORKSPACE/lily/lily/palm
  64. local -a files=(
  65. "lily_pb2.py"
  66. "lily_pb2_grpc.py"
  67. )
  68. for f in "${files[@]}"
  69. do
  70. local t=$target/$f
  71. if [ -f $t ]
  72. then
  73. rm $t
  74. fi
  75. done
  76. $PROTOBUF_ROOT/bin/protoc -I $WORKSPACE/protocols \
  77. -I $PROTOBUF_ROOT/include/google/protobuf \
  78. --python_out=$target --grpc_out=$target \
  79. --plugin=protoc-gen-grpc=$PROTOBUF_ROOT/bin/grpc_python_plugin \
  80. $WORKSPACE/protocols/lily.proto
  81. sed -i 's/import lily_/from . import lily_/g' $target/lily_pb2_grpc.py
  82. }
  83. function generate_grpc_for_php() {
  84. if [ -d $1 ]
  85. then
  86. rm -r $1
  87. fi
  88. mkdir -p $1
  89. $PROTOBUF_ROOT/bin/protoc -I $WORKSPACE/protocols \
  90. -I $PROTOBUF_ROOT/include/google/protobuf \
  91. --php_out=$1 --grpc_out=generate_server:$1 \
  92. --plugin=protoc-gen-grpc=$PROTOBUF_ROOT/bin/grpc_php_plugin \
  93. $WORKSPACE/protocols/*.proto
  94. }
  95. # -----------------------------------------------------------------------------
  96. declare -a languages=(
  97. "python"
  98. "ruby"
  99. "cpp"
  100. "csharp"
  101. "java"
  102. )
  103. for l in "${languages[@]}"
  104. do
  105. generate_grpc_by_lang $l
  106. done
  107. generate_grpc_for_php $WORKSPACE/sdk/php
  108. generate_for_morus
  109. generate_for_lily
  110. generate_grpc_web $WORKSPACE/../dashboard
  111. # ----------------------------------------------------------
  112. echo 'done.'
  113. exit 0