Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@
pass_filenames: false
always_run: true
minimum_pre_commit_version: '0.19.0'
- id: sbt-scalafmt-apply
name: scalafmt formatting fix
stages: [commit,push]
language: python
entry: scalafmt-apply
pass_filenames: false
always_run: true
minimum_pre_commit_version: '0.19.0'
4 changes: 3 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Currently, they include the following:
- `sbt-unused-imports` - as above, but also adds the "unused imports" warning.
- `sbt-scalafmt` - runs `scalafmtCheckAll`.
- `sbt-wartremover` - runs the wartremover plugin.
- `sbt-scalafmt-apply` - runs `scalafmtAll`, as sbt-scalafmt but also applies reformatting to changed files.

To add one or more of the hooks into your repo:

Expand All @@ -45,6 +46,7 @@ repos:
- id: sbt-unused-imports #includes fatal warnings, arguments optional
args: [--scope={defaultScope}]
- id: sbt-scalafmt
- id: sbt-scalafmt-apply
- id: sbt-wartremover #arguments are optional
args: [--warts=Warts.unsafe, --scope={defaultScope}]
----
Expand All @@ -53,7 +55,7 @@ repos:

[NOTE]
--
All hooks except for `sbt-scalafmt` have the optional `scope` argument, which allows to define what source scopes
All hooks except for `sbt-scalafmt` and `sbt-scalafmt-apply` have the optional `scope` argument, which allows to define what source scopes
are relevant for the hook's check. The default is `{defaultScope}`.
--

Expand Down
14 changes: 14 additions & 0 deletions pre_commit_hooks/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,17 @@ def run_sbt_command(task_def, missing_plugin_check_string=None, missing_plugin_e
print(raw_output)

return sbt_process.returncode

def run_git_add_modified():
"""Adds stages files if changes are detected."""
try:
# Check if there are modified files before running git add -u
status = subprocess.run(["git", "status", "--porcelain"], capture_output=True, text=True)

if status.stdout.strip(): # If there are modified files
print("Staged formatted files.")
return subprocess.run(["git", "add", "-u"], check=True)

except subprocess.CalledProcessError as e:
print(f"Error: {e}")
return e.returncode
19 changes: 19 additions & 0 deletions pre_commit_hooks/scalafmt_apply.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from pre_commit_hooks.runner import run_sbt_command, run_git_add_modified
from colorama import init as colorama_init, Fore

TASK_SCALAFMT = 'scalafmtAll'
MISSING_PLUGIN_CHECK_STRING = 'Not a valid key: scalafmtAll'
MISSING_PLUGIN_ERROR_MSG = f'{Fore.RED}ERROR: scalafmt SBT plugin not present! See {Fore.BLUE}https://scalameta.org/scalafmt/docs/installation.html#sbt{Fore.RED} for installation instructions.'


def main(argv=None):
colorama_init()

sbt = run_sbt_command(f'; clean ; {TASK_SCALAFMT}', MISSING_PLUGIN_CHECK_STRING, MISSING_PLUGIN_ERROR_MSG)
run_git_add_modified()

return sbt


if __name__ == '__main__':
exit(main())
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ console_scripts =
sbt-wartremover = pre_commit_hooks.sbt_wartremover:main
scalafmt = pre_commit_hooks.scalafmt:main
sbt-fatal-warnings = pre_commit_hooks.sbt_fatal_warnings:main
scalafmt-apply = pre_commit_hooks.scalafmt_apply:main