Skip to content

Commit 1b19c61

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 1b19c61

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-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

+13-1
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

+4
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

+2-1
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 '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

+1
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)