diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..6b86cc7 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,37 @@ +# Pre-commit hooks for this Java/Maven project (the Java-conventional +# equivalent of husky hooks in JS projects). +# +# All hooks are CHECK-ONLY (no auto-fix): +# - lint via `mvn checkstyle:check` +# - format via `mvn spotless:check` (use `mvn spotless:apply` manually to fix) +# - unit tests via `mvn test` (integration tests tagged `integration` are +# already excluded from the default build via surefire `excludedGroups` +# in pom.xml, so no live backend is needed) +# +# Install/activate (per clone): +# pip install pre-commit # or: brew install pre-commit +# pre-commit install +# +# Run manually: +# pre-commit run --all-files +repos: + - repo: local + hooks: + - id: checkstyle-check + name: "lint: mvn checkstyle:check" + entry: mvn -B checkstyle:check + language: system + pass_filenames: false + always_run: true + - id: spotless-check + name: "format: mvn spotless:check" + entry: mvn -B spotless:check + language: system + pass_filenames: false + always_run: true + - id: unit-tests + name: "test: mvn test (unit only, integration excluded)" + entry: mvn -B test + language: system + pass_filenames: false + always_run: true diff --git a/README.md b/README.md index 148e859..1f22d8a 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,24 @@ mvn javafx:run в `Program.java`). Для проверки вёрстки отдельной страницы можно временно подменить `INITIAL_PAGE_PATH` на нужный FXML. +## Git hooks + +Перед каждым коммитом автоматически проверяются: линт (`checkstyle:check`), +формат (`spotless:check`) и unit-тесты (`mvn test`, без интеграционных). +Настройка один раз на клон: + +```powershell +# Windows (PowerShell 5.1 / 7+): +.\setup.ps1 + +# macOS / Linux: +pip install pre-commit +pre-commit install +``` + +Ручная проверка всего сразу: `pre-commit run --all-files`. +Если упал формат — поправить: `mvn spotless:apply`. + ## RBAC В системе предусмотрено 3 роли: пользователь, модератор и администратор: diff --git a/setup.ps1 b/setup.ps1 new file mode 100644 index 0000000..1a11d1d --- /dev/null +++ b/setup.ps1 @@ -0,0 +1,44 @@ +<# +.SYNOPSIS + Sets up git pre-commit hooks for this repo (pre-commit framework). + +.DESCRIPTION + Installs the `pre-commit` package if missing and runs `pre-commit install` + so the lint/format/unit-test checks run automatically on every `git commit`. + Works on Windows PowerShell 5.1 and PowerShell 7+. +#> +$ErrorActionPreference = 'Stop' + +function Test-Command($Name) { + return $null -ne (Get-Command $Name -ErrorAction SilentlyContinue) +} + +if (-not (Test-Command 'git')) { + Write-Error 'git not found in PATH. Install Git for Windows and retry.' + exit 1 +} + +$Python = if (Test-Command 'python') { 'python' } elseif (Test-Command 'python3') { 'python3' } else { $null } +if (-not $Python) { + Write-Error 'Python not found in PATH. Install Python 3 (python.org) with "Add to PATH" and retry.' + exit 1 +} + +# Must run inside the repo so hooks land in .git/hooks. +$null = git rev-parse --show-toplevel 2>$null +if ($LASTEXITCODE -ne 0) { + Write-Error 'Not inside a git repository. Run this script from the repo root.' + exit 1 +} + +& $Python -m pre_commit --version > $null 2>&1 +if ($LASTEXITCODE -ne 0) { + Write-Host 'pre-commit not found, installing via pip...' + & $Python -m pip install --user pre-commit +} + +# Use `python -m` so it works even if pip --user Scripts dir is not on PATH. +& $Python -m pre_commit install +Write-Host '' +Write-Host 'Done. Hooks will run on every commit.' +Write-Host 'To check everything right now: pre-commit run --all-files'