Search docs

Concepts

Architecture

Trace the released package modules and command flow.

1. Overview#

Spawn is a Python CLI tool built with Typer and Rich. It scaffolds new Python projects from templates, optionally publishes them to GitHub, and scores existing projects for environment health.

The main capabilities are project generation, optional GitHub publishing, project health checking, static inspection and repository onboarding.

2. Technology Stack#

Tool Version Role
Python 3.12+ Core language
Typer 0.26.7+ CLI framework
Rich 15.0.0+ Terminal UI
uv any Environment management
Git any Version control
Hatchling any Build backend

Entry point: spawn.cli.app:main (defined in pyproject.toml as [project.scripts]).

3. Repository Structure#

text
src/spawn/
├── __init__.py         # __version__ via importlib.metadata, with source fallback kept in sync with pyproject.toml
├── cli/
│   ├── app.py          # Typer app: create, version (and --version), doctor, inspect, onboard commands
│   ├── inspect_render.py # Rich rendering of an InspectionReport for `spawn inspect`
│   ├── noninteractive.py # Build config from CLI flags or JSON config file
│   └── prompts.py      # Interactive prompts; derives menu from registry
├── core/
│   ├── exceptions.py   # SpawnError — base exception
│   ├── models.py       # ProjectConfig dataclass (name, template, use_git, framework, extras, license)
│   └── registry.py     # TemplateMetadata, TEMPLATES dict, get_template(),
│                       # instantiate_template(), get_metadata(), list_templates()
├── generators/
│   ├── custom_structure.py   # Custom structure parser and generator
│   ├── project_files.py      # Standalone file writers for LICENSE, README, configs, CI
│   └── project_generator.py  # Orchestrates generation, dependency install, meta.json write
├── github/
│   ├── exceptions.py   # GitHubPublishError
│   ├── publisher.py    # GitHubPublisher — stages, commits, pushes
│   └── validators.py   # GitHub URL validation
├── inspection/         # Read-only repository inspection (never imports spawn.cli)
│   ├── models.py       # Report/finding/evidence/diagnostic models, SCHEMA_VERSION, validate_report
│   ├── base.py         # Analyzer protocol
│   ├── discovery.py    # validate_inspection_root, discover_repository (single inventory scan)
│   ├── languages.py    # LanguageDetector
│   ├── frameworks.py   # FrameworkDetector
│   ├── dependencies.py # DependencyAnalyzer (dependencies, package managers)
│   ├── tooling.py      # ToolingAnalyzer (tools, CI, containers, databases, structure)
│   ├── orchestrator.py # run_inspection: runs analyzers, aggregates, builds the report
│   ├── serialization.py # to_json / from_json
│   ├── exceptions.py   # InspectionError (exit code 7)
│   └── _reading.py     # Bounded file reads
├── templates/
│   ├── base.py         # BaseTemplate dataclass
│   ├── shared_content.py     # README_CONTENT, GITIGNORE_CONTENT, CI, and tool configs
│   ├── backend_api/
│   │   ├── __init__.py       # BackendAPITemplate (dispatches by framework)
│   │   └── content.py        # All FastAPI/Flask/Django/Docker/CI content strings
│   ├── cli_application/
│   │   ├── __init__.py       # CLITemplate (dispatches by framework + cli_type)
│   │   └── content.py        # All framework × cli_type content strings
│   ├── automation/
│   │   ├── __init__.py       # AutomationTemplate
│   │   └── content.py        # Workflow, task, logger, test, README content strings
│   ├── chatbot/
│   │   ├── __init__.py       # ChatbotTemplate (branches on framework)
│   │   └── content.py        # LLM provider, chat, prompt, test, README strings
│   ├── python_script/        # Reserved — not in active menu
│   ├── data_science/         # Reserved — not in active menu
│   └── ml_project/           # Reserved — not in active menu
└── utils/
    ├── banner.py       # show_banner() — ASCII wordmark with ice-fade colours
    ├── console.py      # Shared Rich Console instance
    ├── doctor.py       # ProjectHealthChecker, run_health_check()
    ├── doctor_fix.py   # FixAction, FixResult, plan_fixes(), apply_fixes(), tool_hints()
    ├── git.py          # initialize_git(), run_git_command(), add_all(), commit() etc.
    ├── success.py      # show_success() — renders the success panel
    ├── theme.py        # UI theme colors and styles
    ├── uv.py           # initialize_uv(), install_packages()
    └── validators.py   # validate_project_name()

4. Key Flows#

4a. Project Creation Flow#

  1. User runs spawn create
  2. app.create() builds a ProjectConfig:
    • non-interactive (--name or --config): cli/noninteractive.py calls core/planning.py::plan_project, which validates the name, rejects an existing destination, and resolves template options, extras and license. Config-file mode ignores explicit --no-git/--no-uv with a warning.
    • interactive: cli/prompts.py::get_project_config() collects the answers (menus derived from the registry) and builds the config directly
  3. --dry-run prints the resolved config and stops; nothing is written
  4. generators/pipeline.py::generate_project(config) dispatches on config.template: ProjectGenerator.generate() for registry templates, CustomStructureGenerator.generate() for "custom"
  5. Both generators call generators/destination.py: resolve_destination (single-directory name check), assert_available (fails with FilesystemError if the path exists in any form, including symlinks) and, for custom structures, safe_join (rejects traversal and absolute paths)
  6. ProjectGenerator resolves the template via instantiate_template(config), creates directories, writes starter files, README and .gitignore, runs initialize_git(), initialize_uv(), install_packages() and post_install() (the uv steps are skipped with --no-uv; file-only extras are still written and the rest are reported in one "Skipped without uv" warning), then generators/metadata.py::write_project_meta
  7. show_success() renders the success panel
  8. If Git was enabled and not in non-interactive/--yes mode, optionally GitHubPublisher.publish() stages, commits, and pushes

Cleanup rule: on any failure a generator removes the project directory only if that run created it (destination.cleanup_created, which also refuses symlinks, the cwd and its parents, the home directory and filesystem roots). A pre-existing directory is never touched.

text
spawn create
    │
    ▼
show_banner()                 banner.py
    │
    ▼
plan_project()                planning.py    (flags / --config)
get_project_config()          prompts.py     (interactive)
    │  → ProjectConfig
    ▼
generate_project()            pipeline.py
    ├── ProjectGenerator / CustomStructureGenerator
    │     ├── resolve_destination + assert_available   destination.py
    │     ├── instantiate_template()  registry.py  (forwards framework + extras)
    │     ├── mkdir + write files, README, .gitignore
    │     ├── initialize_git()        git.py         (if use_git)
    │     ├── initialize_uv() / install_packages()   (if use_uv)
    │     └── post_install()          template       (ruff/pytest config, Docker, CI)
    └── write_project_meta()    metadata.py  → .spawn/meta.json
                                (called by ProjectGenerator, or by generate_project for custom)
    │
    ▼
show_success()                success.py

4b. Template System#

BaseTemplate is a dataclass with these fields:

Field Type Purpose
name str Display name shown in the success panel
folders list[str] Directories created under the project root
starter_files list[tuple[str, str]] (relative_path, content_template) pairs; {project_name} substituted at write time
next_steps list[str] Run commands shown after creation; {project_name} substituted at display time

BaseTemplate also provides overridable methods:

Method Default Purpose
generate(project_path, context) Creates folders and writes starter files Template-specific generation
get_readme_content(context) Returns None (use shared README) Custom README per template
get_dependencies() Returns [] Packages to install via uv add
post_install(project_path) No-op Config writes, Docker files, CI files

Each template lives in its own subdirectory with an __init__.py (class) and content.py (string constants).

Registry#

registry.py holds TEMPLATES: dict[str, TemplateMetadata]. Current entries in order:

Key Template class Frameworks Extras
backend-api BackendAPITemplate fastapi, flask, django ruff, pytest, docker, github-actions, pre-commit, mypy
cli CLITemplate typer, click, argparse ruff, pytest, github-actions, pre-commit, mypy
automation AutomationTemplate none ruff, pytest, github-actions, pre-commit, mypy
chatbot ChatbotTemplate pydantic-ai, openai-sdk, litellm ruff, pytest, rich, github-actions, pre-commit, mypy
agent AgentTemplate pydantic-ai, openai-agents ruff, pytest, github-actions, pre-commit, mypy
rag RAGTemplate none ruff, pytest, github-actions, pre-commit, mypy
data DataProjectTemplate none ruff, pytest, github-actions, pre-commit, mypy
mcp MCPServerTemplate none ruff, pytest, github-actions, pre-commit, mypy

get_template(slug) returns a default-constructed instance. instantiate_template(config) forwards framework, extras, and cli_type from ProjectConfig to templates whose constructors accept them, using signature introspection.

_REMOVED_SLUGS = {"fastapi", "python", "data-science", "ml"} documents slugs that existed in previous versions but are no longer registered.

4c. Backend API Dispatch#

BackendAPITemplate.__init__ branches on framework to select the correct folder list, starter files, and next steps:

text
framework == "flask"   → FLASK_FOLDERS, FLASK_FILES, flask next steps
framework == "django"  → DJANGO_FOLDERS, DJANGO_FILES, django next steps
else (None/"fastapi")  → FASTAPI_FOLDERS, FASTAPI_FILES, fastapi next steps

get_dependencies() returns base deps for the framework plus any selected extras. post_install() writes ruff config, pytest filterwarnings, Dockerfile, .dockerignore, and/or .github/workflows/ci.yml depending on self.extras.

4d. Error Handling#

text
SpawnError                      exit 1   core/exceptions.py
├── InvalidInputError           exit 1
│   └── StructureParseError     exit 1
├── ConfigError                 exit 1
├── FilesystemError             exit 3
├── ToolchainError              exit 4
├── GenerationError             exit 5
│   └── TemplateError           exit 5
└── PublishError                exit 6
    └── GitHubPublishError      exit 6   github/exceptions.py

Generators convert OSError to FilesystemError, remove only the directory the current run created, and re-raise. cli/errors.py owns the CLI handlers: fail(e) prints ❌ message in red and exits with e.exit_code; report_unexpected(e) handles any other exception, printing a short message and exiting 10 (UNEXPECTED_EXIT_CODE), or re-raising for a full traceback when SPAWN_DEBUG is set to anything other than empty/0/false/no. Ctrl+C, EOF and declined prompts exit 130; Click usage errors exit 2. See Exit codes.

5. Adding a New Template#

  1. Create src/spawn/templates/your_intent/ with __init__.py and content.py
  2. Subclass BaseTemplate; set name, folders, starter_files, next_steps
  3. Override get_dependencies(), post_install(), get_readme_content() as needed
  4. Register in registry.py — add a TemplateMetadata entry to TEMPLATES
  5. Write tests in test_templates.py and test_registry.py

6. Running Tests#

bash
uv run pytest
uv run pytest -v
uv run pytest tests/test_generator.py
uv run ruff check .

7. Pipeline#

text
input -> plan_project -> ProjectConfig -> generate_project -> filesystem -> meta -> publish (optional)
Step Owner
Input (flags, --config JSON, prompts) cli/noninteractive.py, cli/prompts.py
Plan: validate and resolve options core/planning.py::plan_project
Canonical model core/models.py::ProjectConfig
Generate: folders, files, git, uv, rollback generators/pipeline.py::generate_project
Meta: .spawn/meta.json generators/metadata.py::write_project_meta
Publish github/publisher.py::GitHubPublisher (called from cli/app.py)

8. Canonical model#

ProjectConfig is the single model passed between stages.

Field Meaning
name, template Project name; template slug ("custom" for pasted structures)
framework, provider, cli_type, data_type Template options
extras, license, generate_claude_md Optional tooling, license (mit/none), also write CLAUDE.md
use_git, use_uv Toolchain steps
custom_* Custom-structure entries, dependencies, dev setup, gitignore extras, source format
destination Target directory (Path or None); excluded from equality

to_dict() returns a JSON-serializable dict of every field (destination as a string).

.spawn/meta.json is a historical record, not authoritative: it stores intent, framework, provider, spawn_version, created_at, generator, git, uv and source only. It does not record name, cli_type, data_type, extras, license or generate_claude_md, and the project may have changed since. ProjectConfig is generation intent only; it cannot be reconstructed from metadata.

9. Extension boundary#

Code that analyses existing projects must not depend on the CLI. It obtains state through core/, generators/ and templates/:

Need Use
State of an existing project core.project.read_project_metadata(path): a ProjectMetadata, or None if there is no .spawn/meta.json; raises ConfigError if the file is unreadable or invalid. Read-only; does not validate the intent against the registry
Resolve options into a config core.planning.plan_project(...)
Template catalogue core.registry.get_metadata(), list_templates()
Create a project generators.pipeline.generate_project(config)

Rule: no module under core/, generators/, templates/, utils/ or github/ may import spawn.cli. tests/test_boundaries.py enforces this.

To add a template: subclass BaseTemplate in templates/<slug>/ and add a TemplateMetadata entry to TEMPLATES (see section 5).

10. Error model#

All Spawn errors derive from SpawnError (core/exceptions.py); each class carries the CLI exit_code. The CLI prints ❌ message and exits with that code. See Exit codes.

11. Inspection pipeline#

text
spawn inspect -> run_inspection -> validate root -> discover once -> analyzers -> aggregate/dedupe/sort -> InspectionReport -> human or JSON
Step Owner
CLI: argument, --json, exit codes cli/app.py::inspect_command
Validate root (bad root raises InspectionError, exit 7) inspection/discovery.py::validate_inspection_root
Discover once: sorted files, directories, skipped, diagnostics; symlinks never followed inspection/discovery.py::discover_repository
Analyzers, fixed order: language, framework, dependency, tooling; each gets the same immutable inventory inspection/orchestrator.py::ANALYZERS
Isolate: a crashing or invalid analyzer becomes a failed result, others continue orchestrator._run_one
Aggregate: validate findings, merge by (category, subject), sort; derive summary and structure orchestrator._aggregate, _summary
Render: Rich output, or to_json on stdout cli/inspect_render.py, inspection/serialization.py

Analyzers only consume the inventory (plus bounded reads of files it lists); they do not walk the tree. Nothing is executed, installed, fetched or written. .spawn/meta.json is read at most once, as untrusted recorded metadata.

Module boundary: inspection/ never imports spawn.cli; tests/test_boundaries.py enforces this. Statuses and semantics are in the inspection contract; what is detected is in supported technologies.

Repository onboarding#

spawn.onboarding.build_onboarding_report exposes the onboarding report; inspection/entry_points.py and inspection/development_workflows.py analyze static evidence. cli/onboard_render.py renders the terminal report. JSON output has its own schema.

ESC