blob: a23f11b8c72d6fa6401d21bd1be8e22414ab4b8f [file] [log] [blame] [edit]
#!/usr/bin/env bash
set -euo pipefail
BUILDIFIER_SHORT_PATH=@@BUILDIFIER_SHORT_PATH@@
ARGS=@@ARGS@@
WORKSPACE="@@WORKSPACE@@"
# Get the absolute path to the buildifier executable
buildifier_short_path=$(readlink "$BUILDIFIER_SHORT_PATH")
# 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 . \
-type "${FIND_FILE_TYPE:-f}" \
@@EXCLUDE_PATTERNS@@ \
\( -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[@]}"