From 51c15085e18a4a5274e81cf39bdf05314c7b0ac1 Mon Sep 17 00:00:00 2001 From: Jordan Bonser Date: Thu, 6 Mar 2025 14:57:14 +0000 Subject: [PATCH 1/3] feat: add `//shfmt` hook --- MODULE.bazel | 20 ++++++++++++++++++++ shfmt/BUILD.bazel | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 shfmt/BUILD.bazel diff --git a/MODULE.bazel b/MODULE.bazel index 30174b1..90b9648 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -20,6 +20,7 @@ python.toolchain( ) download_archive = use_repo_rule("@download_utils//download/archive:defs.bzl", "download_archive") +download_file = use_repo_rule("@download_utils//download/file:defs.bzl", "download_file") # Ruff binaries [ @@ -38,3 +39,22 @@ download_archive = use_repo_rule("@download_utils//download/archive:defs.bzl", " ("arm64-windows", "aarch64-pc-windows-msvc", ".zip", "sha256-ckodh0IeEldZ6bfbqxak0QNIks5BgKqvYePrvn5nr6I="), ) ] + +# Shfmt binaries +[ + download_file( + name = "shfmt-{}".format(name), + integrity = integrity, + urls = ["https://github.com/mvdan/sh/releases/download/v3.10.0/shfmt_v3.10.0_{}{}".format(_tuple, ext)], + executable = True, + output = "shfmt-{}".format(name) + ) + for name, _tuple, ext, integrity in ( + ("amd64-linux", "linux_amd64", "", "sha256-H1ejhNWVQvj6xfUD2h8+pEJC9G3/lpVp6AtSTWS3Hbw="), + ("arm64-linux", "linux_arm64", "", "sha256-nSMBPVZkDiKHMv0qBKnt4KtGvC12S/IqSjX7GxTXB6g="), + ("amd64-macos", "darwin_amd64", "", "sha256-742XCz9pWn6OfUBzDu3S2TWrlZn3ijZfMZxRW8WdTIM="), + ("arm64-macos", "darwin_arm64", "", "sha256-hgMFM6gjwKfNkt7g90CU5bkBwyd7Q972M31eGeVv5VM="), + ("amd64-windows", "windows_amd64", ".exe", "sha256-bkxqzTjee0sbqPgIK55ojfjJuGHT+LLnuxtycCAaNYc="), + # ("arm64-windows", "windows_arm64", ".exe", ""), Currently doesn't have binary for arm64 Windows. + ) +] diff --git a/shfmt/BUILD.bazel b/shfmt/BUILD.bazel new file mode 100644 index 0000000..84cfa49 --- /dev/null +++ b/shfmt/BUILD.bazel @@ -0,0 +1,35 @@ +load("@pre-commit//pre-commit/hook:defs.bzl", "pre_commit_hook") +load("@toolchain_utils//toolchain/symlink/target:defs.bzl", "toolchain_symlink_target") + +alias( + name = "exe", + actual = select( + { + "@toolchain_utils//toolchain/constraint:amd64-linux": "@shfmt-amd64-linux//:shfmt-amd64-linux", + "@toolchain_utils//toolchain/constraint:arm64-linux": "@shfmt-arm64-linux//:shfmt-arm64-linux", + "@toolchain_utils//toolchain/constraint:amd64-macos": "@shfmt-amd64-macos//:shfmt-amd64-macos", + "@toolchain_utils//toolchain/constraint:arm64-macos": "@shfmt-arm64-macos//:shfmt-arm64-macos", + "@toolchain_utils//toolchain/constraint:amd64-windows": "@shfmt-amd64-windows//:shfmt-amd64-windows", + # "@toolchain_utils//toolchain/constraint:arm64-windows": "@shfmt-arm64-windows//:shfmt-arm64-windows", + }, + no_match_error = "No hermetic `shfmt` found for `//pre-commit/hook/shfmt:cli`. Please set it to a hermetic `shfmt` download in `MODULE.bazel` for the current host platform.", + ), +) + +toolchain_symlink_target( + name = "cli", + target = ":exe", +) + +pre_commit_hook( + name = "shfmt", + src = ":cli", + args = [ + "-w", + ], + description = "Performs formatting with `shfmt` against sh files.", + stages = ["@pre-commit//pre-commit/stage:pre-commit"], + summary = "Format sh files with `shfmt`", + types = ["@pre-commit//pre-commit/tag:sh"], + visibility = ["//visibility:public"], +) -- GitLab From 53f0f955cd1a9fbde8c7ebb0e691fb09992290ec Mon Sep 17 00:00:00 2001 From: Jordan Bonser Date: Thu, 6 Mar 2025 15:08:37 +0000 Subject: [PATCH 2/3] feat: add shfmt to precommit config. --- BUILD.bazel | 1 + MODULE.bazel | 5 +++-- hooks/.pre-commit-config.yaml | 10 ++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index 5217c94..85a0740 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -24,6 +24,7 @@ pre_commit_hooks( srcs = [ "//buildifier", "//ruff", + "//shfmt", ], visibility = ["//visibility:public"], ) diff --git a/MODULE.bazel b/MODULE.bazel index 90b9648..f09b9d1 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -20,6 +20,7 @@ python.toolchain( ) download_archive = use_repo_rule("@download_utils//download/archive:defs.bzl", "download_archive") + download_file = use_repo_rule("@download_utils//download/file:defs.bzl", "download_file") # Ruff binaries @@ -44,10 +45,10 @@ download_file = use_repo_rule("@download_utils//download/file:defs.bzl", "downlo [ download_file( name = "shfmt-{}".format(name), + executable = True, integrity = integrity, + output = "shfmt-{}".format(name), urls = ["https://github.com/mvdan/sh/releases/download/v3.10.0/shfmt_v3.10.0_{}{}".format(_tuple, ext)], - executable = True, - output = "shfmt-{}".format(name) ) for name, _tuple, ext, integrity in ( ("amd64-linux", "linux_amd64", "", "sha256-H1ejhNWVQvj6xfUD2h8+pEJC9G3/lpVp6AtSTWS3Hbw="), diff --git a/hooks/.pre-commit-config.yaml b/hooks/.pre-commit-config.yaml index 733365f..0f886c5 100644 --- a/hooks/.pre-commit-config.yaml +++ b/hooks/.pre-commit-config.yaml @@ -38,6 +38,16 @@ repos: entry: bazel run --config=pre-commit -- //ruff types: - python + - id: //shfmt + name: Format sh files with `shfmt` + description: Performs formatting with `shfmt` against sh files. + language: system + require_serial: true + stages: + - pre-commit + entry: bazel run --config=pre-commit -- //shfmt + types: + - sh - id: //hooks:config name: Update `pre-commit` config description: Keeps the `.pre-commit-config.yaml` in sync with any changes to the -- GitLab From c5302337f556fadce710baf1cbb03dedd5c801ab Mon Sep 17 00:00:00 2001 From: Jordan Bonser Date: Fri, 7 Mar 2025 15:02:45 +0000 Subject: [PATCH 3/3] fix: fixed pre-commit tag for shell also addressed review comments. --- MODULE.bazel | 4 ++-- e2e/shell/echo.sh | 6 ++++++ e2e/shfmt/BUILD.bazel | 9 +++++++++ hooks/.pre-commit-config.yaml | 6 +++--- shfmt/BUILD.bazel | 6 +++--- 5 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 e2e/shell/echo.sh create mode 100644 e2e/shfmt/BUILD.bazel diff --git a/MODULE.bazel b/MODULE.bazel index f09b9d1..2c47e6a 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -48,9 +48,9 @@ download_file = use_repo_rule("@download_utils//download/file:defs.bzl", "downlo executable = True, integrity = integrity, output = "shfmt-{}".format(name), - urls = ["https://github.com/mvdan/sh/releases/download/v3.10.0/shfmt_v3.10.0_{}{}".format(_tuple, ext)], + urls = ["https://github.com/mvdan/sh/releases/download/v3.10.0/shfmt_v3.10.0_{}{}".format(couplet, ext)], ) - for name, _tuple, ext, integrity in ( + for name, couplet, ext, integrity in ( ("amd64-linux", "linux_amd64", "", "sha256-H1ejhNWVQvj6xfUD2h8+pEJC9G3/lpVp6AtSTWS3Hbw="), ("arm64-linux", "linux_arm64", "", "sha256-nSMBPVZkDiKHMv0qBKnt4KtGvC12S/IqSjX7GxTXB6g="), ("amd64-macos", "darwin_amd64", "", "sha256-742XCz9pWn6OfUBzDu3S2TWrlZn3ijZfMZxRW8WdTIM="), diff --git a/e2e/shell/echo.sh b/e2e/shell/echo.sh new file mode 100644 index 0000000..1f10270 --- /dev/null +++ b/e2e/shell/echo.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env sh + +set -o errexit +set -o nounset + +builtin echo "$@" diff --git a/e2e/shfmt/BUILD.bazel b/e2e/shfmt/BUILD.bazel new file mode 100644 index 0000000..7a1190b --- /dev/null +++ b/e2e/shfmt/BUILD.bazel @@ -0,0 +1,9 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") + +build_test( + name = "shfmt", + size = "small", + targets = [ + "@pre-commit-hooks//shfmt", + ], +) diff --git a/hooks/.pre-commit-config.yaml b/hooks/.pre-commit-config.yaml index 0f886c5..02c93e2 100644 --- a/hooks/.pre-commit-config.yaml +++ b/hooks/.pre-commit-config.yaml @@ -39,15 +39,15 @@ repos: types: - python - id: //shfmt - name: Format sh files with `shfmt` - description: Performs formatting with `shfmt` against sh files. + name: Format Shell files with `shfmt` + description: Performs formatting with `shfmt` against Shell files. language: system require_serial: true stages: - pre-commit entry: bazel run --config=pre-commit -- //shfmt types: - - sh + - shell - id: //hooks:config name: Update `pre-commit` config description: Keeps the `.pre-commit-config.yaml` in sync with any changes to the diff --git a/shfmt/BUILD.bazel b/shfmt/BUILD.bazel index 84cfa49..5791d12 100644 --- a/shfmt/BUILD.bazel +++ b/shfmt/BUILD.bazel @@ -27,9 +27,9 @@ pre_commit_hook( args = [ "-w", ], - description = "Performs formatting with `shfmt` against sh files.", + description = "Performs formatting with `shfmt` against Shell files.", stages = ["@pre-commit//pre-commit/stage:pre-commit"], - summary = "Format sh files with `shfmt`", - types = ["@pre-commit//pre-commit/tag:sh"], + summary = "Format Shell files with `shfmt`", + types = ["@pre-commit//pre-commit/tag:shell"], visibility = ["//visibility:public"], ) -- GitLab