|
| 1 | +"""Shared infrastructure and registration for ``specify bundle`` commands. |
| 2 | +
|
| 3 | +Command handlers live in ``command_*.py`` modules. The nested ``catalog`` |
| 4 | +namespace registers through ``bundles.catalog``; domain behavior remains in |
| 5 | +Typer-free modules in this package. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import typer |
| 13 | +from rich.markup import escape as _escape_markup |
| 14 | + |
| 15 | +from .._console import err_console |
| 16 | +from . import BundlerError |
| 17 | +from .project import active_integration |
| 18 | +from .records import load_records |
| 19 | + |
| 20 | +bundle_app = typer.Typer( |
| 21 | + name="bundle", |
| 22 | + help="Discover, install, and author Spec Kit bundles", |
| 23 | + add_completion=False, |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +def _fail(message: str) -> None: |
| 28 | + """Print an actionable error to stderr and exit non-zero.""" |
| 29 | + # Use the stderr console so the error never lands on stdout, which under |
| 30 | + # ``--json`` carries the machine-readable payload and must stay parseable. |
| 31 | + # Escape the message: every caller passes ``str(exc)`` from a BundlerError |
| 32 | + # that interpolates untrusted data (a CLI argument, a catalog url, a |
| 33 | + # bundle.yml field), so a '[...]' in it would be parsed as a Rich style tag |
| 34 | + # -- silently swallowing the text, or raising MarkupError on an unbalanced |
| 35 | + # closer and replacing the whole message with a traceback. |
| 36 | + err_console.print(f"[red]Error:[/red] {_escape_markup(message)}", style=None) |
| 37 | + raise typer.Exit(code=1) |
| 38 | + |
| 39 | + |
| 40 | +def _user_config_dir() -> Path: |
| 41 | + # User-scope Spec Kit config lives under ~/.specify (same convention as |
| 42 | + # auth.json, extension/preset catalogs). Passing this through to the source |
| 43 | + # stack is what makes the documented project > user > built-in precedence |
| 44 | + # reachable from the CLI. |
| 45 | + return Path.home() / ".specify" |
| 46 | + |
| 47 | + |
| 48 | +def _build_stack(project_root: Path, *, offline: bool): |
| 49 | + from .adapters import make_catalog_fetcher |
| 50 | + from .catalog_stack import CatalogStack |
| 51 | + |
| 52 | + fetcher = make_catalog_fetcher(allow_network=not offline) |
| 53 | + return CatalogStack.load(project_root, fetcher, user_config_dir=_user_config_dir()) |
| 54 | + |
| 55 | + |
| 56 | +def _speckit_version() -> str: |
| 57 | + from .._assets import get_speckit_version |
| 58 | + |
| 59 | + return get_speckit_version() |
| 60 | + |
| 61 | + |
| 62 | +def _trust_level(verified: bool) -> str: |
| 63 | + """Trust framing for a catalog entry (FR-010): org-curated vs community.""" |
| 64 | + return "verified" if verified else "community" |
| 65 | + |
| 66 | + |
| 67 | +def _trust_badge(verified: bool) -> str: |
| 68 | + return "[green]✔ verified[/green]" if verified else "[yellow]community[/yellow]" |
| 69 | + |
| 70 | + |
| 71 | +def _default_script_type() -> str: |
| 72 | + """OS-appropriate default script flavor (FR-013).""" |
| 73 | + import os |
| 74 | + |
| 75 | + return "ps" if os.name == "nt" else "sh" |
| 76 | + |
| 77 | + |
| 78 | +def _run_init(integration: str, *, script_type: str, offline: bool = False) -> None: |
| 79 | + """Idempotently scaffold a Spec Kit project here via the existing ``init`` machinery. |
| 80 | +
|
| 81 | + Reuses the real ``specify init`` command callback in-process (Principle I) |
| 82 | + with ``--here --force`` so it is non-interactive and merges into the current |
| 83 | + directory. |
| 84 | + """ |
| 85 | + from .. import app |
| 86 | + |
| 87 | + init_cb = next( |
| 88 | + c.callback |
| 89 | + for c in app.registered_commands |
| 90 | + if c.callback and c.callback.__name__ == "init" |
| 91 | + ) |
| 92 | + try: |
| 93 | + init_cb( |
| 94 | + project_name=None, |
| 95 | + script_type=script_type, |
| 96 | + ignore_agent_tools=True, |
| 97 | + here=True, |
| 98 | + force=True, |
| 99 | + skip_tls=False, |
| 100 | + debug=False, |
| 101 | + github_token=None, |
| 102 | + offline=offline, |
| 103 | + preset=None, |
| 104 | + integration=integration, |
| 105 | + integration_options=None, |
| 106 | + extensions=None, |
| 107 | + trust_extension_urls=False, |
| 108 | + ) |
| 109 | + except typer.Exit as exc: |
| 110 | + if exc.exit_code: |
| 111 | + raise BundlerError( |
| 112 | + f"Failed to initialize a Spec Kit project (integration '{integration}')." |
| 113 | + ) from exc |
| 114 | + |
| 115 | + |
| 116 | +def _resolve_init_integration(override: str | None, manifest) -> str: |
| 117 | + """Precedence (FR-013): explicit override → bundle-declared → default.""" |
| 118 | + from .._agent_config import resolve_default_init_integration |
| 119 | + |
| 120 | + if override: |
| 121 | + return override |
| 122 | + if manifest is not None and manifest.integration is not None: |
| 123 | + return manifest.integration.id |
| 124 | + return resolve_default_init_integration() |
| 125 | + |
| 126 | + |
| 127 | +def _bundle_overlaps(project_root: Path, manifest, *, offline: bool) -> list[str]: |
| 128 | + """Return informational overlaps between *manifest* and installed bundles.""" |
| 129 | + if manifest is None: |
| 130 | + return [] |
| 131 | + try: |
| 132 | + from .conflict import detect_conflicts |
| 133 | + |
| 134 | + report = detect_conflicts( |
| 135 | + manifest, |
| 136 | + active_integration(project_root), |
| 137 | + load_records(project_root), |
| 138 | + ) |
| 139 | + return list(report.overlaps) |
| 140 | + except BundlerError: |
| 141 | + return [] |
| 142 | + |
| 143 | + |
| 144 | +def register(app: typer.Typer) -> None: |
| 145 | + """Attach the bundle command group to the root Typer app.""" |
| 146 | + from .catalog import register as register_catalog |
| 147 | + |
| 148 | + register_catalog(bundle_app) |
| 149 | + |
| 150 | + # isort: off |
| 151 | + from . import command_search # noqa: F401 — registers handler via decorator |
| 152 | + from . import command_info # noqa: F401 — registers handler via decorator |
| 153 | + from . import command_list # noqa: F401 — registers handler via decorator |
| 154 | + from . import command_install # noqa: F401 — registers handler via decorator |
| 155 | + from . import command_add # noqa: F401 — registers handler via decorator |
| 156 | + from . import command_update # noqa: F401 — registers handler via decorator |
| 157 | + from . import command_remove # noqa: F401 — registers handler via decorator |
| 158 | + from . import command_validate # noqa: F401 — registers handler via decorator |
| 159 | + from . import command_build # noqa: F401 — registers handler via decorator |
| 160 | + from . import command_init # noqa: F401 — registers handler via decorator |
| 161 | + # isort: on |
| 162 | + |
| 163 | + app.add_typer(bundle_app, name="bundle") |
0 commit comments