Skip to content

Commit

Permalink
feat: introduce JSON schema for metadata format (bazelbuild#1149)
Browse files Browse the repository at this point in the history
This allows for nice developer ergonomics in the editor, which can now provide autocomplete and error highlighting in metadata.json files, if a $schema property is added.
It also adds a layer of validation for things like required fields in these json documents.

Fixes bazelbuild#1148
  • Loading branch information
alexeagle authored Nov 23, 2023
1 parent 32f01f3 commit a1c2b57
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tools/__pycache__
tools/node_modules
/bazel-*
21 changes: 21 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("@npm//tools:ajv-cli/package_json.bzl", ajv = "bin")

_METADATA_FILES = glob(["modules/*/metadata.json"])

[
ajv.ajv_test(
name = "test_metadata." + s.removesuffix("/metadata.json"),
args = [
"validate",
"-s",
"$(execpath metadata.schema.json)",
"-d",
"$(execpath %s)" % s,
],
data = [
s,
"metadata.schema.json",
],
)
for s in _METADATA_FILES
]
10 changes: 10 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "aspect_rules_js", version = "1.34.1")
bazel_dep(name = "rules_python", version = "0.26.0")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
Expand All @@ -20,3 +21,12 @@ pip.parse(
requirements_lock = "//tools:requirements_lock.txt",
)
use_repo(pip, "pip")

npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm", dev_dependency = True)

npm.npm_translate_lock(
name = "npm",
pnpm_lock = "//tools:pnpm-lock.yaml",
)

use_repo(npm, "npm")
50 changes: 50 additions & 0 deletions metadata.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"$id": "https://github.com/bazelbuild/bazel-central-registry/modules/metadata.schema.json",
"title": "Metadata for a Bazel module",
"type": "object",
"properties": {
"$schema": { "type": "string" },
"homepage": {"type": "string"},
"maintainers": {
"description": "Individuals who can be notified when the module requires human attention",
"type": "array",
"items": {
"type": "object",
"properties": {
"github": {
"type": "string",
"description": "maintainer's github username"
},
"email": {
"type": "string",
"description": "maintainer's email address"
},
"name": {
"type": "string",
"description": "maintainer's name"
}
}
}
},
"repository": {
"type": "array",
"items": {
"description": "repository, typically in the form github:[github org]/[github repo]",
"type": "string"
}
},
"versions": {
"type": "array",
"items": {
"description": "semver version",
"type": "string"
}
},
"yanked_versions": {
"type": "object",
"additionalProperties": true
}
},
"additionalProperties": false,
"required": ["homepage", "versions"]
}
2 changes: 1 addition & 1 deletion modules/rules_nixpkgs_core/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"versions": [
"0.10.0"
],
"yanked_versions": []
"yanked_versions": {}
}
3 changes: 3 additions & 0 deletions tools/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("@python_versions//3.11:defs.bzl", compile_pip_requirements_3_11 = "compile_pip_requirements")
load("@pip//:requirements.bzl", "requirement")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

compile_pip_requirements_3_11(
name = "requirements",
Expand Down
6 changes: 6 additions & 0 deletions tools/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"devDependencies": {
"ajv-cli": "*"
}
}
173 changes: 173 additions & 0 deletions tools/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a1c2b57

Please sign in to comment.