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