regen.sh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash -e
  2. #
  3. # This script rebuilds the generated code for the protocol buffers.
  4. # To run this you will need protoc and goprotobuf installed;
  5. # see https://github.com/golang/protobuf for instructions.
  6. # You also need Go and Git installed.
  7. PKG=google.golang.org/cloud/bigtable
  8. UPSTREAM=https://github.com/GoogleCloudPlatform/cloud-bigtable-client
  9. UPSTREAM_SUBDIR=bigtable-protos/src/main/proto
  10. function die() {
  11. echo 1>&2 $*
  12. exit 1
  13. }
  14. # Sanity check that the right tools are accessible.
  15. for tool in go git protoc protoc-gen-go; do
  16. q=$(which $tool) || die "didn't find $tool"
  17. echo 1>&2 "$tool: $q"
  18. done
  19. tmpdir=$(mktemp -d -t regen-cbt.XXXXXX)
  20. trap 'rm -rf $tmpdir' EXIT
  21. echo -n 1>&2 "finding package dir... "
  22. pkgdir=$(go list -f '{{.Dir}}' $PKG)
  23. echo 1>&2 $pkgdir
  24. base=$(echo $pkgdir | sed "s,/$PKG\$,,")
  25. echo 1>&2 "base: $base"
  26. cd $base
  27. echo 1>&2 "fetching latest protos... "
  28. git clone -q $UPSTREAM $tmpdir
  29. # Pass 1: build mapping from upstream filename to our filename.
  30. declare -A filename_map
  31. for f in $(cd $PKG && find internal -name '*.proto'); do
  32. echo -n 1>&2 "looking for latest version of $f... "
  33. up=$(cd $tmpdir/$UPSTREAM_SUBDIR && find * -name $(basename $f))
  34. echo 1>&2 $up
  35. if [ $(echo $up | wc -w) != "1" ]; then
  36. die "not exactly one match"
  37. fi
  38. filename_map[$up]=$f
  39. done
  40. # Pass 2: build sed script for fixing imports.
  41. import_fixes=$tmpdir/fix_imports.sed
  42. for up in "${!filename_map[@]}"; do
  43. f=${filename_map[$up]}
  44. echo >>$import_fixes "s,\"$up\",\"$PKG/$f\","
  45. done
  46. cat $import_fixes | sed 's,^,### ,' 1>&2
  47. # Pass 3: copy files, making necessary adjustments.
  48. for up in "${!filename_map[@]}"; do
  49. f=${filename_map[$up]}
  50. cat $tmpdir/$UPSTREAM_SUBDIR/$up |
  51. # Adjust proto imports.
  52. sed -f $import_fixes |
  53. # Drop the UndeleteCluster RPC method. It returns a google.longrunning.Operation.
  54. sed '/^ rpc UndeleteCluster(/,/^ }$/d' |
  55. # Drop annotations, long-running operations and timestamps. They aren't supported (yet).
  56. sed '/"google\/longrunning\/operations.proto"/d' |
  57. sed '/google.longrunning.Operation/d' |
  58. sed '/"google\/protobuf\/timestamp.proto"/d' |
  59. sed '/google\.protobuf\.Timestamp/d' |
  60. sed '/"google\/api\/annotations.proto"/d' |
  61. sed '/option.*google\.api\.http.*{.*};$/d' |
  62. cat > $PKG/$f
  63. done
  64. # Run protoc once per package.
  65. for dir in $(find $PKG/internal -name '*.proto' | xargs dirname | sort | uniq); do
  66. echo 1>&2 "* $dir"
  67. protoc --go_out=plugins=grpc:. $dir/*.proto
  68. done
  69. echo 1>&2 "All OK"