Skip to content

Commit 2dfacb4

Browse files
authored
feat: expand_template does execpath and vars substitutions (#6)
1 parent c2aa476 commit 2dfacb4

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

docs/expand_make_vars.md

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ Template expansion
1515
This performs a simple search over the template file for the keys in substitutions,
1616
and replaces them with the corresponding values.
1717

18+
Values may also use location templates as documented in [expand_locations](#expand_locations)
19+
as well as [configuration variables] such as `$(BINDIR)`, `$(TARGET_CPU)`, and `$(COMPILATION_MODE)`.
20+
21+
[configuration variables]: https://docs.bazel.build/versions/main/skylark/lib/ctx.html#var
22+
1823

1924
**ATTRIBUTES**
2025

lib/private/expand_make_vars.bzl

+15-5
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,18 @@ def expand_variables(ctx, s, outs = [], output_dir = False, attribute_name = "ar
164164
return ctx.expand_make_variables(attribute_name, s, additional_substitutions)
165165

166166
def _expand_template_impl(ctx):
167+
template = ctx.file.template
168+
substitutions = ctx.attr.substitutions
169+
170+
subs = dict({
171+
k: expand_locations(ctx, v, ctx.attr.data)
172+
for k, v in substitutions.items()
173+
}, **ctx.var)
174+
167175
ctx.actions.expand_template(
168-
template = ctx.file.template,
176+
template = template,
169177
output = ctx.outputs.out,
170-
substitutions = {
171-
k: expand_locations(v, ctx.attr.data)
172-
for k, v in ctx.attr.substitutions.items()
173-
},
178+
substitutions = subs,
174179
is_executable = ctx.attr.is_executable,
175180
)
176181

@@ -179,6 +184,11 @@ expand_template = struct(
179184
180185
This performs a simple search over the template file for the keys in substitutions,
181186
and replaces them with the corresponding values.
187+
188+
Values may also use location templates as documented in [expand_locations](#expand_locations)
189+
as well as [configuration variables] such as `$(BINDIR)`, `$(TARGET_CPU)`, and `$(COMPILATION_MODE)`.
190+
191+
[configuration variables]: https://docs.bazel.build/versions/main/skylark/lib/ctx.html#var
182192
""",
183193
implementation = _expand_template_impl,
184194
attrs = {

lib/tests/BUILD.bazel

+33
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"tests for libs"
2+
3+
load("@bazel_skylib//rules:write_file.bzl", "write_file")
4+
load("//lib:expand_make_vars.bzl", "expand_template")
15
load(":expand_make_vars_test.bzl", "expand_make_vars_test_suite")
26
load(":utils_test.bzl", "utils_test_suite")
37
load(":paths_test.bzl", "paths_test_suite")
@@ -7,3 +11,32 @@ expand_make_vars_test_suite()
711
paths_test_suite()
812

913
utils_test_suite()
14+
15+
write_file(
16+
name = "gen_template",
17+
out = "template.txt",
18+
content = [
19+
"#!/bin/bash",
20+
"set -o errexit",
21+
"""[ "{thing}" == "stuff" ]""",
22+
"""[ "%path%" == "BINDIR/lib/tests/template.txt" ]""",
23+
],
24+
)
25+
26+
expand_template(
27+
name = "expand_template",
28+
out = "expand_template_test.sh",
29+
data = [":gen_template"],
30+
is_executable = True,
31+
substitutions = {
32+
"{thing}": "stuff",
33+
"%path%": "$(execpath :gen_template)",
34+
"BINDIR": "$(BINDIR)",
35+
},
36+
template = "template.txt",
37+
)
38+
39+
sh_test(
40+
name = "expand_template_test",
41+
srcs = ["expand_template"],
42+
)

0 commit comments

Comments
 (0)