diff --git a/pkl/builtins/buildifier_format.pkl b/pkl/builtins/buildifier_format.pkl new file mode 100644 index 000000000..8874fe5af --- /dev/null +++ b/pkl/builtins/buildifier_format.pkl @@ -0,0 +1,54 @@ +import "../Builtins.pkl" +import "../Config.pkl" +import "./test/helpers.pkl" + +@Builtins.meta { + category = "Build Systems" + description = "Formatter for Bazel BUILD, WORKSPACE, and Starlark files" + project_indicators { + new { file = "WORKSPACE" } + new { file = "WORKSPACE.bazel" } + new { file = "MODULE.bazel" } + new { glob = "**/BUILD.bazel" } + } +} +buildifier_format = new Config.Step { + glob = + List( + "**/BUILD", + "**/BUILD.bazel", + "WORKSPACE", + "WORKSPACE.bazel", + "MODULE.bazel", + "**/*.bzl", + "**/*.star", + "**/*.sky", + ) + check = "buildifier -mode=check {{ files }}" + fix = "buildifier {{ files }}" + tests { + local const testMaker = new helpers.TestMaker { filename = "BUILD.bazel" } + local const before = + """ + load("@rules_cc//cc:cc_library.bzl", "cc_library") + + cc_library( name="foo", + srcs=["Foo.cc"],) + + """ + local const after = + """ + load("@rules_cc//cc:cc_library.bzl", "cc_library") + + cc_library( + name = "foo", + srcs = ["Foo.cc"], + ) + + """ + ["check bad file"] = testMaker.checkFail(before, 4) + ["check good file"] = testMaker.checkPass(after) + ["fix bad file"] = testMaker.fixPass(before, after) + ["fix good file"] = testMaker.fixPass(after, after) + } +} diff --git a/pkl/builtins/buildifier_lint.pkl b/pkl/builtins/buildifier_lint.pkl new file mode 100644 index 000000000..62ec9634b --- /dev/null +++ b/pkl/builtins/buildifier_lint.pkl @@ -0,0 +1,54 @@ +import "../Builtins.pkl" +import "../Config.pkl" +import "./test/helpers.pkl" + +@Builtins.meta { + category = "Build Systems" + description = "Linter for Bazel BUILD, WORKSPACE, and Starlark files" + project_indicators { + new { file = "WORKSPACE" } + new { file = "WORKSPACE.bazel" } + new { file = "MODULE.bazel" } + new { glob = "**/BUILD.bazel" } + } +} +buildifier_lint = new Config.Step { + glob = + List( + "**/BUILD", + "**/BUILD.bazel", + "WORKSPACE", + "WORKSPACE.bazel", + "MODULE.bazel", + "**/*.bzl", + "**/*.star", + "**/*.sky", + ) + check = "buildifier -mode=check -lint=warn {{ files }}" + fix = "buildifier -lint=fix {{ files }}" + tests { + local const testMaker = new helpers.TestMaker { filename = "BUILD.bazel" } + local const before = + """ + cc_library( + name = "foo", + srcs = ["Foo.cc"], + ) + + """ + local const after = + """ + load("@rules_cc//cc:cc_library.bzl", "cc_library") + + cc_library( + name = "foo", + srcs = ["Foo.cc"], + ) + + """ + ["check bad file"] = testMaker.checkFail(before, 4) + ["check good file"] = testMaker.checkPass(after) + ["fix bad file"] = testMaker.fixPass(before, after) + ["fix good file"] = testMaker.fixPass(after, after) + } +} diff --git a/test/builtin_tool_stubs/buildifier b/test/builtin_tool_stubs/buildifier new file mode 100755 index 000000000..124da7c66 --- /dev/null +++ b/test/builtin_tool_stubs/buildifier @@ -0,0 +1,4 @@ +#!/usr/bin/env -S mise tool-stub + +version = "8.5.1" +tool = "buildifier"