-
Notifications
You must be signed in to change notification settings - Fork 964
Expand file tree
/
Copy pathAspireEditorCommandProvider.ts
More file actions
159 lines (132 loc) · 6.55 KB
/
Copy pathAspireEditorCommandProvider.ts
File metadata and controls
159 lines (132 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import * as vscode from 'vscode';
import * as path from 'path';
import { noAppHostInWorkspace } from '../loc/strings';
import { getResourceDebuggerExtensions } from '../debugger/debuggerExtensions';
import { AspireCommandType } from '../dcp/types';
import { AppHostDiscoveryService, getDebugTargetForCandidate, selectWorkspaceAppHostPath } from '../utils/appHostDiscovery';
import type { CandidateAppHostDisplayInfo } from '../utils/appHostDiscovery';
import { extensionLogOutputChannel } from '../utils/logging';
export class AspireEditorCommandProvider implements vscode.Disposable {
private _disposables: vscode.Disposable[] = [];
constructor(private readonly _appHostDiscoveryService: AppHostDiscoveryService) {
this._disposables.push(vscode.workspace.onDidChangeWorkspaceFolders(event => {
void this.updateWorkspaceAppHostContext();
}));
this._disposables.push(this._appHostDiscoveryService.onDidChangeCandidates(workspaceFolder => {
void this.processActiveDocumentForWorkspace(workspaceFolder);
}));
this._disposables.push(vscode.window.onDidChangeActiveTextEditor(async (editor) => {
if (editor) {
await this.processDocument(editor.document);
}
}));
// Initialize context for the currently active document
this.initializeActiveDocument();
}
private async initializeActiveDocument(): Promise<void> {
const activeDocument = vscode.window.activeTextEditor?.document;
if (activeDocument) {
await this.processDocument(activeDocument);
}
}
private async processActiveDocumentForWorkspace(workspaceFolder: vscode.WorkspaceFolder): Promise<void> {
const activeDocument = vscode.window.activeTextEditor?.document;
if (!activeDocument) {
await this.updateWorkspaceAppHostContext();
return;
}
const activeWorkspaceFolder = vscode.workspace.getWorkspaceFolder(activeDocument.uri);
if (activeWorkspaceFolder?.uri.toString() === workspaceFolder.uri.toString()) {
await this.processDocument(activeDocument);
}
}
public async processDocument(document: vscode.TextDocument): Promise<void> {
const fileExtension = path.extname(document.uri.fsPath).toLowerCase();
const isSupportedFile = getResourceDebuggerExtensions().some(extension => extension.getSupportedFileTypes().includes(fileExtension));
vscode.commands.executeCommand('setContext', 'aspire.editorSupportsRunDebug', isSupportedFile);
vscode.commands.executeCommand('setContext', 'aspire.fileIsAppHost', await this.tryFindCandidateForEditorFile(document.uri.fsPath) !== undefined);
await this.updateWorkspaceAppHostContext();
}
private async updateWorkspaceAppHostContext(): Promise<void> {
const workspaceFolder = vscode.window.activeTextEditor
? vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)
: vscode.workspace.workspaceFolders?.[0];
if (!workspaceFolder) {
vscode.commands.executeCommand('setContext', 'aspire.workspaceHasAppHost', false);
return;
}
const appHostPath = await this.trySelectWorkspaceAppHostPath(workspaceFolder);
vscode.commands.executeCommand('setContext', 'aspire.workspaceHasAppHost', appHostPath !== undefined);
}
/**
* Returns the resolved AppHost path from the active editor or workspace settings, or null if none is available.
*/
public async getAppHostPath(): Promise<string | null> {
if (vscode.window.activeTextEditor) {
const candidate = await this.tryFindCandidateForEditorFile(vscode.window.activeTextEditor.document.uri.fsPath);
if (candidate) {
return getDebugTargetForCandidate(candidate);
}
}
const workspaceFolder = vscode.window.activeTextEditor
? vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)
: vscode.workspace.workspaceFolders?.[0];
if (!workspaceFolder) {
return null;
}
return await this.trySelectWorkspaceAppHostPath(workspaceFolder) ?? null;
}
private async tryFindCandidateForEditorFile(filePath: string): Promise<CandidateAppHostDisplayInfo | undefined> {
try {
return await this._appHostDiscoveryService.tryFindCandidateForEditorFile(filePath);
}
catch (error) {
extensionLogOutputChannel.warn(`Failed to discover AppHost for editor file ${filePath}: ${error}`);
return undefined;
}
}
private async trySelectWorkspaceAppHostPath(workspaceFolder: vscode.WorkspaceFolder): Promise<string | undefined> {
try {
const appHosts = await this._appHostDiscoveryService.discover(workspaceFolder);
return await selectWorkspaceAppHostPath(workspaceFolder, appHosts);
}
catch (error) {
extensionLogOutputChannel.warn(`Failed to discover AppHost candidates for workspace ${workspaceFolder.uri.fsPath}: ${error}`);
return undefined;
}
}
public async tryExecuteRunAppHost(noDebug: boolean): Promise<void> {
await this.launchAspireDebugSession('run', noDebug);
}
public async tryExecuteDeployAppHost(noDebug: boolean): Promise<void> {
await this.launchAspireDebugSession('deploy', noDebug);
}
public async tryExecutePublishAppHost(noDebug: boolean): Promise<void> {
await this.launchAspireDebugSession('publish', noDebug);
}
public async tryExecuteDoAppHost(noDebug: boolean, doStep?: string): Promise<void> {
await this.launchAspireDebugSession('do', noDebug, doStep);
}
private async launchAspireDebugSession(aspireCommand: AspireCommandType, noDebug: boolean, doStep?: string): Promise<void> {
const appHostToRun = await this.getAppHostPath();
if (!appHostToRun) {
vscode.window.showErrorMessage(noAppHostInWorkspace);
return;
}
const config: vscode.DebugConfiguration = {
type: 'aspire',
name: `Aspire ${aspireCommand}: ${vscode.workspace.asRelativePath(appHostToRun)}`,
request: 'launch',
program: appHostToRun,
command: aspireCommand,
noDebug: noDebug
};
if (doStep) {
config.step = doStep;
}
await vscode.debug.startDebugging(undefined, config);
}
dispose() {
this._disposables.forEach(disposable => disposable.dispose());
}
}