|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import * as cp from 'child_process'; |
| 3 | +import * as path from 'path'; |
| 4 | + |
| 5 | +// Output channel for logging |
| 6 | +let outputChannel: vscode.OutputChannel; |
| 7 | + |
| 8 | +export function activate(context: vscode.ExtensionContext) { |
| 9 | + outputChannel = vscode.window.createOutputChannel("Container Maker"); |
| 10 | + outputChannel.appendLine('Container Maker extension is now active!'); |
| 11 | + |
| 12 | + // Register commands |
| 13 | + context.subscriptions.push( |
| 14 | + vscode.commands.registerCommand('container-maker.checkStatus', checkStatus), |
| 15 | + vscode.commands.registerCommand('container-maker.up', workspaceUp), |
| 16 | + vscode.commands.registerCommand('container-maker.down', workspaceDown) |
| 17 | + ); |
| 18 | + |
| 19 | + // Register Tree Data Providers (Scaffolding) |
| 20 | + const envProvider = new EnvironmentProvider(); |
| 21 | + vscode.window.registerTreeDataProvider('cmEnvironments', envProvider); |
| 22 | + |
| 23 | + const svcProvider = new ServiceProvider(); |
| 24 | + vscode.window.registerTreeDataProvider('cmServices', svcProvider); |
| 25 | +} |
| 26 | + |
| 27 | +export function deactivate() { } |
| 28 | + |
| 29 | +// --- Commands --- |
| 30 | + |
| 31 | +async function checkStatus() { |
| 32 | + const result = await runCMCommand('env status'); |
| 33 | + if (result) { |
| 34 | + outputChannel.show(true); |
| 35 | + outputChannel.appendLine(result); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +async function workspaceUp() { |
| 40 | + vscode.window.withProgress({ |
| 41 | + location: vscode.ProgressLocation.Notification, |
| 42 | + title: "Starting Workspace...", |
| 43 | + cancellable: false |
| 44 | + }, async (progress) => { |
| 45 | + try { |
| 46 | + const result = await runCMCommand('up -d'); |
| 47 | + vscode.window.showInformationMessage('Workspace started successfully'); |
| 48 | + outputChannel.appendLine(result || ''); |
| 49 | + } catch (error) { |
| 50 | + vscode.window.showErrorMessage(`Failed to start workspace: ${error}`); |
| 51 | + } |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +async function workspaceDown() { |
| 56 | + const answer = await vscode.window.showWarningMessage( |
| 57 | + "Are you sure you want to stop the workspace?", |
| 58 | + "Yes", "No" |
| 59 | + ); |
| 60 | + |
| 61 | + if (answer === "Yes") { |
| 62 | + await runCMCommand('down'); |
| 63 | + vscode.window.showInformationMessage('Workspace stopped'); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// --- Helper Functions --- |
| 68 | + |
| 69 | +function getCMPath(): string { |
| 70 | + const config = vscode.workspace.getConfiguration('containerMaker'); |
| 71 | + return config.get('executablePath') || 'cm'; |
| 72 | +} |
| 73 | + |
| 74 | +function runCMCommand(args: string): Promise<string> { |
| 75 | + return new Promise((resolve, reject) => { |
| 76 | + const cmPath = getCMPath(); |
| 77 | + const rootPath = vscode.workspace.workspaceFolders?.[0].uri.fsPath; |
| 78 | + const cwd = rootPath || process.cwd(); |
| 79 | + |
| 80 | + const cmd = `"${cmPath}" ${args}`; |
| 81 | + outputChannel.appendLine(`> Executing: ${cmd} in ${cwd}`); |
| 82 | + |
| 83 | + cp.exec(cmd, { cwd }, (err, stdout, stderr) => { |
| 84 | + if (err) { |
| 85 | + outputChannel.appendLine(`Error: ${err.message}`); |
| 86 | + outputChannel.appendLine(`Stderr: ${stderr}`); |
| 87 | + reject(stderr || err.message); |
| 88 | + return; |
| 89 | + } |
| 90 | + resolve(stdout); |
| 91 | + }); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +// --- Tree View Scaffolding --- |
| 96 | + |
| 97 | +class EnvironmentProvider implements vscode.TreeDataProvider<vscode.TreeItem> { |
| 98 | + getTreeItem(element: vscode.TreeItem): vscode.TreeItem { |
| 99 | + return element; |
| 100 | + } |
| 101 | + |
| 102 | + getChildren(element?: vscode.TreeItem): Thenable<vscode.TreeItem[]> { |
| 103 | + // Mock data for scaffolding |
| 104 | + if (!element) { |
| 105 | + return Promise.resolve([ |
| 106 | + new vscode.TreeItem("Dev Environment", vscode.TreeItemCollapsibleState.None), |
| 107 | + new vscode.TreeItem("Staging Environment", vscode.TreeItemCollapsibleState.None) |
| 108 | + ]); |
| 109 | + } |
| 110 | + return Promise.resolve([]); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +class ServiceProvider implements vscode.TreeDataProvider<vscode.TreeItem> { |
| 115 | + getTreeItem(element: vscode.TreeItem): vscode.TreeItem { |
| 116 | + return element; |
| 117 | + } |
| 118 | + |
| 119 | + getChildren(element?: vscode.TreeItem): Thenable<vscode.TreeItem[]> { |
| 120 | + // Mock data for scaffolding |
| 121 | + if (!element) { |
| 122 | + return Promise.resolve([ |
| 123 | + new vscode.TreeItem("frontend (Node.js)", vscode.TreeItemCollapsibleState.None), |
| 124 | + new vscode.TreeItem("backend (Go)", vscode.TreeItemCollapsibleState.None), |
| 125 | + new vscode.TreeItem("database (Postgres)", vscode.TreeItemCollapsibleState.None) |
| 126 | + ]); |
| 127 | + } |
| 128 | + return Promise.resolve([]); |
| 129 | + } |
| 130 | +} |
0 commit comments