Skip to content

Commit d5ae39f

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 d5ae39f

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

docs/expand_template_doc.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A rule that performs template expansion.
77
## expand_template
88

99
<pre>
10-
expand_template(<a href="#expand_template-name">name</a>, <a href="#expand_template-out">out</a>, <a href="#expand_template-substitutions">substitutions</a>, <a href="#expand_template-template">template</a>)
10+
expand_template(<a href="#expand_template-name">name</a>, <a href="#expand_template-data">data</a>, <a href="#expand_template-out">out</a>, <a href="#expand_template-substitutions">substitutions</a>, <a href="#expand_template-template">template</a>)
1111
</pre>
1212

1313
Template expansion
@@ -24,6 +24,7 @@ explicitly add delimiters to the key strings, for example "{KEY}" or "@KEY@".
2424
| Name | Description | Type | Mandatory | Default |
2525
| :------------- | :------------- | :------------- | :------------- | :------------- |
2626
| <a id="expand_template-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
27+
| <a id="expand_template-data"></a>data | data dependencies. See https://bazel.build/reference/be/common-definitions#typical.data | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
2728
| <a id="expand_template-out"></a>out | The destination of the expanded file. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
2829
| <a id="expand_template-substitutions"></a>substitutions | A dictionary mapping strings to their substitutions. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | required | |
2930
| <a id="expand_template-template"></a>template | The template file to expand. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |

rules/expand_template.bzl

+17-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,21 @@
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_make_variables(
22+
"substitutions",
23+
ctx.expand_location(
24+
value,
25+
targets = ctx.attr.data,
26+
),
27+
{},
28+
)
29+
1930
ctx.actions.expand_template(
2031
template = ctx.file.template,
2132
output = ctx.outputs.out,
22-
substitutions = ctx.attr.substitutions,
33+
substitutions = expanded_substitutions,
2334
)
2435

2536
expand_template = rule(
@@ -32,6 +43,11 @@ substitutions, and replaces them with the corresponding values.
3243
There is no special syntax for the keys. To avoid conflicts, you would need to
3344
explicitly add delimiters to the key strings, for example "{KEY}" or "@KEY@".""",
3445
attrs = {
46+
"data": attr.label_list(
47+
allow_files = True,
48+
doc = "data dependencies. See" +
49+
" https://bazel.build/reference/be/common-definitions#typical.data",
50+
),
3551
"template": attr.label(
3652
mandatory = True,
3753
allow_single_file = True,

tests/expand_template/BUILD

+15
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,36 @@
1414

1515
# This package aids testing the 'diff_test' rule.
1616

17+
load("//rules:common_settings.bzl", "string_flag")
1718
load("//rules:expand_template.bzl", "expand_template")
1819

1920
package(
2021
default_applicable_licenses = ["//:license"],
2122
default_testonly = 1,
2223
)
2324

25+
string_flag(
26+
name = "my_string_flag",
27+
build_setting_default = "VAR",
28+
make_variable = "SOMEVAR",
29+
)
30+
2431
expand_template(
2532
name = "filled_template",
2633
out = "foo/test.yaml",
34+
data = [
35+
":version",
36+
],
2737
substitutions = {
2838
"@name@": "test",
2939
"@version@": "1.1.1",
40+
"@path@": "$(rlocationpath :version)",
41+
"@toolchain@": "prefix$(SOMEVAR)suffix",
3042
},
3143
template = "test.tpl.yaml",
44+
toolchains = [
45+
":my_string_flag",
46+
],
3247
)
3348

3449
sh_test(

tests/expand_template/template_test.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ 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 'tests/expand_template/version.h'
36+
expect_log 'toolchain: prefixVARsuffix'
3537
}
3638

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

tests/expand_template/test.tpl.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
name: @name@
22
version: @version@
3+
path: @path@
4+
toolchain: @toolchain@

0 commit comments

Comments
 (0)