Coverage for src/ai_shell/scaffold.py: 100%
30 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-05 22:06 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-05 22:06 +0000
1"""Project scaffolding for ai-shell init.
3Template content lives in ``ai_shell/templates/`` as plain files
4(YAML, TOML) so they can be edited directly.
5"""
7from __future__ import annotations
9import logging
10from importlib import resources
11from pathlib import Path
13from rich.console import Console
15logger = logging.getLogger(__name__)
16console = Console(stderr=True)
18_TEMPLATES = resources.files("ai_shell.templates")
20_PROJECT_FILES = [".ai-shell.yaml", ".ai-shell.yml", ".ai-shell.toml", "ai-shell.toml"]
23# ── Helpers ─────────────────────────────────────────────────────────
26def _read_template(*parts: str) -> str:
27 """Read a template file from the ``ai_shell.templates`` package."""
28 ref = _TEMPLATES.joinpath(*parts)
29 content = ref.read_text(encoding="utf-8")
30 return content.replace("\r\n", "\n")
33def _write_file(path: Path, content: str, *, overwrite: bool) -> bool:
34 """Write *content* to *path*, creating parent dirs as needed.
36 Returns ``True`` if the file was written, ``False`` if skipped.
37 """
38 if path.exists() and not overwrite:
39 console.print(f"[yellow]Skipped (already exists): {path}[/yellow]")
40 return False
42 label = "Updated" if path.exists() else "Created"
43 path.parent.mkdir(parents=True, exist_ok=True)
44 path.write_text(content, encoding="utf-8", newline="\n")
45 console.print(f"[green]{label}: {path}[/green]")
46 return True
49# ── Public API ──────────────────────────────────────────────────────
52def scaffold_global() -> None:
53 """Create ``~/.augint/.env.example`` and ``~/.augint/.ai-shell.example.yaml``."""
54 augint_dir = Path.home() / ".augint"
55 augint_dir.mkdir(parents=True, exist_ok=True)
57 _write_file(
58 augint_dir / ".env.example",
59 _read_template("augint-env.example"),
60 overwrite=True,
61 )
62 _write_file(
63 augint_dir / ".ai-shell.example.yaml",
64 _read_template("augint-ai-shell.example.yaml"),
65 overwrite=True,
66 )
69def scaffold_project(target_dir: Path) -> None:
70 """Create ``.ai-shell.yaml`` in *target_dir* if it does not already exist."""
71 _write_file(
72 target_dir / ".ai-shell.yaml",
73 _read_template("ai-shell.yaml"),
74 overwrite=False,
75 )
76 console.print("[bold green]Project configuration ready.[/bold green]")