...
1#! /usr/bin/env bash
2
3BUILDIFIER_SHORT_PATH=@@BUILDIFIER_SHORT_PATH@@
4ARGS=@@ARGS@@
5WORKSPACE="@@WORKSPACE@@"
6
7# Get the absolute path to the buildifier executable
8buildifier_short_path=$(readlink "$BUILDIFIER_SHORT_PATH")
9
10# Use TEST_WORKSPACE to determine if the script is being ran under a test
11if [[ ! -z "${TEST_WORKSPACE+x}" && -z "${BUILD_WORKSPACE_DIRECTORY+x}" ]]; then
12 FIND_FILE_TYPE="l"
13 # If WORKSPACE was provided, then the script is being run under a test in no_sandbox mode
14 if [[ ! -z "${WORKSPACE:+x}" ]]; then
15 FIND_FILE_TYPE="f"
16
17 # resolve the WORKSPACE symlink
18 # use `realpath` if available and `readlink` otherwise (typically macOS)
19 if command -v realpath &> /dev/null; then
20 WORKSPACE_LINK="$(realpath ${WORKSPACE})"
21 elif command -v readlink &> /dev/null; then
22 WORKSPACE_LINK="$(readlink ${WORKSPACE})"
23 else
24 echo "Unable to resolve symlink (WORKSPACE: ${WORKSPACE})"
25 exit 1
26 fi
27
28 # Find the directory containing the WORKSPACE file
29 WORKSPACE_PATH="$(dirname "$WORKSPACE_LINK")"
30
31 # Change the working directory to the WORKSPACE parent
32 if ! cd "$WORKSPACE_PATH" ; then
33 echo "Unable to change to workspace (WORKSPACE_PATH: ${WORKSPACE_PATH})"
34 fi
35 fi
36else
37 # Change into the workspace directory if this is _not_ a test
38 if ! cd "$BUILD_WORKSPACE_DIRECTORY"; then
39 echo "Unable to change to workspace (BUILD_WORKSPACE_DIRECTORY: ${BUILD_WORKSPACE_DIRECTORY})"
40 exit 1
41 fi
42fi
43
44# Run buildifier on all starlark files
45find . \
46 -type "${FIND_FILE_TYPE:-f}" \
47 @@EXCLUDE_PATTERNS@@ \
48 \( -name '*.bzl' \
49 -o -name '*.sky' \
50 -o -name BUILD.bazel \
51 -o -name BUILD \
52 -o -name '*.BUILD' \
53 -o -name 'BUILD.*.bazel' \
54 -o -name 'BUILD.*.oss' \
55 -o -name MODULE.bazel \
56 -o -name WORKSPACE \
57 -o -name WORKSPACE.bazel \
58 -o -name WORKSPACE.oss \
59 -o -name WORKSPACE.bzlmod \
60 -o -name 'WORKSPACE.*.bazel' \
61 -o -name 'WORKSPACE.*.oss' \
62 \) -print | xargs "$buildifier_short_path" "${ARGS[@]}"
View as plain text