googleapis/load_json.bzl
Google APIs b6b04bf5bd build: specify gapic generator versions in a JSON file
This allows us to easily change versions programmatically for tests.

PiperOrigin-RevId: 772244894
2025-06-16 18:08:18 -07:00

25 lines
818 B
Python

"""Helper to load configuration from JSON files."""
def _load_json_impl(repository_ctx):
"""Create a starlark equivalent of a parsed JSON file
Args:
repository_ctx: repository context
"""
json_data = json.decode(repository_ctx.read(repository_ctx.attr.src))
repository_ctx.file("BUILD", "exports_files([ \"json.bzl\"])")
repository_ctx.file("json.bzl", "{}={}".format(
repository_ctx.attr.variable_name,
repr(json_data),
))
load_json = repository_rule(
implementation = _load_json_impl,
attrs = {
"src": attr.label(allow_single_file = True, default = "//:example-file.json", doc = "a JSON file"),
"variable_name": attr.string(default = "version_json", doc = "symbol to define by the contents of the JSON file"),
},
local = True,
)