github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/protobuf/ptypes/regen.sh (about)

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