|
| 1 | +/** |
| 2 | + * Codev: Run Worktree Setup — execute `worktree.postSpawn` against an |
| 3 | + * existing builder's worktree, without recreating it. |
| 4 | + * |
| 5 | + * Use cases: lockfile changed and dependencies need reinstalling; a new |
| 6 | + * step was added to `worktree.postSpawn` after the builder spawned; |
| 7 | + * setup aborted mid-spawn and the worktree needs recovery; running |
| 8 | + * setup for the first time on a builder that predates the config. |
| 9 | + * |
| 10 | + * Opens a fresh VSCode terminal in the worktree and chains the configured |
| 11 | + * commands so output streams live (preferable to buffered execution for |
| 12 | + * long-running installs). |
| 13 | + */ |
| 14 | + |
| 15 | +import * as vscode from 'vscode'; |
| 16 | +import { readFile } from 'node:fs/promises'; |
| 17 | +import * as path from 'node:path'; |
| 18 | +import type { ConnectionManager } from '../connection-manager.js'; |
| 19 | + |
| 20 | +export async function runWorktreeSetup( |
| 21 | + connectionManager: ConnectionManager, |
| 22 | + builderIdArg: string | undefined, |
| 23 | +): Promise<void> { |
| 24 | + const client = connectionManager.getClient(); |
| 25 | + const workspacePath = connectionManager.getWorkspacePath(); |
| 26 | + if (!client || !workspacePath || connectionManager.getState() !== 'connected') { |
| 27 | + vscode.window.showErrorMessage('Codev: Not connected to Tower'); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + const overview = await client.getOverview(workspacePath); |
| 32 | + const builders = overview?.builders ?? []; |
| 33 | + if (builders.length === 0) { |
| 34 | + vscode.window.showInformationMessage('Codev: No builders available'); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + const builder = builderIdArg |
| 39 | + ? builders.find(b => b.id === builderIdArg) |
| 40 | + : await pickBuilder(builders); |
| 41 | + if (!builder) { |
| 42 | + if (builderIdArg) { |
| 43 | + vscode.window.showErrorMessage(`Codev: No builder found for "${builderIdArg}"`); |
| 44 | + } |
| 45 | + return; |
| 46 | + } |
| 47 | + if (!builder.worktreePath) { |
| 48 | + vscode.window.showErrorMessage(`Codev: Builder ${builder.id} has no worktree on record`); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const postSpawn = await readPostSpawn(workspacePath); |
| 53 | + if (postSpawn.length === 0) { |
| 54 | + vscode.window.showInformationMessage( |
| 55 | + 'Codev: No worktree.postSpawn configured in .codev/config.json. Nothing to do.', |
| 56 | + ); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // Open a fresh VSCode integrated terminal scoped to the worktree. |
| 61 | + // sendText streams each command live — for long-running installs the |
| 62 | + // reviewer sees pnpm progress, uv resolver output, etc. in real time. |
| 63 | + const terminal = vscode.window.createTerminal({ |
| 64 | + name: `Codev: Setup ${builder.id}`, |
| 65 | + cwd: builder.worktreePath, |
| 66 | + }); |
| 67 | + terminal.show(); |
| 68 | + for (const cmd of postSpawn) { |
| 69 | + terminal.sendText(cmd); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +async function readPostSpawn(workspacePath: string): Promise<string[]> { |
| 74 | + const configPath = path.join(workspacePath, '.codev', 'config.json'); |
| 75 | + try { |
| 76 | + const raw = await readFile(configPath, 'utf-8'); |
| 77 | + const parsed = JSON.parse(raw) as { worktree?: { postSpawn?: unknown } }; |
| 78 | + const list = parsed.worktree?.postSpawn; |
| 79 | + if (!Array.isArray(list)) { return []; } |
| 80 | + return list.filter((x): x is string => typeof x === 'string' && x.length > 0); |
| 81 | + } catch { |
| 82 | + return []; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +interface BuilderLike { |
| 87 | + id: string; |
| 88 | + issueId: string | null; |
| 89 | + issueTitle: string | null; |
| 90 | +} |
| 91 | + |
| 92 | +async function pickBuilder<T extends BuilderLike>(builders: T[]): Promise<T | undefined> { |
| 93 | + const picked = await vscode.window.showQuickPick( |
| 94 | + builders.map(b => ({ |
| 95 | + label: `#${b.issueId ?? b.id} ${b.issueTitle ?? ''}`, |
| 96 | + builder: b, |
| 97 | + })), |
| 98 | + { placeHolder: 'Select builder whose worktree to re-setup' }, |
| 99 | + ); |
| 100 | + return picked?.builder; |
| 101 | +} |
0 commit comments