2021-12-09 00:47:45 +00:00
|
|
|
"""Public API for jq"""
|
|
|
|
|
|
|
|
load("//lib/private:jq.bzl", _jq_lib = "jq_lib")
|
|
|
|
|
|
|
|
_jq_rule = rule(
|
|
|
|
attrs = _jq_lib.attrs,
|
|
|
|
implementation = _jq_lib.implementation,
|
|
|
|
toolchains = ["@aspect_bazel_lib//lib:jq_toolchain_type"],
|
|
|
|
)
|
|
|
|
|
2021-12-20 20:47:39 +00:00
|
|
|
def jq(name, srcs, filter = None, filter_file = None, args = [], out = None, **kwargs):
|
2021-12-09 00:47:45 +00:00
|
|
|
"""Invoke jq with a filter on a set of json input files.
|
|
|
|
|
|
|
|
For jq documentation, see https://stedolan.github.io/jq/.
|
|
|
|
|
2022-02-04 20:13:10 +00:00
|
|
|
To use this rule you must register the jq toolchain in your WORKSPACE:
|
|
|
|
|
|
|
|
```starlark
|
|
|
|
load("@aspect_bazel_lib//lib:repositories.bzl", "register_jq_toolchains")
|
2022-07-02 01:45:31 +00:00
|
|
|
|
|
|
|
register_jq_toolchains()
|
2022-02-04 20:13:10 +00:00
|
|
|
```
|
|
|
|
|
2021-12-16 00:02:28 +00:00
|
|
|
Usage examples:
|
|
|
|
|
|
|
|
```starlark
|
2022-02-04 20:13:10 +00:00
|
|
|
load("@aspect_bazel_lib//lib:jq.bzl", "jq")
|
|
|
|
|
2021-12-16 00:02:28 +00:00
|
|
|
# Remove fields from package.json
|
|
|
|
jq(
|
|
|
|
name = "no_dev_deps",
|
|
|
|
srcs = ["package.json"],
|
|
|
|
filter = "del(.devDependencies)",
|
|
|
|
)
|
|
|
|
|
|
|
|
# Merge bar.json on top of foo.json
|
|
|
|
jq(
|
|
|
|
name = "merged",
|
|
|
|
srcs = ["foo.json", "bar.json"],
|
|
|
|
filter = ".[0] * .[1]",
|
|
|
|
args = ["--slurp"],
|
|
|
|
out = "foobar.json",
|
|
|
|
)
|
|
|
|
|
|
|
|
# Long filters can be split over several lines with comments
|
|
|
|
jq(
|
|
|
|
name = "complex",
|
|
|
|
srcs = ["a.json", "b.json"],
|
|
|
|
filter = \"\"\"
|
|
|
|
.[0] as $a
|
|
|
|
# Take select fields from b.json
|
|
|
|
| (.[1] | {foo, bar, tags}) as $b
|
|
|
|
# Merge b onto a
|
|
|
|
| ($a * $b)
|
|
|
|
# Combine 'tags' array from both
|
|
|
|
| .tags = ($a.tags + $b.tags)
|
|
|
|
# Add new field
|
|
|
|
+ {\\\"aspect_is_cool\\\": true}
|
|
|
|
\"\"\",
|
|
|
|
args = ["--slurp"],
|
|
|
|
)
|
2021-12-20 20:47:39 +00:00
|
|
|
|
|
|
|
# Load filter from a file
|
|
|
|
jq(
|
|
|
|
name = "merged",
|
|
|
|
srcs = ["foo.json", "bar.json"],
|
|
|
|
filter_file = "filter.txt",
|
|
|
|
args = ["--slurp"],
|
|
|
|
out = "foobar.json",
|
|
|
|
)
|
2022-06-17 16:46:11 +00:00
|
|
|
|
|
|
|
# Convert genquery output to json
|
|
|
|
genquery(
|
|
|
|
name = "deps",
|
|
|
|
expression = "deps(//some:target)",
|
|
|
|
scope = ["//some:target"],
|
|
|
|
)
|
|
|
|
|
|
|
|
jq(
|
|
|
|
name = "deps_json",
|
|
|
|
srcs = [":deps"],
|
|
|
|
args = [
|
|
|
|
"--raw-input",
|
|
|
|
"--slurp",
|
|
|
|
],
|
|
|
|
filter = "{ deps: split(\"\\n\") | map(select(. | length > 0)) }",
|
|
|
|
)
|
2021-12-16 00:02:28 +00:00
|
|
|
```
|
|
|
|
|
2021-12-09 00:47:45 +00:00
|
|
|
Args:
|
|
|
|
name: Name of the rule
|
2022-06-17 16:46:11 +00:00
|
|
|
srcs: List of input files
|
2021-12-20 20:47:39 +00:00
|
|
|
filter: Filter expression (https://stedolan.github.io/jq/manual/#Basicfilters)
|
|
|
|
filter_file: File containing filter expression (alternative to `filter`)
|
|
|
|
args: Additional args to pass to jq
|
2021-12-09 00:47:45 +00:00
|
|
|
out: Name of the output json file; defaults to the rule name plus ".json"
|
2021-12-20 20:47:39 +00:00
|
|
|
**kwargs: Other common named parameters such as `tags` or `visibility`
|
2021-12-09 00:47:45 +00:00
|
|
|
"""
|
2022-06-22 01:27:49 +00:00
|
|
|
default_name = name + ".json"
|
|
|
|
if not out and not default_name in srcs:
|
|
|
|
out = default_name
|
2021-12-09 00:47:45 +00:00
|
|
|
|
|
|
|
_jq_rule(
|
|
|
|
name = name,
|
|
|
|
srcs = srcs,
|
|
|
|
filter = filter,
|
2021-12-20 20:47:39 +00:00
|
|
|
filter_file = filter_file,
|
2021-12-09 00:47:45 +00:00
|
|
|
args = args,
|
|
|
|
out = out,
|
2021-12-18 00:55:00 +00:00
|
|
|
**kwargs
|
2021-12-09 00:47:45 +00:00
|
|
|
)
|