Skip to content

Commit

Permalink
feat(toolchain): add xsv and typos
Browse files Browse the repository at this point in the history
  • Loading branch information
mgred committed May 2, 2024
1 parent df3fcb5 commit b1926fb
Show file tree
Hide file tree
Showing 15 changed files with 704 additions and 162 deletions.
5 changes: 4 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
load("@buildifier_prebuilt//:rules.bzl", "buildifier", "buildifier_test")
load("//lib:typos.bzl", "typos_test")

exports_files(glob(["*"]))

typos_test(name = "typos")

buildifier(
name = "format",
exclude_patterns = [
Expand All @@ -21,5 +24,5 @@ buildifier_test(
lint_mode = "warn",
mode = "check",
no_sandbox = True,
workspace = "//:WORKSPACE",
workspace = ":WORKSPACE",
)
12 changes: 7 additions & 5 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ bazel_dep(name = "aspect_bazel_lib", version = "2.3.0")

bazel_dep(name = "buildifier_prebuilt", version = "6.4.0", dev_dependency = True)

ext = use_extension("//lib:extensions.bzl", "ext")
ext.goawk()
ext.ripgrep()
use_repo(ext, "goawk", "ripgrep")
tools = use_extension("//lib:extensions.bzl", "tools")
tools.goawk()
tools.ripgrep()
tools.typos()
tools.xsv()
use_repo(tools, "goawk", "ripgrep", "typos", "xsv")

register_toolchains("@goawk//:all", "@ripgrep//:all")
register_toolchains("@goawk//:all", "@ripgrep//:all", "@typos//:all", "@xsv//:all")
270 changes: 215 additions & 55 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion e2e/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@bzlparty_tools//lib:goawk.bzl", "goawk")
load("@bzlparty_tools//lib:ripgrep.bzl", "ripgrep")
load("@bzlparty_tools//lib:ripgrep.bzl", "ripgrep", "ripgrep_binary")

goawk(
name = "goawk",
Expand All @@ -23,6 +23,13 @@ ripgrep(
pattern = "oo$$",
)

ripgrep_binary(
name = "ripgrep_bin",
data = [
"ripgrep_test_data.txt",
],
)

diff_test(
name = "ripgrep_test",
file1 = ":ripgrep_result.txt",
Expand Down
288 changes: 233 additions & 55 deletions e2e/MODULE.bazel.lock

Large diffs are not rendered by default.

39 changes: 22 additions & 17 deletions lib/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,31 @@
load("//lib:toolchains.bzl", "platform_toolchains")
load("//toolchains/goawk:assets.bzl", GOAWK_ASSETS = "ASSETS")
load("//toolchains/ripgrep:assets.bzl", RIPGREP_ASSETS = "ASSETS")
load("//toolchains/typos:assets.bzl", TYPOS_ASSETS = "ASSETS")
load("//toolchains/xsv:assets.bzl", XSV_ASSETS = "ASSETS")

TOOLS = {
"goawk": GOAWK_ASSETS,
"ripgrep": RIPGREP_ASSETS,
"typos": TYPOS_ASSETS,
"xsv": XSV_ASSETS,
}

TAG_CLASSES = {
t: tag_class(attrs = {"name": attr.string(default = t)})
for t in TOOLS.keys()
}

def _has_tag(module, tag):
return hasattr(module.tags, tag) and len(getattr(module.tags, tag)) > 0

def _impl(ctx):
for module in ctx.modules:
if len(module.tags.goawk) > 0:
platform_toolchains(name = "goawk", assets = GOAWK_ASSETS)
if len(module.tags.ripgrep) > 0:
platform_toolchains(name = "ripgrep", assets = RIPGREP_ASSETS)
for name, assets in TOOLS.items():
if _has_tag(module, name):
platform_toolchains(name = name, assets = assets)

ext = module_extension(
tools = module_extension(
_impl,
tag_classes = {
"goawk": tag_class(
attrs = {
"name": attr.string(default = "goawk"),
},
),
"ripgrep": tag_class(
attrs = {
"name": attr.string(default = "ripgrep"),
},
),
},
tag_classes = TAG_CLASSES,
)
23 changes: 23 additions & 0 deletions lib/ripgrep.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"# ripgrep"

load("@bazel_skylib//rules:write_file.bzl", "write_file")

# buildifier: disable=function-docstring
def ripgrep(name, out, file = None, pattern = None, **kwargs):
args = []
Expand All @@ -20,3 +22,24 @@ def ripgrep(name, out, file = None, pattern = None, **kwargs):
toolchains = ["@bzlparty_tools//toolchains:ripgrep"],
**kwargs
)

def ripgrep_binary(name, **kwargs):
write_file(
name = "%s_sh" % name,
out = "%s.sh" % name,
content = [
"#!/usr/bin/env bash",
"echo $1",
"env",
],
)
native.sh_binary(
name = name,
srcs = ["%s_sh" % name],
args = [
"$(RIPGREP_BIN)",
"$(rootpaths %s)" % " ".join(kwargs.get("data")),
],
toolchains = ["@bzlparty_tools//toolchains:ripgrep"],
**kwargs
)
27 changes: 0 additions & 27 deletions lib/toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,3 @@ _binary_toolchains = repository_rule(
"build_file": attr.string(mandatory = True),
},
)

def _resolved_toolchain_impl(toolchain):
def _impl(ctx):
toolchain_info = ctx.toolchains[toolchain]
return [
toolchain_info,
toolchain_info.default,
toolchain_info.binary_info,
toolchain_info.template_variables,
]

return _impl

GOAWK_TOOLCHAIN_TYPE = "@bzlparty_tools//toolchains:goawk_toolchain_type"
RIPGREP_TOOLCHAIN_TYPE = "@bzlparty_tools//toolchains:ripgrep_toolchain_type"

goawk_resolved_toolchain = rule(
implementation = _resolved_toolchain_impl(GOAWK_TOOLCHAIN_TYPE),
toolchains = [GOAWK_TOOLCHAIN_TYPE],
incompatible_use_toolchain_transition = True,
)

ripgrep_resolved_toolchain = rule(
implementation = _resolved_toolchain_impl(RIPGREP_TOOLCHAIN_TYPE),
toolchains = [RIPGREP_TOOLCHAIN_TYPE],
incompatible_use_toolchain_transition = True,
)
50 changes: 50 additions & 0 deletions lib/typos.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# buildifier: disable=module-docstring
def _typos_impl(ctx):
output = ctx.actions.declare_file("%s.bash" % ctx.attr.name)
typos = ctx.toolchains["@bzlparty_tools//toolchains:typos_toolchain_type"].binary_info.binary
ctx.actions.write(
output = output,
content = """\
#!/usr/bin/env bash
set -o pipefail -o errexit
typos_bin=$(realpath "{path}");
workspace=$(dirname "$(realpath "{workspace}")")
eval "$typos_bin $workspace"
""".format(
path = typos.path,
workspace = ctx.file.workspace.path,
),
is_executable = True,
)

return [
DefaultInfo(
files = depset([output]),
runfiles = ctx.runfiles(files = [typos, ctx.file.workspace]),
executable = output,
),
]

_typos_test = rule(
_typos_impl,
attrs = {
"workspace": attr.label(allow_single_file = True, default = Label("//:MODULE.bazel")),
},
toolchains = ["@bzlparty_tools//toolchains:typos_toolchain_type"],
test = True,
)

# buildifier: disable=function-docstring
def typos_test(**kwargs):
if kwargs.get("no_sandbox", True):
tags = kwargs.get("tags", [])

for t in ["no-sandbox", "no-cache", "external"]:
if t not in tags:
tags.append(t)

kwargs["tags"] = tags
_typos_test(**kwargs)
28 changes: 27 additions & 1 deletion toolchains/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
load("//lib:toolchains.bzl", "goawk_resolved_toolchain", "ripgrep_resolved_toolchain")
load(
":toolchains.bzl",
"goawk_resolved_toolchain",
"ripgrep_resolved_toolchain",
"typos_resolved_toolchain",
"xsv_resolved_toolchain",
)

package(default_visibility = ["//visibility:public"])

Expand All @@ -21,3 +27,23 @@ ripgrep_resolved_toolchain(
name = "ripgrep",
visibility = ["//visibility:public"],
)

toolchain_type(
name = "typos_toolchain_type",
visibility = ["//visibility:public"],
)

typos_resolved_toolchain(
name = "typos",
visibility = ["//visibility:public"],
)

toolchain_type(
name = "xsv_toolchain_type",
visibility = ["//visibility:public"],
)

xsv_resolved_toolchain(
name = "xsv",
visibility = ["//visibility:public"],
)
41 changes: 41 additions & 0 deletions toolchains/toolchains.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# buildifier: disable=module-docstring
def _resolved_toolchain_impl(toolchain):
def _impl(ctx):
toolchain_info = ctx.toolchains[toolchain]
return [
toolchain_info,
toolchain_info.default,
toolchain_info.binary_info,
toolchain_info.template_variables,
]

return _impl

GOAWK_TOOLCHAIN_TYPE = "@bzlparty_tools//toolchains:goawk_toolchain_type"
RIPGREP_TOOLCHAIN_TYPE = "@bzlparty_tools//toolchains:ripgrep_toolchain_type"
TYPOS_TOOLCHAIN_TYPE = "@bzlparty_tools//toolchains:typos_toolchain_type"
XSV_TOOLCHAIN_TYPE = "@bzlparty_tools//toolchains:xsv_toolchain_type"

goawk_resolved_toolchain = rule(
implementation = _resolved_toolchain_impl(GOAWK_TOOLCHAIN_TYPE),
toolchains = [GOAWK_TOOLCHAIN_TYPE],
incompatible_use_toolchain_transition = True,
)

ripgrep_resolved_toolchain = rule(
implementation = _resolved_toolchain_impl(RIPGREP_TOOLCHAIN_TYPE),
toolchains = [RIPGREP_TOOLCHAIN_TYPE],
incompatible_use_toolchain_transition = True,
)

typos_resolved_toolchain = rule(
implementation = _resolved_toolchain_impl(TYPOS_TOOLCHAIN_TYPE),
toolchains = [TYPOS_TOOLCHAIN_TYPE],
incompatible_use_toolchain_transition = True,
)

xsv_resolved_toolchain = rule(
implementation = _resolved_toolchain_impl(XSV_TOOLCHAIN_TYPE),
toolchains = [XSV_TOOLCHAIN_TYPE],
incompatible_use_toolchain_transition = True,
)
29 changes: 29 additions & 0 deletions toolchains/typos/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("//lib:platform_binary.bzl", "platform_binaries")

VERSION = "1.21.0"

SUPPORTED_PLATFORMS = [
"darwin_amd64",
"darwin_arm64",
"linux_amd64",
"linux_arm64",
"windows_amd64",
"windows_arm64",
]

GITHUB_URL = "https://github.com/cargo-prebuilt/index/releases/download/typos-cli-%s" % VERSION

platform_binaries(
name = "typos",
binary = "typos",
platforms = SUPPORTED_PLATFORMS,
platforms_map = {
"darwin_amd64": "x85_64-apple-darwin",
"darwin_arm64": "aarch64-apple-darwin",
"linux_amd64": "x86_64-unknown-linux-musl",
"linux_arm64": "aarch64-unknown-linux-musl",
"windows_amd64": "x86_64-pc-windows-msvc",
"windows_arm64": "aarch64-pc-windows-msvc",
},
url = "%s/{platform}.tar.gz" % GITHUB_URL,
)
8 changes: 8 additions & 0 deletions toolchains/typos/assets.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ASSETS = {
"darwin_amd64": struct(binary = "typos", url = "https://github.com/cargo-prebuilt/index/releases/download/typos-cli-1.21.0/x85_64-apple-darwin.tar.gz", integrity = "sha384-AI+HQg6X3xX+Su/0qIwiSw2Ygga0UNjAFFCZPicIJ8xlzT70tejy5Gx4ZEJ/dHOm"),
"darwin_arm64": struct(binary = "typos", url = "https://github.com/cargo-prebuilt/index/releases/download/typos-cli-1.21.0/aarch64-apple-darwin.tar.gz", integrity = "sha384-8nOMf5jj3iggOtsS/dChGnhHJBdHkPkJw+2DoGkuOncJfI1TVfxfiOMlNx6cYBjr"),
"linux_amd64": struct(binary = "typos", url = "https://github.com/cargo-prebuilt/index/releases/download/typos-cli-1.21.0/x86_64-unknown-linux-musl.tar.gz", integrity = "sha384-YdD3XLwJTKSKSftpdZ6JbNoiKouHkZ0D/BGQk3736KlzsvQ9sefTkoAYvYIU96Zg"),
"linux_arm64": struct(binary = "typos", url = "https://github.com/cargo-prebuilt/index/releases/download/typos-cli-1.21.0/aarch64-unknown-linux-musl.tar.gz", integrity = "sha384-AkCCxhe7jU4ykyZwW9A6R6sihsi0+21/tZ0kWaFhyV2UqwlZC85qN2qPokprTkow"),
"windows_amd64": struct(binary = "typos.exe", url = "https://github.com/cargo-prebuilt/index/releases/download/typos-cli-1.21.0/x86_64-pc-windows-msvc.tar.gz", integrity = "sha384-B45/wcXU/Q5zB7oxY6kuZNDmj4lu1Shnn47/5Eij12JCJQpoMHqlc2NE+CswBJhS"),
"windows_arm64": struct(binary = "typos.exe", url = "https://github.com/cargo-prebuilt/index/releases/download/typos-cli-1.21.0/aarch64-pc-windows-msvc.tar.gz", integrity = "sha384-AuGDiX1gA1199GW2oUL9KqjIhdOpa+N8WFBxq3xNtOOk6FbOIHCLnL0tOmU5dB+N"),
}
29 changes: 29 additions & 0 deletions toolchains/xsv/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("//lib:platform_binary.bzl", "platform_binaries")

VERSION = "0.13.0"

SUPPORTED_PLATFORMS = [
"darwin_amd64",
"darwin_arm64",
"linux_amd64",
"linux_arm64",
"windows_amd64",
"windows_arm64",
]

GITHUB_URL = "https://github.com/cargo-prebuilt/index/releases/download/xsv-%s" % VERSION

platform_binaries(
name = "xsv",
binary = "xsv",
platforms = SUPPORTED_PLATFORMS,
platforms_map = {
"darwin_amd64": "x85_64-apple-darwin",
"darwin_arm64": "aarch64-apple-darwin",
"linux_amd64": "x86_64-unknown-linux-musl",
"linux_arm64": "aarch64-unknown-linux-musl",
"windows_amd64": "x86_64-pc-windows-msvc",
"windows_arm64": "aarch64-pc-windows-msvc",
},
url = "%s/{platform}.tar.gz" % GITHUB_URL,
)
8 changes: 8 additions & 0 deletions toolchains/xsv/assets.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ASSETS = {
"darwin_amd64": struct(binary = "xsv", url = "https://github.com/cargo-prebuilt/index/releases/download/xsv-0.13.0/x85_64-apple-darwin.tar.gz", integrity = "sha384-AI+HQg6X3xX+Su/0qIwiSw2Ygga0UNjAFFCZPicIJ8xlzT70tejy5Gx4ZEJ/dHOm"),
"darwin_arm64": struct(binary = "xsv", url = "https://github.com/cargo-prebuilt/index/releases/download/xsv-0.13.0/aarch64-apple-darwin.tar.gz", integrity = "sha384-LNKs3vYVqUzH09ylwPhkdKMMhV26tmSro8+lXkqvOVtlaCbKWjdYMiaipQ9L+jcb"),
"linux_amd64": struct(binary = "xsv", url = "https://github.com/cargo-prebuilt/index/releases/download/xsv-0.13.0/x86_64-unknown-linux-musl.tar.gz", integrity = "sha384-a9R7blrnvbuiwK3fxRdPvRErETlXLQUnVRnq0TRaVNLQ5ppbXhYZcvbrHd9WuHK8"),
"linux_arm64": struct(binary = "xsv", url = "https://github.com/cargo-prebuilt/index/releases/download/xsv-0.13.0/aarch64-unknown-linux-musl.tar.gz", integrity = "sha384-S+MRIKADdpHoFvJd5IJVvAu6QbK4lQxAqopDIZbYZC1dZGzHwiKBFtYbAqWS0gea"),
"windows_amd64": struct(binary = "xsv.exe", url = "https://github.com/cargo-prebuilt/index/releases/download/xsv-0.13.0/x86_64-pc-windows-msvc.tar.gz", integrity = "sha384-IxVWBJuknchdv83BQ6HjbPkUwTDX8cQzdqhRMNmMrB1/DiJn0Hhq3atrCeYhQngx"),
"windows_arm64": struct(binary = "xsv.exe", url = "https://github.com/cargo-prebuilt/index/releases/download/xsv-0.13.0/aarch64-pc-windows-msvc.tar.gz", integrity = "sha384-X72q7PrI9fLqaLWjEd/SwKljfaADYKNsuA/jvFHEPiyMSkMt/LDmWUuhTroIam4d"),
}

0 comments on commit b1926fb

Please sign in to comment.