Skip to content

Commit ba0e90a

Browse files
authored
[tools] Finish porting to bzlmod (#108)
Specifically, respell the "download buildifier release binaries from github" into something bzlmod is happy with. While we're here, also remove custom upgrade scripting for `bazel`. Dependabot handles that upgrade automation for us now.
1 parent 574ede1 commit ba0e90a

10 files changed

+53
-79
lines changed

.bazelrc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ common --keep_going=yes
55
build --test_output=errors
66
build --test_summary=terse
77

8-
# TODO(jwnimmer-tri) For the moment, we still need WORKSPACE.bzlmod.
9-
# Once we can delete that file, we should remove the next line as well.
10-
common --enable_workspace=true
11-
128
# Add `bazel test --config=lint` shortcut for linting.
139
build:lint --test_tag_filters=lint
1410

BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ bazel_lint_test(
2424
srcs = [
2525
"BUILD.bazel",
2626
"MODULE.bazel",
27-
"WORKSPACE.bzlmod",
2827
],
2928
)
3029

MODULE.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,9 @@ http_file(
4949
]
5050
],
5151
)
52+
53+
buildifier_repositories = use_extension("//tools:buildifier_repositories.bzl", "buildifier_repositories")
54+
use_repo(buildifier_repositories, "buildifier-darwin-amd64")
55+
use_repo(buildifier_repositories, "buildifier-darwin-arm64")
56+
use_repo(buildifier_repositories, "buildifier-linux-amd64")
57+
use_repo(buildifier_repositories, "buildifier-linux-arm64")

WORKSPACE.bzlmod

Lines changed: 0 additions & 21 deletions
This file was deleted.

tools/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ bazel_lint_test(
8484
name = "bazel_lint_test",
8585
srcs = [
8686
"BUILD.bazel",
87+
"buildifier_repositories.bzl",
88+
"buildifier_version.bzl",
8789
"defs.bzl",
88-
"workspace_versions.bzl",
8990
],
9091
)
9192

tools/buildifier_repositories.bzl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
2+
load("//tools:buildifier_version.bzl", "BUILDIFIER_VERSION")
3+
4+
def _impl(_ctx):
5+
releases = "https://github.com/bazelbuild/buildtools/releases"
6+
version = BUILDIFIER_VERSION["version"]
7+
for name, sha256 in BUILDIFIER_VERSION["binaries"].items():
8+
http_file(
9+
name = name,
10+
executable = True,
11+
sha256 = sha256,
12+
url = "{releases}/download/v{version}/{name}".format(
13+
releases = releases,
14+
version = version,
15+
name = name,
16+
),
17+
)
18+
19+
buildifier_repositories = module_extension(
20+
implementation = _impl,
21+
)

tools/buildifier_version.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
BUILDIFIER_VERSION = {
2+
"version": "7.3.1",
3+
"binaries": {
4+
"buildifier-darwin-amd64": "375f823103d01620aaec20a0c29c6cbca99f4fd0725ae30b93655c6704f44d71",
5+
"buildifier-darwin-arm64": "5a6afc6ac7a09f5455ba0b89bd99d5ae23b4174dc5dc9d6c0ed5ce8caac3f813",
6+
"buildifier-linux-amd64": "5474cc5128a74e806783d54081f581662c4be8ae65022f557e9281ed5dc88009",
7+
"buildifier-linux-arm64": "0bf86c4bfffaf4f08eed77bde5b2082e4ae5039a11e2e8b03984c173c34a561c",
8+
},
9+
}

tools/upgrade.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ me=$(python3 -c 'import os; print(os.path.realpath("'"$0"'"))')
1313
cd $(dirname "$me")/..
1414

1515
python3 -B ./tools/upgrade_helper.py
16-
./bazel run //tools:buildifier tools/workspace_versions.bzl
16+
./bazel run //tools:buildifier tools/buildifier_version.bzl
1717

1818
./bazel run //:requirements.update -- --upgrade
1919
./bazel run //examples:requirements.update -- --upgrade

tools/upgrade_helper.py

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,22 @@ def _write_bazelisk_version(new, sha256):
3434
f.write(new_content)
3535

3636

37-
def _get_current_bazel_version() -> str:
38-
"""Parses ``.bazelversion`` for the old version of bazel."""
39-
with open(".bazelversion", encoding="utf-8") as f:
40-
lines = f.read().splitlines()
41-
(line,) = lines
42-
return line.strip()
43-
44-
45-
def _write_bazel_version(new):
46-
"""Overwrites ``.bazelversion`` with the new version of bazel."""
47-
with open(".bazelversion", "w", encoding="utf-8") as f:
48-
f.write(f"{new}\n")
49-
50-
51-
def _get_current_workspace_versions():
52-
"""Parses ``workspace_versions.bzl`` for the old versions."""
53-
with open("tools/workspace_versions.bzl", encoding="utf-8") as f:
37+
def _get_current_buildifier_version():
38+
"""Parses ``buildifier_version.bzl`` for the old version."""
39+
with open("tools/buildifier_version.bzl", encoding="utf-8") as f:
5440
content = f.read()
55-
prefix = "WORKSPACE_VERSIONS = "
41+
prefix = "BUILDIFIER_VERSION = "
5642
assert content.startswith(prefix)
5743
return ast.literal_eval(content.removeprefix(prefix))
5844

5945

60-
def _write_workspace_versions(new):
61-
"""Overwrites ``workspace_versions.bzl`` with the new versions.
46+
def _write_buildifier_version(new):
47+
"""Overwrites ``buildifier_version.bzl`` with the new version.
6248
We assume that tools/update.sh will run buildifier formatting afterwards.
6349
"""
64-
prefix = "WORKSPACE_VERSIONS = "
50+
prefix = "BUILDIFIER_VERSION = "
6551
content = prefix + pformat(new, width=1, sort_dicts=False)
66-
with open("tools/workspace_versions.bzl", "w", encoding="utf-8") as f:
52+
with open("tools/buildifier_version.bzl", "w", encoding="utf-8") as f:
6753
f.write(content)
6854

6955

@@ -111,39 +97,27 @@ def _upgrade_bazelisk():
11197
_write_bazelisk_version(new, sha256)
11298

11399

114-
def _upgrade_bazel():
115-
"""Upgrades bazel to its latest version (if necessary)."""
116-
old = _get_current_bazel_version()
117-
new = _find_latest_github_release("bazelbuild/bazel")
118-
if new == old:
119-
print(f"bazel is already at the latest version {new}")
120-
return
121-
print(f"bazel will be upgraded to version {new}")
122-
_write_bazel_version(new)
123-
124-
125100
def _upgrade_buildifier():
126101
"""Upgrades buildifier to its latest version (if necessary)."""
127-
workspace_versions = _get_current_workspace_versions()
128-
old = workspace_versions["buildifier"]["version"]
102+
buildifier_version = _get_current_buildifier_version()
103+
old = buildifier_version["version"]
129104
new = _find_latest_github_release("bazelbuild/buildtools")
130105
if new == old:
131106
print(f"buildifier is already at the latest version {new}")
132107
return
133108
print(f"buildifier will be upgraded to version {new}")
134-
workspace_versions["buildifier"]["version"] = new
135-
names = list(workspace_versions["buildifier"]["binaries"].keys())
109+
buildifier_version["version"] = new
110+
names = list(buildifier_version["binaries"].keys())
136111
releases = "https://github.com/bazelbuild/buildtools/releases"
137112
for name in names:
138-
workspace_versions["buildifier"]["binaries"][name] = _get_url_checksum(
113+
buildifier_version["binaries"][name] = _get_url_checksum(
139114
f"{releases}/download/v{new}/{name}"
140115
)
141-
_write_workspace_versions(workspace_versions)
116+
_write_buildifier_version(buildifier_version)
142117

143118

144119
def _main():
145120
_upgrade_bazelisk()
146-
_upgrade_bazel()
147121
_upgrade_buildifier()
148122

149123

tools/workspace_versions.bzl

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)