|
| 1 | +import * as path from 'path'; |
| 2 | +import * as vscode from 'vscode'; |
| 3 | +import { |
| 4 | + addIntegrationTestProjectRequiresCSharpAppHost, |
| 5 | + addIntegrationTestProjectUnsupported, |
| 6 | +} from '../loc/strings'; |
| 7 | +import { aspireTestAppHostCapability } from '../types/configInfo'; |
| 8 | +import { AspireTerminalProvider } from '../utils/AspireTerminalProvider'; |
| 9 | +import { ConfigInfoProvider } from '../utils/configInfoProvider'; |
| 10 | +import { |
| 11 | + CliPathResolutionTarget, |
| 12 | + windowCliPathTarget, |
| 13 | + workspaceFolderCliPathTarget, |
| 14 | +} from '../utils/cliPathVariables'; |
| 15 | +import { extensionLogOutputChannel } from '../utils/logging'; |
| 16 | + |
| 17 | +export const addIntegrationTestProjectSupportedContext = 'aspire.addIntegrationTestProjectSupported'; |
| 18 | + |
| 19 | +export class AddIntegrationTestProjectAvailability implements vscode.Disposable { |
| 20 | + private _refreshGeneration = 0; |
| 21 | + private _disposed = false; |
| 22 | + |
| 23 | + constructor(private readonly _configInfoProvider: ConfigInfoProvider) { |
| 24 | + } |
| 25 | + |
| 26 | + async refresh(forceRefresh = false): Promise<void> { |
| 27 | + const generation = ++this._refreshGeneration; |
| 28 | + await this._publish(false, generation); |
| 29 | + if (this._disposed || generation !== this._refreshGeneration) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + try { |
| 34 | + const supported = await this._configInfoProvider.hasCapability( |
| 35 | + aspireTestAppHostCapability, |
| 36 | + { |
| 37 | + target: getAvailabilityTarget(), |
| 38 | + forceRefresh, |
| 39 | + suppressErrors: true, |
| 40 | + }); |
| 41 | + await this._publish(supported, generation); |
| 42 | + } |
| 43 | + catch (error) { |
| 44 | + if (!this._disposed && generation === this._refreshGeneration) { |
| 45 | + extensionLogOutputChannel.warn(`Unable to determine integration test scaffolding availability: ${String(error)}`); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + dispose(): void { |
| 51 | + this._disposed = true; |
| 52 | + this._refreshGeneration++; |
| 53 | + void vscode.commands.executeCommand('setContext', addIntegrationTestProjectSupportedContext, false); |
| 54 | + } |
| 55 | + |
| 56 | + private async _publish(supported: boolean, generation: number): Promise<void> { |
| 57 | + if (!this._disposed && generation === this._refreshGeneration) { |
| 58 | + await vscode.commands.executeCommand( |
| 59 | + 'setContext', |
| 60 | + addIntegrationTestProjectSupportedContext, |
| 61 | + supported); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +export async function addIntegrationTestProject( |
| 67 | + terminalProvider: AspireTerminalProvider, |
| 68 | + configInfoProvider: ConfigInfoProvider, |
| 69 | + appHostPath: string, |
| 70 | + target: CliPathResolutionTarget, |
| 71 | + cliPath: string, |
| 72 | +): Promise<void> { |
| 73 | + if (path.extname(appHostPath).toLowerCase() !== '.csproj') { |
| 74 | + await vscode.window.showErrorMessage(addIntegrationTestProjectRequiresCSharpAppHost); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const supported = await configInfoProvider.hasCapability( |
| 79 | + aspireTestAppHostCapability, |
| 80 | + { |
| 81 | + cliPath, |
| 82 | + target, |
| 83 | + forceRefresh: true, |
| 84 | + suppressErrors: true, |
| 85 | + }); |
| 86 | + if (!supported) { |
| 87 | + await vscode.window.showErrorMessage(addIntegrationTestProjectUnsupported); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + await terminalProvider.sendAspireCommandToAspireTerminal( |
| 92 | + ['new', 'aspire-test'], |
| 93 | + true, |
| 94 | + ['--apphost', appHostPath], |
| 95 | + { cliPath, target }); |
| 96 | +} |
| 97 | + |
| 98 | +function getAvailabilityTarget(): CliPathResolutionTarget { |
| 99 | + const activeUri = vscode.window.activeTextEditor?.document.uri; |
| 100 | + const workspaceFolder = activeUri |
| 101 | + ? vscode.workspace.getWorkspaceFolder(activeUri) |
| 102 | + : vscode.workspace.workspaceFolders?.[0]; |
| 103 | + return workspaceFolder |
| 104 | + ? workspaceFolderCliPathTarget(workspaceFolder) |
| 105 | + : windowCliPathTarget; |
| 106 | +} |
0 commit comments