Skip to content

Commit 564fe42

Browse files
test: add an example Bazel module that uses the jsonnet module
This is mostly so that I can set it up to be used in the module registry presubmit, but it does also act as an example.
1 parent e504463 commit 564fe42

File tree

9 files changed

+101
-0
lines changed

9 files changed

+101
-0
lines changed

examples/bazel/.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
common --lockfile_mode=off

examples/bazel/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MODULE.bazel.lock

examples/bazel/BUILD

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
load("@rules_python//python:defs.bzl", "py_binary")
2+
3+
cc_binary(
4+
name = "use_cpp_lib",
5+
srcs = ["use_cpp_lib.cc"],
6+
deps = ["@jsonnet//cpp:libjsonnet++"],
7+
)
8+
9+
cc_binary(
10+
name = "use_c_lib",
11+
srcs = ["use_c_lib.c"],
12+
deps = ["@jsonnet//core:libjsonnet"],
13+
)
14+
15+
py_binary(
16+
name = "use_py_module",
17+
srcs = ["use_py_module.py"],
18+
deps = ["@jsonnet//python:_jsonnet"],
19+
)
20+
21+
genrule(
22+
name = "use_jsonnet_cmd",
23+
srcs = ["example.jsonnet"],
24+
outs = ["example.out"],
25+
cmd = "$(location @jsonnet//cmd:jsonnet) $< > $@",
26+
tools = ["@jsonnet//cmd:jsonnet"],
27+
)

examples/bazel/MODULE.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Example of using jsonnet by depending on the Bazel module.
2+
3+
bazel_dep(name = "rules_python", version = "1.2.0")
4+
bazel_dep(name = "jsonnet")
5+
local_path_override(module_name = "jsonnet", path = "../..")

examples/bazel/example.jsonnet

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
local Person(name='Alice') = {
2+
name: name,
3+
welcome: 'Hello ' + name + '!',
4+
};
5+
{
6+
person1: Person(),
7+
person2: Person('Bob'),
8+
}

examples/bazel/expect.out

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"person1": {
3+
"name": "Alice",
4+
"welcome": "Hello Alice!"
5+
},
6+
"person2": {
7+
"name": "Bob",
8+
"welcome": "Hello Bob!"
9+
}
10+
}

examples/bazel/use_c_lib.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <libjsonnet.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
int main(int argc, char **argv) {
6+
int ret = 0;
7+
fprintf(stderr, "Jsonnet version: %s\n", jsonnet_version());
8+
struct JsonnetVm *vm = jsonnet_make();
9+
if (!vm) { return 1; }
10+
11+
int err = 0;
12+
char *result = jsonnet_evaluate_file(vm, "example.jsonnet", &err);
13+
if (!err) {
14+
fprintf(stdout, "%s\n", result ? result : "-- no output --");
15+
} else {
16+
fprintf(stderr, "%s\n", result ? result : "-- no output --");
17+
ret = 2;
18+
}
19+
20+
jsonnet_destroy(vm);
21+
return ret;
22+
}

examples/bazel/use_cpp_lib.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <libjsonnet++.h>
2+
#include <iostream>
3+
4+
int main(int argc, char **argv) {
5+
int ret = 0;
6+
std::cerr << "Jsonnet version: " << jsonnet::Jsonnet::version() << std::endl;
7+
8+
jsonnet::Jsonnet vm;
9+
if (!vm.init()) {
10+
return 1;
11+
}
12+
13+
std::string result;
14+
bool ok = vm.evaluateFile("example.jsonnet", &result);
15+
if (ok) {
16+
std::cout << result << std::endl;
17+
} else {
18+
std::cerr << vm.lastError() << std::endl;
19+
ret = 2;
20+
}
21+
return ret;
22+
}

examples/bazel/use_py_module.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import sys
2+
import _jsonnet
3+
4+
result = _jsonnet.evaluate_file("example.jsonnet")
5+
sys.stdout.write(result)

0 commit comments

Comments
 (0)