Skip to content

Commit

Permalink
Add node_urls parameter to bzlmod toolchain in the node extension
Browse files Browse the repository at this point in the history
  • Loading branch information
redsun82 committed Jun 24, 2024
1 parent 6f9c4ad commit fad6edd
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions nodejs/extensions.bzl
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
"extensions for bzlmod"

load(":repositories.bzl", "DEFAULT_NODE_REPOSITORY", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")
load(
":repositories.bzl",
"DEFAULT_NODE_REPOSITORY",
"DEFAULT_NODE_URL",
"DEFAULT_NODE_VERSION",
"nodejs_register_toolchains",
)

def _toolchain_extension(module_ctx):
registrations = {}
for mod in module_ctx.modules:
for toolchain in mod.tags.toolchain:
if toolchain.name != DEFAULT_NODE_REPOSITORY and not mod.is_root:
fail("Only the root module may provide a name for the node toolchain.")

if toolchain.name in registrations.keys():
if toolchain.name == DEFAULT_NODE_REPOSITORY:
# Prioritize the root-most registration of the default node toolchain version and
# ignore any further registrations (modules are processed breadth-first)
continue
if toolchain.node_version == registrations[toolchain.name].node_version and toolchain.node_version_from_nvmrc == registrations[toolchain.name].node_version_from_nvmrc:
if (toolchain.node_version != registrations[toolchain.name].node_version or
toolchain.node_version_from_nvmrc != registrations[toolchain.name].node_version_from_nvmrc):
fail("Multiple conflicting toolchains declared for name {} ({} and {})".format(
toolchain.name,
toolchain.node_version,
registrations[toolchain.name].node_version,
))
elif toolchain.node_urls != registrations[toolchain.name].node_urls:
fail("Multiple toolchains with conflicting urls declared for name {} ({} and {})".format(
toolchain.name,
toolchain.node_urls,
registrations[toolchain.name].node_urls,
))
else:
# No problem to register a matching toolchain twice
continue
fail("Multiple conflicting toolchains declared for name {} ({} and {})".format(
toolchain.name,
toolchain.node_version,
registrations[toolchain.name],
))
else:
registrations[toolchain.name] = struct(
node_version = toolchain.node_version,
node_version_from_nvmrc = toolchain.node_version_from_nvmrc,
include_headers = toolchain.include_headers,
)
registrations[toolchain.name] = toolchain

for k, v in registrations.items():
for k, t in registrations.items():
nodejs_register_toolchains(
name = k,
node_version = v.node_version,
node_version_from_nvmrc = v.node_version_from_nvmrc,
include_headers = v.include_headers,
node_version = t.node_version,
node_version_from_nvmrc = t.node_version_from_nvmrc,
node_urls = t.node_urls,
include_headers = t.include_headers,
register = False,
)

Expand Down Expand Up @@ -62,6 +72,10 @@ If set then the version found in the .nvmrc file is used instead of the one spec
This setting creates a dependency on a c++ toolchain.
""",
),
"node_urls": attr.string_list(
doc = "Custom list of URL formats to use to download NodeJS, containing {version} and {filename}",
default = [DEFAULT_NODE_URL],
),
}),
},
)

0 comments on commit fad6edd

Please sign in to comment.