-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
repo.bzl
200 lines (171 loc) · 5.45 KB
/
repo.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# `@envoy_repo` repository rule for managing the repo and querying its metadata.
def _envoy_repo_impl(repository_ctx):
"""This provides information about the Envoy repository
You can access the current project and api versions and the path to the repository in
.bzl/BUILD files as follows:
```starlark
load("@envoy_repo//:version.bzl", "VERSION", "API_VERSION")
```
`*VERSION` can be used to derive version-specific rules and can be passed
to the rules.
The `VERSION`s and also the local `PATH` to the repo can be accessed in
python libraries/binaries. By adding `@envoy_repo` to `deps` they become
importable through the `envoy_repo` namespace.
As the `PATH` is local to the machine, it is generally only useful for
jobs that will run locally.
This can be useful, for example, for bazel run jobs to run bazel queries that cannot be run
within the constraints of a `genquery`, or that otherwise need access to the repository
files.
Project and repo data can be accessed in JSON format using `@envoy_repo//:project`, eg:
```starlark
load("@aspect_bazel_lib//lib:jq.bzl", "jq")
jq(
name = "project_version",
srcs = ["@envoy_repo//:data"],
out = "version.txt",
args = ["-r"],
filter = ".version",
)
```
"""
repo_version_path = repository_ctx.path(repository_ctx.attr.envoy_version)
api_version_path = repository_ctx.path(repository_ctx.attr.envoy_api_version)
version = repository_ctx.read(repo_version_path).strip()
api_version = repository_ctx.read(api_version_path).strip()
repository_ctx.file("version.bzl", "VERSION = '%s'\nAPI_VERSION = '%s'" % (version, api_version))
repository_ctx.file("path.bzl", "PATH = '%s'" % repo_version_path.dirname)
repository_ctx.file("__init__.py", "PATH = '%s'\nVERSION = '%s'\nAPI_VERSION = '%s'" % (repo_version_path.dirname, version, api_version))
repository_ctx.file("WORKSPACE", "")
repository_ctx.file("BUILD", '''
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
load("//:path.bzl", "PATH")
py_library(
name = "envoy_repo",
srcs = ["__init__.py"],
visibility = ["//visibility:public"],
)
py_console_script_binary(
name = "get_project_json",
pkg = "@base_pip3//envoy_base_utils",
script = "envoy.project_data",
data = [":__init__.py"],
)
genrule(
name = "generate_release_hash_bin",
outs = ["generate_release_hash.sh"],
cmd = """
echo "
#!/usr/bin/env bash
set -e -o pipefail
git ls-remote --tags https://github.com/envoyproxy/envoy \\\\
| grep -E 'refs/tags/v[0-9]+\\\\.[0-9]+\\\\.[0-9]+$$' \\\\
| sort -u \\\\
| sha256sum \\\\
| cut -d ' ' -f 1" > $@
chmod +x $@
"""
)
sh_binary(
name = "generate_release_hash",
srcs = [":generate_release_hash_bin"],
visibility = ["//visibility:public"],
)
# This sets a default hash based on currently visible tagged versions.
# Its very questionably hermetic, making assumptions about git, the repo remotes and so on.
# The general idea here is to make this cache blow any time there are release changes.
# You can use the above sh_binary to generate a custom/correct hash to override below.
genrule(
name = "default_release_hash",
outs = ["default_release_hash.txt"],
cmd = """
$(location :generate_release_hash) > $@
""",
stamp = True,
tags = ["no-remote-exec"],
tools = [":generate_release_hash"],
)
label_flag(
name = "release-hash",
build_setting_default = ":default_release_hash",
visibility = ["//visibility:public"],
)
genrule(
name = "project",
outs = ["project.json"],
cmd = """
$(location :get_project_json) $$(dirname $(location @envoy//:VERSION.txt)) > $@
""",
tools = [
":get_project_json",
":release-hash",
"@envoy//:VERSION.txt",
"@envoy//changelogs",
],
visibility = ["//visibility:public"],
)
py_console_script_binary(
name = "release",
args = [
"release",
PATH,
"--release-message-path=$(location @envoy//changelogs:summary)",
],
data = [
":__init__.py",
"@envoy//changelogs:summary",
],
pkg = "@base_pip3//envoy_base_utils",
script = "envoy.project",
)
py_console_script_binary(
name = "dev",
args = [
"dev",
PATH,
],
pkg = "@base_pip3//envoy_base_utils",
script = "envoy.project",
data = [":__init__.py"],
)
py_console_script_binary(
name = "sync",
args = [
"sync",
PATH,
],
pkg = "@base_pip3//envoy_base_utils",
script = "envoy.project",
data = [":__init__.py"],
)
py_console_script_binary(
name = "publish",
args = [
"publish",
PATH,
],
pkg = "@base_pip3//envoy_base_utils",
script = "envoy.project",
data = [":__init__.py"],
)
py_console_script_binary(
name = "trigger",
args = [
"trigger",
PATH,
],
pkg = "@base_pip3//envoy_base_utils",
script = "envoy.project",
data = [":__init__.py"],
)
''')
_envoy_repo = repository_rule(
implementation = _envoy_repo_impl,
attrs = {
"envoy_version": attr.label(default = "@envoy//:VERSION.txt"),
"envoy_api_version": attr.label(default = "@envoy//:API_VERSION.txt"),
},
)
def envoy_repo():
if "envoy_repo" not in native.existing_rules().keys():
_envoy_repo(name = "envoy_repo")