|
| 1 | +#! /bin/bash |
| 2 | +# Copyright 2019 The gRPC Authors |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +set -e |
| 17 | + |
| 18 | +BUILDOZER_VERSION="4.2.2" |
| 19 | +TEMP_BUILDOZER_PATH="/tmp/buildozer-for-grpc" |
| 20 | + |
| 21 | +function error_handling() { |
| 22 | + error=$1 |
| 23 | + if [[ -x "$error" ]]; then |
| 24 | + echo "${error}" |
| 25 | + exit 1 |
| 26 | + fi |
| 27 | +} |
| 28 | + |
| 29 | +function download_buildozer() { |
| 30 | + platform="$(uname -s)" |
| 31 | + case "${platform}" in |
| 32 | + Linux*) download_link="https://github.com/bazelbuild/buildtools/releases/download/${BUILDOZER_VERSION}/buildozer-linux-amd64";; |
| 33 | + Darwin*) download_link="https://github.com/bazelbuild/buildtools/releases/download/${BUILDOZER_VERSION}/buildozer-darwin-amd64";; |
| 34 | + *) error_handling "Unsupported platform: ${platform}";; |
| 35 | + esac |
| 36 | + |
| 37 | + if [ -x "$(command -v curl)" ]; then |
| 38 | + curl -L -o ${TEMP_BUILDOZER_PATH} ${download_link} |
| 39 | + elif [ -x "$(command -v wget)" ]; then |
| 40 | + wget -O ${TEMP_BUILDOZER_PATH} ${download_link} |
| 41 | + else |
| 42 | + error_handling "Download failed: curl and wget not available" |
| 43 | + fi |
| 44 | + |
| 45 | + chmod +x ${TEMP_BUILDOZER_PATH} |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +# Get the correct version of buildozer |
| 50 | +if [ -x "$(command -v buildozer)" ]; then |
| 51 | + existing_buildozer_version="$(buildozer -version 2>&1 | head -n1 | cut -d" " -f3)" |
| 52 | + if [[ "${existing_buildozer_version}" != "${BUILDOZER_VERSION}" ]]; then |
| 53 | + download_buildozer |
| 54 | + buildozer_bin="${TEMP_BUILDOZER_PATH}" |
| 55 | + else |
| 56 | + buildozer_bin="buildozer" |
| 57 | + fi |
| 58 | +else |
| 59 | + if [ -x ${TEMP_BUILDOZER_PATH} ]; then |
| 60 | + existing_buildozer_version="$(${TEMP_BUILDOZER_PATH} -version 2>&1 | head -n1 | cut -d" " -f3)" |
| 61 | + if [[ "${existing_buildozer_version}" != "${BUILDOZER_VERSION}" ]]; then |
| 62 | + download_buildozer |
| 63 | + fi |
| 64 | + else |
| 65 | + download_buildozer |
| 66 | + fi |
| 67 | + buildozer_bin="${TEMP_BUILDOZER_PATH}" |
| 68 | +fi |
| 69 | + |
| 70 | +# cd to repo root |
| 71 | +dir=$(dirname "${0}") |
| 72 | +cd "${dir}/../.." |
| 73 | + |
| 74 | +set -ex |
| 75 | + |
| 76 | +# shellcheck disable=SC2086,SC2068 |
| 77 | +${buildozer_bin} "$@" |
0 commit comments