Skip to content

Commit

Permalink
feat(toolchains): add biome
Browse files Browse the repository at this point in the history
  • Loading branch information
mgred committed Jul 8, 2024
1 parent b04b268 commit f97dab2
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 0 deletions.
12 changes: 12 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@buildifier_prebuilt//:rules.bzl", "buildifier", "buildifier_test")
load("@rules_license//rules:license.bzl", "license")
load("//lib:biome.bzl", "biome", "biome_test")
load("//lib:topiary.bzl", "topiary")
load("//lib:typos.bzl", "typos_test")

Expand All @@ -20,6 +21,7 @@ exports_files([
"LICENSE",
"MODULE.bazel",
"extensions.bzl",
"biome.json",
])

typos_test(
Expand All @@ -30,6 +32,16 @@ typos_test(
],
)

biome(
name = "check_write",
config = "//:biome.json",
)

biome_test(
name = "check",
config = "//:biome.json",
)

topiary(
name = "topiary",
extensions = ["json"],
Expand Down
3 changes: 3 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ bazel_dep(name = "buildifier_prebuilt", version = "6.4.0", dev_dependency = True
#endif

tools = use_extension("//:extensions.bzl", "tools")
tools.biome()
tools.fd()
tools.goawk()
tools.jql()
Expand All @@ -28,6 +29,7 @@ tools.typos()
tools.xsv()
use_repo(
tools,
"biome",
"fd",
"goawk",
"jql",
Expand All @@ -41,6 +43,7 @@ use_repo(
)

register_toolchains(
"@biome//:all",
"@fd//:all",
"@goawk//:all",
"@jql//:all",
Expand Down
18 changes: 18 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"organizeImports": {
"enabled": true
},
"formatter": {
"indentStyle": "space"
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"files": {
"ignore": ["bazel-*/**"]
}
}
71 changes: 71 additions & 0 deletions lib/biome.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"Biome"

def _biome_impl(ctx):
launcher = ctx.actions.declare_file("{}_/{}".format(ctx.label.name, ctx.label.name))
bin = ctx.toolchains["@bzlparty_tools//toolchains:biome_toolchain_type"].binary_info.binary

args = []
if not ctx.attr.testonly:
args.append("--write")

ctx.actions.write(
output = launcher,
content = """\
#!/usr/bin/env bash
set -o pipefail -o errexit
bin_path=$(realpath "{bin}");
workspace_path=$(dirname "$(realpath "{workspace}")")
cd "$workspace_path"
exec "$bin_path" {mode} {args} --config-path "{config}" .
""".format(
args = " ".join(args),
bin = bin.path,
config = ctx.file.config.path,
mode = ctx.attr.mode,
workspace = ctx.file.workspace.path,
),
is_executable = True,
)

return [
DefaultInfo(
runfiles = ctx.runfiles(files = [bin, ctx.file.workspace, ctx.file.config]),
executable = launcher,
),
]

biome = rule(
_biome_impl,
attrs = {
"config": attr.label(allow_single_file = True),
"workspace": attr.label(default = "//:MODULE.bazel", allow_single_file = True),
"mode": attr.string(default = "check", values = ["check", "lint"]),
},
toolchains = ["@bzlparty_tools//toolchains:biome_toolchain_type"],
executable = True,
)

_biome_test = rule(
_biome_impl,
attrs = {
"config": attr.label(allow_single_file = True),
"workspace": attr.label(default = "//:MODULE.bazel", allow_single_file = True),
"mode": attr.string(default = "check", values = ["check", "lint"]),
},
toolchains = ["@bzlparty_tools//toolchains:biome_toolchain_type"],
test = True,
)

# buildifier: disable=function-docstring
def biome_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
_biome_test(**kwargs)
1 change: 1 addition & 0 deletions toolchains/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load(":toolchains.bzl", "bzlparty_toolchains")
package(default_visibility = ["//visibility:public"])

TOOLS = [
"biome",
"fd",
"goawk",
"jql",
Expand Down
25 changes: 25 additions & 0 deletions toolchains/biome/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("//lib:platform_asset.bzl", "multi_platform_assets")

VERSION = "1.8.3"

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

GITHUB_URL = "https://github.com/biomejs/biome/releases/download/cli/v%s" % VERSION

multi_platform_assets(
name = "biome",
binary = "biome",
platforms = SUPPORTED_PLATFORMS,
platforms_map = {
"darwin_amd64": "darwin-x64",
"darwin_arm64": "darwin-arm64",
"linux_amd64": "linux-x64-musl",
"linux_arm64": "linux-arm64-musl",
},
url = "%s/biome-{platform}" % GITHUB_URL,
)
6 changes: 6 additions & 0 deletions toolchains/biome/assets.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ASSETS = {
"darwin_amd64": struct(url = "https://github.com/biomejs/biome/releases/download/cli/v1.8.3/biome-darwin-x64", binary = "biome", integrity = "sha384-3VVL0H+g0w8E/FFwGWMYo5iaLXxG9GgA1clrMGYNgWzeTBe8AOxzvDvOb1xct0gx"),
"darwin_arm64": struct(url = "https://github.com/biomejs/biome/releases/download/cli/v1.8.3/biome-darwin-arm64", binary = "biome", integrity = "sha384-jMJmEWWOIBtIp8hAfQ7Di38wu9zER8cE+mt/dtNEiKSaH/3c/gj/W3vR0VP2TsIk"),
"linux_amd64": struct(url = "https://github.com/biomejs/biome/releases/download/cli/v1.8.3/biome-linux-x64-musl", binary = "biome", integrity = "sha384-YqNS5kpL2TV6Re/cS8tw9uJgghUaSldQBabJOxpze03LNmfrBVG/BF/19En5lZYC"),
"linux_arm64": struct(url = "https://github.com/biomejs/biome/releases/download/cli/v1.8.3/biome-linux-arm64-musl", binary = "biome", integrity = "sha384-e6DsWDqE3r4KNxl+FKslWgwGpSncK/HaTKplJSQrpBK1hbQcdL1AcS/KiMtYD+6u"),
}
18 changes: 18 additions & 0 deletions toolchains/toolchains.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# buildifier: disable=module-docstring
load("//lib:toolchains.bzl", "resolved_toolchain_impl")
load("//toolchains/biome:assets.bzl", BIOME_ASSETS = "ASSETS")
load("//toolchains/fd:assets.bzl", FD_ASSETS = "ASSETS")
load("//toolchains/goawk:assets.bzl", GOAWK_ASSETS = "ASSETS")
load("//toolchains/jql:assets.bzl", JQL_ASSETS = "ASSETS")
Expand All @@ -12,6 +13,7 @@ load("//toolchains/typos:assets.bzl", TYPOS_ASSETS = "ASSETS")
load("//toolchains/xsv:assets.bzl", XSV_ASSETS = "ASSETS")

TOOLS = {
"biome": BIOME_ASSETS,
"fd": FD_ASSETS,
"goawk": GOAWK_ASSETS,
"jql": JQL_ASSETS,
Expand All @@ -24,6 +26,14 @@ TOOLS = {
"xsv": XSV_ASSETS,
}

BIOME_TOOLCHAIN_TYPE = "@bzlparty_tools//toolchains:biome_toolchain_type"

biome_resolved_toolchain = rule(
implementation = resolved_toolchain_impl(BIOME_TOOLCHAIN_TYPE),
toolchains = [BIOME_TOOLCHAIN_TYPE],
incompatible_use_toolchain_transition = True,
)

FD_TOOLCHAIN_TYPE = "@bzlparty_tools//toolchains:fd_toolchain_type"

fd_resolved_toolchain = rule(
Expand Down Expand Up @@ -106,6 +116,14 @@ xsv_resolved_toolchain = rule(

# buildifier: disable=function-docstring
def bzlparty_toolchains(name = "toolchains"):
native.toolchain_type(
name = "biome_toolchain_type",
visibility = ["//visibility:public"],
)
biome_resolved_toolchain(
name = "biome",
visibility = ["//visibility:public"],
)
native.toolchain_type(
name = "fd_toolchain_type",
visibility = ["//visibility:public"],
Expand Down

0 comments on commit f97dab2

Please sign in to comment.