diff --git a/docs/expand_template_doc.md b/docs/expand_template_doc.md index 34bbb3a..b5f4add 100755 --- a/docs/expand_template_doc.md +++ b/docs/expand_template_doc.md @@ -7,7 +7,7 @@ A rule that performs template expansion. ## expand_template
-expand_template(name, out, substitutions, template)
+expand_template(name, data, out, substitutions, template)
 
Template expansion @@ -24,6 +24,7 @@ explicitly add delimiters to the key strings, for example "{KEY}" or "@KEY@". | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | +| data | data dependencies. See https://bazel.build/reference/be/common-definitions#typical.data | List of labels | optional | `[]` | | out | The destination of the expanded file. | Label | required | | | substitutions | A dictionary mapping strings to their substitutions. | Dictionary: String -> String | required | | | template | The template file to expand. | Label | required | | diff --git a/rules/expand_template.bzl b/rules/expand_template.bzl index f95507e..2c1a7e3 100644 --- a/rules/expand_template.bzl +++ b/rules/expand_template.bzl @@ -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, diff --git a/tests/expand_template/BUILD b/tests/expand_template/BUILD index 07fc579..6dbe34a 100644 --- a/tests/expand_template/BUILD +++ b/tests/expand_template/BUILD @@ -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( diff --git a/tests/expand_template/template_test.sh b/tests/expand_template/template_test.sh index 10099a4..3ab7c12 100755 --- a/tests/expand_template/template_test.sh +++ b/tests/expand_template/template_test.sh @@ -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" \ No newline at end of file +run_suite "expand_template_tests test suite" diff --git a/tests/expand_template/test.tpl.yaml b/tests/expand_template/test.tpl.yaml index 03d9fdd..1c8636d 100644 --- a/tests/expand_template/test.tpl.yaml +++ b/tests/expand_template/test.tpl.yaml @@ -1,2 +1,4 @@ name: @name@ version: @version@ +path: @path@ +toolchain: @toolchain@