genproto.sh 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env bash
  2. #
  3. # Generate all etcd protobuf bindings.
  4. # Run from repository root.
  5. #
  6. set -e
  7. if ! [[ "$0" =~ scripts/genproto.sh ]]; then
  8. echo "must be run from repository root"
  9. exit 255
  10. fi
  11. if [[ $(protoc --version | cut -f2 -d' ') != "3.7.1" ]]; then
  12. echo "could not find protoc 3.7.1, is it installed + in PATH?"
  13. exit 255
  14. fi
  15. # directories containing protos to be built
  16. DIRS="./wal/walpb ./etcdserver/etcdserverpb ./snap/snappb ./raft/raftpb ./mvcc/mvccpb ./lease/leasepb ./auth/authpb ./etcdserver/api/v3lock/v3lockpb ./etcdserver/api/v3election/v3electionpb"
  17. # disable go mod
  18. export GO111MODULE=off
  19. # exact version of packages to build
  20. GOGO_PROTO_SHA="1adfc126b41513cc696b209667c8656ea7aac67c"
  21. GRPC_GATEWAY_SHA="92583770e3f01b09a0d3e9bdf64321d8bebd48f2"
  22. SCHWAG_SHA="b7d0fc9aadaaae3d61aaadfc12e4a2f945514912"
  23. # set up self-contained GOPATH for building
  24. export GOPATH=${PWD}/gopath.proto
  25. export GOBIN=${PWD}/bin
  26. export PATH="${GOBIN}:${PATH}"
  27. ETCD_IO_ROOT="${GOPATH}/src/github.com/coreos/etcd"
  28. ETCD_ROOT="${ETCD_IO_ROOT}/etcd"
  29. GOGOPROTO_ROOT="${GOPATH}/src/github.com/gogo/protobuf"
  30. SCHWAG_ROOT="${GOPATH}/src/github.com/hexfusion/schwag"
  31. GOGOPROTO_PATH="${GOGOPROTO_ROOT}:${GOGOPROTO_ROOT}/protobuf"
  32. GRPC_GATEWAY_ROOT="${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway"
  33. function cleanup {
  34. # Remove the whole fake GOPATH which can really confuse go mod.
  35. rm -rf "${PWD}/gopath.proto"
  36. }
  37. cleanup
  38. trap cleanup EXIT
  39. mkdir -p "${ETCD_IO_ROOT}"
  40. ln -s "${PWD}" "${ETCD_ROOT}"
  41. echo "Installing gogo/protobuf..."
  42. GOGOPROTO_ROOT="$GOPATH/src/github.com/gogo/protobuf"
  43. # rm -rf $GOGOPROTO_ROOT
  44. mkdir -p $GOPATH/src/github.com/gogo
  45. pushd $GOPATH/src/github.com/gogo
  46. git clone https://github.com/gogo/protobuf.git
  47. popd
  48. pushd "${GOGOPROTO_ROOT}"
  49. git reset --hard HEAD
  50. make install
  51. popd
  52. echo "Installing grpc-ecosystem/grpc-gateway..."
  53. GRPC_GATEWAY_ROOT="$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway"
  54. # rm -rf $GRPC_GATEWAY_ROOT
  55. mkdir -p $GOPATH/src/github.com/grpc-ecosystem
  56. go get -v -d github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
  57. go get -v -d github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
  58. pushd $GOPATH/src/github.com/grpc-ecosystem
  59. rm -rf ./grpc-gateway
  60. git clone https://github.com/grpc-ecosystem/grpc-gateway.git
  61. popd
  62. pushd "${GRPC_GATEWAY_ROOT}"
  63. git reset --hard "${GRPC_GATEWAY_SHA}"
  64. go install -v ./protoc-gen-grpc-gateway
  65. go install -v ./protoc-gen-swagger
  66. popd
  67. for dir in ${DIRS}; do
  68. pushd "${dir}"
  69. protoc --gofast_out=plugins=grpc,import_prefix=github.com/coreos/:. -I=".:${GOGOPROTO_PATH}:${ETCD_IO_ROOT}:${GRPC_GATEWAY_ROOT}/third_party/googleapis" ./*.proto
  70. sed -i.bak -E "s/github\.com\/coreos\/(gogoproto|github\.com|golang\.org|google\.golang\.org)/\1/g" ./*.pb.go
  71. sed -i.bak -E 's/github\.com\/coreos\/(errors|fmt|io|context|math\/bits)/\1/g' ./*.pb.go
  72. sed -i.bak -E 's/import _ \"gogoproto\"//g' ./*.pb.go
  73. sed -i.bak -E 's/import fmt \"fmt\"//g' ./*.pb.go
  74. sed -i.bak -E 's/import _ \"github\.com\/coreos\/google\/api\"//g' ./*.pb.go
  75. sed -i.bak -E 's/import _ \"google\.golang\.org\/genproto\/googleapis\/api\/annotations\"//g' ./*.pb.go
  76. rm -f ./*.bak
  77. goimports -w ./*.pb.go
  78. popd
  79. done