Skip to content

Commit 44814b2

Browse files
committed
Add support for data in expand_template
This can be useful for encoding the paths to things in your expansions
1 parent 27d429d commit 44814b2

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

rules/expand_template.bzl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@
1616
"""
1717

1818
def _expand_template_impl(ctx):
19+
expanded_substitutions = {}
20+
for key, value in ctx.attr.substitutions.items():
21+
expanded_substitutions[key] = ctx.expand_location(
22+
value,
23+
targets = ctx.attr.data,
24+
)
25+
1926
ctx.actions.expand_template(
2027
template = ctx.file.template,
2128
output = ctx.outputs.out,
22-
substitutions = ctx.attr.substitutions,
29+
substitutions = expanded_substitutions,
2330
)
2431

2532
expand_template = rule(
@@ -32,6 +39,11 @@ substitutions, and replaces them with the corresponding values.
3239
There is no special syntax for the keys. To avoid conflicts, you would need to
3340
explicitly add delimiters to the key strings, for example "{KEY}" or "@KEY@".""",
3441
attrs = {
42+
"data": attr.label_list(
43+
allow_files = True,
44+
doc = "data dependencies. See" +
45+
" https://bazel.build/reference/be/common-definitions#typical.data",
46+
),
3547
"template": attr.label(
3648
mandatory = True,
3749
allow_single_file = True,

tests/expand_template/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ package(
2424
expand_template(
2525
name = "filled_template",
2626
out = "foo/test.yaml",
27+
data = [
28+
":version",
29+
],
2730
substitutions = {
2831
"@name@": "test",
2932
"@version@": "1.1.1",
33+
"@path@": "$(rlocationpath :version)",
3034
},
3135
template = "test.tpl.yaml",
3236
)

tests/expand_template/template_test.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function test_expand_template() {
3232
cat "$(rlocation $TEST_WORKSPACE/tests/expand_template/foo/test.yaml)" >"$TEST_log"
3333
expect_log 'name: test'
3434
expect_log 'version: 1.1.1'
35+
expect_log 'path: _main/tests/expand_template/version.h'
3536
}
3637

37-
run_suite "expand_template_tests test suite"
38+
run_suite "expand_template_tests test suite"

tests/expand_template/test.tpl.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
name: @name@
22
version: @version@
3+
path: @path@

0 commit comments

Comments
 (0)