diff --git a/BUILD.bazel b/BUILD.bazel index a784dbb..fb69bfd 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,10 +1,13 @@ load("@buildifier_prebuilt//:rules.bzl", "buildifier", "buildifier_test") +load("//lib:topiary.bzl", "topiary") load("//lib:typos.bzl", "typos_test") exports_files(glob(["*"])) typos_test(name = "typos") +topiary(name = "topiary") + buildifier( name = "format", exclude_patterns = [ diff --git a/MODULE.bazel b/MODULE.bazel index e6630ab..dc2d30e 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -41,3 +41,7 @@ register_toolchains( "@typos//:all", "@xsv//:all", ) + +files = use_extension("//lib:extensions.bzl", "files") +files.topiary_queries() +use_repo(files, "topiary_queries") diff --git a/lib/extensions.bzl b/lib/extensions.bzl index 4dd3e2f..a6b7574 100644 --- a/lib/extensions.bzl +++ b/lib/extensions.bzl @@ -1,4 +1,5 @@ # buildifier: disable=module-docstring +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("//lib:toolchains.bzl", "platform_toolchains") load("//toolchains:toolchains.bzl", "TOOLS") @@ -16,6 +17,37 @@ def _impl(ctx): if _has_tag(module, name): platform_toolchains(name = name, assets = assets) +def load_files(**kwargs): + build_file = """\ +filegroup( + name = "files", + srcs = glob({files}), + visibility = ["//visibility:public"], +) +""".format(files = kwargs.pop("files")) + http_archive( + build_file_content = build_file, + **kwargs + ) + +def _files_impl(ctx): + for module in ctx.modules: + if _has_tag(module, "topiary_queries"): + load_files( + name = "topiary_queries", + url = "https://github.com/tweag/topiary/archive/refs/tags/v0.4.0.tar.gz", + integrity = "sha384-nwKyTRwfVVWvIybcmGf+/jxGFETvr71qAqbANbT24h3jt+7VEBuT45Fe9gmwGyI7", + strip_prefix = "topiary-0.4.0", + files = ["topiary-queries/queries/*.scm"], + ) + +files = module_extension( + _files_impl, + tag_classes = { + "topiary_queries": tag_class(), + }, +) + tools = module_extension( _impl, tag_classes = TAG_CLASSES, diff --git a/lib/topiary.bzl b/lib/topiary.bzl new file mode 100644 index 0000000..8ab3cad --- /dev/null +++ b/lib/topiary.bzl @@ -0,0 +1,37 @@ +# buildifier: disable=module-docstring +def _topiary_impl(ctx): + launcher = ctx.actions.declare_file("topiary_format.bash") + queries = ctx.files._queries + topiary = ctx.toolchains["@bzlparty_tools//toolchains:topiary_toolchain_type"].binary_info.binary + ctx.actions.write( + output = launcher, + content = """\ +export TOPIARY_LANGUAGE_DIR="{queries_dir}"; +files=$(find "$BUILD_WORKING_DIRECTORY" -name "*.sh" -type f -not -path "**/e2e/*spec.sh") +{bin} format --skip-idempotence $files +""".format( + bin = topiary.path, + queries_dir = ctx.files._queries[0].dirname, + ), + is_executable = True, + ) + + return [ + DefaultInfo( + files = depset([launcher]), + runfiles = ctx.runfiles([launcher, topiary] + queries), + executable = launcher, + ), + ] + +topiary = rule( + _topiary_impl, + toolchains = ["@bzlparty_tools//toolchains:topiary_toolchain_type"], + attrs = { + "_queries": attr.label( + default = "@topiary_queries//:files", + allow_files = True, + ), + }, + executable = True, +)