set up elm format diffs for the library and component-catalog

This commit is contained in:
Brian Hicks 2023-05-05 08:27:19 -05:00
parent b8a1d3163d
commit a044547808
No known key found for this signature in database
GPG Key ID: C4F324B9CAAB0D50
4 changed files with 83 additions and 2 deletions

7
BUCK
View File

@ -1,5 +1,5 @@
# A list of available rules and their signatures can be found here: https://buck2.build/docs/api/rules/
load("@prelude-nri//:elm.bzl", "elm_docs")
load("@prelude-nri//:elm.bzl", "elm_docs", "elm_format_diffs")
load("@prelude-nri//:node.bzl", "node_modules", "npm_bin", "npm_script_test")
elm_docs(
@ -44,3 +44,8 @@ npm_script_test(
"script/puppeteer-tests.js": "script/puppeteer-tests.js",
},
)
elm_format_diffs(
name = "elm_format_diffs",
srcs = glob(["src/**/*.elm"]),
)

View File

@ -1,4 +1,4 @@
load("@prelude-nri//:elm.bzl", "elm_app")
load("@prelude-nri//:elm.bzl", "elm_app", "elm_format_diffs")
elm_app(
name = "app",
@ -26,3 +26,8 @@ filegroup(
"//:puppeteer",
],
)
elm_format_diffs(
name = "elm_format_diffs",
srcs = glob(["src/**/*.elm"]),
)

View File

@ -111,3 +111,44 @@ elm_app = rule(
),
},
)
def _elm_format_diffs_impl(ctx: "context"):
suggested_changes = []
for src in ctx.attrs.srcs:
suggestion = ctx.actions.declare_output("__suggested_changes__", src.short_path)
ctx.actions.run(
[
"bash",
"-xo", "pipefail",
"-c",
"""
$1 --stdin < $2 | diff -u $2 - > $3
if test "$?" -eq 1; then
# we're fine with files being different; we'll catch that in
# the BXL script.
exit 0
fi
""",
"--",
ctx.attrs._elm_toolchain.get(ElmToolchainInfo).elm_format,
src,
suggestion.as_output(),
],
category = "elm_format_diffs",
identifier = src.short_path,
)
suggested_changes.append(suggestion)
return [DefaultInfo(default_outputs = suggested_changes)]
elm_format_diffs = rule(
impl = _elm_format_diffs_impl,
attrs = {
"srcs": attrs.list(attrs.source()),
"_elm_toolchain": attrs.toolchain_dep(
default="toolchains//:elm",
providers = [ElmToolchainInfo],
),
}
)

View File

@ -26,3 +26,33 @@ genrule(
executable = True,
visibility = ["PUBLIC"],
)
ELM_FORMAT_URL = select({
"config//os:linux": "https://github.com/avh4/elm-format/releases/download/0.8.7/elm-format-0.8.7-linux-x64.tgz",
"config//os:macos": select({
"config//cpu:arm64": "https://github.com/avh4/elm-format/releases/download/0.8.7/elm-format-0.8.7-mac-arm64.tgz",
"config//cpu:x86_64": "https://github.com/avh4/elm-format/releases/download/0.8.7/elm-format-0.8.7-mac-x64.tgz",
}),
})
ELM_FORMAT_SHA256 = select({
"config//os:linux": "44344c7b6f838dc5d9495dfe4253280a698c2251ee8cfa29b6d1a032b6efb13b",
"config//os:macos": select({
"config//cpu:arm64": "d8f898be599fa767d3b6607256e273dd4f62ea7abc41369a068e903159787098",
"config//cpu:x86_64": "064102cd471550beb43ff7eb3dd6ac7c2a1946cf038dbde389873384f62cbdc4",
}),
})
http_archive(
name = "elm_format_archive",
urls = [ELM_FORMAT_URL],
sha256 = ELM_FORMAT_SHA256,
)
genrule(
name = "elm_format_binary",
out = "elm-format",
cmd = "cp $(location :elm_format_archive)/elm-format $OUT",
executable = True,
visibility = ["PUBLIC"],
)