diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..32c2fec --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +*.egg-info +*.py[co] +.*.sw[a-z] +.coverage +.tox +.venv.touch +/.mypy_cache +/.pytest_cache +/venv* +coverage-html +dist diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..a099003 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,37 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.4.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-docstring-first + - id: check-added-large-files + - id: check-yaml + - id: debug-statements + - id: double-quote-string-fixer + - id: requirements-txt-fixer +- repo: https://gitlab.com/pycqa/flake8 + rev: 3.7.1 + hooks: + - id: flake8 +- repo: https://github.com/pre-commit/mirrors-autopep8 + rev: v1.4.3 + hooks: + - id: autopep8 +- repo: https://github.com/pre-commit/pre-commit + rev: v1.14.2 + hooks: + - id: validate_manifest +- repo: https://github.com/asottile/reorder_python_imports + rev: v1.3.5 + hooks: + - id: reorder-python-imports + language_version: python3 +- repo: https://github.com/asottile/pyupgrade + rev: v1.11.1 + hooks: + - id: pyupgrade +- repo: https://github.com/asottile/add-trailing-comma + rev: v0.7.1 + hooks: + - id: add-trailing-comma diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 0000000..f98c0db --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,18 @@ +- id: sbt-compile + name: sbt compile + description: Builds project using "sbt compile" + entry: sbt-compile + language: python + pass_filenames: false +- id: sbt-test + name: sbt test + description: Builds and tests project using "sbt test" + entry: sbt-test + language: python + pass_filenames: false +- id: sbt-task + name: sbt task + description: Runs provided sbt task + entry: sbt-task + language: python + pass_filenames: false diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e1ec7bb --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Blaž Kranjc + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ae4d5ee --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# pre-commit-sbt + +Pre-commit hooks for sbt based projects for use with [pre-commit](https://github.com/pre-commit/pre-commit). + +## Using pre-commit-sbt with pre-commit + +Add this to your `.pre-commit-config.yaml` + +```yaml +- repo: https://github.com/blaz-kranjc/pre-commit-sbt + rev: v1.0.0 # Use the ref you want to point at + hooks: + - id: sbt-test + # - id: ... +``` + +## Available hooks + +* `sbt-compile` +Checks that `sbt compile` completes successfully. + +* `sbt-test` +Checks that `sbt test` completes successfully. + +* `sbt-task` +Checks that provided sbt task completes successfully. +Use `args: []` with the desired task. diff --git a/pre_commit_hooks/__init__.py b/pre_commit_hooks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pre_commit_hooks/sbt_compile.py b/pre_commit_hooks/sbt_compile.py new file mode 100644 index 0000000..46e73b9 --- /dev/null +++ b/pre_commit_hooks/sbt_compile.py @@ -0,0 +1,11 @@ +from typing import Sequence + +from pre_commit_hooks.sbt_task import run_sbt_task + + +def main(argv: Sequence[str] | None = None) -> int: + return run_sbt_task('compile') + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/pre_commit_hooks/sbt_task.py b/pre_commit_hooks/sbt_task.py new file mode 100644 index 0000000..f950549 --- /dev/null +++ b/pre_commit_hooks/sbt_task.py @@ -0,0 +1,29 @@ +import argparse +import os +import subprocess +from shutil import which +from typing import Sequence + + +def run_sbt_task(task: str) -> int: + sbt_path = which('sbt', mode=os.X_OK) + if not sbt_path: + print('sbt not found in path') + return 1 + + proc = subprocess.run([sbt_path, task]) + if proc.returncode != 0: + print(f'sbt task {task} failed') + + return proc.returncode + + +def main(argv: Sequence[str] | None = None) -> int: + parser = argparse.ArgumentParser() + parser.add_argument('task', metavar='task', type=str) + args = parser.parse_args() + return run_sbt_task(args.task) + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/pre_commit_hooks/sbt_test.py b/pre_commit_hooks/sbt_test.py new file mode 100644 index 0000000..5951f5b --- /dev/null +++ b/pre_commit_hooks/sbt_test.py @@ -0,0 +1,11 @@ +from typing import Sequence + +from pre_commit_hooks.sbt_task import run_sbt_task + + +def main(argv: Sequence[str] | None = None) -> int: + return run_sbt_task('test') + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..9b89a99 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,44 @@ +[metadata] +name = pre_commit_sbt +version = 1.0.0 +description = sbt hooks for pre-commit. +long_description = file: README.md +long_description_content_type = text/markdown +url = https://github.com/blaz-kranjc/pre-commit-sbt +author = Blaz Kranjc +author_email = blaz@kranjc.xyz +license = MIT +license_file = LICENSE +classifiers = + License :: OSI Approved :: MIT License + Programming Language :: Python :: 3 + Programming Language :: Python :: 3 :: Only + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 + Programming Language :: Python :: Implementation :: CPython + Programming Language :: Python :: Implementation :: PyPy + +[options] +packages = find: +install_requires = +python_requires = >=3.7 + +[options.entry_points] +console_scripts = + sbt-compile = pre_commit_hooks.sbt_compile:main + sbt-test = pre_commit_hooks.sbt_test:main + sbt-task = pre_commit_hooks.sbt_task:main + +[bdist_wheel] +universal = True + +[mypy] +check_untyped_defs = true +disallow_any_generics = true +disallow_incomplete_defs = true +disallow_untyped_defs = true +no_implicit_optional = true +warn_redundant_casts = true +warn_unused_ignores = true diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..8bf1ba9 --- /dev/null +++ b/setup.py @@ -0,0 +1,2 @@ +from setuptools import setup +setup()