|
| 1 | +import * as path from 'path'; |
| 2 | +import * as vscode from 'vscode'; |
| 3 | +import type { AspireEditorCommandProvider } from '../editor/AspireEditorCommandProvider'; |
| 4 | +import { |
| 5 | + addIntegrationTestProjectUnavailable, |
| 6 | + addIntegrationTestProjectUnsupported, |
| 7 | + noAppHostInWorkspace, |
| 8 | +} from '../loc/strings'; |
| 9 | +import { aspireTestAppHostCapability, type CapabilityStatus } from '../types/configInfo'; |
| 10 | +import { AspireTerminalProvider, shellArg } from '../utils/AspireTerminalProvider'; |
| 11 | +import { ConfigInfoProvider } from '../utils/configInfoProvider'; |
| 12 | +import { extensionLogOutputChannel } from '../utils/logging'; |
| 13 | +import { getCliPathTargetForUri, type CliPathResolutionTarget } from '../utils/cliPathVariables'; |
| 14 | + |
| 15 | +export const addIntegrationTestProjectSupportedContext = 'aspire.addIntegrationTestProjectSupported'; |
| 16 | + |
| 17 | +const aspireTestAppHostFallback = { |
| 18 | + args: ['new', 'aspire-test', '--help'], |
| 19 | + option: '--apphost', |
| 20 | +} as const; |
| 21 | + |
| 22 | +interface AspireTestAppHostCapabilityOptions { |
| 23 | + readonly cancellationToken?: vscode.CancellationToken; |
| 24 | + readonly forceRefresh?: boolean; |
| 25 | +} |
| 26 | + |
| 27 | +export type AddIntegrationTestProjectRefreshSubscription = |
| 28 | + (listener: () => void) => vscode.Disposable; |
| 29 | + |
| 30 | +export async function getAspireTestAppHostCapabilityStatus( |
| 31 | + configInfoProvider: ConfigInfoProvider, |
| 32 | + cliPath: string, |
| 33 | + target: CliPathResolutionTarget, |
| 34 | + options?: AspireTestAppHostCapabilityOptions, |
| 35 | +): Promise<CapabilityStatus> { |
| 36 | + return await configInfoProvider.getCapabilityStatus( |
| 37 | + aspireTestAppHostCapability, |
| 38 | + { |
| 39 | + cliPath, |
| 40 | + target, |
| 41 | + cancellationToken: options?.cancellationToken, |
| 42 | + fallbackCliCommandOption: aspireTestAppHostFallback, |
| 43 | + forceRefresh: options?.forceRefresh, |
| 44 | + suppressErrors: true, |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +export class AddIntegrationTestProjectAvailability implements vscode.Disposable { |
| 49 | + private _refreshGeneration = 0; |
| 50 | + private _refreshCancellation: vscode.CancellationTokenSource | undefined; |
| 51 | + private _disposed = false; |
| 52 | + private readonly _refreshSubscription: vscode.Disposable; |
| 53 | + |
| 54 | + constructor( |
| 55 | + private readonly _editorCommandProvider: AspireEditorCommandProvider, |
| 56 | + private readonly _terminalProvider: AspireTerminalProvider, |
| 57 | + private readonly _configInfoProvider: ConfigInfoProvider, |
| 58 | + subscribeToRefresh: AddIntegrationTestProjectRefreshSubscription, |
| 59 | + ) { |
| 60 | + this._refreshSubscription = subscribeToRefresh(() => void this.refresh()); |
| 61 | + } |
| 62 | + |
| 63 | + async refresh(): Promise<void> { |
| 64 | + const generation = ++this._refreshGeneration; |
| 65 | + this._refreshCancellation?.cancel(); |
| 66 | + this._refreshCancellation?.dispose(); |
| 67 | + const cancellation = new vscode.CancellationTokenSource(); |
| 68 | + this._refreshCancellation = cancellation; |
| 69 | + |
| 70 | + await this._publish(false, generation); |
| 71 | + if (this._isStale(generation)) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + const appHostPath = await this._editorCommandProvider.getAppHostPath(); |
| 77 | + if (!appHostPath |
| 78 | + || path.extname(appHostPath).toLowerCase() !== '.csproj' |
| 79 | + || this._isStale(generation)) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const target = getCliPathTargetForUri(vscode.Uri.file(appHostPath)); |
| 84 | + const cliPath = await this._terminalProvider.getAspireCliExecutablePath(target); |
| 85 | + if (this._isStale(generation)) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + const capabilityStatus = await getAspireTestAppHostCapabilityStatus( |
| 90 | + this._configInfoProvider, |
| 91 | + cliPath, |
| 92 | + target, |
| 93 | + { cancellationToken: cancellation.token }); |
| 94 | + await this._publish(capabilityStatus === 'supported', generation); |
| 95 | + } |
| 96 | + catch (error) { |
| 97 | + if (!cancellation.token.isCancellationRequested && !this._isStale(generation)) { |
| 98 | + extensionLogOutputChannel.warn(`Unable to determine integration test scaffolding availability: ${String(error)}`); |
| 99 | + } |
| 100 | + } |
| 101 | + finally { |
| 102 | + if (this._refreshCancellation === cancellation) { |
| 103 | + this._refreshCancellation = undefined; |
| 104 | + } |
| 105 | + cancellation.dispose(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + dispose(): void { |
| 110 | + this._disposed = true; |
| 111 | + this._refreshGeneration++; |
| 112 | + this._refreshCancellation?.cancel(); |
| 113 | + this._refreshCancellation?.dispose(); |
| 114 | + this._refreshCancellation = undefined; |
| 115 | + this._refreshSubscription.dispose(); |
| 116 | + void vscode.commands.executeCommand('setContext', addIntegrationTestProjectSupportedContext, false); |
| 117 | + } |
| 118 | + |
| 119 | + private _isStale(generation: number): boolean { |
| 120 | + return this._disposed || generation !== this._refreshGeneration; |
| 121 | + } |
| 122 | + |
| 123 | + private async _publish(supported: boolean, generation: number): Promise<void> { |
| 124 | + if (this._isStale(generation)) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + await vscode.commands.executeCommand( |
| 129 | + 'setContext', |
| 130 | + addIntegrationTestProjectSupportedContext, |
| 131 | + supported); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +export async function addIntegrationTestProject( |
| 136 | + editorCommandProvider: AspireEditorCommandProvider, |
| 137 | + terminalProvider: AspireTerminalProvider, |
| 138 | + configInfoProvider: ConfigInfoProvider, |
| 139 | +): Promise<void> { |
| 140 | + const appHostPath = await editorCommandProvider.getAppHostPath(); |
| 141 | + if (!appHostPath) { |
| 142 | + await vscode.window.showErrorMessage(noAppHostInWorkspace); |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + const target = getCliPathTargetForUri(vscode.Uri.file(appHostPath)); |
| 147 | + let cliPath: string; |
| 148 | + try { |
| 149 | + cliPath = await terminalProvider.getAspireCliExecutablePath(target); |
| 150 | + } catch (error) { |
| 151 | + extensionLogOutputChannel.warn(`Unable to resolve the Aspire CLI for integration test scaffolding: ${String(error)}`); |
| 152 | + await vscode.window.showErrorMessage(addIntegrationTestProjectUnavailable); |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + const capabilityStatus = await getAspireTestAppHostCapabilityStatus( |
| 157 | + configInfoProvider, |
| 158 | + cliPath, |
| 159 | + target, |
| 160 | + { forceRefresh: true }); |
| 161 | + if (capabilityStatus === 'unsupported') { |
| 162 | + await vscode.window.showErrorMessage(addIntegrationTestProjectUnsupported); |
| 163 | + return; |
| 164 | + } |
| 165 | + if (capabilityStatus === 'unavailable') { |
| 166 | + await vscode.window.showErrorMessage(addIntegrationTestProjectUnavailable); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + await terminalProvider.sendAspireCommandToAspireTerminal( |
| 171 | + ['new', 'aspire-test', '--apphost', shellArg(appHostPath)], |
| 172 | + true, |
| 173 | + undefined, |
| 174 | + { cliPath, target }); |
| 175 | +} |
0 commit comments