|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Updates .bazelrc files to enable or disable WORKSPACE builds. |
| 4 | +# |
| 5 | +# This is for testing `WORKSPACE` and Bzlmod compatibility. The produced changes |
| 6 | +# should never be checked in. Run `git restore` and `git clean` to revert them |
| 7 | +# before committing. |
| 8 | + |
| 9 | +ROOTDIR="${BASH_SOURCE[0]%/*}/.." |
| 10 | +cd "$ROOTDIR" |
| 11 | + |
| 12 | +if [[ "$?" -ne 0 ]]; then |
| 13 | + echo "Could not change to $ROOTDIR." >&2 |
| 14 | + exit 1 |
| 15 | +elif [[ "$#" -gt 1 || "${1:-disable}" != 'disable' ]]; then |
| 16 | + printf '%s\n' >&2 \ |
| 17 | + "Usage: $0 [ disable ]" \ |
| 18 | + "" \ |
| 19 | + "Updates .bazelrc files to enable or disable WORKSPACE builds." |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +workspace_options="build --noenable_bzlmod" |
| 24 | +bzlmod_options="build --enable_bzlmod" |
| 25 | +bazel_version="$(bazel --version 2>&1)" |
| 26 | + |
| 27 | +if [[ "$?" -ne 0 ]]; then |
| 28 | + echo "failed to run `bazel --version`: $bazel_version" >&2 |
| 29 | + exit 1 |
| 30 | +elif [[ "${bazel_version#* }" =~ ^(7|8)\. ]]; then |
| 31 | + workspace_options="${workspace_options} --enable_workspace" |
| 32 | + bzlmod_options="${bzlmod_options} --noenable_workspace" |
| 33 | +fi |
| 34 | + |
| 35 | +enabled_options="$workspace_options" |
| 36 | +disabled_options="$bzlmod_options" |
| 37 | + |
| 38 | +if [[ "$1" == "disable" ]]; then |
| 39 | + enabled_options="$bzlmod_options" |
| 40 | + disabled_options="$workspace_options" |
| 41 | +fi |
| 42 | + |
| 43 | +already_enabled="($enabled_options|import [./]*/.bazelrc)" |
| 44 | + |
| 45 | +# Searches for WORKSPACE instead of .bazelrc because not all repos may have a |
| 46 | +# .bazelrc file. |
| 47 | +while IFS="" read repo_marker_path; do |
| 48 | + repo_path="${repo_marker_path%/*}" |
| 49 | + bazelrc_path="$repo_path/.bazelrc" |
| 50 | + |
| 51 | + # The top level repo is a special case. |
| 52 | + if [[ "$repo_path" == "$repo_marker_path" ]]; then |
| 53 | + bazelrc_path="./.bazelrc" |
| 54 | + fi |
| 55 | + |
| 56 | + if [[ ! -f "$bazelrc_path" ]]; then |
| 57 | + echo "$enabled_options" > "$bazelrc_path" |
| 58 | + continue |
| 59 | + fi |
| 60 | + |
| 61 | + content="$(< "$bazelrc_path")" |
| 62 | + |
| 63 | + if [[ "$content" =~ $disabled_options ]]; then |
| 64 | + echo "${content//$disabled_options/$enabled_options}" >"$bazelrc_path" |
| 65 | + elif [[ ! "$content" =~ $already_enabled ]]; then |
| 66 | + echo "$enabled_options" >> "$bazelrc_path" |
| 67 | + fi |
| 68 | + |
| 69 | +done < <(find [A-Za-z0-9]* -name "WORKSPACE") |
0 commit comments