Skip to content

Commit

Permalink
[tools] Finish porting to bzlmod
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jwnimmer-tri committed Dec 10, 2024
1 parent 574ede1 commit 48a55ca
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 79 deletions.
4 changes: 0 additions & 4 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ common --keep_going=yes
build --test_output=errors
build --test_summary=terse

# TODO(jwnimmer-tri) For the moment, we still need WORKSPACE.bzlmod.
# Once we can delete that file, we should remove the next line as well.
common --enable_workspace=true

# Add `bazel test --config=lint` shortcut for linting.
build:lint --test_tag_filters=lint

Expand Down
1 change: 0 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ bazel_lint_test(
srcs = [
"BUILD.bazel",
"MODULE.bazel",
"WORKSPACE.bzlmod",
],
)

Expand Down
6 changes: 6 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ http_file(
]
],
)

buildifier_repositories = use_extension("//tools:buildifier_repositories.bzl", "buildifier_repositories")
use_repo(buildifier_repositories, "buildifier-darwin-amd64")
use_repo(buildifier_repositories, "buildifier-darwin-arm64")
use_repo(buildifier_repositories, "buildifier-linux-amd64")
use_repo(buildifier_repositories, "buildifier-linux-arm64")
21 changes: 0 additions & 21 deletions WORKSPACE.bzlmod

This file was deleted.

3 changes: 2 additions & 1 deletion tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ bazel_lint_test(
name = "bazel_lint_test",
srcs = [
"BUILD.bazel",
"buildifier_repositories.bzl",
"buildifier_version.bzl",
"defs.bzl",
"workspace_versions.bzl",
],
)

Expand Down
21 changes: 21 additions & 0 deletions tools/buildifier_repositories.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
load("//tools:buildifier_version.bzl", "BUILDIFIER_VERSION")

def _impl(_ctx):
releases = "https://github.com/bazelbuild/buildtools/releases"
version = BUILDIFIER_VERSION["version"]
for name, sha256 in BUILDIFIER_VERSION["binaries"].items():
http_file(
name = name,
executable = True,
sha256 = sha256,
url = "{releases}/download/v{version}/{name}".format(
releases = releases,
version = version,
name = name,
),
)

buildifier_repositories = module_extension(
implementation = _impl,
)
9 changes: 9 additions & 0 deletions tools/buildifier_version.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BUILDIFIER_VERSION = {
"version": "7.3.1",
"binaries": {
"buildifier-darwin-amd64": "375f823103d01620aaec20a0c29c6cbca99f4fd0725ae30b93655c6704f44d71",
"buildifier-darwin-arm64": "5a6afc6ac7a09f5455ba0b89bd99d5ae23b4174dc5dc9d6c0ed5ce8caac3f813",
"buildifier-linux-amd64": "5474cc5128a74e806783d54081f581662c4be8ae65022f557e9281ed5dc88009",
"buildifier-linux-arm64": "0bf86c4bfffaf4f08eed77bde5b2082e4ae5039a11e2e8b03984c173c34a561c",
},
}
2 changes: 1 addition & 1 deletion tools/upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ me=$(python3 -c 'import os; print(os.path.realpath("'"$0"'"))')
cd $(dirname "$me")/..

python3 -B ./tools/upgrade_helper.py
./bazel run //tools:buildifier tools/workspace_versions.bzl
./bazel run //tools:buildifier tools/buildifier_version.bzl

./bazel run //:requirements.update -- --upgrade
./bazel run //examples:requirements.update -- --upgrade
Expand Down
54 changes: 14 additions & 40 deletions tools/upgrade_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,22 @@ def _write_bazelisk_version(new, sha256):
f.write(new_content)


def _get_current_bazel_version() -> str:
"""Parses ``.bazelversion`` for the old version of bazel."""
with open(".bazelversion", encoding="utf-8") as f:
lines = f.read().splitlines()
(line,) = lines
return line.strip()


def _write_bazel_version(new):
"""Overwrites ``.bazelversion`` with the new version of bazel."""
with open(".bazelversion", "w", encoding="utf-8") as f:
f.write(f"{new}\n")


def _get_current_workspace_versions():
"""Parses ``workspace_versions.bzl`` for the old versions."""
with open("tools/workspace_versions.bzl", encoding="utf-8") as f:
def _get_current_buildifier_version():
"""Parses ``buildifier_version.bzl`` for the old version."""
with open("tools/buildifier_version.bzl", encoding="utf-8") as f:
content = f.read()
prefix = "WORKSPACE_VERSIONS = "
prefix = "BUILDIFIER_VERSION = "
assert content.startswith(prefix)
return ast.literal_eval(content.removeprefix(prefix))


def _write_workspace_versions(new):
"""Overwrites ``workspace_versions.bzl`` with the new versions.
def _write_buildifier_version(new):
"""Overwrites ``buildifier_version.bzl`` with the new version.
We assume that tools/update.sh will run buildifier formatting afterwards.
"""
prefix = "WORKSPACE_VERSIONS = "
prefix = "BUILDIFIER_VERSION = "
content = prefix + pformat(new, width=1, sort_dicts=False)
with open("tools/workspace_versions.bzl", "w", encoding="utf-8") as f:
with open("tools/buildifier_version.bzl", "w", encoding="utf-8") as f:
f.write(content)


Expand Down Expand Up @@ -111,39 +97,27 @@ def _upgrade_bazelisk():
_write_bazelisk_version(new, sha256)


def _upgrade_bazel():
"""Upgrades bazel to its latest version (if necessary)."""
old = _get_current_bazel_version()
new = _find_latest_github_release("bazelbuild/bazel")
if new == old:
print(f"bazel is already at the latest version {new}")
return
print(f"bazel will be upgraded to version {new}")
_write_bazel_version(new)


def _upgrade_buildifier():
"""Upgrades buildifier to its latest version (if necessary)."""
workspace_versions = _get_current_workspace_versions()
old = workspace_versions["buildifier"]["version"]
buildifier_version = _get_current_buildifier_version()
old = buildifier_version["version"]
new = _find_latest_github_release("bazelbuild/buildtools")
if new == old:
print(f"buildifier is already at the latest version {new}")
return
print(f"buildifier will be upgraded to version {new}")
workspace_versions["buildifier"]["version"] = new
names = list(workspace_versions["buildifier"]["binaries"].keys())
buildifier_version["version"] = new
names = list(buildifier_version["binaries"].keys())
releases = "https://github.com/bazelbuild/buildtools/releases"
for name in names:
workspace_versions["buildifier"]["binaries"][name] = _get_url_checksum(
buildifier_version["binaries"][name] = _get_url_checksum(
f"{releases}/download/v{new}/{name}"
)
_write_workspace_versions(workspace_versions)
_write_buildifier_version(buildifier_version)


def _main():
_upgrade_bazelisk()
_upgrade_bazel()
_upgrade_buildifier()


Expand Down
11 changes: 0 additions & 11 deletions tools/workspace_versions.bzl

This file was deleted.

0 comments on commit 48a55ca

Please sign in to comment.