55
66load ("@bazel_tools//tools/build_defs/repo:http.bzl" , "http_archive" )
77load ("@bazel_tools//tools/build_defs/repo:utils.bzl" , "maybe" )
8+ load ("@rules_shellcheck//shellcheck:shellcheck_toolchain.bzl" , "shellcheck_toolchain" )
9+
10+ _HUB_BUILD_CONTENT = """\
11+ {toolchains}
12+ """
13+
14+ _CONSTRAINTS = {
15+ "darwin_aarch64" : [
16+ "@platforms//os:macos" ,
17+ "@platforms//cpu:aarch64" ,
18+ ],
19+ "darwin_x86_64" : [
20+ "@platforms//os:macos" ,
21+ "@platforms//cpu:x86_64" ,
22+ ],
23+ "linux_aarch64" : [
24+ "@platforms//os:linux" ,
25+ "@platforms//cpu:aarch64" ,
26+ ],
27+ "linux_armv6hf" : [
28+ "@platforms//cpu:armv6-m" ,
29+ "@platforms//os:linux" ,
30+ ],
31+ "linux_x86_64" : [
32+ "@platforms//os:linux" ,
33+ "@platforms//cpu:x86_64" ,
34+ ],
35+ "windows_x86_64" : [
36+ "@platforms//os:windows" ,
37+ "@platforms//cpu:x86_64" ,
38+ ],
39+ }
40+
41+ _TOOLCHAIN_ENTRY = """\
42+ toolchain(
43+ name = "shellcheck_toolchain_{arch}",
44+ toolchain_type = "@rules_shellcheck//shellcheck:toolchain_type",
45+ toolchain = "{toolchain}",
46+ exec_compatible_with = {constraints},
47+ visibility = ["//visibility:public"],
48+ )
49+ """
50+
51+ def _shellcheck_toolchains_hub_impl (repository_ctx ):
52+ toolchains = []
53+ for toolchain , arch in repository_ctx .attr .toolchains .items ():
54+ toolchains .append (_TOOLCHAIN_ENTRY .format (
55+ arch = arch ,
56+ constraints = repr (_CONSTRAINTS [arch ]),
57+ toolchain = str (toolchain ),
58+ ))
59+
60+ repository_ctx .file ("BUILD.bazel" , _HUB_BUILD_CONTENT .format (
61+ toolchains = "\n " .join (toolchains ),
62+ ))
63+
64+ repository_ctx .file ("WORKSPACE.bazel" , """workspace(name = "{}")""" .format (
65+ repository_ctx .name ,
66+ ))
67+
68+ shellcheck_toolchains_hub = repository_rule (
69+ doc = "A repository rule for defining shellcheck toolchains" ,
70+ implementation = _shellcheck_toolchains_hub_impl ,
71+ attrs = {
72+ "toolchains" : attr .label_keyed_string_dict (
73+ doc = "A mapping of toolchain labels to platforms." ,
74+ mandatory = True ,
75+ ),
76+ },
77+ )
878
979def _urls (arch , version ):
1080 archive_template_name = {
@@ -27,6 +97,48 @@ def _urls(arch, version):
2797 url ,
2898 ]
2999
100+ def create_shellcheck_repository_targets (name , shellcheck ):
101+ """A utility function for defining shellcheck repositories
102+
103+ Args:
104+ name (str): The name of the repository.
105+ shellcheck (str): THe path to the shellcheck binary.
106+ """
107+ visibility = ["//visibility:public" ]
108+
109+ native .exports_files (
110+ [shellcheck ],
111+ visibility = visibility ,
112+ )
113+
114+ native .alias (
115+ name = name ,
116+ actual = shellcheck ,
117+ visibility = visibility ,
118+ )
119+
120+ if shellcheck .endswith (".exe" ):
121+ native .alias (
122+ name = shellcheck [:- 4 ],
123+ actual = shellcheck ,
124+ visibility = visibility ,
125+ )
126+
127+ shellcheck_toolchain (
128+ name = "toolchain" ,
129+ shellcheck = shellcheck ,
130+ visibility = visibility ,
131+ )
132+
133+ _SHELLCHECK_CONTENT = """\
134+ load("@rules_shellcheck//shellcheck/internal:extensions.bzl", "create_shellcheck_repository_targets")
135+
136+ create_shellcheck_repository_targets(
137+ name = "{name}",
138+ shellcheck = "{shellcheck}",
139+ )
140+ """
141+
30142def shellcheck_dependencies ():
31143 """Define shellcheck repositories"""
32144 version = "v0.11.0"
@@ -36,27 +148,40 @@ def shellcheck_dependencies():
36148 "linux_aarch64" : "12b331c1d2db6b9eb13cfca64306b1b157a86eb69db83023e261eaa7e7c14588" ,
37149 "linux_armv6hf" : "8afc50b302d5feeac9381ea114d563f0150d061520042b254d6eb715797c8223" ,
38150 "linux_x86_64" : "8c3be12b05d5c177a04c29e3c78ce89ac86f1595681cab149b65b97c4e227198" ,
151+ "windows_x86_64" : "8a4e35ab0b331c85d73567b12f2a444df187f483e5079ceffa6bda1faa2e740e" ,
39152 }
40153
154+ toolchains = {}
155+
41156 for arch , sha256 in sha256 .items ():
157+ name = "shellcheck_{arch}" .format (arch = arch )
158+
159+ strip_prefix = "shellcheck-{version}" .format (version = version )
160+ shellcheck_bin = "shellcheck"
161+
162+ # Special case, as it is a zip archive with no prefix to strip.
163+ if "windows" in arch :
164+ strip_prefix = None
165+ shellcheck_bin = "shellcheck.exe"
166+
42167 maybe (
43168 http_archive ,
44- name = "shellcheck_{arch}" .format (arch = arch ),
45- strip_prefix = "shellcheck-{version}" .format (version = version ),
46- build_file_content = """exports_files(["shellcheck"])
47- """ ,
169+ name = name ,
170+ strip_prefix = strip_prefix ,
171+ build_file_content = _SHELLCHECK_CONTENT .format (
172+ name = name ,
173+ shellcheck = shellcheck_bin ,
174+ ),
48175 sha256 = sha256 ,
49176 urls = _urls (arch = arch , version = version ),
50177 )
51178
52- # Special case, as it is a zip archive with no prefix to strip.
179+ toolchains ["@{}//:toolchain" .format (name )] = arch
180+
53181 maybe (
54- http_archive ,
55- name = "shellcheck_windows_x86_64" ,
56- build_file_content = """exports_files(["shellcheck"])
57- """ ,
58- sha256 = "8a4e35ab0b331c85d73567b12f2a444df187f483e5079ceffa6bda1faa2e740e" ,
59- urls = _urls (arch = "windows_x86_64" , version = version ),
182+ shellcheck_toolchains_hub ,
183+ name = "shellcheck_toolchains" ,
184+ toolchains = toolchains ,
60185 )
61186
62187def _impl (_ ):
0 commit comments