2
0
Эх сурвалжийг харах

CI: Integrate pre-commit for style checks

Max Hilbrunner 7 сар өмнө
parent
commit
f9e72e2bad

+ 4 - 12
.github/workflows/ci.yml

@@ -15,19 +15,11 @@ jobs:
       - name: Checkout
         uses: actions/checkout@v4
 
-      - name: Install dependencies
-        run: |
-          # Install tools used by `_tools/format.sh`.
-          sudo apt-get -qq update
-          sudo apt-get -qq install dos2unix recode
-          sudo pip3 install -r requirements.txt
-          sudo pip3 install codespell
-
-      - name: Linter checks
-        run: |
-          bash _tools/format.sh
+      - name: Style checks via pre-commit
+        uses: pre-commit/[email protected]
 
-          codespell -D- -D _tools/codespell-dict.txt -I _tools/codespell-ignore.txt -x _tools/codespell-ignore-lines.txt -S tutorials/i18n/locales.rst {about,community,contributing,getting_started,tutorials}/{*.rst,**/*.rst,**/**/*.rst,**/**/**/*.rst}
+      - name: Install dependencies
+        run: sudo pip3 install -r requirements.txt
 
       # Use dummy builder to improve performance as we don't need the generated HTML in this workflow.
       - name: Sphinx build

+ 17 - 0
.pre-commit-config.yaml

@@ -0,0 +1,17 @@
+default_language_version:
+  python: python3
+
+repos:
+  - repo: https://github.com/codespell-project/codespell
+    rev: v2.3.0
+    hooks:
+      - id: codespell
+        files: ^(about|community|contributing|getting_started|tutorials)/.*\.rst$
+        additional_dependencies: [tomli]
+
+  - repo: https://github.com/pre-commit/pre-commit-hooks
+    rev: v5.0.0
+    hooks:
+      - id: fix-byte-order-marker
+      - id: mixed-line-ending
+        args: ['--fix=lf']

+ 0 - 42
_tools/format.sh

@@ -1,42 +0,0 @@
-#!/usr/bin/env bash
-
-set -uo pipefail
-IFS=$'\n\t'
-
-# Loops through all text files tracked by Git.
-git grep -zIl '' |
-while IFS= read -rd '' f; do
-    # Exclude csproj and hdr files.
-    if [[ "$f" == *"csproj" ]]; then
-        continue
-    elif [[ "$f" == *"hdr" ]]; then
-        continue
-    fi
-    # Ensures that files are UTF-8 formatted.
-    recode UTF-8 "$f" 2> /dev/null
-    # Ensures that files have LF line endings.
-    dos2unix "$f" 2> /dev/null
-    # Ensures that files do not contain a BOM.
-    sed -i '1s/^\xEF\xBB\xBF//' "$f"
-    # Ensures that files end with newline characters.
-    tail -c1 < "$f" | read -r _ || echo >> "$f";
-done
-
-git diff > patch.patch
-FILESIZE="$(stat -c%s patch.patch)"
-MAXSIZE=5
-
-# If no patch has been generated all is OK, clean up, and exit.
-if (( FILESIZE < MAXSIZE )); then
-    printf "Files in this commit comply with the formatting rules.\n"
-    rm -f patch.patch
-    exit 0
-fi
-
-# A patch has been created, notify the user, clean up, and exit.
-printf "\n*** The following differences were found between the code "
-printf "and the formatting rules:\n\n"
-cat patch.patch
-printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
-rm -f patch.patch
-exit 1

+ 8 - 0
pyproject.toml

@@ -0,0 +1,8 @@
+[tool.ruff]
+line-length = 120
+
+[tool.codespell]
+dictionary = ["_tools/codespell-dict.txt", "-"]
+ignore-words = "_tools/codespell-ignore.txt"
+exclude-file = "_tools/codespell-ignore-lines.txt"
+skip = "tutorials/i18n/locales.rst"