regen.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/bash -e
  2. #
  3. # This script fetches and rebuilds the "well-known types" 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=github.com/golang/protobuf/ptypes
  8. UPSTREAM=https://github.com/google/protobuf
  9. UPSTREAM_SUBDIR=src/google/protobuf
  10. PROTO_FILES='
  11. any.proto
  12. duration.proto
  13. empty.proto
  14. struct.proto
  15. timestamp.proto
  16. wrappers.proto
  17. '
  18. function die() {
  19. echo 1>&2 $*
  20. exit 1
  21. }
  22. # Sanity check that the right tools are accessible.
  23. for tool in go git protoc protoc-gen-go; do
  24. q=$(which $tool) || die "didn't find $tool"
  25. echo 1>&2 "$tool: $q"
  26. done
  27. tmpdir=$(mktemp -d -t regen-wkt.XXXXXX)
  28. trap 'rm -rf $tmpdir' EXIT
  29. echo -n 1>&2 "finding package dir... "
  30. pkgdir=$(go list -f '{{.Dir}}' $PKG)
  31. echo 1>&2 $pkgdir
  32. base=$(echo $pkgdir | sed "s,/$PKG\$,,")
  33. echo 1>&2 "base: $base"
  34. cd $base
  35. echo 1>&2 "fetching latest protos... "
  36. git clone -q $UPSTREAM $tmpdir
  37. # Pass 1: build mapping from upstream filename to our filename.
  38. declare -A filename_map
  39. for f in $(cd $PKG && find * -name '*.proto'); do
  40. echo -n 1>&2 "looking for latest version of $f... "
  41. up=$(cd $tmpdir/$UPSTREAM_SUBDIR && find * -name $(basename $f) | grep -v /testdata/)
  42. echo 1>&2 $up
  43. if [ $(echo $up | wc -w) != "1" ]; then
  44. die "not exactly one match"
  45. fi
  46. filename_map[$up]=$f
  47. done
  48. # Pass 2: copy files, making necessary adjustments.
  49. for up in "${!filename_map[@]}"; do
  50. f=${filename_map[$up]}
  51. shortname=$(basename $f | sed 's,\.proto$,,')
  52. cat $tmpdir/$UPSTREAM_SUBDIR/$up |
  53. # Adjust proto package.
  54. # TODO(dsymonds): Remove when the right go_package options are upstream.
  55. sed '/^package /a option go_package = "github.com\/golang\/protobuf\/ptypes\/'${shortname}'";' |
  56. # Unfortunately "package struct" and "package type" don't work.
  57. sed '/option go_package/s,struct",struct;structpb",' |
  58. cat > $PKG/$f
  59. done
  60. # Run protoc once per package.
  61. for dir in $(find $PKG -name '*.proto' | xargs dirname | sort | uniq); do
  62. echo 1>&2 "* $dir"
  63. protoc --go_out=. $dir/*.proto
  64. done
  65. echo 1>&2 "All OK"