|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import { getDevProxyExe } from './detect'; |
| 3 | +import { VersionPreference } from './enums'; |
| 4 | + |
| 5 | +interface DevProxyTaskDefinition extends vscode.TaskDefinition { |
| 6 | + type: 'devproxy'; |
| 7 | + command: 'start' | 'stop'; |
| 8 | + configFile?: string; |
| 9 | + args?: string[]; |
| 10 | + label?: string; |
| 11 | +} |
| 12 | + |
| 13 | +export class DevProxyTaskProvider implements vscode.TaskProvider { |
| 14 | + static DevProxyType = 'devproxy'; |
| 15 | + private devProxyExe: string; |
| 16 | + |
| 17 | + constructor(private context: vscode.ExtensionContext) { |
| 18 | + const configuration = vscode.workspace.getConfiguration('dev-proxy-toolkit'); |
| 19 | + const versionPreference = configuration.get('version') as VersionPreference; |
| 20 | + this.devProxyExe = getDevProxyExe(versionPreference); |
| 21 | + } |
| 22 | + |
| 23 | + provideTasks(): Thenable<vscode.Task[]> | undefined { |
| 24 | + return this.getDevProxyTasks(); |
| 25 | + } |
| 26 | + |
| 27 | + resolveTask(task: vscode.Task): vscode.Task | undefined { |
| 28 | + const definition = task.definition as DevProxyTaskDefinition; |
| 29 | + |
| 30 | + if (definition.type !== DevProxyTaskProvider.DevProxyType) { |
| 31 | + return undefined; |
| 32 | + } |
| 33 | + |
| 34 | + return this.createTaskFromDefinition(definition); |
| 35 | + } |
| 36 | + |
| 37 | + private async getDevProxyTasks(): Promise<vscode.Task[]> { |
| 38 | + const tasks: vscode.Task[] = []; |
| 39 | + |
| 40 | + tasks.push(this.createStartTask()); |
| 41 | + tasks.push(this.createStopTask()); |
| 42 | + |
| 43 | + return tasks; |
| 44 | + } |
| 45 | + |
| 46 | + private createStartTask(): vscode.Task { |
| 47 | + const definition: DevProxyTaskDefinition = { |
| 48 | + type: 'devproxy', |
| 49 | + command: 'start', |
| 50 | + label: 'Start Dev Proxy' |
| 51 | + }; |
| 52 | + |
| 53 | + return this.createTaskFromDefinition(definition); |
| 54 | + } |
| 55 | + |
| 56 | + private createStopTask(): vscode.Task { |
| 57 | + const definition: DevProxyTaskDefinition = { |
| 58 | + type: 'devproxy', |
| 59 | + command: 'stop', |
| 60 | + label: 'Stop Dev Proxy' |
| 61 | + }; |
| 62 | + |
| 63 | + return this.createTaskFromDefinition(definition); |
| 64 | + } |
| 65 | + |
| 66 | + private createTaskFromDefinition(definition: DevProxyTaskDefinition): vscode.Task { |
| 67 | + let execution: vscode.ShellExecution; |
| 68 | + |
| 69 | + if (definition.command === 'start') { |
| 70 | + const args = this.buildArgumentsFromDefinition(definition); |
| 71 | + execution = new vscode.ShellExecution(this.devProxyExe, args, { |
| 72 | + cwd: '${workspaceFolder}' |
| 73 | + }); |
| 74 | + } else if (definition.command === 'stop') { |
| 75 | + // Use curl to stop Dev Proxy via API |
| 76 | + const configuration = vscode.workspace.getConfiguration('dev-proxy-toolkit'); |
| 77 | + const apiPort = configuration.get('apiPort', 8897); |
| 78 | + execution = new vscode.ShellExecution('curl', [ |
| 79 | + '-X', 'POST', |
| 80 | + `http://localhost:${apiPort}/proxy/stopproxy` |
| 81 | + ]); |
| 82 | + } else { |
| 83 | + throw new Error(`Unsupported command: ${definition.command}`); |
| 84 | + } |
| 85 | + |
| 86 | + const task = new vscode.Task( |
| 87 | + definition, |
| 88 | + vscode.TaskScope.Workspace, |
| 89 | + definition.label || `Dev Proxy: ${definition.command}`, |
| 90 | + DevProxyTaskProvider.DevProxyType, |
| 91 | + execution, |
| 92 | + definition.command === 'start' ? ['$devproxy-watch'] : [] |
| 93 | + ); |
| 94 | + |
| 95 | + // Configure task properties based on command |
| 96 | + if (definition.command === 'start') { |
| 97 | + task.group = vscode.TaskGroup.Build; |
| 98 | + task.isBackground = true; |
| 99 | + task.presentationOptions = { |
| 100 | + echo: true, |
| 101 | + reveal: vscode.TaskRevealKind.Always, |
| 102 | + focus: false, |
| 103 | + panel: vscode.TaskPanelKind.Dedicated, |
| 104 | + showReuseMessage: true, |
| 105 | + clear: false |
| 106 | + }; |
| 107 | + } else if (definition.command === 'stop') { |
| 108 | + task.group = vscode.TaskGroup.Build; |
| 109 | + task.presentationOptions = { |
| 110 | + echo: false, |
| 111 | + reveal: vscode.TaskRevealKind.Silent, |
| 112 | + focus: false |
| 113 | + }; |
| 114 | + } |
| 115 | + |
| 116 | + return task; |
| 117 | + } |
| 118 | + |
| 119 | + private buildArgumentsFromDefinition(definition: DevProxyTaskDefinition): string[] { |
| 120 | + const args: string[] = []; |
| 121 | + |
| 122 | + // Handle specific properties |
| 123 | + if (definition.configFile) { |
| 124 | + args.push('--config-file', definition.configFile); |
| 125 | + } |
| 126 | + |
| 127 | + // Add any additional args |
| 128 | + if (definition.args) { |
| 129 | + args.push(...definition.args); |
| 130 | + } |
| 131 | + |
| 132 | + return args; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +export const registerTaskProvider = (context: vscode.ExtensionContext) => { |
| 137 | + const provider = new DevProxyTaskProvider(context); |
| 138 | + context.subscriptions.push( |
| 139 | + vscode.tasks.registerTaskProvider(DevProxyTaskProvider.DevProxyType, provider) |
| 140 | + ); |
| 141 | +}; |
0 commit comments