|
| 1 | +# `@cilium_proxy_repo` repository rule for managing the repo and querying its metadata. |
| 2 | + |
| 3 | +def _cilium_proxy_repo_impl(repository_ctx): |
| 4 | + """This provides information about the Envoy repository |
| 5 | +
|
| 6 | + You can access the current project and api versions and the path to the repository in |
| 7 | + .bzl/BUILD files as follows: |
| 8 | +
|
| 9 | + ```starlark |
| 10 | + load("@cilium_proxy_repo//:version.bzl", "VERSION", "API_VERSION") |
| 11 | + ``` |
| 12 | +
|
| 13 | + `*VERSION` can be used to derive version-specific rules and can be passed |
| 14 | + to the rules. |
| 15 | +
|
| 16 | + The `VERSION`s and also the local `PATH` to the repo can be accessed in |
| 17 | + python libraries/binaries. By adding `@cilium_proxy_repo` to `deps` they become |
| 18 | + importable through the `cilium_proxy_repo` namespace. |
| 19 | +
|
| 20 | + As the `PATH` is local to the machine, it is generally only useful for |
| 21 | + jobs that will run locally. |
| 22 | +
|
| 23 | + This can be useful, for example, for bazel run jobs to run bazel queries that cannot be run |
| 24 | + within the constraints of a `genquery`, or that otherwise need access to the repository |
| 25 | + files. |
| 26 | +
|
| 27 | + Project and repo data can be accessed in JSON format using `@cilium_proxy_repo//:project`, eg: |
| 28 | +
|
| 29 | + ```starlark |
| 30 | + load("@aspect_bazel_lib//lib:jq.bzl", "jq") |
| 31 | +
|
| 32 | + jq( |
| 33 | + name = "project_version", |
| 34 | + srcs = ["@cilium_proxy_repo//:data"], |
| 35 | + out = "version.txt", |
| 36 | + args = ["-r"], |
| 37 | + filter = ".version", |
| 38 | + ) |
| 39 | +
|
| 40 | + ``` |
| 41 | +
|
| 42 | + """ |
| 43 | + repo_version_path = repository_ctx.path(repository_ctx.attr.envoy_version) |
| 44 | + api_version_path = repository_ctx.path(repository_ctx.attr.envoy_api_version) |
| 45 | + version = repository_ctx.read(repo_version_path).strip() |
| 46 | + api_version = repository_ctx.read(api_version_path).strip() |
| 47 | + repository_ctx.file("version.bzl", "VERSION = '%s'\nAPI_VERSION = '%s'" % (version, api_version)) |
| 48 | + repository_ctx.file("path.bzl", "PATH = '%s'" % repo_version_path.dirname) |
| 49 | + repository_ctx.file("__init__.py", "PATH = '%s'\nVERSION = '%s'\nAPI_VERSION = '%s'" % (repo_version_path.dirname, version, api_version)) |
| 50 | + repository_ctx.file("WORKSPACE", "") |
| 51 | + repository_ctx.file("BUILD", ''' |
| 52 | +load("@rules_python//python:defs.bzl", "py_library") |
| 53 | +load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary") |
| 54 | +load("//:path.bzl", "PATH") |
| 55 | +
|
| 56 | +py_library( |
| 57 | + name = "cilium_proxy_repo", |
| 58 | + srcs = ["__init__.py"], |
| 59 | + visibility = ["//visibility:public"], |
| 60 | +) |
| 61 | +
|
| 62 | +''') |
| 63 | + |
| 64 | +_cilium_proxy_repo = repository_rule( |
| 65 | + implementation = _cilium_proxy_repo_impl, |
| 66 | + attrs = { |
| 67 | + #todo(nezdolik) add cilium version |
| 68 | + "envoy_version": attr.label(default = "@envoy//:VERSION.txt"), |
| 69 | + "envoy_api_version": attr.label(default = "@envoy//:API_VERSION.txt"), |
| 70 | + }, |
| 71 | +) |
| 72 | + |
| 73 | +def cilium_proxy_repo(): |
| 74 | + if "cilium_proxy_repo" not in native.existing_rules().keys(): |
| 75 | + _cilium_proxy_repo(name = "cilium_proxy_repo") |
0 commit comments