feat: expand_template does execpath and vars substitutions (#6)
This commit is contained in:
parent
c2aa4762d4
commit
2dfacb4d88
|
@ -15,6 +15,11 @@ Template expansion
|
|||
This performs a simple search over the template file for the keys in substitutions,
|
||||
and replaces them with the corresponding values.
|
||||
|
||||
Values may also use location templates as documented in [expand_locations](#expand_locations)
|
||||
as well as [configuration variables] such as `$(BINDIR)`, `$(TARGET_CPU)`, and `$(COMPILATION_MODE)`.
|
||||
|
||||
[configuration variables]: https://docs.bazel.build/versions/main/skylark/lib/ctx.html#var
|
||||
|
||||
|
||||
**ATTRIBUTES**
|
||||
|
||||
|
|
|
@ -164,13 +164,18 @@ def expand_variables(ctx, s, outs = [], output_dir = False, attribute_name = "ar
|
|||
return ctx.expand_make_variables(attribute_name, s, additional_substitutions)
|
||||
|
||||
def _expand_template_impl(ctx):
|
||||
template = ctx.file.template
|
||||
substitutions = ctx.attr.substitutions
|
||||
|
||||
subs = dict({
|
||||
k: expand_locations(ctx, v, ctx.attr.data)
|
||||
for k, v in substitutions.items()
|
||||
}, **ctx.var)
|
||||
|
||||
ctx.actions.expand_template(
|
||||
template = ctx.file.template,
|
||||
template = template,
|
||||
output = ctx.outputs.out,
|
||||
substitutions = {
|
||||
k: expand_locations(v, ctx.attr.data)
|
||||
for k, v in ctx.attr.substitutions.items()
|
||||
},
|
||||
substitutions = subs,
|
||||
is_executable = ctx.attr.is_executable,
|
||||
)
|
||||
|
||||
|
@ -179,6 +184,11 @@ expand_template = struct(
|
|||
|
||||
This performs a simple search over the template file for the keys in substitutions,
|
||||
and replaces them with the corresponding values.
|
||||
|
||||
Values may also use location templates as documented in [expand_locations](#expand_locations)
|
||||
as well as [configuration variables] such as `$(BINDIR)`, `$(TARGET_CPU)`, and `$(COMPILATION_MODE)`.
|
||||
|
||||
[configuration variables]: https://docs.bazel.build/versions/main/skylark/lib/ctx.html#var
|
||||
""",
|
||||
implementation = _expand_template_impl,
|
||||
attrs = {
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
"tests for libs"
|
||||
|
||||
load("@bazel_skylib//rules:write_file.bzl", "write_file")
|
||||
load("//lib:expand_make_vars.bzl", "expand_template")
|
||||
load(":expand_make_vars_test.bzl", "expand_make_vars_test_suite")
|
||||
load(":utils_test.bzl", "utils_test_suite")
|
||||
load(":paths_test.bzl", "paths_test_suite")
|
||||
|
@ -7,3 +11,32 @@ expand_make_vars_test_suite()
|
|||
paths_test_suite()
|
||||
|
||||
utils_test_suite()
|
||||
|
||||
write_file(
|
||||
name = "gen_template",
|
||||
out = "template.txt",
|
||||
content = [
|
||||
"#!/bin/bash",
|
||||
"set -o errexit",
|
||||
"""[ "{thing}" == "stuff" ]""",
|
||||
"""[ "%path%" == "BINDIR/lib/tests/template.txt" ]""",
|
||||
],
|
||||
)
|
||||
|
||||
expand_template(
|
||||
name = "expand_template",
|
||||
out = "expand_template_test.sh",
|
||||
data = [":gen_template"],
|
||||
is_executable = True,
|
||||
substitutions = {
|
||||
"{thing}": "stuff",
|
||||
"%path%": "$(execpath :gen_template)",
|
||||
"BINDIR": "$(BINDIR)",
|
||||
},
|
||||
template = "template.txt",
|
||||
)
|
||||
|
||||
sh_test(
|
||||
name = "expand_template_test",
|
||||
srcs = ["expand_template"],
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue