github.com/google/cloudprober@v0.11.3/tools/gen_pb_go.sh (about)

     1  #!/bin/bash -eu
     2  #
     3  # Copyright 2017 Google Inc.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #      http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # This script generates Go code for the config protobufs.
    18  
    19  PROTOC_VERSION="3.17.3"
    20  PROJECT="github.com/google/cloudprober"
    21  
    22  GOPATH=$(go env GOPATH)
    23  
    24  if [ -z "$GOPATH" ]; then
    25    echo "Go environment is not setup correctly. Please look at"
    26    echo "https://golang.org/doc/code.html to set up Go environment."
    27    exit 1
    28  fi
    29  echo GOPATH=${GOPATH}
    30  
    31  if [ -z ${PROJECTROOT+x} ]; then
    32    # If PROJECTROOT is not set, try to determine it from script's location
    33    SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    34    if [[ $SCRIPTDIR == *"$PROJECT/tools"* ]]; then
    35      PROJECTROOT="${SCRIPTDIR}/../../../.."
    36    else
    37      echo "PROJECTROOT is not set and we are not able to determine PROJECTROOT"
    38      echo "from script's path. PROJECTROOT should be set such that project files "
    39      echo " are located at $PROJECT relative to the PROJECTROOT."
    40      exit 1
    41    fi
    42  fi
    43  echo PROJECTROOT=${PROJECTROOT}
    44  
    45  # Change directory to the project workspace in GOPATH
    46  project_dir="${PROJECTROOT}/${PROJECT}"
    47  
    48  if [ ! -d "${project_dir}" ];then
    49    echo "${PROJECT} not found under Go workspace: ${GOPATH}/src. Please download"
    50    echo " cloudprober source code from github.com/google/cloudprober and set it "
    51    echo "such that it's available at ${project_dir}."
    52    exit 1
    53  fi
    54  
    55  # Make sure protobuf compilation is set up correctly.
    56  export protoc_path=$(which protoc)
    57  if [ -z ${protoc_path} ] || [ ! -x  ${protoc_path} ]; then
    58    echo "protoc (protobuf compiler) not found on the path. Trying to install it "
    59    echo "from the internet. To avoid repeating this step, please install protoc"
    60    echo " from https://github.com/google/protobuf, at a system-wide location, "
    61    echo "that is accessible through PATH environment variable."
    62    echo "======================================================================"
    63    sleep 1
    64  
    65    if [ "$(uname -s)" == "Darwin" ]; then
    66      os="osx"
    67    elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
    68      os="linux"
    69    else
    70      echo "OS unsupported by this this build script. Please install protoc manually."
    71    fi
    72    arch=$(uname -m)
    73    protoc_package="protoc-${PROTOC_VERSION}-${os}-${arch}.zip"
    74    protoc_package_url="https://github.com/google/protobuf/releases/download/v${PROTOC_VERSION}/${protoc_package}"
    75  
    76    TMPDIR=$(mktemp -d)
    77    cd $TMPDIR
    78    echo -e "Downloading protobuf compiler from..\n${protoc_package_url}"
    79    echo "======================================================================"
    80    wget "${protoc_package_url}"
    81    unzip "${protoc_package}"
    82    export protoc_path=${PWD}/bin/protoc
    83    cd -
    84  
    85    function cleanup {
    86      echo "Removing temporary directory used for protoc installation: ${TMPDIR}"
    87      rm  -r "${TMPDIR}"
    88    }
    89    trap cleanup EXIT
    90  fi
    91  
    92  # Get go plugin for protoc
    93  go install github.com/golang/protobuf/protoc-gen-go
    94  
    95  echo "Generating Go code for protobufs.."
    96  echo "======================================================================"
    97  # Generate protobuf code from the root directory to ensure proper import paths.
    98  cd $PROJECTROOT
    99  find $PROJECT -type d | \
   100    while read -r dir
   101    do
   102      # Ignore directories with no proto files.
   103      ls ${dir}/*.proto > /dev/null 2>&1 || continue
   104      ${protoc_path} --go_out=plugins=grpc:. ${dir}/*.proto
   105    done
   106  cd -