Skip to content

Commit

Permalink
feat: don't require 'out' on expand_template (#798)
Browse files Browse the repository at this point in the history
In a lot of cases the name of the generated file is unimportant.
For example in bazel-contrib/rules_oci#534 I wanted to remove 'out' in a bunch of these calls.
  • Loading branch information
alexeagle authored Mar 26, 2024
1 parent 0fc8388 commit fea9515
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/expand_template.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions lib/private/expand_template.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ def _expand_substitutions(ctx, output, substitutions):
def _expand_template_impl(ctx):
output = ctx.outputs.out
if not output:
if not ctx.file.template or not ctx.file.template.is_source:
fail("Template must be a source file if out is not specified")
output = ctx.actions.declare_file(ctx.file.template.basename, sibling = ctx.file.template)
if ctx.file.template and ctx.file.template.is_source:
output = ctx.actions.declare_file(ctx.file.template.basename, sibling = ctx.file.template)
else:
output = ctx.actions.declare_file(ctx.attr.name + ".txt")

substitutions = _expand_substitutions(ctx, output, ctx.attr.substitutions)
expand_template_info = ctx.toolchains["@aspect_bazel_lib//lib:expand_template_toolchain_type"].expand_template_info
Expand Down Expand Up @@ -90,12 +91,15 @@ such as `$(BINDIR)`, `$(TARGET_CPU)`, and `$(COMPILATION_MODE)` as documented in
"out": attr.output(
doc = """Where to write the expanded file.
If unset, the template must be a source file and the output file
will be named the same as the template file and outputted to the same
If the `template` is a source file, then `out` defaults to
be named the same as the template file and outputted to the same
workspace-relative path. In this case there will be no pre-declared
label for the output file. It can be referenced by the target label
instead. This pattern is similar to `copy_to_bin` but with substitutions on
the copy.""",
the copy.
Otherwise, `out` defaults to `[name].txt`.
""",
),
"stamp_substitutions": attr.string_dict(
doc = """Mapping of strings to substitutions.
Expand Down
4 changes: 2 additions & 2 deletions lib/tests/expand_template/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ assert_contains(
expected = "WORKSPACE:",
)

# No `out` specified, because we don't care what the name of the generated file is.
expand_template(
name = "inline_template",
out = "inline.txt",
substitutions = {"line2": "line3"},
template = [
"line1",
Expand All @@ -71,7 +71,7 @@ expand_template(

assert_contains(
name = "inline_template_test",
actual = ":inline.txt",
actual = ":inline_template",
expected = "line3",
)

Expand Down

0 comments on commit fea9515

Please sign in to comment.