2021-12-09 00:47:45 +00:00
|
|
|
"""Implementation for jq rule"""
|
|
|
|
|
2022-08-22 17:59:38 +00:00
|
|
|
load("//lib:stamping.bzl", "STAMP_ATTRS", "maybe_stamp")
|
|
|
|
|
|
|
|
_jq_attrs = dict({
|
2021-12-09 00:47:45 +00:00
|
|
|
"srcs": attr.label_list(
|
2022-06-17 16:46:11 +00:00
|
|
|
allow_files = True,
|
2021-12-09 00:47:45 +00:00
|
|
|
mandatory = True,
|
|
|
|
allow_empty = True,
|
|
|
|
),
|
2021-12-20 20:47:39 +00:00
|
|
|
"filter": attr.string(),
|
|
|
|
"filter_file": attr.label(allow_single_file = True),
|
2021-12-09 00:47:45 +00:00
|
|
|
"args": attr.string_list(),
|
2022-06-22 01:27:49 +00:00
|
|
|
"out": attr.output(),
|
2022-08-22 17:59:38 +00:00
|
|
|
"_parse_status_file_filter": attr.label(
|
|
|
|
allow_single_file = True,
|
|
|
|
default = Label("//lib/private:parse_status_file.jq"),
|
|
|
|
),
|
|
|
|
}, **STAMP_ATTRS)
|
2021-12-09 00:47:45 +00:00
|
|
|
|
|
|
|
def _jq_impl(ctx):
|
|
|
|
jq_bin = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"].jqinfo.bin
|
|
|
|
|
2022-06-22 01:27:49 +00:00
|
|
|
out = ctx.outputs.out or ctx.actions.declare_file(ctx.attr.name + ".json")
|
2021-12-09 00:47:45 +00:00
|
|
|
args = ctx.attr.args
|
2021-12-20 20:47:39 +00:00
|
|
|
inputs = ctx.files.srcs[:]
|
|
|
|
|
|
|
|
if not ctx.attr.filter and not ctx.attr.filter_file:
|
|
|
|
fail("Must provide a filter or a filter_file")
|
|
|
|
if ctx.attr.filter and ctx.attr.filter_file:
|
|
|
|
fail("Cannot provide both a filter and a filter_file")
|
2021-12-09 00:47:45 +00:00
|
|
|
|
|
|
|
# jq hangs when there are no input sources unless --null-input flag is passed
|
|
|
|
if len(ctx.attr.srcs) == 0 and "-n" not in args and "--null-input" not in args:
|
|
|
|
args = args + ["--null-input"]
|
|
|
|
|
2021-12-20 20:47:39 +00:00
|
|
|
if ctx.attr.filter_file:
|
|
|
|
args = args + ["--from-file '%s'" % ctx.file.filter_file.path]
|
|
|
|
inputs.append(ctx.file.filter_file)
|
|
|
|
|
2022-08-22 17:59:38 +00:00
|
|
|
stamp = maybe_stamp(ctx)
|
|
|
|
if stamp:
|
|
|
|
# create an action that gives a JSON representation of the stamp keys
|
|
|
|
stamp_json = ctx.actions.declare_file("_%s_stamp.json" % ctx.label.name)
|
|
|
|
ctx.actions.run_shell(
|
|
|
|
tools = [jq_bin],
|
|
|
|
inputs = [stamp.stable_status_file, stamp.volatile_status_file, ctx.file._parse_status_file_filter],
|
|
|
|
outputs = [stamp_json],
|
|
|
|
command = "{jq} -s -R -f {filter} {stable} {volatile} > {out}".format(
|
|
|
|
jq = jq_bin.path,
|
|
|
|
filter = ctx.file._parse_status_file_filter.path,
|
|
|
|
stable = stamp.stable_status_file.path,
|
|
|
|
volatile = stamp.volatile_status_file.path,
|
|
|
|
out = stamp_json.path,
|
|
|
|
),
|
|
|
|
mnemonic = "ConvertStatusToJson",
|
|
|
|
)
|
|
|
|
inputs.append(stamp_json)
|
|
|
|
|
|
|
|
# jq says of --argfile:
|
|
|
|
# > Do not use. Use --slurpfile instead.
|
|
|
|
# > (This option is like --slurpfile, but when the file has just one text,
|
|
|
|
# > then that is used, else an array of texts is used as in --slurpfile.)
|
|
|
|
# However there's no indication that it's deprecated. Maybe it's a style convention.
|
|
|
|
# For our purposes, "$STAMP.BUILD_TIMESTAMP" looks a lot more sensible in a BUILD file
|
|
|
|
# than "$STAMP[0].BUILD_TIMESTAMP".
|
|
|
|
args = args + ["--argfile", "STAMP", stamp_json.path]
|
|
|
|
|
2021-12-20 20:47:39 +00:00
|
|
|
cmd = "{jq} {args} {filter} {sources} > {out}".format(
|
2021-12-09 00:47:45 +00:00
|
|
|
jq = jq_bin.path,
|
|
|
|
args = " ".join(args),
|
2021-12-20 20:47:39 +00:00
|
|
|
filter = "'%s'" % ctx.attr.filter if ctx.attr.filter else "",
|
2021-12-09 00:47:45 +00:00
|
|
|
sources = " ".join(["'%s'" % file.path for file in ctx.files.srcs]),
|
|
|
|
out = out.path,
|
|
|
|
)
|
|
|
|
|
|
|
|
ctx.actions.run_shell(
|
|
|
|
tools = [jq_bin],
|
2021-12-20 20:47:39 +00:00
|
|
|
inputs = inputs,
|
2021-12-09 00:47:45 +00:00
|
|
|
outputs = [out],
|
|
|
|
command = cmd,
|
2022-06-03 01:08:16 +00:00
|
|
|
mnemonic = "Jq",
|
2021-12-09 00:47:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return DefaultInfo(files = depset([out]), runfiles = ctx.runfiles([out]))
|
|
|
|
|
|
|
|
jq_lib = struct(
|
|
|
|
attrs = _jq_attrs,
|
|
|
|
implementation = _jq_impl,
|
|
|
|
)
|