1from __future__ import annotations
 2
 3import os
 4from pathlib import Path
 5
 6import click
 7
 8from plain.cli import register_cli
 9from plain.cli.check import check_short, run_core_checks
10from plain.cli.runtime import without_runtime_setup
11
12
13def install_git_hook() -> None:
14    hook_path = os.path.join(".git", "hooks", "pre-commit")
15    if os.path.exists(hook_path):
16        print("pre-commit hook already exists")
17    else:
18        with open(hook_path, "w") as f:
19            f.write(
20                """#!/bin/sh
21uv run plain pre-commit"""
22            )
23        os.chmod(hook_path, 0o755)
24        print("pre-commit hook installed")
25
26
27@register_cli("pre-commit")
28@click.group(invoke_without_command=True)
29@click.pass_context
30def cli(ctx: click.Context) -> None:
31    """Git pre-commit checks"""
32    # If no subcommand is provided, run the checks
33    if ctx.invoked_subcommand is None:
34        run_checks()
35
36
37@cli.command()
38@without_runtime_setup
39def install() -> None:
40    """Install the pre-commit git hook"""
41    install_git_hook()
42
43
44def run_checks() -> None:
45    """Run all pre-commit checks"""
46
47    if Path("uv.lock").exists():
48        check_short("uv lock --check", "uv", "lock", "--check")
49
50    run_core_checks(skip_test=False)
51
52    check_short("plain build", "plain", "build")