-
Notifications
You must be signed in to change notification settings - Fork 280
/
chromiumDebugConfigurationProvider.ts
220 lines (189 loc) · 7.05 KB
/
chromiumDebugConfigurationProvider.ts
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import { inject, injectable } from 'inversify';
import { tmpdir } from 'os';
import { basename, join } from 'path';
import * as vscode from 'vscode';
import * as nls from 'vscode-nls';
import { DebugType } from '../../common/contributionUtils';
import { existsWithoutDeref } from '../../common/fsUtils';
import {
AnyChromiumConfiguration,
AnyChromiumLaunchConfiguration,
IChromiumAttachConfiguration,
IChromiumLaunchConfiguration,
INodeLaunchConfiguration,
ResolvingConfiguration,
} from '../../configuration';
import { ExtensionContext, ExtensionLocation, FS, FsPromises } from '../../ioc-extras';
import { BaseConfigurationProvider } from './baseConfigurationProvider';
import { BaseConfigurationResolver } from './baseConfigurationResolver';
import { NodeConfigurationResolver } from './nodeDebugConfigurationResolver';
import { TerminalDebugConfigurationResolver } from './terminalDebugConfigurationResolver';
const localize = nls.loadMessageBundle();
const isLaunch = (
value: ResolvingConfiguration<unknown>,
): value is ResolvingConfiguration<IChromiumLaunchConfiguration> => value.request === 'launch';
const isAttach = (
value: ResolvingConfiguration<unknown>,
): value is ResolvingConfiguration<IChromiumAttachConfiguration> => value.request === 'attach';
/**
* Configuration provider for Chrome debugging.
*/
@injectable()
export abstract class ChromiumDebugConfigurationResolver<T extends AnyChromiumConfiguration>
extends BaseConfigurationResolver<T>
implements vscode.DebugConfigurationProvider
{
constructor(
@inject(ExtensionContext) context: vscode.ExtensionContext,
@inject(NodeConfigurationResolver)
private readonly nodeProvider: NodeConfigurationResolver,
@inject(TerminalDebugConfigurationResolver)
private readonly terminalProvider: TerminalDebugConfigurationResolver,
@inject(ExtensionLocation) private readonly location: ExtensionLocation,
@inject(FS) private readonly fs: FsPromises,
) {
super(context);
}
protected async resolveBrowserCommon(
folder: vscode.WorkspaceFolder | undefined,
config: ResolvingConfiguration<T>,
) {
if (config.request === 'attach') {
// todo https://github.com/microsoft/vscode-chrome-debug/blob/ee5ae7ac7734f369dba58ba57bb910aac467c97a/src/extension.ts#L48
}
if (config.server && 'program' in config.server) {
const serverOpts = {
...config.server,
type: DebugType.Node,
request: 'launch',
name: `${config.name}: Server`,
};
config.server = (await this.nodeProvider.resolveDebugConfiguration(
folder,
serverOpts,
)) as INodeLaunchConfiguration;
} else if (config.server && 'command' in config.server) {
config.server = await this.terminalProvider.resolveDebugConfiguration(folder, {
...config.server,
type: DebugType.Terminal,
request: 'launch',
name: `${config.name}: Server`,
});
}
const browserLocation = this.location === 'remote' ? 'ui' : 'workspace';
if (isLaunch(config) && !config.browserLaunchLocation) {
config.browserLaunchLocation = browserLocation;
}
if (isAttach(config) && !config.browserAttachLocation) {
config.browserAttachLocation = browserLocation;
}
if (config.request === 'launch') {
const cast = config as ResolvingConfiguration<AnyChromiumLaunchConfiguration>;
this.applyDefaultRuntimeExecutable(cast);
}
}
/**
* @override
*/
protected getSuggestedWorkspaceFolders(config: AnyChromiumConfiguration) {
return [config.rootPath, config.webRoot];
}
/**
* @inheritdoc
*/
public async resolveDebugConfigurationWithSubstitutedVariables?(
_folder: vscode.WorkspaceFolder | undefined,
debugConfiguration: vscode.DebugConfiguration,
): Promise<vscode.DebugConfiguration | undefined> {
if ('__pendingTargetId' in debugConfiguration) {
return debugConfiguration as T;
}
let config = debugConfiguration as T;
if (config.request === 'launch') {
const resolvedDataDir = await this.ensureNoLockfile(config);
if (resolvedDataDir === undefined) {
return;
}
config = resolvedDataDir;
}
return config;
}
protected async ensureNoLockfile(config: T): Promise<T | undefined> {
if (config.request !== 'launch') {
return config;
}
const cast = config as ResolvingConfiguration<AnyChromiumLaunchConfiguration>;
// for no user data dirs, with have nothing to look at
if (cast.userDataDir === false) {
return config;
}
// if there's a port configured and something's there, we can connect to it regardless
if (cast.port) {
return config;
}
const userDataDir =
typeof cast.userDataDir === 'string'
? cast.userDataDir
: join(
this.extensionContext.storagePath ?? tmpdir(),
cast.runtimeArgs?.includes('--headless') ? '.headless-profile' : '.profile',
);
const lockfile =
process.platform === 'win32'
? join(userDataDir, 'lockfile')
: join(userDataDir, 'SingletonLock');
if (await existsWithoutDeref(this.fs, lockfile)) {
const debugAnyway = localize('existingBrowser.debugAnyway', 'Debug Anyway');
const result = await vscode.window.showErrorMessage(
localize(
'existingBrowser.alert',
'It looks like a browser is already running from {0}. Please close it before trying to debug, otherwise VS Code may not be able to connect to it.',
cast.userDataDir === true
? localize('existingBrowser.location.default', 'an old debug session')
: localize('existingBrowser.location.userDataDir', 'the configured userDataDir'),
),
debugAnyway,
localize('cancel', 'Cancel'),
);
if (result !== debugAnyway) {
return undefined;
}
}
return { ...config, userDataDir };
}
}
@injectable()
export abstract class ChromiumDebugConfigurationProvider<
T extends AnyChromiumLaunchConfiguration,
> extends BaseConfigurationProvider<T> {
protected provide() {
return this.createLaunchConfigFromContext() || this.getDefaultLaunch();
}
protected getTriggerKind() {
return vscode.DebugConfigurationProviderTriggerKind.Initial;
}
public createLaunchConfigFromContext() {
const editor = vscode.window.activeTextEditor;
if (editor && editor.document.languageId === 'html') {
return {
type: this.getType(),
request: 'launch',
name: `Open ${basename(editor.document.uri.fsPath)}`,
file: editor.document.uri.fsPath,
} as ResolvingConfiguration<T>;
}
return undefined;
}
protected getDefaultLaunch() {
return {
type: this.getType(),
request: 'launch',
name: localize('chrome.launch.name', 'Launch Chrome against localhost'),
url: 'http://localhost:8080',
webRoot: '${workspaceFolder}',
} as ResolvingConfiguration<T>;
}
}