Some checks failed
CI / build (push) Failing after 12s
GitOrigin-RevId: 6370f6ea785709295b6abcf9c60717cacf3ac432
70 lines
3 KiB
Python
70 lines
3 KiB
Python
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
|
|
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
|
|
|
|
def _jsonnet_deps_impl(ctx):
|
|
# 1. Read the lockfile
|
|
# We assume the lockfile is at //third_party/jsonnet/jsonnetfile.lock.json
|
|
# relative to the module root.
|
|
lockfile_content = ctx.read(ctx.path(Label("//third_party/jsonnet:jsonnetfile.lock.json")))
|
|
lockfile = json.decode(lockfile_content)
|
|
|
|
# 2. Iterate over dependencies
|
|
for dep in lockfile.get("dependencies", []):
|
|
source = dep.get("source", {})
|
|
git_source = source.get("git", {})
|
|
|
|
remote = git_source.get("remote")
|
|
commit = dep.get("version")
|
|
subdir = git_source.get("subdir", "")
|
|
|
|
# Construct a unique name for the repository
|
|
# e.g., https://github.com/grafana/jsonnet-libs.git (subdir: tanka-util) -> github_com_grafana_jsonnet_libs_tanka_util
|
|
name_part = remote.removeprefix("https://").removeprefix("http://").removesuffix(".git")
|
|
repo_name = name_part.replace("/", "_").replace("-", "_").replace(".", "_")
|
|
if subdir:
|
|
repo_name += "_" + subdir.replace("/", "_").replace("-", "_").replace(".", "_")
|
|
subdir += "/"
|
|
|
|
# Optional: Use http_archive for GitHub to avoid git overhead
|
|
if "github.com" in remote:
|
|
# https://github.com/user/repo.git -> https://github.com/user/repo/archive/<commit>.zip
|
|
url = remote.removesuffix(".git") + "/archive/" + commit + ".zip"
|
|
http_archive(
|
|
name = repo_name,
|
|
url = url,
|
|
# We interpret "subdir" by generating a BUILD file that exposes files in that subdir.
|
|
# Because the archive unzips to "repo-commit", we usually strip_prefix.
|
|
# However, predicting strip_prefix is hard without knowing the repo name in the zip.
|
|
# GitHub usually uses "repo-name-commit" or "repo-name-tag".
|
|
# For safety in this prototype, we'll strip the first component.
|
|
strip_prefix = remote.split("/")[-1].removesuffix(".git") + "-" + commit,
|
|
build_file_content = """
|
|
load("@rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_library")
|
|
|
|
jsonnet_library(
|
|
name = "lib",
|
|
srcs = glob(["{subdir}**/*.libsonnet", "{subdir}**/*.jsonnet", "{subdir}**/*.json"], allow_empty = True),
|
|
visibility = ["//visibility:public"],
|
|
)
|
|
""".format(subdir = subdir)
|
|
)
|
|
else:
|
|
# Fallback to git_repository for non-GitHub
|
|
git_repository(
|
|
name = repo_name,
|
|
remote = remote,
|
|
commit = commit,
|
|
build_file_content = """
|
|
load("@rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_library")
|
|
|
|
jsonnet_library(
|
|
name = "lib",
|
|
srcs = glob(["{subdir}**/*.libsonnet", "{subdir}**/*.jsonnet", "{subdir}**/*.json"], allow_empty = True),
|
|
visibility = ["//visibility:public"],
|
|
)
|
|
""".format(subdir = subdir)
|
|
)
|
|
|
|
jsonnet_deps = module_extension(
|
|
implementation = _jsonnet_deps_impl,
|
|
)
|