-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuildifier.sh
executable file
·64 lines (53 loc) · 1.75 KB
/
buildifier.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
set -eu
# Install buildifier
if ! curl -f -L -o /usr/bin/buildifier \
"https://github.com/bazelbuild/buildtools/releases/download/${INPUT_BUILDIFIER_VERSION}/buildifier-linux-amd64"; then
echo "Could not download buildifier ${INPUT_BUILDIFIER_VERSION}"
exit 1
fi
chmod +x /usr/bin/buildifier
buildifier --version
declare -a BUILDIFIER_ARGS=(
"--lint=warn"
"--mode=check"
)
if [[ -n "${INPUT_WARNINGS:-""}" ]]; then
BUILDIFIER_ARGS+=("--warnings=${INPUT_WARNINGS}")
fi
readonly BUILDIFIER_ARGS
# If defaults are used for filetypes and excludes, just do a recursive check.
if [[ -z "${INPUT_FILETYPES:-""}" && -z "${INPUT_EXCLUDES:-""}" ]]; then
set -x
buildifier "${BUILDIFIER_ARGS[@]}" -r .
exit 0
fi
declare -a FILETYPES
IFS=',' read -r -a FILETYPES <<< \
"${INPUT_FILETYPES:-"BUILD,bzl,sky,bzl,WORKSPACE,bazel"}"
readonly FILETYPES
# Generate find args for filetypes using -iname
declare FIND_NAME_ARGS
FIND_NAME_ARGS="$(printf -- "-iname %s -o " "${FILETYPES[@]}")"
# Remove the last ' -o ' 4 characters, they are only needed in between args
FIND_NAME_ARGS="${FIND_NAME_ARGS::-4}"
readonly FIND_NAME_ARGS
# Generate find args for any path exclusions using '-not -path'
declare FIND_PATH_EXCLUDES
if [[ -n "${INPUT_EXCLUDES:-""}" ]]; then
declare EXCLUDES=()
IFS=',' read -r -a EXCLUDES <<< "${INPUT_EXCLUDES:-""}"
readonly EXCLUDES
FIND_PATH_EXCLUDES="$(printf -- "-not -path \"%s\" " "${EXCLUDES[@]}")"
fi
readonly FIND_PATH_EXCLUDES
# Construct our full find command to get the bazel files we want to pass to
# buildifier
declare -a BAZEL_FILES
BAZEL_FILES=(\
$(eval "find . -type f \( ${FIND_NAME_ARGS} \) ${FIND_PATH_EXCLUDES}"))
readonly BAZEL_FILES
set -x
buildifier "${BUILDIFIER_ARGS[@]}" "${BAZEL_FILES[@]}"
set +x
echo "Success!"