Skip to content

Commit 2dc9540

Browse files
committed
WIP: Bzlmod part the one hundred-fifteenth
Add restore mode to scripts/toggle-workspace.sh Realized I could update the script to reliably undo any of its uncommitted changes, rather than having the user run `git {status,restore,clean}` separately.
1 parent 30c32a3 commit 2dc9540

File tree

1 file changed

+106
-43
lines changed

1 file changed

+106
-43
lines changed

scripts/toggle-workspace.sh

Lines changed: 106 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,130 @@
33
# Updates .bazelrc files to enable or disable WORKSPACE builds.
44
#
55
# 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.
88

99
ROOTDIR="${BASH_SOURCE[0]%/*}/.."
1010
cd "$ROOTDIR"
1111

1212
if [[ "$?" -ne 0 ]]; then
1313
echo "Could not change to $ROOTDIR." >&2
1414
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
2115
fi
2216

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+
}
2627

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
3441

35-
enabled_options="$workspace_options"
36-
disabled_options="$bzlmod_options"
42+
local enabled_options="$workspace_options"
43+
local disabled_options="$bzlmod_options"
3744

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
4249

43-
already_enabled="($enabled_options|import [./]*/.bazelrc)"
50+
already_enabled="($enabled_options|import [./]*/.bazelrc)"
4451

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"
5057

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
5567

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
5992
fi
6093

61-
content="$(< "$bazelrc_path")"
94+
if [[ "${#unstaged[@]}" -ne 0 ]]; then
95+
git restore "${unstaged[@]}"
96+
fi
6297

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[@]}"
67100
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
68121

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

Comments
 (0)