Skip to content

Commit

Permalink
feat: support bzlmod
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Dec 20, 2021
1 parent 7e88e26 commit 9e609b9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 10 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rules_swc_dependencies()
load("//swc:repositories.bzl", "swc_register_toolchains")

swc_register_toolchains(
name = "swc",
name = "default_swc",
swc_version = "v1.2.119",
)

Expand Down
4 changes: 2 additions & 2 deletions examples/macro/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def my_ts_project(name, srcs = []):
$(execpath @aspect_rules_swc//swc:cli) \\
$(location in.ts) \\
-o $@""",
toolchains = ["@swc_toolchains//:resolved_toolchain"],
toolchains = ["@default_swc_toolchains//:resolved_toolchain"],
tools = [
"@aspect_rules_swc//swc:cli",
"@swc_toolchains//:resolved_toolchain",
"@default_swc_toolchains//:resolved_toolchain",
],
)
8 changes: 6 additions & 2 deletions swc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ write_file(
nodejs_binary(
name = "cli",
data = [
"@npm__swc_cli-0.1.52",
"@npm__swc_core-1.2.119",
# Don't use the shorter syntax here because the user or bzlmod
# might use repository mapping to give a different name
# to the repository, e.g.
# @aspect_rules_swc.0.2.0.swc.npm__swc_core-1.2.119
"@npm__swc_cli-0.1.52//:pkg",
"@npm__swc_core-1.2.119//:pkg",
],
entry_point = "main.js",
visibility = ["//visibility:public"],
Expand Down
6 changes: 3 additions & 3 deletions swc/dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def rules_swc_dependencies():
maybe(
http_archive,
name = "aspect_rules_js",
sha256 = "e8576a74a7e80b873179514cf1ad48b62b18ae024e74200ecd40ae6dc00c515a",
strip_prefix = "rules_js-0.3.0",
url = "https://github.com/aspect-build/rules_js/archive/v0.3.0.tar.gz",
sha256 = "dd78b4911b7c2e6c6e919b85cd31572cc15e5baa62b9e7354d8a1065c67136e3",
strip_prefix = "rules_js-0.3.1",
url = "https://github.com/aspect-build/rules_js/archive/v0.3.1.tar.gz",
)
35 changes: 35 additions & 0 deletions swc/extensions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"extensions for bzlmod"

load(":repositories.bzl", "swc_register_toolchains")

swc_toolchain = tag_class(attrs = {
"name": attr.string(doc = "Base name for generated repositories"),
"swc_version": attr.string(doc = "Version of the swc rust binding"),
})

def _toolchain_extension(module_ctx):
registrations = {}
for mod in module_ctx.modules:
for toolchain in mod.tags.toolchain:
if toolchain.name in registrations.keys():
if toolchain.swc_version == registrations[toolchain.name]:
# No problem to register a matching toolchain twice
continue
fail("Multiple conflicting toolchains declared for name {} ({} and {}".format(
toolchain.name,
toolchain.swc_version,
registrations[toolchain.name],
))
else:
registrations[toolchain.name] = toolchain.swc_version
for name, swc_version in registrations.items():
swc_register_toolchains(
name = name,
swc_version = swc_version,
register = False,
)

swc = module_extension(
implementation = _toolchain_extension,
tag_classes = {"toolchain": swc_toolchain},
)
7 changes: 5 additions & 2 deletions swc/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ swc_repositories = repository_rule(
)

# Wrapper macro around everything above, this is the primary API
def swc_register_toolchains(name, **kwargs):
def swc_register_toolchains(name, register = True, **kwargs):
"""Convenience macro for users which does typical setup.
- create a repository for each built-in platform like "swc_linux_amd64" -
Expand All @@ -52,6 +52,8 @@ def swc_register_toolchains(name, **kwargs):
Users can avoid this macro and do these steps themselves, if they want more control.
Args:
name: base name for all created repos, like "swc"
register: whether to call through to native.register_toolchains.
Should be True for WORKSPACE users, but false when used under bzlmod extension
**kwargs: passed to each node_repositories call
"""
for platform in PLATFORMS.keys():
Expand All @@ -60,7 +62,8 @@ def swc_register_toolchains(name, **kwargs):
platform = platform,
**kwargs
)
native.register_toolchains("@%s_toolchains//:%s_toolchain" % (name, platform))
if register:
native.register_toolchains("@%s_toolchains//:%s_toolchain" % (name, platform))

toolchains_repo(
name = name + "_toolchains",
Expand Down

0 comments on commit 9e609b9

Please sign in to comment.