Skip to content

Commit

Permalink
Add cue_cmd rule, using it to implement the cue_dump macro.
Browse files Browse the repository at this point in the history
This will make it easier to use a toolchain configuration for CUE in the future. The `dump` CUE command had to be replaced with a `dumpFile` one due to bazelbuild/bazel#5511.

Bug: 168620448
Bug: 171800001
Change-Id: I54dffdc414ba86592fe088570723e9bb3d455960
  • Loading branch information
SanjayVas committed Feb 1, 2022
1 parent 20bdf96 commit 8a3c44e
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions cue/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,47 @@ def cue_string_field(name, src, identifier, package = None, **kwargs):
tool = "//build/cue:gen-cue-string-field",
**kwargs
)

def _cue_cmd_impl(ctx):
outs = ctx.outputs.outs
outs_dict = {file.basename: file.path for file in outs}

args = ctx.actions.args()
args.add("cmd")
for field, value in ctx.attr.tagged_field_values.items():
args.add("--inject")
if value in outs_dict:
value = outs_dict[value]
args.add(field + "=" + value)
args.add(ctx.attr.command)
args.add_all(ctx.files.srcs)

ctx.actions.run(
outputs = outs,
inputs = ctx.files.srcs,
executable = "cue",
mnemonic = "CueCmd",
arguments = [args],
)

cue_cmd = rule(
implementation = _cue_cmd_impl,
attrs = {
"command": attr.string(
doc = "User-defined CUE shell command to run.",
mandatory = True,
),
"srcs": attr.label_list(
doc = "Source CUE files.",
allow_files = [".cue"],
allow_empty = False,
),
"outs": attr.output_list(
doc = "Output files generated by this command.",
allow_empty = False,
),
"tagged_field_values": attr.string_dict(
doc = "Dict of tagged field to value for injection.",
),
},
)

0 comments on commit 8a3c44e

Please sign in to comment.