diff --git a/BUCK b/BUCK index 385b937d..ac417b78 100644 --- a/BUCK +++ b/BUCK @@ -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"]), +) diff --git a/component-catalog/BUCK b/component-catalog/BUCK index 496caf1d..9dcf4ee1 100644 --- a/component-catalog/BUCK +++ b/component-catalog/BUCK @@ -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"]), +) diff --git a/prelude-nri/elm.bzl b/prelude-nri/elm.bzl index 4c17e509..8b09db6c 100644 --- a/prelude-nri/elm.bzl +++ b/prelude-nri/elm.bzl @@ -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], + ), + } +) \ No newline at end of file diff --git a/prelude-nri/elm/BUCK b/prelude-nri/elm/BUCK index c54017f9..d5e533d5 100644 --- a/prelude-nri/elm/BUCK +++ b/prelude-nri/elm/BUCK @@ -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"], +)