|
3 | 3 | # Updates .bazelrc files to enable or disable WORKSPACE builds. |
4 | 4 | # |
5 | 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. |
| 6 | +# should never be checked in. Run this script with the `restore` argument to |
| 7 | +# revert them before committing. |
8 | 8 |
|
9 | 9 | ROOTDIR="${BASH_SOURCE[0]%/*}/.." |
10 | 10 | cd "$ROOTDIR" |
11 | 11 |
|
12 | 12 | if [[ "$?" -ne 0 ]]; then |
13 | 13 | echo "Could not change to $ROOTDIR." >&2 |
14 | 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 | 15 | fi |
22 | 16 |
|
23 | | -workspace_options="build --noenable_bzlmod" |
24 | | -bzlmod_options="build --enable_bzlmod" |
25 | | -bazel_version="$(bazel --version 2>&1)" |
| 17 | +usage() { |
| 18 | + local lines=() |
| 19 | + while IFS='' read line; do |
| 20 | + if [[ "${line:0:1}" != '#' ]]; then |
| 21 | + printf '%s\n' "Usage: $0 [ disable | restore ]" "${lines[@]:1}" >&2 |
| 22 | + exit "$1" |
| 23 | + fi |
| 24 | + lines+=("${line:2}") |
| 25 | + done <$0 |
| 26 | +} |
26 | 27 |
|
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 |
| 28 | +update_bazelrc_files() { |
| 29 | + local mode="$1" |
| 30 | + local workspace_options="build --noenable_bzlmod" |
| 31 | + local bzlmod_options="build --enable_bzlmod" |
| 32 | + local bazel_version="$(bazel --version 2>&1)" |
| 33 | + |
| 34 | + if [[ "$?" -ne 0 ]]; then |
| 35 | + echo "failed to run `bazel --version`: $bazel_version" >&2 |
| 36 | + exit 1 |
| 37 | + elif [[ "${bazel_version#* }" =~ ^(7|8)\. ]]; then |
| 38 | + workspace_options="${workspace_options} --enable_workspace" |
| 39 | + bzlmod_options="${bzlmod_options} --noenable_workspace" |
| 40 | + fi |
34 | 41 |
|
35 | | -enabled_options="$workspace_options" |
36 | | -disabled_options="$bzlmod_options" |
| 42 | + local enabled_options="$workspace_options" |
| 43 | + local disabled_options="$bzlmod_options" |
37 | 44 |
|
38 | | -if [[ "$1" == "disable" ]]; then |
39 | | - enabled_options="$bzlmod_options" |
40 | | - disabled_options="$workspace_options" |
41 | | -fi |
| 45 | + if [[ "$mode" == "disable" ]]; then |
| 46 | + enabled_options="$bzlmod_options" |
| 47 | + disabled_options="$workspace_options" |
| 48 | + fi |
42 | 49 |
|
43 | | -already_enabled="($enabled_options|import [./]*/.bazelrc)" |
| 50 | + already_enabled="($enabled_options|import [./]*/.bazelrc)" |
44 | 51 |
|
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" |
| 52 | + # Searches for WORKSPACE instead of .bazelrc because not all repos may have a |
| 53 | + # .bazelrc file. |
| 54 | + while IFS='' read repo_marker_path; do |
| 55 | + local repo_path="${repo_marker_path%/*}" |
| 56 | + local bazelrc_path="$repo_path/.bazelrc" |
50 | 57 |
|
51 | | - # The top level repo is a special case. |
52 | | - if [[ "$repo_path" == "$repo_marker_path" ]]; then |
53 | | - bazelrc_path="./.bazelrc" |
54 | | - fi |
| 58 | + # The top level repo is a special case. |
| 59 | + if [[ "$repo_path" == "$repo_marker_path" ]]; then |
| 60 | + bazelrc_path="./.bazelrc" |
| 61 | + fi |
| 62 | + |
| 63 | + if [[ ! -f "$bazelrc_path" ]]; then |
| 64 | + echo "$enabled_options" > "$bazelrc_path" |
| 65 | + continue |
| 66 | + fi |
55 | 67 |
|
56 | | - if [[ ! -f "$bazelrc_path" ]]; then |
57 | | - echo "$enabled_options" > "$bazelrc_path" |
58 | | - continue |
| 68 | + content="$(< "$bazelrc_path")" |
| 69 | + |
| 70 | + if [[ "$content" =~ $disabled_options ]]; then |
| 71 | + echo "${content//$disabled_options/$enabled_options}" >"$bazelrc_path" |
| 72 | + elif [[ ! "$content" =~ $already_enabled ]]; then |
| 73 | + echo "$enabled_options" >> "$bazelrc_path" |
| 74 | + fi |
| 75 | + |
| 76 | + done < <(find [A-Za-z0-9]* -name "WORKSPACE") |
| 77 | +} |
| 78 | + |
| 79 | +restore_bazelrc_files() { |
| 80 | + local staged=() |
| 81 | + local unstaged=() |
| 82 | + local untracked=() |
| 83 | + |
| 84 | + get_bazelrc_files_by_status |
| 85 | + |
| 86 | + if [[ "${#staged[@]}" -ne 0 ]]; then |
| 87 | + # Staged files can be untracked after unstaging, so recalculate. |
| 88 | + git restore --staged "${staged[@]}" |
| 89 | + unstaged=() |
| 90 | + untracked=() |
| 91 | + get_bazelrc_files_by_status |
59 | 92 | fi |
60 | 93 |
|
61 | | - content="$(< "$bazelrc_path")" |
| 94 | + if [[ "${#unstaged[@]}" -ne 0 ]]; then |
| 95 | + git restore "${unstaged[@]}" |
| 96 | + fi |
62 | 97 |
|
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" |
| 98 | + if [[ "${#untracked[@]}" -ne 0 ]]; then |
| 99 | + git clean -f "${untracked[@]}" |
67 | 100 | fi |
| 101 | +} |
| 102 | + |
| 103 | +get_bazelrc_files_by_status() { |
| 104 | + while IFS='' read status_line; do |
| 105 | + local status_code="${status_line:0:1}" |
| 106 | + local bazelrc_file="${status_line:3}" |
| 107 | + |
| 108 | + if [[ "$status_code" == '?' ]]; then |
| 109 | + untracked+=("$bazelrc_file") |
| 110 | + elif [[ "$status_code" == ' ' ]]; then |
| 111 | + unstaged+=("$bazelrc_file") |
| 112 | + else |
| 113 | + staged+=("$bazelrc_file") |
| 114 | + fi |
| 115 | + done < <(git status -s '.bazelrc' '**/.bazelrc') |
| 116 | +} |
| 117 | + |
| 118 | +if [[ "$#" -gt 1 ]]; then |
| 119 | + usage 1 |
| 120 | +fi |
68 | 121 |
|
69 | | -done < <(find [A-Za-z0-9]* -name "WORKSPACE") |
| 122 | +case "$1" in |
| 123 | + ""|disable) |
| 124 | + update_bazelrc_files "$1" |
| 125 | + ;; |
| 126 | + restore) |
| 127 | + restore_bazelrc_files |
| 128 | + ;; |
| 129 | + *) |
| 130 | + usage 1 |
| 131 | + ;; |
| 132 | +esac |
0 commit comments