-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathexpand_template.bzl
133 lines (116 loc) · 5.15 KB
/
expand_template.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"expand_template rule"
load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("//lib:stamping.bzl", "STAMP_ATTRS", "maybe_stamp")
load(":expand_variables.bzl", "expand_variables")
def _expand_substitutions(ctx, output, substitutions):
result = {}
for k, v in substitutions.items():
result[k] = expand_variables(ctx, ctx.expand_location(v, targets = ctx.attr.data), outs = [output], attribute_name = "substitutions")
return result
def _expand_template_impl(ctx):
output = ctx.outputs.out
if not output:
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
stamp = maybe_stamp(ctx)
if stamp:
substitutions = dicts.add(substitutions, _expand_substitutions(ctx, output, ctx.attr.stamp_substitutions))
substitutions_out = ctx.actions.declare_file("{}_substitutions.json".format(ctx.label.name))
ctx.actions.write(
output = substitutions_out,
content = json.encode(substitutions),
)
inputs = [
ctx.file.template,
stamp.volatile_status_file,
stamp.stable_status_file,
substitutions_out,
]
args = ctx.actions.args()
args.add(ctx.file.template)
args.add(output)
args.add(substitutions_out)
args.add(stamp.volatile_status_file)
args.add(stamp.stable_status_file)
args.add(ctx.attr.is_executable)
ctx.actions.run(
arguments = [args],
outputs = [output],
inputs = inputs,
executable = expand_template_info.bin,
toolchain = "@aspect_bazel_lib//lib:expand_template_toolchain_type",
)
else:
ctx.actions.expand_template(
template = ctx.file.template,
output = output,
substitutions = substitutions,
is_executable = ctx.attr.is_executable,
)
all_outs = [output]
runfiles = ctx.runfiles(files = all_outs)
return [DefaultInfo(files = depset(all_outs), runfiles = runfiles)]
expand_template_lib = struct(
doc = """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](https://github.com/bazel-contrib/bazel-lib/blob/main/docs/expand_make_vars.md#expand_locations)
as well as [configuration variables](https://docs.bazel.build/versions/main/skylark/lib/ctx.html#var)
such as `$(BINDIR)`, `$(TARGET_CPU)`, and `$(COMPILATION_MODE)` as documented in
[expand_variables](https://github.com/bazel-contrib/bazel-lib/blob/main/docs/expand_make_vars.md#expand_variables).
""",
implementation = _expand_template_impl,
toolchains = ["@aspect_bazel_lib//lib:expand_template_toolchain_type"],
attrs = dicts.add({
"data": attr.label_list(
doc = "List of targets for additional lookup information.",
allow_files = True,
),
"is_executable": attr.bool(
doc = "Whether to mark the output file as executable.",
),
"out": attr.output(
doc = """Where to write the expanded file.
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.
Otherwise, `out` defaults to `[name].txt`.
""",
),
"stamp_substitutions": attr.string_dict(
doc = """Mapping of strings to substitutions.
There are overlaid on top of substitutions when stamping is enabled
for the target.
Substitutions can contain $(execpath :target) and $(rootpath :target)
expansions, $(MAKEVAR) expansions and {{STAMP_VAR}} expansions when
stamping is enabled for the target.
""",
),
"substitutions": attr.string_dict(
doc = """Mapping of strings to substitutions.
Substitutions can contain $(execpath :target) and $(rootpath :target)
expansions, $(MAKEVAR) expansions and {{STAMP_VAR}} expansions when
stamping is enabled for the target.
""",
),
"template": attr.label(
doc = "The template file to expand.",
mandatory = True,
allow_single_file = True,
),
}, **STAMP_ATTRS),
)
expand_template = rule(
doc = expand_template_lib.doc,
implementation = expand_template_lib.implementation,
toolchains = expand_template_lib.toolchains,
attrs = expand_template_lib.attrs,
)