Add support for data in expand_template
This can be useful for encoding the paths to things in your expansions
This commit is contained in:
parent
27d429d8d0
commit
d5ae39f077
|
@ -7,7 +7,7 @@ A rule that performs template expansion.
|
|||
## expand_template
|
||||
|
||||
<pre>
|
||||
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>)
|
||||
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>)
|
||||
</pre>
|
||||
|
||||
Template expansion
|
||||
|
@ -24,6 +24,7 @@ explicitly add delimiters to the key strings, for example "{KEY}" or "@KEY@".
|
|||
| Name | Description | Type | Mandatory | Default |
|
||||
| :------------- | :------------- | :------------- | :------------- | :------------- |
|
||||
| <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 | |
|
||||
| <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 | `[]` |
|
||||
| <a id="expand_template-out"></a>out | The destination of the expanded file. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
|
||||
| <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 | |
|
||||
| <a id="expand_template-template"></a>template | The template file to expand. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
|
||||
|
|
|
@ -16,10 +16,21 @@
|
|||
"""
|
||||
|
||||
def _expand_template_impl(ctx):
|
||||
expanded_substitutions = {}
|
||||
for key, value in ctx.attr.substitutions.items():
|
||||
expanded_substitutions[key] = ctx.expand_make_variables(
|
||||
"substitutions",
|
||||
ctx.expand_location(
|
||||
value,
|
||||
targets = ctx.attr.data,
|
||||
),
|
||||
{},
|
||||
)
|
||||
|
||||
ctx.actions.expand_template(
|
||||
template = ctx.file.template,
|
||||
output = ctx.outputs.out,
|
||||
substitutions = ctx.attr.substitutions,
|
||||
substitutions = expanded_substitutions,
|
||||
)
|
||||
|
||||
expand_template = rule(
|
||||
|
@ -32,6 +43,11 @@ substitutions, and replaces them with the corresponding values.
|
|||
There is no special syntax for the keys. To avoid conflicts, you would need to
|
||||
explicitly add delimiters to the key strings, for example "{KEY}" or "@KEY@".""",
|
||||
attrs = {
|
||||
"data": attr.label_list(
|
||||
allow_files = True,
|
||||
doc = "data dependencies. See" +
|
||||
" https://bazel.build/reference/be/common-definitions#typical.data",
|
||||
),
|
||||
"template": attr.label(
|
||||
mandatory = True,
|
||||
allow_single_file = True,
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
|
||||
# This package aids testing the 'diff_test' rule.
|
||||
|
||||
load("//rules:common_settings.bzl", "string_flag")
|
||||
load("//rules:expand_template.bzl", "expand_template")
|
||||
|
||||
package(
|
||||
|
@ -21,14 +22,28 @@ package(
|
|||
default_testonly = 1,
|
||||
)
|
||||
|
||||
string_flag(
|
||||
name = "my_string_flag",
|
||||
build_setting_default = "VAR",
|
||||
make_variable = "SOMEVAR",
|
||||
)
|
||||
|
||||
expand_template(
|
||||
name = "filled_template",
|
||||
out = "foo/test.yaml",
|
||||
data = [
|
||||
":version",
|
||||
],
|
||||
substitutions = {
|
||||
"@name@": "test",
|
||||
"@version@": "1.1.1",
|
||||
"@path@": "$(rlocationpath :version)",
|
||||
"@toolchain@": "prefix$(SOMEVAR)suffix",
|
||||
},
|
||||
template = "test.tpl.yaml",
|
||||
toolchains = [
|
||||
":my_string_flag",
|
||||
],
|
||||
)
|
||||
|
||||
sh_test(
|
||||
|
|
|
@ -32,6 +32,8 @@ function test_expand_template() {
|
|||
cat "$(rlocation $TEST_WORKSPACE/tests/expand_template/foo/test.yaml)" >"$TEST_log"
|
||||
expect_log 'name: test'
|
||||
expect_log 'version: 1.1.1'
|
||||
expect_log 'tests/expand_template/version.h'
|
||||
expect_log 'toolchain: prefixVARsuffix'
|
||||
}
|
||||
|
||||
run_suite "expand_template_tests test suite"
|
|
@ -1,2 +1,4 @@
|
|||
name: @name@
|
||||
version: @version@
|
||||
path: @path@
|
||||
toolchain: @toolchain@
|
||||
|
|
Loading…
Reference in New Issue