Skip to content

Commit 1c35fea

Browse files
feat: show a tested full end-to-end example
1 parent fe98db2 commit 1c35fea

File tree

17 files changed

+137
-30
lines changed

17 files changed

+137
-30
lines changed

.bazelignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# see "Ignored" section in full-examples/README.md
2+
full-examples

.github/cspell.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,22 @@ language: en
1818
# words - list of words to be always considered correct: an Allowlist per-se
1919
words:
2020
- allanc
21+
- argsjson
2122
- bazel
2223
- bazelisk
2324
- bazelrc
25+
- bitrot
26+
- buildifier
2427
- bzlmod
2528
- chickenandpork
2629
- ibazel
2730
- jqlang
2831
- ruleset
2932
- slamdev
3033
- starlark
34+
- genrule
35+
- kwargs
36+
- runfiles
37+
- softlinks
38+
- srcs
39+
- stardoc

.github/workflows/ci.bazelrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ build --repository_cache=$HOME/.cache/bazel-repo
77

88
# XDG_CACHE_HOME is where bazelisk -- if used -- will cache downloads; workflows use setup-bazelisk
99
# which aliases 'bazel' to bazelisk. We inject this into the test runner env like an action_env
10-
test --test_env=XDG_CACHE_HOME
10+
# used with "common" rather than "test" because "test" differing from build discards analysis cache.
11+
common --test_env=XDG_CACHE_HOME

.github/workflows/ci.yaml

+15-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
# https://github.com/bazelbuild/rules_docker/issues/1524
2424
# https://github.com/bazelbuild/rules_docker/issues/1438
2525
# - windows-latest
26-
bazel: [ 4.2.1, 5.0.0rc4 ]
26+
bazel: [4.2.4, 5.4.1, 6.2.0]
2727
# The type of runner that the job will run on
2828
runs-on: ${{ matrix.os }}
2929

@@ -58,3 +58,17 @@ jobs:
5858
# Bazelisk will download bazel to here
5959
XDG_CACHE_HOME: ~/.cache/bazel-repo
6060
run: bazel --bazelrc=.github/workflows/ci.bazelrc --bazelrc=.bazelrc test ...
61+
- name: Build examples to ensure functionality
62+
# linux and latest
63+
if: ${{ matrix.os == 'ubuntu-latest' && startsWith(matrix.bazel, '6.') }}
64+
run: |
65+
for d in $(find full-examples -name WORKSPACE -exec dirname {} \; ); do
66+
echo "::group::example: ${d}"
67+
echo ${{ matrix.bazel }} > ${d}/.bazelversion
68+
case ${RUNNER_ENVIRONMENT}:${d} in
69+
# opportunity to skip really big tests
70+
# github-hosted:full-examples/huggingface_transformer_question_answering) echo "TOO big: skip in github-hosted" ;;
71+
*) (cd ${d} && bazel test //... --bazelrc=../../.github/workflows/ci.bazelrc --bazelrc=../../.bazelrc && bazel clean );;
72+
esac
73+
echo "::endgroup::"
74+
done

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
body: |
4141
```starlark
4242
http_archive(
43-
name = "bazel_rules_jq",
43+
name = "rules_jq",
4444
sha256 = "${{ env.RELEASE_CHECKSUM }}",
4545
url = "https://github.com/jqlang/bazel_rules_jq/releases/download/${{ env.RELEASE_VERSION }}/bazel_rules_jq-v${{ env.RELEASE_VERSION }}.tar.gz",
4646
)

.pre-commit-config.yaml

+2-5
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ repos:
2020
rev: v1.32.0
2121
hooks:
2222
- id: yamllint
23-
args: [
24-
'-d',
25-
'{extends: relaxed, rules: {line-length: {max: 120}}}'
26-
]
23+
args: ["-d", "{extends: relaxed, rules: {line-length: {max: 120}}}"]
2724
# Enforce that commit messages allow for later changelog generation
2825
- repo: https://github.com/commitizen-tools/commitizen
2926
rev: 3.12.0
@@ -63,5 +60,5 @@ repos:
6360
# a local allowlist of dictionary words in .github/cspell.yaml. This can get comical --
6461
# adding new words in every PR -- but the cost/benefit balance may avoid some embarassing
6562
# typos
66-
args: [ '--config', '.github/cspell.yaml' ]
63+
args: ["--config", ".github/cspell.yaml"]
6764
types: [file, markdown]

BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary")
12
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
23
load("@rules_pkg//:pkg.bzl", "pkg_tar")
3-
load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary")
44

55
gazelle_binary(
66
name = "gazelle_bin",

README.md

+20-12
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,38 @@ Examples will be created in the "examples" directory that should be cut-n-pasted
88
(and soon your MODULE.bazel).
99

1010
The intent of this work is to:
11-
* offer a traditional WORKSPACE and newer bzlmod capability
12-
* re-home slamdev's work in making binaries available,
13-
* address the wider array of jq binaries now produced in a release,
14-
* adapt to the new naming schema, and
15-
* offer a fallback of building JQ if a release is not available or the user so chooses
1611

12+
- offer a traditional WORKSPACE and newer bzlmod capability
13+
- re-home slamdev's work in making binaries available,
14+
- address the wider array of jq binaries now produced in a release,
15+
- adapt to the new naming schema, and
16+
- offer a fallback of building JQ if a release is not available or the user so chooses
1717

1818
## Installation
1919

2020
### Traditional WORKSPACE
2121

22-
(soon)
23-
24-
Goal is something as simple as Slamdev's work:
2522
```starlark
2623
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2724

28-
http_archive( name = "rules_jq", ...)
25+
http_archive(
26+
name = "rules_jq",
27+
sha256 = "52771b5f7fb5017b0f5a3816b46624d2b9a336ddc178b261de988d969cd329d8",
28+
url = "https://github.com/jqlang/bazel_rules_jq/releases/download/0.0.1/bazel_rules_jq-v0.0.1.tar.gz",
29+
)
30+
31+
load("@rules_jq//jq:repositories.bzl", "jq_register_toolchains", "rules_jq_dependencies")
2932

30-
load("@rules_jq//some_file.bzl", "jq_register_toolchains", "rules_jq_dependencies")
3133
rules_jq_dependencies()
32-
jq_register_toolchains(name = "jq")
34+
35+
jq_register_toolchains(
36+
name = "jq",
37+
jq_version = "1.6",
38+
)
3339
```
34-
(resulting in a @jq//:jq binary or similar)
40+
41+
The resulting JQ binary cannot be directly called, but can be used via `jq_eval()`
42+
(see example/eval) or as a Makefile-style variable (see example/genrule)
3543

3644
### bzlmod Modular Format
3745

WORKSPACE

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ jq_register_toolchains(
1919
jq_version = "1.6",
2020
)
2121

22+
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
23+
2224
############################################
2325
# Gazelle, for generating bzl_library targets
2426
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
25-
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
2627

2728
go_rules_dependencies()
2829

example/eval/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
load("@bazel_skylib//rules:write_file.bzl", "write_file")
21
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
2+
load("@bazel_skylib//rules:write_file.bzl", "write_file")
33
load("//jq:defs.bzl", "jq_eval")
44

55
jq_eval(

example/eval/sample.yaml

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"a": {
3-
"b": "123"
4-
}
5-
}
1+
{ "a": { "b": "123" } }

example/genrule/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
load("@bazel_skylib//rules:write_file.bzl", "write_file")
21
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
2+
load("@bazel_skylib//rules:write_file.bzl", "write_file")
33

44
genrule(
55
name = "actual",

full-examples/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Full Examples
2+
3+
These examples are intended to show more completely how to use the `jq` rules: rather than just
4+
snippets of untested code that may bitrot over time, these examples are tested on each PR, and are
5+
nearly identical to what a consumer of the rules would use -- except that the local_repository()
6+
rule should be replaced with a http_archive() rule as documented in WORKSPACE (and MODULE.bazel if
7+
appropriate)
8+
9+
Sure, the actual examples are those used as validation in the "example" directory, but I intend to
10+
add to them. ...and test on every PR (at least with a linux build on the latest bazel)
11+
12+
## Running Full Examples
13+
14+
with reference to the changes in ci.yaml, you can run a full-example as follows:
15+
16+
```
17+
(cd full-examples/basic && bazel build //...)
18+
```
19+
20+
Of course, if you have an HTTP cache or federated bazel cache, you can reuse the work done in a
21+
build at the root workspace.
22+
23+
## Ignored
24+
25+
NOTE that the `full-examples` directory is ignored from a wildcard build: the `bazel-*` softlinks,
26+
and the recursive nature of the resulting tree-transversal after a build is done, becomes a really
27+
problematic endless loop. Don't remove them.

full-examples/basic/BUILD.bazel

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
2+
load("@bazel_skylib//rules:write_file.bzl", "write_file")
3+
load("@rules_jq//jq:defs.bzl", "jq_eval")
4+
5+
jq_eval(
6+
name = "actual",
7+
srcs = ["sample.yaml"],
8+
filter = ".a.b",
9+
raw_output = True,
10+
)
11+
12+
write_file(
13+
name = "expected",
14+
out = "out.yaml",
15+
content = [
16+
"123",
17+
"",
18+
],
19+
)
20+
21+
diff_test(
22+
name = "check_eval",
23+
file1 = ":expected",
24+
file2 = ":actual",
25+
)

full-examples/basic/WORKSPACE

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
workspace(name = "basic")
2+
3+
# When using in your project, this can be a direct cut-n-paste, however you should replace the
4+
# local_repository with a http_archive() block similar to the following, using the version and
5+
# sha256 matching your desired release
6+
#
7+
# load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
8+
#
9+
# http_archive(
10+
# name = "rules_jq",
11+
# sha256 = "52771b5f7fb5017b0f5a3816b46624d2b9a336ddc178b261de988d969cd329d8",
12+
# url = "https://github.com/jqlang/bazel_rules_jq/releases/download/0.0.1/bazel_rules_jq-v0.0.1.tar.gz",
13+
#)
14+
15+
local_repository(
16+
name = "rules_jq",
17+
path = "../..",
18+
)
19+
20+
load("@rules_jq//jq:repositories.bzl", "jq_register_toolchains", "rules_jq_dependencies")
21+
22+
rules_jq_dependencies()
23+
24+
jq_register_toolchains(
25+
name = "jq",
26+
jq_version = "1.6",
27+
)

full-examples/basic/sample.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "a": { "b": "123" } }

jq/private/rules/eval.bzl

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"jq_eval rule"
22

3-
load("@bazel_skylib//lib:collections.bzl", "collections")
43
load("@bazel_skylib//lib:shell.bzl", "shell")
54

65
_DOC = "Defines a jq eval execution."

0 commit comments

Comments
 (0)