Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/expand_template_doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 | |
Expand Down
18 changes: 17 additions & 1 deletion rules/expand_template.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ctx.expand_make_variable is marked as "Deprecated" in Bazel documentation. People before me marked it that way and I think we shouldn't introduce new uses.

Please remove it's use.

I believe it's implementable in Starlark, and skylib would be a nice location for such an implementation. (And we might already have one implementation in the C++ rules in builtins).

The rest of the PR seems fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relevant convo https://groups.google.com/g/bazel-discuss/c/WEnG-WocaTc

so you're saying we should make a new function to do the same thing in skylib and use that here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I bumped bazelbuild/bazel#5859 to P3.

And I noticed also #486 which might already be doing that. Would you like to help in review?

There might be other PRs, I haven't gone through all of them yet.

"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(
Expand All @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions tests/expand_template/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,36 @@

# This package aids testing the 'diff_test' rule.

load("//rules:common_settings.bzl", "string_flag")
load("//rules:expand_template.bzl", "expand_template")

package(
default_applicable_licenses = ["//:license"],
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(
Expand Down
4 changes: 3 additions & 1 deletion tests/expand_template/template_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
run_suite "expand_template_tests test suite"
2 changes: 2 additions & 0 deletions tests/expand_template/test.tpl.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
name: @name@
version: @version@
path: @path@
toolchain: @toolchain@