1
1
"extensions for bzlmod"
2
2
3
- load (":repositories.bzl" , "DEFAULT_NODE_REPOSITORY" , "DEFAULT_NODE_VERSION" , "nodejs_register_toolchains" )
3
+ load (
4
+ ":repositories.bzl" ,
5
+ "DEFAULT_NODE_REPOSITORY" ,
6
+ "DEFAULT_NODE_URL" ,
7
+ "DEFAULT_NODE_VERSION" ,
8
+ "nodejs_register_toolchains" ,
9
+ )
10
+
11
+ def _toolchain_repr (toolchain ):
12
+ """ Return a `toolchain` tag object representation useful for diagnostics """
13
+ key_values = [(attr , getattr (toolchain , attr )) for attr in _ATTRS ]
14
+ return ", " .join (["%s = %r" % (attr , value ) for attr , value in key_values if value ])
15
+
16
+ def _toolchains_equal (lhs , rhs ):
17
+ """ Compare two `toolchain` tag objects """
18
+ for attr in _ATTRS :
19
+ if getattr (lhs , attr ) != getattr (rhs , attr ):
20
+ return False
21
+ return True
4
22
5
23
def _toolchain_extension (module_ctx ):
6
24
registrations = {}
@@ -14,54 +32,63 @@ def _toolchain_extension(module_ctx):
14
32
# Prioritize the root-most registration of the default node toolchain version and
15
33
# ignore any further registrations (modules are processed breadth-first)
16
34
continue
17
- if toolchain .node_version == registrations [toolchain .name ].node_version and toolchain .node_version_from_nvmrc == registrations [toolchain .name ].node_version_from_nvmrc :
35
+ if not _toolchains_equal (toolchain , registrations [toolchain .name ]):
36
+ fail ("Multiple conflicting toolchains declared:\n * {}\n * {}" .format (
37
+ _toolchain_repr (toolchain ),
38
+ _toolchain_repr (registrations [toolchain .name ]),
39
+ ))
40
+ else :
18
41
# No problem to register a matching toolchain twice
19
42
continue
20
- fail ("Multiple conflicting toolchains declared for name {} ({} and {})" .format (
21
- toolchain .name ,
22
- toolchain .node_version ,
23
- registrations [toolchain .name ],
24
- ))
25
43
else :
26
- registrations [toolchain .name ] = struct (
27
- node_version = toolchain .node_version ,
28
- node_version_from_nvmrc = toolchain .node_version_from_nvmrc ,
29
- include_headers = toolchain .include_headers ,
30
- )
44
+ registrations [toolchain .name ] = toolchain
31
45
32
46
for k , v in registrations .items ():
33
47
nodejs_register_toolchains (
34
48
name = k ,
35
49
node_version = v .node_version ,
36
50
node_version_from_nvmrc = v .node_version_from_nvmrc ,
51
+ node_urls = v .node_urls ,
37
52
include_headers = v .include_headers ,
38
53
register = False ,
39
54
)
40
55
41
- node = module_extension (
42
- implementation = _toolchain_extension ,
43
- tag_classes = {
44
- "toolchain" : tag_class (attrs = {
45
- "name" : attr .string (
46
- doc = "Base name for generated repositories" ,
47
- default = DEFAULT_NODE_REPOSITORY ,
48
- ),
49
- "node_version" : attr .string (
50
- doc = "Version of the Node.js interpreter" ,
51
- default = DEFAULT_NODE_VERSION ,
52
- ),
53
- "node_version_from_nvmrc" : attr .label (
54
- allow_single_file = True ,
55
- doc = """The .nvmrc file containing the version of Node.js to use.
56
+ _ATTRS = {
57
+ "name" : attr .string (
58
+ doc = "Base name for generated repositories" ,
59
+ default = DEFAULT_NODE_REPOSITORY ,
60
+ ),
61
+ "node_version" : attr .string (
62
+ doc = "Version of the Node.js interpreter" ,
63
+ default = DEFAULT_NODE_VERSION ,
64
+ ),
65
+ "node_version_from_nvmrc" : attr .label (
66
+ allow_single_file = True ,
67
+ doc = """The .nvmrc file containing the version of Node.js to use.
56
68
57
69
If set then the version found in the .nvmrc file is used instead of the one specified by node_version.""" ,
58
- ),
59
- "include_headers" : attr .bool (
60
- doc = """Set headers field in NodeInfo provided by this toolchain.
70
+ ),
71
+ "include_headers" : attr .bool (
72
+ doc = """Set headers field in NodeInfo provided by this toolchain.
61
73
62
74
This setting creates a dependency on a c++ toolchain.
63
75
""" ,
64
- ),
65
- }),
76
+ ),
77
+ "node_urls" : attr .string_list (
78
+ doc = """List of URLs to use to download Node.js.
79
+
80
+ Each entry is a template for downloading a node distribution.
81
+
82
+ The `{version}` parameter is substituted with the `node_version` attribute,
83
+ and `{filename}` with the matching entry from the `node_repositories` attribute.
84
+ """ ,
85
+ default = [DEFAULT_NODE_URL ],
86
+ ),
87
+ }
88
+
89
+ node = module_extension (
90
+ implementation = _toolchain_extension ,
91
+ tag_classes = {
92
+ "toolchain" : tag_class (attrs = _ATTRS ),
66
93
},
67
94
)
0 commit comments