diff --git a/.githooks/post-commit b/.githooks/post-commit deleted file mode 100755 index 39ba6f0..0000000 --- a/.githooks/post-commit +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env python3 - -import subprocess - -def run_linter(): - result = subprocess.run(["make", "lint"], capture_output=True, text=True) - return result.returncode == 0 - -if not run_linter(): - print("Applying linter failed. Commit aborted.") - exit(1) - -print("Linting passed. Proceeding with commit.") -exit(0) diff --git a/.github/workflows/linter_require_black.yaml b/.github/workflows/prek.yaml similarity index 74% rename from .github/workflows/linter_require_black.yaml rename to .github/workflows/prek.yaml index ec3f5d2..aa922bd 100644 --- a/.github/workflows/linter_require_black.yaml +++ b/.github/workflows/prek.yaml @@ -1,4 +1,4 @@ -name: Lint +name: Prek Checks on: pull_request: @@ -10,9 +10,11 @@ on: - '.github/workflows/**' jobs: - lint: + prek: if: github.event_name == 'pull_request' || github.event.pull_request == null runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: psf/black@stable \ No newline at end of file + with: + submodules: 'recursive' + - uses: j178/prek-action@v1 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..41cb44e --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,13 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v5.0.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + + - repo: https://github.com/psf/black + rev: 24.8.0 + hooks: + - id: black diff --git a/Makefile b/Makefile index fe47776..51909a7 100644 --- a/Makefile +++ b/Makefile @@ -42,13 +42,23 @@ check_jq: jq --version; \ fi +check_prek: + @echo "$(YELLOW)🔍Checking prek version...$(RESET)" + @if ! command -v prek > /dev/null 2>&1; then \ + echo "$(RED)prek is not installed. Please install prek before proceeding.$(RESET)"; \ + echo "$(RED)curl --proto '=https' --tlsv1.2 -LsSf https://github.com/j178/prek/releases/download/v0.3.3/prek-installer.sh | sh$(RESET)"; \ + exit 1; \ + else \ + prek --version; \ + fi + ######################################################## # Setup githooks for linting ######################################################## -setup_githooks: - @echo "$(YELLOW)🔨Setting up githooks on post-commit...$(RESET)" - chmod +x .githooks/post-commit - git config core.hooksPath .githooks +install_hooks: check_prek + @echo "$(YELLOW)🔨Installing pre-commit hooks with prek...$(RESET)" + @git config --unset core.hooksPath || true + @prek install ######################################################## @@ -75,7 +85,7 @@ view_python_venv_size_by_libraries: # Run Main Application ######################################################## -all: update_python_dep setup_githooks +all: update_python_dep install_hooks @echo "$(GREEN)🏁Running main application...$(RESET)" @$(PYTHON) main.py @echo "$(GREEN)✅ Main application run completed.$(RESET)" @@ -102,6 +112,11 @@ test: check_rye IGNORE_LINT_DIRS = .venv|venv LINE_LENGTH = 88 +lint: check_prek + @echo "$(YELLOW)🔍Running pre-commit hooks...$(RESET)" + @prek run --all-files + @echo "$(GREEN)✅Pre-commit hooks passed.$(RESET)" + fmt: check_rye check_jq @echo "$(YELLOW)✨Formatting project with Black...$(RESET)" @rye run black --exclude '/($(IGNORE_LINT_DIRS))/' . --line-length $(LINE_LENGTH) diff --git a/README.md b/README.md index e7fa5f1..1beddc0 100644 --- a/README.md +++ b/README.md @@ -52,11 +52,16 @@ ``` curl -sSf https://rye.astral.sh/get | bash ``` +- [prek](https://github.com/j178/prek) + ``` + curl --proto '=https' --tlsv1.2 -LsSf https://github.com/j178/prek/releases/download/v0.3.3/prek-installer.sh | sh + ``` ## Quick Start - `make all` - runs `main.py` - `make fmt` - runs `black` linter, an opinionated linter +- `make lint` - runs `prek` hooks (linting, formatting, checks) - `make banner` - create a new banner that makes the README nice 😊 - `make test` - runs all tests defined by `TEST_TARGETS = tests/folder1 tests/folder2` @@ -83,6 +88,7 @@ This software uses the following tools: - [Cursor: The AI Code Editor](cursor.com) - [Rye: a Hassle-Free Python Experience](https://rye.astral.sh/) +- [prek: Better pre-commit, re-engineered in Rust](https://github.com/j178/prek) - [DSPY: Pytorch for LLM Inference](https://dspy.ai/) - [LangFuse: LLM Observability Tool](https://langfuse.com/)