Skip to content

Commit 37405fe

Browse files
authored
Add extra presets (#62)
* Add extra presets * Add extra test for flags from dict
1 parent 537b6d8 commit 37405fe

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

bazelrc-preset.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def _verify_command_overrides(meta):
5353
if sets.length(unique_commands) != len(meta):
5454
fail("Multiple flag overrides use the same command. Make sure flag overrides use different command.")
5555

56+
def _get_flags_from_extra_presets(extra_presets_json):
57+
return {key: struct(**value) for key, value in json.decode(extra_presets_json).items()}
58+
5659
def _generate_preset(ctx):
5760
content = ctx.actions.args().set_param_file_format("multiline")
5861
content.add_all([
@@ -61,7 +64,7 @@ def _generate_preset(ctx):
6164
" bazel run {}.update".format(ctx.label),
6265
], format_each = "# %s")
6366

64-
flags = FLAGS | (MIGRATIONS if ctx.attr.strict else {})
67+
flags = FLAGS | (MIGRATIONS if ctx.attr.strict else {}) | _get_flags_from_extra_presets(ctx.attr.extra_presets)
6568
for flag, meta in flags.items():
6669
# Syntax sugar: allow a struct to stand in for a singleton list
6770
if type(meta) != type([]):
@@ -77,6 +80,11 @@ def _generate_preset(ctx):
7780
generate_preset = rule(
7881
implementation = _generate_preset,
7982
attrs = {
83+
"extra_presets": attr.string(
84+
doc = """\
85+
A json encoded string of presets to add to the preset generation matching the format of the FLAGS dictionary.
86+
""",
87+
),
8088
"out": attr.output(),
8189
"strict": attr.bool(
8290
default = False,
@@ -110,6 +118,7 @@ def bazelrc_preset(name, out_file = None, **kwargs):
110118
Label("//:strict.true"): True,
111119
"//conditions:default": False,
112120
})),
121+
extra_presets = json.encode(kwargs.pop("extra_presets", {})),
113122
**kwargs
114123
)
115124
if not out_file:

tests/BUILD

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
load("@aspect_bazel_lib//lib:testing.bzl", "assert_contains")
12
load("@bazel_skylib//rules:native_binary.bzl", "native_test")
23
load("//:bazelrc-preset.bzl", "bazelrc_preset")
4+
load(":extra_test_presets.bzl", "EXTRA_TEST_PRESETS")
35

46
bazelrc_preset(
57
name = "preset",
@@ -13,3 +15,29 @@ native_test(
1315
args = ["$(rootpath :preset)"],
1416
data = [":preset"],
1517
)
18+
19+
bazelrc_preset(
20+
name = "preset_with_extra_presets",
21+
extra_presets = EXTRA_TEST_PRESETS | {
22+
"extra_preset_as_dict": {
23+
"command": "common:bar",
24+
"default": True,
25+
"description": """\
26+
Extra preset that can be provided by the user.
27+
""",
28+
},
29+
},
30+
tags = ["manual"],
31+
)
32+
33+
assert_contains(
34+
name = "test_with_extra_presets_as_struct",
35+
actual = ":preset_with_extra_presets",
36+
expected = "common:foo --extra_preset_as_struct",
37+
)
38+
39+
assert_contains(
40+
name = "test_with_extra_presets_as_dict",
41+
actual = ":preset_with_extra_presets",
42+
expected = "common:bar --extra_preset_as_dict",
43+
)

tests/extra_test_presets.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Extra test presets for bazelrc-preset"""
2+
3+
EXTRA_TEST_PRESETS = {
4+
"extra_preset_as_struct": struct(
5+
command = "common:foo",
6+
default = True,
7+
description = """\
8+
Extra preset that can be provided by the user.
9+
For instance to predefine https://github.com/aspect-build/toolchains_protoc?tab=readme-ov-file#ensure-protobuf-and-grpc-never-built.
10+
""",
11+
),
12+
}

0 commit comments

Comments
 (0)