xref: /aosp_15_r20/external/bazelbuild-rules_python/tools/update_deleted_packages.sh (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1#!/usr/bin/env bash
2# Copyright 2023 The Bazel Authors. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
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
26set -euxo pipefail
27
28DIR="$(dirname $0)/.."
29cd $DIR
30
31# The sed -i.bak pattern is compatible between macos and linux
32sed -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
40