blob: 031dac1e649bdf99df1ef9495a33805047cdacb5 [file]
#!/usr/bin/env bash
set -euo pipefail
BUILDIFIER_SHORT_PATH=@@BUILDIFIER_SHORT_PATH@@
ARGS=@@ARGS@@
WORKSPACE="@@WORKSPACE@@"
function resolve_runfile() {
local path=$1
local runfiles_path=${path#../}
local candidate
local manifest
local line
if [[ -e "$path" ]]; then
realpath "$path"
return 0
fi
if [[ -n "${RUNFILES_DIR:-}" ]]; then
candidate="${RUNFILES_DIR}/${runfiles_path}"
if [[ -e "$candidate" ]]; then
realpath "$candidate"
return 0
fi
fi
for manifest in \
"${RUNFILES_MANIFEST_FILE:-}" \
"$0.runfiles_manifest" \
"$0.exe.runfiles_manifest" \
"$0.runfiles/MANIFEST"; do
[[ -n "$manifest" && -f "$manifest" ]] || continue
while IFS= read -r line; do
if [[ "$line" == "${runfiles_path} "* ]]; then
printf '%s\n' "${line#* }"
return 0
elif [[ "$line" == "$runfiles_path" ]]; then
printf '%s\n' "$runfiles_path"
return 0
fi
done < "$manifest"
done
return 1
}
if ! buildifier_short_path=$(resolve_runfile "$BUILDIFIER_SHORT_PATH"); then
echo "Unable to locate buildifier runfile: $BUILDIFIER_SHORT_PATH" >&2
exit 1
fi
# Use TEST_WORKSPACE to determine if the script is being ran under a test
if [[ -n "${TEST_WORKSPACE+x}" && -z "${BUILD_WORKSPACE_DIRECTORY+x}" ]]; then
FIND_FILE_TYPE="l"
# If WORKSPACE was provided, then the script is being run under a test in no_sandbox mode
# cd to the directory containing the WORKSPACE file
if [[ ! -z "${WORKSPACE+x}" ]]; then
FIND_FILE_TYPE="f"
WORKSPACE_PATH="$(dirname "$(realpath ${WORKSPACE})")"
if ! cd "$WORKSPACE_PATH" ; then
echo "Unable to change to workspace (WORKSPACE_PATH: ${WORKSPACE_PATH})"
fi
fi
else
# Change into the workspace directory if this is _not_ a test
if ! cd "$BUILD_WORKSPACE_DIRECTORY"; then
echo "Unable to change to workspace (BUILD_WORKSPACE_DIRECTORY: ${BUILD_WORKSPACE_DIRECTORY})"
exit 1
fi
fi
# Run buildifier on all starlark files
find . \
@@EXCLUDE_PATTERNS@@ \
-type "${FIND_FILE_TYPE:-f}" \
\( -name '*.bzl' \
-o -name '*.sky' \
-o -name '*.bazel' \
-o -name BUILD \
-o -name '*.BUILD' \
-o -name 'BUILD.*.oss' \
-o -name WORKSPACE \
-o -name WORKSPACE.bzlmod \
-o -name WORKSPACE.oss \
-o -name 'WORKSPACE.*.oss' \
\) -print | xargs "$buildifier_short_path" "${ARGS[@]}"