xref: /aosp_15_r20/external/bazelbuild-rules_python/tools/update_deleted_packages.sh (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1*60517a1eSAndroid Build Coastguard Worker#!/usr/bin/env bash
2*60517a1eSAndroid Build Coastguard Worker# Copyright 2023 The Bazel Authors. All rights reserved.
3*60517a1eSAndroid Build Coastguard Worker#
4*60517a1eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
5*60517a1eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
6*60517a1eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
7*60517a1eSAndroid Build Coastguard Worker#
8*60517a1eSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
9*60517a1eSAndroid Build Coastguard Worker#
10*60517a1eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
11*60517a1eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
12*60517a1eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*60517a1eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
14*60517a1eSAndroid Build Coastguard Worker# limitations under the License.
15*60517a1eSAndroid Build Coastguard Worker
16*60517a1eSAndroid Build Coastguard Worker# For integration tests, we want to be able to glob() up the sources inside a nested package
17*60517a1eSAndroid Build Coastguard Worker# See explanation in .bazelrc
18*60517a1eSAndroid Build Coastguard Worker#
19*60517a1eSAndroid Build Coastguard Worker# This script ensures that we only delete subtrees that have something a file
20*60517a1eSAndroid Build Coastguard Worker# signifying a new bazel workspace, whether it be bzlmod or classic. Generic
21*60517a1eSAndroid Build Coastguard Worker# algorithm:
22*60517a1eSAndroid Build Coastguard Worker#   1. Get all directories where a WORKSPACE or MODULE.bazel exists.
23*60517a1eSAndroid Build Coastguard Worker#   2. For each of the directories, get all directories that contains a BUILD.bazel file.
24*60517a1eSAndroid Build Coastguard Worker#   3. Sort and remove duplicates.
25*60517a1eSAndroid Build Coastguard Worker
26*60517a1eSAndroid Build Coastguard Workerset -euxo pipefail
27*60517a1eSAndroid Build Coastguard Worker
28*60517a1eSAndroid Build Coastguard WorkerDIR="$(dirname $0)/.."
29*60517a1eSAndroid Build Coastguard Workercd $DIR
30*60517a1eSAndroid Build Coastguard Worker
31*60517a1eSAndroid Build Coastguard Worker# The sed -i.bak pattern is compatible between macos and linux
32*60517a1eSAndroid Build Coastguard Workersed -i.bak "/^[^#].*--deleted_packages/s#=.*#=$(\
33*60517a1eSAndroid Build Coastguard Worker    find examples/*/* tests/*/* \( -name WORKSPACE -or -name MODULE.bazel \) |
34*60517a1eSAndroid Build Coastguard Worker        xargs -n 1 dirname |
35*60517a1eSAndroid Build Coastguard Worker        xargs -n 1 -I{} find {} \( -name BUILD -or -name BUILD.bazel \) |
36*60517a1eSAndroid Build Coastguard Worker        xargs -n 1 dirname |
37*60517a1eSAndroid Build Coastguard Worker        sort -u |
38*60517a1eSAndroid Build Coastguard Worker        paste -sd, -\
39*60517a1eSAndroid Build Coastguard Worker)#" $DIR/.bazelrc && rm .bazelrc.bak
40