Skip to content

Commit ebb909f

Browse files
committed
feat: Stage 2b — --integration flag, routing, agent.json, shared infra
- Added --integration flag to init() (mutually exclusive with --ai) - --ai copilot auto-promotes to integration path with migration nudge - Integration setup writes .specify/agent.json with integration key - _install_shared_infra() copies scripts and templates to .specify/ - init-options.json records 'integration' key when used - 4 new CLI tests: mutual exclusivity, unknown rejection, copilot end-to-end, auto-promote (74 total integration tests) Part of github#1924
1 parent b32f101 commit ebb909f

2 files changed

Lines changed: 232 additions & 4 deletions

File tree

‎src/specify_cli/__init__.py‎

Lines changed: 116 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,55 @@ def _locate_release_script() -> tuple[Path, str]:
11971197
raise FileNotFoundError(f"Release script '{name}' not found in core_pack or source checkout")
11981198

11991199

1200+
def _install_shared_infra(
1201+
project_path: Path,
1202+
script_type: str,
1203+
tracker: StepTracker | None = None,
1204+
) -> bool:
1205+
"""Install shared infrastructure files into *project_path*.
1206+
1207+
Copies ``.specify/scripts/``, ``.specify/templates/``, and
1208+
``.specify/memory/`` from the bundled core_pack or source checkout.
1209+
Returns ``True`` on success.
1210+
"""
1211+
core = _locate_core_pack()
1212+
1213+
# Scripts
1214+
if core and (core / "scripts").is_dir():
1215+
scripts_src = core / "scripts"
1216+
else:
1217+
repo_root = Path(__file__).parent.parent.parent
1218+
scripts_src = repo_root / "scripts"
1219+
1220+
if scripts_src.is_dir():
1221+
dest_scripts = project_path / ".specify" / "scripts"
1222+
dest_scripts.mkdir(parents=True, exist_ok=True)
1223+
variant_dir = "bash" if script_type == "sh" else "powershell"
1224+
variant_src = scripts_src / variant_dir
1225+
if variant_src.is_dir():
1226+
dest_variant = dest_scripts / variant_dir
1227+
if dest_variant.exists():
1228+
shutil.rmtree(dest_variant)
1229+
shutil.copytree(variant_src, dest_variant)
1230+
1231+
# Page templates (not command templates, not vscode-settings.json)
1232+
if core and (core / "templates").is_dir():
1233+
templates_src = core / "templates"
1234+
else:
1235+
repo_root = Path(__file__).parent.parent.parent
1236+
templates_src = repo_root / "templates"
1237+
1238+
if templates_src.is_dir():
1239+
dest_templates = project_path / ".specify" / "templates"
1240+
dest_templates.mkdir(parents=True, exist_ok=True)
1241+
for f in templates_src.iterdir():
1242+
if f.is_file() and f.name != "vscode-settings.json" and not f.name.startswith("."):
1243+
# Skip command templates directory
1244+
shutil.copy2(f, dest_templates / f.name)
1245+
1246+
return True
1247+
1248+
12001249
def scaffold_from_core_pack(
12011250
project_path: Path,
12021251
ai_assistant: str,
@@ -1828,6 +1877,7 @@ def init(
18281877
offline: bool = typer.Option(False, "--offline", help="Use assets bundled in the specify-cli package instead of downloading from GitHub (no network access required). Bundled assets will become the default in v0.6.0 and this flag will be removed."),
18291878
preset: str = typer.Option(None, "--preset", help="Install a preset during initialization (by preset ID)"),
18301879
branch_numbering: str = typer.Option(None, "--branch-numbering", help="Branch numbering strategy: 'sequential' (001, 002, ...) or 'timestamp' (YYYYMMDD-HHMMSS)"),
1880+
integration: str = typer.Option(None, "--integration", help="Use the new integration system (e.g. --integration copilot). Mutually exclusive with --ai."),
18311881
):
18321882
"""
18331883
Initialize a new Specify project.
@@ -1889,6 +1939,34 @@ def init(
18891939
if ai_assistant:
18901940
ai_assistant = AI_ASSISTANT_ALIASES.get(ai_assistant, ai_assistant)
18911941

1942+
# --integration and --ai are mutually exclusive
1943+
if integration and ai_assistant:
1944+
console.print("[red]Error:[/red] --integration and --ai are mutually exclusive")
1945+
console.print("[yellow]Use:[/yellow] --integration for the new integration system, or --ai for the legacy path")
1946+
raise typer.Exit(1)
1947+
1948+
# Auto-promote: --ai copilot → integration path with a nudge
1949+
use_integration = False
1950+
if integration:
1951+
from .integrations import get_integration
1952+
resolved_integration = get_integration(integration)
1953+
if not resolved_integration:
1954+
console.print(f"[red]Error:[/red] Unknown integration: '{integration}'")
1955+
console.print("[yellow]Available integrations:[/yellow] copilot")
1956+
raise typer.Exit(1)
1957+
use_integration = True
1958+
# Map integration key to the ai_assistant variable for downstream compatibility
1959+
ai_assistant = integration
1960+
elif ai_assistant == "copilot":
1961+
from .integrations import get_integration
1962+
resolved_integration = get_integration("copilot")
1963+
if resolved_integration:
1964+
use_integration = True
1965+
console.print(
1966+
"[dim]Tip: Use [bold]--integration copilot[/bold] instead of "
1967+
"--ai copilot. The --ai flag will be deprecated in a future release.[/dim]"
1968+
)
1969+
18921970
if project_name == ".":
18931971
here = True
18941972
project_name = None # Clear project_name to use existing validation logic
@@ -2057,7 +2135,10 @@ def init(
20572135
"This will become the default in v0.6.0."
20582136
)
20592137

2060-
if use_github:
2138+
if use_integration:
2139+
tracker.add("integration", "Install integration")
2140+
tracker.add("shared-infra", "Install shared infrastructure")
2141+
elif use_github:
20612142
for key, label in [
20622143
("fetch", "Fetch latest release"),
20632144
("download", "Download template"),
@@ -2092,7 +2173,35 @@ def init(
20922173
verify = not skip_tls
20932174
local_ssl_context = ssl_context if verify else False
20942175

2095-
if use_github:
2176+
if use_integration:
2177+
# Integration-based scaffolding (new path)
2178+
from .integrations.manifest import IntegrationManifest
2179+
tracker.start("integration")
2180+
manifest = IntegrationManifest(
2181+
resolved_integration.key, project_path, version=get_speckit_version()
2182+
)
2183+
resolved_integration.setup(
2184+
project_path, manifest,
2185+
script_type=selected_script,
2186+
)
2187+
manifest.save()
2188+
2189+
# Write .specify/agent.json
2190+
agent_json = project_path / ".specify" / "agent.json"
2191+
agent_json.parent.mkdir(parents=True, exist_ok=True)
2192+
agent_json.write_text(json.dumps({
2193+
"integration": resolved_integration.key,
2194+
"version": get_speckit_version(),
2195+
}, indent=2) + "\n", encoding="utf-8")
2196+
2197+
tracker.complete("integration", resolved_integration.config.get("name", resolved_integration.key))
2198+
2199+
# Install shared infrastructure (scripts, templates)
2200+
tracker.start("shared-infra")
2201+
_install_shared_infra(project_path, selected_script, tracker=tracker)
2202+
tracker.complete("shared-infra", f"scripts ({selected_script}) + templates")
2203+
2204+
elif use_github:
20962205
with httpx.Client(verify=local_ssl_context) as local_client:
20972206
download_and_extract_template(
20982207
project_path,
@@ -2227,7 +2336,7 @@ def init(
22272336
# Persist the CLI options so later operations (e.g. preset add)
22282337
# can adapt their behaviour without re-scanning the filesystem.
22292338
# Must be saved BEFORE preset install so _get_skills_dir() works.
2230-
save_init_options(project_path, {
2339+
init_opts = {
22312340
"ai": selected_ai,
22322341
"ai_skills": ai_skills,
22332342
"ai_commands_dir": ai_commands_dir,
@@ -2237,7 +2346,10 @@ def init(
22372346
"offline": offline,
22382347
"script": selected_script,
22392348
"speckit_version": get_speckit_version(),
2240-
})
2349+
}
2350+
if use_integration:
2351+
init_opts["integration"] = resolved_integration.key
2352+
save_init_options(project_path, init_opts)
22412353

22422354
# Install preset if specified
22432355
if preset:

‎tests/test_integrations.py‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,3 +747,119 @@ def test_templates_are_processed(self, tmp_path):
747747
assert "\nagent_scripts:\n" not in content, f"{agent_file.name} has raw agent_scripts: block"
748748
# Paths should be rewritten
749749
assert "scripts/bash/" not in content or ".specify/scripts/bash/" in content
750+
751+
752+
# ═══════════════════════════════════════════════════════════════════════════
753+
# CLI --integration flag (Stage 2b)
754+
# ═══════════════════════════════════════════════════════════════════════════
755+
756+
757+
class TestInitIntegrationFlag:
758+
"""Tests for the --integration flag on specify init."""
759+
760+
def test_integration_and_ai_mutually_exclusive(self):
761+
from typer.testing import CliRunner
762+
from specify_cli import app
763+
764+
runner = CliRunner()
765+
result = runner.invoke(app, [
766+
"init", "test-project",
767+
"--ai", "claude",
768+
"--integration", "copilot",
769+
])
770+
assert result.exit_code != 0
771+
assert "mutually exclusive" in result.output
772+
773+
def test_unknown_integration_rejected(self):
774+
from typer.testing import CliRunner
775+
from specify_cli import app
776+
777+
runner = CliRunner()
778+
result = runner.invoke(app, [
779+
"init", "test-project",
780+
"--integration", "nonexistent",
781+
])
782+
assert result.exit_code != 0
783+
assert "Unknown integration" in result.output
784+
785+
def test_integration_copilot_creates_files(self, tmp_path):
786+
from typer.testing import CliRunner
787+
from specify_cli import app
788+
789+
runner = CliRunner()
790+
result = runner.invoke(app, [
791+
"init", "--here",
792+
"--integration", "copilot",
793+
"--script", "sh",
794+
"--no-git",
795+
], catch_exceptions=False, env={"HOME": str(tmp_path)})
796+
# The init command runs in CWD, which for CliRunner is OS cwd.
797+
# Use a different approach: create project in a named dir
798+
project = tmp_path / "int-test"
799+
project.mkdir()
800+
import os
801+
old_cwd = os.getcwd()
802+
try:
803+
os.chdir(project)
804+
result = runner.invoke(app, [
805+
"init", "--here",
806+
"--integration", "copilot",
807+
"--script", "sh",
808+
"--no-git",
809+
], catch_exceptions=False)
810+
finally:
811+
os.chdir(old_cwd)
812+
813+
assert result.exit_code == 0, f"init failed: {result.output}"
814+
815+
# Copilot-specific files
816+
assert (project / ".github" / "agents" / "speckit.plan.agent.md").exists()
817+
assert (project / ".github" / "prompts" / "speckit.plan.prompt.md").exists()
818+
819+
# Shared infrastructure
820+
assert (project / ".specify" / "scripts" / "bash" / "common.sh").exists()
821+
assert (project / ".specify" / "templates" / "spec-template.md").exists()
822+
823+
# agent.json
824+
agent_json = project / ".specify" / "agent.json"
825+
assert agent_json.exists()
826+
data = json.loads(agent_json.read_text(encoding="utf-8"))
827+
assert data["integration"] == "copilot"
828+
829+
# init-options.json
830+
init_opts = project / ".specify" / "init-options.json"
831+
assert init_opts.exists()
832+
opts = json.loads(init_opts.read_text(encoding="utf-8"))
833+
assert opts["integration"] == "copilot"
834+
assert opts["ai"] == "copilot"
835+
836+
# Manifest recorded
837+
manifest = project / ".specify" / "integrations" / "copilot.manifest.json"
838+
assert manifest.exists()
839+
840+
def test_ai_copilot_auto_promotes(self, tmp_path):
841+
"""--ai copilot should auto-promote to integration path with a nudge."""
842+
from typer.testing import CliRunner
843+
from specify_cli import app
844+
845+
project = tmp_path / "promote-test"
846+
project.mkdir()
847+
import os
848+
old_cwd = os.getcwd()
849+
try:
850+
os.chdir(project)
851+
runner = CliRunner()
852+
result = runner.invoke(app, [
853+
"init", "--here",
854+
"--ai", "copilot",
855+
"--script", "sh",
856+
"--no-git",
857+
], catch_exceptions=False)
858+
finally:
859+
os.chdir(old_cwd)
860+
861+
assert result.exit_code == 0, f"init failed: {result.output}"
862+
# Should show the migration nudge
863+
assert "--integration copilot" in result.output
864+
# Should still produce copilot files via integration path
865+
assert (project / ".github" / "agents" / "speckit.plan.agent.md").exists()

0 commit comments

Comments
 (0)