diff --git a/hooks/.pre-commit-config.yaml b/hooks/.pre-commit-config.yaml index 02c93e2cf9e9c19523f5afb04512fcdddaa9a75f..3d3f8818c9a283196298d1e239eec326314cde1e 100644 --- a/hooks/.pre-commit-config.yaml +++ b/hooks/.pre-commit-config.yaml @@ -27,7 +27,7 @@ repos: entry: bazel run --config=pre-commit -- //buildifier:lint types_or: - bazel - - id: //ruff + - id: //ruff:check name: Check Python files with `ruff` description: Performs checks with `ruff` against Python files. Will read a `ruff.toml` for configuration. @@ -35,7 +35,18 @@ repos: require_serial: true stages: - pre-commit - entry: bazel run --config=pre-commit -- //ruff + entry: bazel run --config=pre-commit -- //ruff:check + types: + - python + - id: //ruff:format + name: Format Python files with `ruff` + description: Performs formatting with `ruff` against Python files. Will read a + `ruff.toml` for configuration. + language: system + require_serial: true + stages: + - pre-commit + entry: bazel run --config=pre-commit -- //ruff:format types: - python - id: //shfmt diff --git a/ruff/BUILD.bazel b/ruff/BUILD.bazel index 5e15fcbad11399caa018c2c96469219a4c625a84..8ebe81b91e7c7950ee11060c282d0c52e58c1ed3 100644 --- a/ruff/BUILD.bazel +++ b/ruff/BUILD.bazel @@ -1,4 +1,5 @@ load("@pre-commit//pre-commit/hook:defs.bzl", "pre_commit_hook") +load("@pre-commit//pre-commit/hooks:defs.bzl", "pre_commit_hooks") load("@toolchain_utils//toolchain/symlink/target:defs.bzl", "toolchain_symlink_target") alias( @@ -22,7 +23,7 @@ toolchain_symlink_target( ) pre_commit_hook( - name = "ruff", + name = "check", src = ":cli", args = [ "check", @@ -35,3 +36,27 @@ pre_commit_hook( types = ["@pre-commit//pre-commit/tag:python"], visibility = ["//visibility:public"], ) + +pre_commit_hook( + name = "format", + src = ":cli", + args = [ + "format", + "--cache-dir=.cache/ruff", + ], + description = "Performs formatting with `ruff` against Python files. Will read a `ruff.toml` for configuration.", + stages = ["@pre-commit//pre-commit/stage:pre-commit"], + summary = "Format Python files with `ruff`", + types = ["@pre-commit//pre-commit/tag:python"], + visibility = ["//visibility:public"], +) + +pre_commit_hooks( + name = "ruff", + srcs = [ + # do not sort + ":check", + ":format", + ], + visibility = ["//visibility:public"], +)