feat: add is_bazel_7_or_greater utility (#714)

This commit is contained in:
Greg Magolan 2024-01-07 16:42:57 -08:00 committed by GitHub
parent 5aaa785b96
commit 516a5fe725
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 6 deletions

View File

@ -6,12 +6,10 @@ load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@buildifier_prebuilt//:rules.bzl", "buildifier")
load("//lib:diff_test.bzl", "diff_test")
load("//lib:testing.bzl", "assert_contains")
load("//lib:utils.bzl", "is_bazel_7_or_greater")
load("//lib:write_source_files.bzl", "write_source_files")
load("//lib:yq.bzl", "yq")
# buildifier: disable=bzl-visibility
load("//lib/private:utils.bzl", "utils")
# gazelle:prefix github.com/aspect-build/bazel-lib
gazelle_binary(
@ -79,7 +77,7 @@ bzl_library(
deps = [
"@bazel_tools//tools/build_defs/repo:http.bzl",
"@bazel_tools//tools/build_defs/repo:utils.bzl",
] + (["@bazel_tools//tools/build_defs/repo:cache.bzl"] if utils.is_bazel_7_or_greater() else []),
] + (["@bazel_tools//tools/build_defs/repo:cache.bzl"] if is_bazel_7_or_greater() else []),
)
# write_source_files() to a git ignored subdirectory of the root

38
docs/utils.md generated
View File

@ -158,6 +158,44 @@ That approach, however, incurs a cost in the user's WORKSPACE.
True if the Bazel version being used is greater than or equal to 6 (including pre-releases and RCs)
<a id="is_bazel_7_or_greater"></a>
## is_bazel_7_or_greater
<pre>
is_bazel_7_or_greater()
</pre>
Detects if the Bazel version being used is greater than or equal to 7 (including Bazel 7 pre-releases and RCs).
Unlike the undocumented `native.bazel_version`, which only works in WORKSPACE and repository rules, this function can
be used in rules and BUILD files.
An alternate approach to make the Bazel version available in BUILD files and rules would be to
use the [host_repo](https://github.com/aspect-build/bazel-lib/blob/main/docs/host_repo.md) repository rule
which contains the bazel_version in the exported `host` struct:
WORKSPACE:
```
load("@aspect_bazel_lib//lib:host_repo.bzl", "host_repo")
host_repo(name = "aspect_bazel_lib_host")
```
BUILD.bazel:
```
load("@aspect_bazel_lib_host//:defs.bzl", "host")
print(host.bazel_version)
```
That approach, however, incurs a cost in the user's WORKSPACE.
**RETURNS**
True if the Bazel version being used is greater than or equal to 7 (including pre-releases and RCs)
<a id="is_bzlmod_enabled"></a>
## is_bzlmod_enabled

View File

@ -1,5 +1,5 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//lib/private:utils.bzl", "utils")
load("//lib:utils.bzl", "is_bazel_7_or_greater")
exports_files(
[
@ -216,7 +216,7 @@ bzl_library(
deps = [
"@bazel_tools//tools/build_defs/repo:http.bzl",
"@bazel_tools//tools/build_defs/repo:utils.bzl",
] + (["@bazel_tools//tools/build_defs/repo:cache.bzl"] if utils.is_bazel_7_or_greater() else []),
] + (["@bazel_tools//tools/build_defs/repo:cache.bzl"] if is_bazel_7_or_greater() else []),
)
# keep

View File

@ -6,6 +6,7 @@ default_timeout = utils.default_timeout
file_exists = utils.file_exists
glob_directories = utils.glob_directories
is_bazel_6_or_greater = utils.is_bazel_6_or_greater
is_bazel_7_or_greater = utils.is_bazel_7_or_greater
is_bzlmod_enabled = utils.is_bzlmod_enabled
is_external_label = utils.is_external_label
maybe_http_archive = utils.maybe_http_archive