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