Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial bzlmod extension impl for elm_repository #24

Merged
merged 6 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ filegroup(
srcs = [
".bazelignore",
"BUILD.bazel",
"MODULE.bazel",
"WORKSPACE",
"//proto:all_files",
"//elm:all_files",
Expand Down
22 changes: 21 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,32 @@ bazel_dep(name = "aspect_bazel_lib", version = "2.7.7")
bazel_dep(name = "rules_nodejs", version = "6.1.0")
bazel_dep(name = "cgrindel_bazel_starlib", version = "0.20.2")
bazel_dep(name = "rules_proto", version = "6.0.2")
bazel_dep(name = "protobuf", version = "23.1", repo_name = "com_google_protobuf")
bazel_dep(name = "rules_nixpkgs_core", version = "0.12.0", dev_dependency = True)
bazel_dep(name = "rules_bazel_integration_test", version = "0.24.1", dev_dependency = True)

npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm", dev_dependency = True)
elm = use_extension("//elm:extensions.bzl", "elm")
# TODO: define toolchain repo
elm.toolchain()
use_repo(
elm,
"com_github_elm_compiler_linux_x86_64",
"com_github_elm_compiler_osx_x86_64",
"com_github_elm_compiler_osx_arm64",
# TODO: define as elm_repository and link Parser.js via rules_js
"com_github_rtfeldman_node_test_runner"
)

register_toolchains(
"@com_github_edschouten_rules_elm//elm/toolchain:linux_x86_64",
"@com_github_edschouten_rules_elm//elm/toolchain:osx_x86_64",
"@com_github_edschouten_rules_elm//elm/toolchain:osx_arm64",
)

npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm")
npm.npm_translate_lock(
name = "com_github_edschouten_rules_elm_npm",
link_workspace = "com_github_edschouten_rules_elm",
pnpm_lock = "//tools/npm:pnpm-lock.yaml",
verify_node_modules_ignored = "//:.bazelignore",
)
Expand Down
71 changes: 71 additions & 0 deletions elm/extensions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
load("@com_github_edschouten_rules_elm//repository:def.bzl", "elm_repository")

load("@bazel_skylib//lib:sets.bzl", "sets")
load(":deps.bzl", "elm_register_toolchains")

def _repository_fun(attrs):
elm_repository(
name = attrs.name,
urls = attrs.urls,
strip_prefix = attrs.strip_prefix,
type = attrs.type,
sha256 = attrs.sha256,
patches = attrs.patches,
patch_tool = attrs.patch_tool,
patch_args = attrs.patch_args,
patch_cmds = attrs.patch_cmds,
)

def _toolchain_fun(attrs):
elm_register_toolchains(register = False)


def _elm_module_extension_impl(module_ctx):
root_direct_deps = sets.make()
root_direct_dev_deps = sets.make()

is_isolated = getattr(module_ctx, "is_isolated", False)

for mod in module_ctx.modules:
is_root = mod.is_root

for repository in mod.tags.repository:
_repository_fun(repository)
if mod.is_root:
deps = root_direct_dev_deps if module_ctx.is_dev_dependency(repository) else root_direct_deps
sets.insert(deps, repository.name)

for toolchain in mod.tags.toolchain:
print(toolchain)
_toolchain_fun(toolchain)

return module_ctx.extension_metadata(
root_module_direct_deps = sets.to_list(root_direct_deps),
root_module_direct_dev_deps = sets.to_list(root_direct_dev_deps),
)

_repository_tag = tag_class(
attrs = {
"name": attr.string(),
"urls": attr.string_list(),
"strip_prefix": attr.string(),
"type": attr.string(),
"sha256": attr.string(),
"patches": attr.label_list(),
"patch_tool": attr.string(default = "patch"),
"patch_args": attr.string_list(default = ["-p0"]),
"patch_cmds": attr.string_list(default = []),
}
)

_toolchain_tag = tag_class(
attrs = {}
)

elm = module_extension(
_elm_module_extension_impl,
tag_classes = {
"repository": _repository_tag,
"toolchain": _toolchain_tag,
},
)
4 changes: 2 additions & 2 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ ERROR:""",
)

rules_elm_integration_test_each_bazel(
name = "elm_todo_mvc",
name = "elm_todomvc",
bazel_binaries = bazel_binaries,
test_runner = ":output_match_runner",
workspace_path = "elm-todomvc",
Expand Down Expand Up @@ -113,7 +113,7 @@ test_suite(
"elm_binary_no_deps_negative",
"elm_binary_opt",
"elm_test",
"elm_todo_mvc",
"elm_todomvc",
],
bazel_binaries = bazel_binaries,
) + [
Expand Down
44 changes: 41 additions & 3 deletions examples/elm-binary/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
bazel_dep(name = "bazel_skylib", version = "1.5.0")
module(name = "elm-binary", version = "1.0")

bazel_dep(name = "com_github_edschouten_rules_elm")
local_path_override(
module_name = "com_github_edschouten_rules_elm",
path = "../..",
)

bazel_dep(name = "rules_nixpkgs_core", version = "0.12.0", dev_dependency = True)
bazel_dep(name = "rules_nodejs", version = "6.1.0")
bazel_dep(name = "aspect_bazel_lib", version = "2.7.7")
bazel_dep(name = "aspect_rules_js", version = "2.0.0-rc4")

elm = use_extension("@com_github_edschouten_rules_elm//elm:extensions.bzl", "elm")
elm.repository(
name = "elm_package_elm_html",
sha256 = "73b885e0a3d2f9781b1c9bbcc1ee9ac032f503f5ef46a27da3ba617cebbf6fd8",
strip_prefix = "html-1.0.0",
urls = ["https://github.com/elm/html/archive/1.0.0.tar.gz"],
)
elm.repository(
name = "elm_package_elm_json",
sha256 = "d0635f33137e4ad3fc323f96ba280e45dc41afa51076c53d9f04fd92c2cf5c4e",
strip_prefix = "json-1.1.3",
urls = ["https://github.com/elm/json/archive/1.1.3.tar.gz"],
)
elm.repository(
name = "elm_package_elm_virtual_dom",
sha256 = "cf87286ed5d1b31aaf99c6a3368ccd340d1356b1973f1afe5f668c47e22b3b60",
strip_prefix = "virtual-dom-1.0.2",
urls = ["https://github.com/elm/virtual-dom/archive/1.0.2.tar.gz"],
)
elm.repository(
name = "elm_package_elm_core",
sha256 = "6e37b11c88c89a68d19d0c7625f1ef39ed70c59e443def95e4de98d6748c80a7",
strip_prefix = "core-1.0.5",
urls = ["https://github.com/elm/core/archive/1.0.5.tar.gz"],
)
use_repo(
elm,
"elm_package_elm_html",
"elm_package_elm_json",
"elm_package_elm_core",
"elm_package_elm_virtual_dom",
)

nix_repo = use_extension("@rules_nixpkgs_core//extensions:repository.bzl", "nix_repo")
nix_repo.default(name = "nixpkgs")
Expand Down
70 changes: 36 additions & 34 deletions examples/elm-binary/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
# TODO: add legacy WORKSPACE file test case

local_repository(
name = "com_github_edschouten_rules_elm",
path = "../..",
)
# load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

load("@com_github_edschouten_rules_elm//elm:deps.bzl", "elm_register_toolchains")
elm_register_toolchains()
# local_repository(
# name = "com_github_edschouten_rules_elm",
# path = "../..",
# )

load("@com_github_edschouten_rules_elm_npm//:repositories.bzl", "npm_repositories")
npm_repositories()
# load("@com_github_edschouten_rules_elm//elm:deps.bzl", "elm_register_toolchains")
# elm_register_toolchains()

load("@com_github_edschouten_rules_elm//repository:def.bzl", "elm_repository")
elm_repository(
name = "elm_package_elm_core",
sha256 = "6e37b11c88c89a68d19d0c7625f1ef39ed70c59e443def95e4de98d6748c80a7",
strip_prefix = "core-1.0.5",
urls = ["https://github.com/elm/core/archive/1.0.5.tar.gz"],
)
elm_repository(
name = "elm_package_elm_json",
sha256 = "d0635f33137e4ad3fc323f96ba280e45dc41afa51076c53d9f04fd92c2cf5c4e",
strip_prefix = "json-1.1.3",
urls = ["https://github.com/elm/json/archive/1.1.3.tar.gz"],
)
elm_repository(
name = "elm_package_elm_html",
sha256 = "73b885e0a3d2f9781b1c9bbcc1ee9ac032f503f5ef46a27da3ba617cebbf6fd8",
strip_prefix = "html-1.0.0",
urls = ["https://github.com/elm/html/archive/1.0.0.tar.gz"],
)
elm_repository(
name = "elm_package_elm_virtual_dom",
sha256 = "cf87286ed5d1b31aaf99c6a3368ccd340d1356b1973f1afe5f668c47e22b3b60",
strip_prefix = "virtual-dom-1.0.2",
urls = ["https://github.com/elm/virtual-dom/archive/1.0.2.tar.gz"],
)
# load("@com_github_edschouten_rules_elm_npm//:repositories.bzl", "npm_repositories")
# npm_repositories()

# load("@com_github_edschouten_rules_elm//repository:def.bzl", "elm_repository")
# elm_repository(
# name = "elm_package_elm_core",
# sha256 = "6e37b11c88c89a68d19d0c7625f1ef39ed70c59e443def95e4de98d6748c80a7",
# strip_prefix = "core-1.0.5",
# urls = ["https://github.com/elm/core/archive/1.0.5.tar.gz"],
# )
# elm_repository(
# name = "elm_package_elm_json",
# sha256 = "d0635f33137e4ad3fc323f96ba280e45dc41afa51076c53d9f04fd92c2cf5c4e",
# strip_prefix = "json-1.1.3",
# urls = ["https://github.com/elm/json/archive/1.1.3.tar.gz"],
# )
# elm_repository(
# name = "elm_package_elm_html",
# sha256 = "73b885e0a3d2f9781b1c9bbcc1ee9ac032f503f5ef46a27da3ba617cebbf6fd8",
# strip_prefix = "html-1.0.0",
# urls = ["https://github.com/elm/html/archive/1.0.0.tar.gz"],
# )
# elm_repository(
# name = "elm_package_elm_virtual_dom",
# sha256 = "cf87286ed5d1b31aaf99c6a3368ccd340d1356b1973f1afe5f668c47e22b3b60",
# strip_prefix = "virtual-dom-1.0.2",
# urls = ["https://github.com/elm/virtual-dom/archive/1.0.2.tar.gz"],
# )
13 changes: 11 additions & 2 deletions examples/elm-proto/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@com_github_edschouten_rules_elm//proto:def.bzl", "elm_proto_library", "ELM_PROTO_TOOLCHAIN", "ELM_PROTO_DEPS")
load("@com_github_edschouten_rules_elm//proto:def.bzl", "elm_proto_library", "ELM_PROTO_TOOLCHAIN")
load("@com_github_edschouten_rules_elm//proto:elm_proto_toolchain_rule.bzl", "elm_proto_toolchain")
load("@com_github_edschouten_rules_elm//elm:def.bzl", "elm_test")
load("@rules_nodejs//nodejs:toolchain.bzl", "nodejs_toolchain")
Expand Down Expand Up @@ -36,6 +36,16 @@ toolchain(
elm_proto_toolchain(
name = "elm_proto_toolchain_opt_json",
proto_compiler = "@nixpkgs_protobuf//:bin/protoc",
deps = [
"@elm_package_eriktim_elm_protocol_buffers",
"@elm_package_anmolitor_elm_protoc_types",
"@elm_package_danfishgold_base64_bytes",
"@elm_package_elm_file",
"@elm_package_elm_http",
"@elm_package_elm_parser",
"@elm_package_anmolitor_elm_protoc_utils",
"@elm_package_rtfeldman_elm_iso8601_date_strings",
]
)

toolchain(
Expand All @@ -55,7 +65,6 @@ elm_proto_library(
name = "elm_book_proto",
proto = ":book_proto",
plugin_opt_json = "json=decode",
deps = ELM_PROTO_DEPS,
)

elm_test(
Expand Down
Loading