Repository navigation
Expand file tree
/
Copy pathextensions.bzl
More file actions
122 lines (112 loc) · 3.99 KB
/
Copy pathextensions.bzl
File metadata and controls
122 lines (112 loc) · 3.99 KB
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
# SPDX-License-Identifier: MIT
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
# See https://github.com/astral-sh/setup-uv/pull/809 (for uv, but ruff is the same).
GITHUB_RELEASES_PREFIX = "https://github.com/astral-sh/ruff/releases/download/"
ASTRAL_MIRROR_PREFIX = "https://releases.astral.sh/github/ruff/releases/download/"
def _toolchain_repository_rule_impl(repo_ctx):
build = """
load(
"@ruff_prebuilt//:toolchains.bzl",
"define_ruff_prebuilt_toolchain",
)
""".replace("\n ", "\n")
repositories = json.decode(repo_ctx.attr.repositories)
for repo_name, details in repositories.items():
binary_name = "ruff.exe" if details["os"] == "windows" else "ruff"
build += """
define_ruff_prebuilt_toolchain(
ruff_executable = "@{repo_name}//:{binary_name}",
cpu = "{cpu}",
os = "{os}",
)
""".replace("\n ", "\n").format(
repo_name = repo_name,
binary_name = binary_name,
**details
)
repo_ctx.file("BUILD.bazel", build)
_toolchain_repository_rule = repository_rule(
implementation = _toolchain_repository_rule_impl,
attrs = {
"repositories": attr.string(
doc = "JSON-encoded dictionary of (`repo_name`, `details`) items that communicates the repositories to create and the requirement arguments (i.e., `cpu` and `os`) for each.",
mandatory = True,
),
},
)
_toolchain_tag_class = tag_class(
attrs = {
"name": attr.string(
doc = "The toolchain name.",
default = "ruff_prebuilt_toolchain",
),
"versions_json": attr.label(
doc = "The version lockfile to load for this toolchain.",
default = "@ruff_prebuilt//:versions.json",
),
},
)
def _sanitize_chars(name):
# See https://bazel.build/rules/lib/globals/module#module for valid chars.
result = ""
for char in name.lower().elems():
if (("0" <= char) and (char <= "9") or
("a" <= char) and (char <= "z") or
char == "." or
char == "-" or
char == "_"):
result += char
else:
result += "_"
return result
def _add_mirrors(urls):
result = []
for url in urls:
if url.startswith(GITHUB_RELEASES_PREFIX):
suffix = url[len(GITHUB_RELEASES_PREFIX):]
result.append(ASTRAL_MIRROR_PREFIX + suffix)
result.append(url)
return result
def _per_tag_impl(module_ctx, toolchain_tag):
versions = json.decode(module_ctx.read(toolchain_tag.versions_json))
version = versions["current"]
available = versions["available"]
downloads = available[version]["downloads"]
repositories = {}
for download in downloads.values():
cpu = download["cpu"]
os = download["os"]
integrity = download["integrity"]
strip_prefix = download["strip_prefix"]
urls = _add_mirrors(download["urls"])
repo_name = "ruff_v{version}_{cpu}_{os}_repo".format(
version = _sanitize_chars(version),
os = os,
cpu = cpu,
)
http_archive(
name = repo_name,
build_file_content = "exports_files(glob(['**/*']))\n",
urls = urls,
integrity = integrity,
strip_prefix = strip_prefix,
)
repositories[repo_name] = struct(
os = os,
cpu = cpu,
)
_toolchain_repository_rule(
name = toolchain_tag.name,
repositories = json.encode(repositories),
)
def _ruff_prebuilt_extension_impl(module_ctx):
for module in module_ctx.modules:
for toolchain_tag in module.tags.toolchain:
_per_tag_impl(module_ctx, toolchain_tag)
return module_ctx.extension_metadata(reproducible = True)
ruff_prebuilt_extension = module_extension(
implementation = _ruff_prebuilt_extension_impl,
tag_classes = {
"toolchain": _toolchain_tag_class,
},
)