bazel-lib/docs/jq.md

106 lines
2.8 KiB
Markdown
Raw Normal View History

2021-12-09 00:47:45 +00:00
<!-- Generated with Stardoc: http://skydoc.bazel.build -->
Public API for jq
2022-06-10 20:05:19 +00:00
<a id="jq"></a>
2021-12-09 00:47:45 +00:00
## jq
<pre>
2021-12-20 20:47:39 +00:00
jq(<a href="#jq-name">name</a>, <a href="#jq-srcs">srcs</a>, <a href="#jq-filter">filter</a>, <a href="#jq-filter_file">filter_file</a>, <a href="#jq-args">args</a>, <a href="#jq-out">out</a>, <a href="#jq-kwargs">kwargs</a>)
2021-12-09 00:47:45 +00:00
</pre>
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")
register_jq_toolchains()
2022-02-04 20:13:10 +00:00
```
Usage examples:
```starlark
2022-02-04 20:13:10 +00:00
load("@aspect_bazel_lib//lib:jq.bzl", "jq")
# 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",
)
# 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-09 00:47:45 +00:00
**PARAMETERS**
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="jq-name"></a>name | Name of the rule | none |
| <a id="jq-srcs"></a>srcs | List of input files | none |
2021-12-20 20:47:39 +00:00
| <a id="jq-filter"></a>filter | Filter expression (https://stedolan.github.io/jq/manual/#Basicfilters) | <code>None</code> |
| <a id="jq-filter_file"></a>filter_file | File containing filter expression (alternative to <code>filter</code>) | <code>None</code> |
| <a id="jq-args"></a>args | Additional args to pass to jq | <code>[]</code> |
2021-12-09 00:47:45 +00:00
| <a id="jq-out"></a>out | Name of the output json file; defaults to the rule name plus ".json" | <code>None</code> |
2021-12-20 20:47:39 +00:00
| <a id="jq-kwargs"></a>kwargs | Other common named parameters such as <code>tags</code> or <code>visibility</code> | none |
2021-12-09 00:47:45 +00:00