1#!/bin/bash
2
3set -e
4
5# Install the working tree's protoc-gen-gen in a tempdir.
6tmpdir=$(mktemp -d -t regen-wkt.XXXXXX)
7trap 'rm -rf $tmpdir' EXIT
8mkdir -p $tmpdir/bin
9PATH=$tmpdir/bin:$PATH
10GOBIN=$tmpdir/bin go install ./protoc-gen-go
11
12# Public imports require at least Go 1.9.
13supportTypeAliases=""
14if go list -f '{{context.ReleaseTags}}' runtime | grep -q go1.9; then
15  supportTypeAliases=1
16fi
17
18# Generate various test protos.
19PROTO_DIRS=(
20  jsonpb/jsonpb_test_proto
21  proto
22  protoc-gen-go/testdata
23)
24for dir in ${PROTO_DIRS[@]}; do
25  for p in `find $dir -name "*.proto"`; do
26    if [[ $p == */import_public/* && ! $supportTypeAliases ]]; then
27      echo "# $p (skipped)"
28      continue;
29    fi
30    echo "# $p"
31    protoc -I$dir --go_out=plugins=grpc,paths=source_relative:$dir $p
32  done
33done
34
35# Deriving the location of the source protos from the path to the
36# protoc binary may be a bit odd, but this is what protoc itself does.
37PROTO_INCLUDE=$(dirname $(dirname $(which protoc)))/include
38
39# Well-known types.
40WKT_PROTOS=(any duration empty struct timestamp wrappers)
41for p in ${WKT_PROTOS[@]}; do
42  echo "# google/protobuf/$p.proto"
43  protoc --go_out=paths=source_relative:$tmpdir google/protobuf/$p.proto
44  cp $tmpdir/google/protobuf/$p.pb.go ptypes/$p
45  cp $PROTO_INCLUDE/google/protobuf/$p.proto ptypes/$p
46done
47
48# descriptor.proto.
49echo "# google/protobuf/descriptor.proto"
50protoc --go_out=paths=source_relative:$tmpdir google/protobuf/descriptor.proto
51cp $tmpdir/google/protobuf/descriptor.pb.go protoc-gen-go/descriptor
52cp $PROTO_INCLUDE/google/protobuf/descriptor.proto protoc-gen-go/descriptor
53