| 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 |
BUILDIFIER_VERSION="4.2.2" |
| 19 |
TEMP_BUILDIFIER_PATH="/tmp/buildifier" |
| 20 |
EXTRA_BUILDIFIER_FLAGS="$*" |
| 21 |
|
| 22 |
function error_handling() { |
| 23 |
error=$1 |
| 24 |
if [[ -x "$error" ]]; then |
| 25 |
echo "${error}" |
| 26 |
exit 1 |
| 27 |
fi |
| 28 |
} |
| 29 |
|
| 30 |
function download_buildifier() { |
| 31 |
platform="$(uname -s)" |
| 32 |
case "${platform}" in |
| 33 |
Linux*) download_link="https://github.com/bazelbuild/buildtools/releases/download/${BUILDIFIER_VERSION}/buildifier";; |
| 34 |
Darwin*) download_link="https://github.com/bazelbuild/buildtools/releases/download/${BUILDIFIER_VERSION}/buildifier.mac";; |
| 35 |
*) error_handling "Unsupported platform: ${platform}";; |
| 36 |
esac |
| 37 |
|
| 38 |
if [ -x "$(command -v curl)" ]; then |
| 39 |
curl -L -o ${TEMP_BUILDIFIER_PATH} ${download_link} |
| 40 |
elif [ -x "$(command -v wget)" ]; then |
| 41 |
wget -O ${TEMP_BUILDIFIER_PATH} ${download_link} |
| 42 |
else |
| 43 |
error_handling "Download failed: curl and wget not available" |
| 44 |
fi |
| 45 |
|
| 46 |
chmod +x ${TEMP_BUILDIFIER_PATH} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
# Get the correct version of buildifier |
| 51 |
if [ -x "$(command -v buildifier)" ]; then |
| 52 |
existing_buildifier_version="$(buildifier -version 2>&1 | head -n1 | cut -d" " -f3)" |
| 53 |
if [[ "${existing_buildifier_version}" != "${BUILDIFIER_VERSION}" ]]; then |
| 54 |
download_buildifier |
| 55 |
buildifier_bin="${TEMP_BUILDIFIER_PATH}" |
| 56 |
else |
| 57 |
buildifier_bin="buildifier" |
| 58 |
fi |
| 59 |
else |
| 60 |
download_buildifier |
| 61 |
buildifier_bin="${TEMP_BUILDIFIER_PATH}" |
| 62 |
fi |
| 63 |
|
| 64 |
# cd to repo root |
| 65 |
dir=$(dirname "${0}") |
| 66 |
cd "${dir}/../.." |
| 67 |
|
| 68 |
bazel_files=$(find . \( -iname 'BUILD' -o -iname '*.bzl' -o -iname '*.bazel' -o -iname 'WORKSPACE' \) -type f -not -path "./third_party/*") |
| 69 |
# shellcheck disable=SC2086,SC2068 |
| 70 |
${buildifier_bin} ${EXTRA_BUILDIFIER_FLAGS[@]} -v ${bazel_files} |