Skip to content

Commit

Permalink
chore: do not call python when invoking elm_repository rule (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
kczulko authored Sep 23, 2024
1 parent 958b6a4 commit 5c6613a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 85 deletions.
2 changes: 1 addition & 1 deletion elm/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ genrule(
name = "extract_elm_compiler",
srcs = [":compiler.gz"],
outs = [ "compiler" ],
cmd = "gunzip -c $(SRCS) > $@ && chmod +x $@"
cmd = "gzip -d -c $(SRCS) > $@ && chmod +x $@"
)
elm_toolchain(
Expand Down
10 changes: 10 additions & 0 deletions repository/BUILD.bazel.elm_library.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# file generated by rules_elm
load("@rules_elm//elm:defs.bzl", "elm_library")

elm_library(
name = {{name}},
srcs = glob(["src/**/*.elm"]),
deps = {{deps}},
strip_import_prefix = "src",
visibility = ["//visibility:public"],
)
16 changes: 16 additions & 0 deletions repository/BUILD.bazel.elm_package.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# file generated by rules_elm
load("@rules_elm//elm:defs.bzl", "elm_package")

elm_package(
name = {{name}},
srcs = [
"elm.json",
] + glob([
"**/*.elm",
"**/*.js",
]),
deps = {{deps}},
package_name = {{package_name}},
package_version = {{package_version}},
visibility = ["//visibility:public"],
)
74 changes: 58 additions & 16 deletions repository/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,22 +1,58 @@
load("@bazel_tools//tools/build_defs/repo:utils.bzl", _patch = "patch")

def _elm_repository_impl(ctx):
ctx.download_and_extract(
url = ctx.attr.urls,
sha256 = ctx.attr.sha256,
type = ctx.attr.type,
stripPrefix = ctx.attr.strip_prefix,
# getting rid of the canonical repo name representation
# TODO: handle possible +
def fix_bzl_mod_repo_name(name):
return name.split("~")[-1]

def _elm_repository_impl(rctx):
rctx.download_and_extract(
url = rctx.attr.urls,
sha256 = rctx.attr.sha256,
type = rctx.attr.type,
stripPrefix = rctx.attr.strip_prefix,
)
_patch(ctx)
_patch(rctx)

metadata = json.decode(rctx.read("elm.json"))
name_arg = rctx.attr.name

result = ctx.execute([
"python",
ctx.path(Label("@rules_elm//repository:generate_build_files.py")),
ctx.attr.name,
])
if result.return_code:
fail("failed to generate BUILD files for %s: %s" %
(ctx.name, result.stderr))
deps = [
"@elm_package_" + name.replace("/", "_").replace("-", "_")
for name in metadata["dependencies"].keys()
]

name = metadata["name"]
substitutions = {
"{{name}}": json.encode(fix_bzl_mod_repo_name(name_arg)),
"{{deps}}": json.encode(deps)
}
if name.startswith("elm/") or name.startswith("elm-explorations/"):
# For official elm/* and elm-explorations/* packages, generate a
# true elm_package(). These packages tend to rely on features
# that are not exposed publicly (e.g., Elm.Kernel.*).
substitutions = substitutions | {
"{{package_name}}": json.encode(metadata["name"]),
"{{package_version}}": json.encode(metadata["version"]),
}
rctx.template(
"BUILD.bazel",
rctx.attr._elm_package_tpl,
substitutions = substitutions,
executable = False,
)
else:
# For other libraries, simply emit an elm_library(). This is
# more useful in our case, as 'elm make' doesn't return sensible
# errors about incorrect dependencies (e.g., version mismatches)
# in our case. By declaring a library, we disable package
# version checking entirely.
rctx.template(
"BUILD.bazel",
rctx.attr._elm_library_tpl,
substitutions = substitutions,
executable = False,
)

elm_repository = repository_rule(
attrs = {
Expand All @@ -25,12 +61,18 @@ elm_repository = repository_rule(
"strip_prefix": attr.string(),
"type": attr.string(),
"sha256": attr.string(),

# Patches to apply after extraction.
"patches": attr.label_list(),
"patch_tool": attr.string(default = "patch"),
"patch_args": attr.string_list(default = ["-p0"]),
"patch_cmds": attr.string_list(default = []),
# build file templates
"_elm_library_tpl": attr.label(
default = Label("@rules_elm//repository:BUILD.bazel.elm_library.tpl")
),
"_elm_package_tpl": attr.label(
default = Label("@rules_elm//repository:BUILD.bazel.elm_package.tpl")
)
},
implementation = _elm_repository_impl,
)
68 changes: 0 additions & 68 deletions repository/generate_build_files.py

This file was deleted.

0 comments on commit 5c6613a

Please sign in to comment.