Add pre-commit to automatically run formatting when committing

This commit is contained in:
Vincent Emonet 2023-07-23 13:24:15 +02:00
parent 7d07967574
commit 25168ba6e1
14 changed files with 74 additions and 34 deletions

28
.pre-commit-config.yaml Normal file
View file

@ -0,0 +1,28 @@
# See https://pre-commit.com for more information
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-added-large-files
name: " 🐘 Check for added large files"
- id: check-toml
name: " ✔️ Check TOML"
- id: check-yaml
name: " ✔️ Check YAML"
args:
- --unsafe
- id: check-json
name: " ✔️ Check JSON"
- id: trailing-whitespace
name: " ✂️ Trim trailing whitespaces"
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.277
hooks:
- id: ruff
name: " ⚡️ Formatting code with Ruff"
args:
- --fix
ci:
autofix_commit_msg: 🎨 [pre-commit] Auto format
autoupdate_commit_msg: ⬆ [pre-commit] pre-commit auto update

View file

@ -33,6 +33,8 @@ git clone https://github.com/LibreTranslate/LibreTranslate.git
cd LibreTranslate cd LibreTranslate
``` ```
Hatch will automatically install the required dependencies in a virtual environment, and enable [`pre-commit`](https://pre-commit.com/), which will run before each commit to run formatting.
Run in development: Run in development:
```bash ```bash
@ -72,6 +74,12 @@ You can also run the tests on multiple python versions:
hatch run all:test hatch run all:test
``` ```
You can clean the virtual environment with:
```bash
hatch env prune
```
## Run with Docker ## Run with Docker
Linux/macOS: `./run.sh [args]` Linux/macOS: `./run.sh [args]`

View file

@ -65,7 +65,7 @@ ltmanage = "libretranslate.manage:manage"
test = [ test = [
"pytest >=7.2.0", "pytest >=7.2.0",
"pytest-cov", "pytest-cov",
"ruff ==0.0.277", "pre-commit >=3.0.0",
"types-requests", "types-requests",
] ]
@ -83,13 +83,16 @@ History = "https://github.com/LibreTranslate/LibreTranslate/releases"
features = [ features = [
"test", "test",
] ]
post-install-commands = [
"pre-commit install",
]
[tool.hatch.envs.default.scripts] [tool.hatch.envs.default.scripts]
dev = "python main.py {args}" dev = "python main.py {args}"
locales = "python scripts/compile_locales.py" locales = "python scripts/compile_locales.py"
fmt = [ fmt = [
"ruff libretranslate scripts --fix", "pre-commit run --all --all-files",
] ]
test = [ test = [
"fmt", "fmt",

View file

@ -1,5 +1,6 @@
from libretranslate import main from libretranslate import main
def app(*args, **kwargs): def app(*args, **kwargs):
import sys import sys
sys.argv = ['--wsgi'] sys.argv = ['--wsgi']