|
13 | 13 | # See the License for the specific language governing permissions and |
14 | 14 | # limitations under the License. |
15 | 15 |
|
16 | | -# For integration tests, we want to be able to glob() up the sources inside a nested package |
17 | | -# See explanation in .bazelrc |
18 | | -# |
19 | | -# This script ensures that we only delete subtrees that have something a file |
20 | | -# signifying a new bazel workspace, whether it be bzlmod or classic. Generic |
21 | | -# algorithm: |
22 | | -# 1. Get all directories where a WORKSPACE or MODULE.bazel exists. |
23 | | -# 2. For each of the directories, get all directories that contains a BUILD.bazel file. |
24 | | -# 3. Sort and remove duplicates. |
25 | | - |
26 | | -set -euxo pipefail |
27 | | - |
28 | | -DIR="$(dirname $0)/.." |
29 | | -cd $DIR |
30 | | - |
31 | | -# The sed -i.bak pattern is compatible between macos and linux |
32 | | -sed -i.bak "/^[^#].*--deleted_packages/s#=.*#=$(\ |
33 | | - find examples/*/* tests/*/* \( -name WORKSPACE -or -name MODULE.bazel \) | |
34 | | - xargs -n 1 dirname | |
35 | | - xargs -n 1 -I{} find {} \( -name BUILD -or -name BUILD.bazel \) | |
36 | | - xargs -n 1 dirname | |
37 | | - sort -u | |
38 | | - paste -sd, -\ |
39 | | -)#" $DIR/.bazelrc && rm .bazelrc.bak |
| 16 | +# This script finds Bazel sub-workspaces and marks them, and all their BUILD |
| 17 | +# containing directories, as ignored by the root level bazel project, via the |
| 18 | +# `--deleted_packages` flag. |
| 19 | + |
| 20 | +set -euo pipefail |
| 21 | +set -x |
| 22 | + |
| 23 | +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" |
| 24 | +ROOT_DIR="$DIR/.." |
| 25 | +BAZELRC="$ROOT_DIR/.bazelrc" |
| 26 | + |
| 27 | +# Find all packages in sub-workspaces |
| 28 | +found_packages() { |
| 29 | + cd "$ROOT_DIR" |
| 30 | + ( |
| 31 | + find . -mindepth 2 \( -name WORKSPACE -o -name MODULE.bazel \) | |
| 32 | + while read -r marker; do |
| 33 | + workspace_dir="$(dirname "$marker")" |
| 34 | + echo "$workspace_dir" |
| 35 | + find "$workspace_dir" \( -name BUILD -o -name BUILD.bazel \) -exec dirname {} \; |
| 36 | + done |
| 37 | + ) | sed 's#^\./##' |
| 38 | +} |
| 39 | + |
| 40 | +# Update .bazelrc |
| 41 | +update_bazelrc() { |
| 42 | + local packages |
| 43 | + packages=$(found_packages | sort -u) |
| 44 | + |
| 45 | + local start_marker="# GENERATED_DELETED_PACKAGES_START" |
| 46 | + local end_marker="# GENERATED_DELETED_PACKAGES_END" |
| 47 | + |
| 48 | + # Create a temporary file |
| 49 | + local tmpfile |
| 50 | + tmpfile=$(mktemp) |
| 51 | + |
| 52 | + # Write the content before the start marker |
| 53 | + sed "/$start_marker/q" "$BAZELRC" > "$tmpfile" |
| 54 | + |
| 55 | + # Write the generated packages |
| 56 | + echo "$start_marker" >> "$tmpfile" |
| 57 | + for pkg in $packages; do |
| 58 | + echo "common --deleted_packages=$pkg" >> "$tmpfile" |
| 59 | + done |
| 60 | + echo "$end_marker" >> "$tmpfile" |
| 61 | + |
| 62 | + # Write the content after the end marker |
| 63 | + sed "1,/$end_marker/d" "$BAZELRC" >> "$tmpfile" |
| 64 | + |
| 65 | + # Replace the original file |
| 66 | + mv "$tmpfile" "$BAZELRC" |
| 67 | +} |
| 68 | + |
| 69 | +update_bazelrc |
0 commit comments