Skip to content

Commit

Permalink
feat: add topiary rule and ability to load exta files
Browse files Browse the repository at this point in the history
  • Loading branch information
mgred committed May 18, 2024
1 parent 9433195 commit 67fca42
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
3 changes: 3 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
4 changes: 4 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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")
32 changes: 32 additions & 0 deletions lib/extensions.bzl
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions lib/topiary.bzl
Original file line number Diff line number Diff line change
@@ -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,
)

0 comments on commit 67fca42

Please sign in to comment.