Skip to content

Commit

Permalink
ENH: determine dependency group for pre-commit (#116)
Browse files Browse the repository at this point in the history
* DX: run `pyright` with `venv`
* ENH: use sparce checkout in `get-skipped-pre-commit-hooks`
* FIX: install `PyYAML` in `style group
* FIX: install `style` group in `dev` dependencies
  • Loading branch information
redeboer authored Oct 28, 2024
1 parent 9bd72cc commit fc13c8e
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 8 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"noreply",
"numprocesses",
"pyright",
"rtoml",
"taplo",
"tomllib"
],
Expand Down
11 changes: 5 additions & 6 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ on:

env:
DISABLE_PRE_COMMIT_UV_PATCH: True
FORCE_COLOR: true

jobs:
determine-hooks:
Expand Down Expand Up @@ -51,10 +52,6 @@ jobs:
**/pyproject.toml
**/uv.lock
enable-cache: true
- name: Determine if repository is Python package
run: |
uv_command=$(uv pip install -e '.[sty]' > /dev/null && uv sync --group style --no-dev && echo "uv run --group style --no-dev" || echo uvx)
echo "UV_COMMAND=$uv_command" | tee -a "$GITHUB_ENV"
- name: Fetch pre-commit cache
uses: actions/cache@v4
with:
Expand All @@ -63,8 +60,10 @@ jobs:
restore-keys: |
pre-commit-${{ runner.os }}-py${{ inputs.python-version }}
path: ~/.cache/pre-commit/
- id: uv-run
uses: ComPWA/actions/run-pre-commit@v2
- if: needs.determine-hooks.outputs.skipped-hooks == 'ALL'
run: ${{ env.UV_COMMAND }} --with pre-commit-uv pre-commit run --all-files --color always
run: ${{ steps.uv-run.outputs.cmd }} --with pre-commit-uv pre-commit run --all-files
- if: needs.determine-hooks.outputs.skipped-hooks != 'ALL'
name: Run pre-commit hooks that don't work on pre-commit.ci
run: |-
Expand All @@ -73,7 +72,7 @@ jobs:
export PRETTIER_LEGACY_CLI=1
fi
for hook in $skipped_hooks; do
${{ env.UV_COMMAND }} --with pre-commit-uv pre-commit run $hook --all-files --color always
${{ steps.uv-run.outputs.cmd }} --with pre-commit-uv pre-commit run $hook --all-files
done
- id: diff
if: always()
Expand Down
2 changes: 2 additions & 0 deletions get-skipped-pre-commit-hooks/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ runs:
using: composite
steps:
- uses: actions/checkout@v4
with:
sparse-checkout: .pre-commit-config.yaml
- uses: actions/setup-python@v5
with:
python-version: "3.12"
Expand Down
13 changes: 11 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ content-type = "text/markdown"
file = "README.md"

[dependency-groups]
dev = ["ruff"]
style = ["packaging"]
dev = [
"ruff",
{include-group = "style"},
]
style = [
"PyYAML",
"packaging",
"rtoml",
]

[tool.setuptools]
include-package-data = false
Expand All @@ -32,6 +39,8 @@ reportUnknownMemberType = false
reportUnknownParameterType = false
reportUnknownVariableType = false
typeCheckingMode = "strict"
venv = ".venv"
venvPath = "."

[tool.ruff]
preview = true
Expand Down
19 changes: 19 additions & 0 deletions run-pre-commit/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Run pre-commit hooks with style environment
description: >-
Run local pre-commit hooks that may require the installation of a virtual environment.
outputs:
cmd:
description: >-
The uv run command to use for `pre-commit`
value: ${{ steps.uv-run.outputs.cmd }}

runs:
using: composite
steps:
- env:
UV_SYSTEM_PYTHON: 1
id: uv-run
name: Determine skipped hooks
run: echo "cmd=$(uv run -p3.12 $GITHUB_ACTION_PATH/main.py)" | tee -a $GITHUB_OUTPUT
shell: bash
42 changes: 42 additions & 0 deletions run-pre-commit/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Determine which pre-commit hooks should be skipped."""

# /// script
# dependencies = ["rtoml"]
# ///
from pathlib import Path

import rtoml


def main() -> None:
cmd = _get_uv_run_command()
print(cmd)


def _get_uv_run_command() -> str:
pyproject_path = Path("pyproject.toml")
if pyproject_path.is_file():
pyproject = rtoml.load(pyproject_path)
dependency_groups = pyproject.get("dependency-groups")
candidate_groups = [
"style",
"sty",
"lint",
]
if dependency_groups is not None:
for group in candidate_groups:
if group in dependency_groups:
return f"uv run --group {group} --no-dev"
project_table = pyproject.get("project", {})
extras = project_table.get("optional-dependencies")
if extras is not None:
for extra in candidate_groups:
if extra in extras:
return f"uv run --extra {extra} --no-dev"
if "dependencies" in project_table:
return "uv run --no-dev"
return "uvx"


if __name__ == "__main__":
raise SystemExit(main())

0 comments on commit fc13c8e

Please sign in to comment.