-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathterminalManager.ts
More file actions
454 lines (408 loc) · 19 KB
/
terminalManager.ts
File metadata and controls
454 lines (408 loc) · 19 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import * as fsapi from 'fs-extra';
import * as path from 'path';
import { Disposable, EventEmitter, ProgressLocation, Terminal, TerminalOptions, Uri } from 'vscode';
import { PythonEnvironment, PythonEnvironmentApi, PythonProject, PythonTerminalCreateOptions } from '../../api';
import { ActivationStrings } from '../../common/localize';
import { traceInfo, traceVerbose } from '../../common/logging';
import {
createTerminal,
onDidChangeWindowState,
onDidCloseTerminal,
onDidOpenTerminal,
terminals,
withProgress,
} from '../../common/window.apis';
import { getConfiguration, onDidChangeConfiguration } from '../../common/workspace.apis';
import { isActivatableEnvironment } from '../common/activation';
import { identifyTerminalShell } from '../common/shellDetector';
import { getPythonApi } from '../pythonApi';
import {
getShellIntegrationEnabledCache,
isWsl,
shellIntegrationForActiveTerminal,
shouldUseProfileActivation,
} from './shells/common/shellUtils';
import { ShellEnvsProvider, ShellSetupState, ShellStartupScriptProvider } from './shells/startupProvider';
import { handleSettingUpShellProfile } from './shellStartupSetupHandlers';
import {
DidChangeTerminalActivationStateEvent,
TerminalActivation,
TerminalActivationInternal,
TerminalEnvironment,
} from './terminalActivationState';
import {
ACT_TYPE_COMMAND,
ACT_TYPE_OFF,
ACT_TYPE_SHELL,
AutoActivationType,
getAutoActivationType,
getEnvironmentForTerminal,
waitForShellIntegration,
} from './utils';
export interface TerminalCreation {
create(environment: PythonEnvironment, options: PythonTerminalCreateOptions): Promise<Terminal>;
}
export interface TerminalGetters {
getProjectTerminal(
project: Uri | PythonProject,
environment: PythonEnvironment,
createNew?: boolean,
): Promise<Terminal>;
getDedicatedTerminal(
terminalKey: Uri | string,
project: Uri | PythonProject,
environment: PythonEnvironment,
createNew?: boolean,
): Promise<Terminal>;
}
export interface TerminalInit {
initialize(api: PythonEnvironmentApi): Promise<void>;
}
export interface TerminalManager
extends TerminalEnvironment,
TerminalInit,
TerminalActivation,
TerminalCreation,
TerminalGetters,
Disposable {}
export class TerminalManagerImpl implements TerminalManager {
private disposables: Disposable[] = [];
private skipActivationOnOpen = new Set<Terminal>();
private shellSetup: Map<string, boolean> = new Map<string, boolean>();
private onTerminalOpenedEmitter = new EventEmitter<Terminal>();
private onTerminalOpened = this.onTerminalOpenedEmitter.event;
private onTerminalClosedEmitter = new EventEmitter<Terminal>();
private onTerminalClosed = this.onTerminalClosedEmitter.event;
private onDidChangeTerminalActivationStateEmitter = new EventEmitter<DidChangeTerminalActivationStateEvent>();
public onDidChangeTerminalActivationState = this.onDidChangeTerminalActivationStateEmitter.event;
private hasFocus = true;
constructor(
private readonly ta: TerminalActivationInternal,
private readonly startupEnvProviders: ShellEnvsProvider[],
private readonly startupScriptProviders: ShellStartupScriptProvider[],
) {
this.disposables.push(
this.onTerminalOpenedEmitter,
this.onTerminalClosedEmitter,
this.onDidChangeTerminalActivationStateEmitter,
onDidOpenTerminal((t: Terminal) => {
this.onTerminalOpenedEmitter.fire(t);
}),
onDidCloseTerminal((t: Terminal) => {
this.onTerminalClosedEmitter.fire(t);
}),
this.onTerminalOpened(async (t) => {
if (this.skipActivationOnOpen.has(t) || (t.creationOptions as TerminalOptions)?.hideFromUser) {
return;
}
let env = this.ta.getEnvironment(t);
if (!env) {
const api = await getPythonApi();
env = await getEnvironmentForTerminal(api, t);
}
if (env) {
await this.autoActivateOnTerminalOpen(t, env);
}
}),
this.onTerminalClosed((t) => {
this.skipActivationOnOpen.delete(t);
}),
this.ta.onDidChangeTerminalActivationState((e) => {
this.onDidChangeTerminalActivationStateEmitter.fire(e);
}),
onDidChangeConfiguration(async (e) => {
if (e.affectsConfiguration('python-envs.terminal.autoActivationType')) {
const actType = getAutoActivationType();
if (actType === ACT_TYPE_SHELL) {
traceInfo(`Auto activation type changed to ${actType}`);
const shells = new Set(
terminals()
.map((t) => identifyTerminalShell(t))
.filter((t) => t !== 'unknown'),
);
if (shells.size > 0) {
await this.handleSetupCheck(shells);
}
} else {
traceVerbose(
`Auto activation type changed to ${actType}, we are cleaning up shell startup setup`,
);
// Teardown scripts when switching away from shell startup activation
await Promise.all(this.startupScriptProviders.map((p) => p.teardownScripts()));
this.shellSetup.clear();
}
}
if (e.affectsConfiguration('terminal.integrated.shellIntegration.enabled')) {
traceInfo('Shell integration setting changed, invalidating cache');
const updatedShellIntegrationSetting = await getShellIntegrationEnabledCache();
if (!updatedShellIntegrationSetting) {
const shells = new Set(
terminals()
.map((t) => identifyTerminalShell(t))
.filter((t) => t !== 'unknown'),
);
if (shells.size > 0) {
await this.handleSetupCheck(shells);
}
}
}
}),
onDidChangeWindowState((e) => {
this.hasFocus = e.focused;
}),
);
}
private async handleSetupCheck(shellType: string | Set<string>): Promise<void> {
const shellTypes = typeof shellType === 'string' ? new Set([shellType]) : shellType;
const providers = this.startupScriptProviders.filter((p) => shellTypes.has(p.shellType));
if (providers.length > 0) {
const shellsToSetup: ShellStartupScriptProvider[] = [];
await Promise.all(
providers.map(async (p) => {
const state = await p.isSetup();
const shellIntegrationEnabledSetting = await getShellIntegrationEnabledCache();
const shellIntegrationActiveTerminal = await shellIntegrationForActiveTerminal(p.name);
const shellIntegrationLikelyAvailable =
shellIntegrationEnabledSetting || shellIntegrationActiveTerminal;
traceVerbose(`Checking shell profile for ${p.shellType}, with state: ${state}`);
if (state === ShellSetupState.NotSetup) {
traceVerbose(
`WSL detected: ${isWsl()}, Shell integration available from setting, or active terminal: ${shellIntegrationEnabledSetting}, or ${await shellIntegrationForActiveTerminal(
p.name,
)}`,
);
if (shellIntegrationLikelyAvailable && !shouldUseProfileActivation(p.shellType)) {
// Shell integration available and NOT in WSL - skip setup
await p.teardownScripts();
this.shellSetup.set(p.shellType, true);
traceVerbose(
`Shell integration available for ${p.shellType} (not WSL), skipping prompt, and profile modification.`,
);
} else {
// WSL (regardless of integration) OR no shell integration - needs setup
this.shellSetup.set(p.shellType, false);
shellsToSetup.push(p);
traceVerbose(
`Shell integration is NOT available. Shell profile for ${p.shellType} is not setup.`,
);
}
} else if (state === ShellSetupState.Setup) {
if (shellIntegrationLikelyAvailable && !shouldUseProfileActivation(p.shellType)) {
await p.teardownScripts();
traceVerbose(
`Shell integration available for ${p.shellType}, removed profile script in favor of shell integration.`,
);
}
this.shellSetup.set(p.shellType, true);
traceVerbose(`Shell profile for ${p.shellType} is setup.`);
} else if (state === ShellSetupState.NotInstalled) {
this.shellSetup.set(p.shellType, false);
traceVerbose(`Shell profile for ${p.shellType} is not installed.`);
}
}),
);
if (shellsToSetup.length === 0) {
traceVerbose(`No shell profiles to setup for ${Array.from(shellTypes).join(', ')}`);
return;
}
if (!this.hasFocus) {
traceVerbose('Window does not have focus, skipping shell profile setup');
return;
}
setImmediate(async () => {
// Avoid blocking this setup on user interaction.
await handleSettingUpShellProfile(shellsToSetup, (p, v) => this.shellSetup.set(p.shellType, v));
});
}
}
private getShellActivationType(shellType: string): AutoActivationType | undefined {
let isSetup = this.shellSetup.get(shellType);
if (isSetup === true) {
traceVerbose(`Shell profile for ${shellType} is already setup.`);
return ACT_TYPE_SHELL;
} else if (isSetup === false) {
traceVerbose(`Shell profile for ${shellType} is not set up, using command fallback.`);
return ACT_TYPE_COMMAND;
}
}
private async getEffectiveActivationType(shellType: string): Promise<AutoActivationType> {
const providers = this.startupScriptProviders.filter((p) => p.shellType === shellType);
if (providers.length > 0) {
traceVerbose(`Shell startup is supported for ${shellType}, using shell startup activation`);
let isSetup = this.getShellActivationType(shellType);
if (isSetup !== undefined) {
return isSetup;
}
await this.handleSetupCheck(shellType);
// Check again after the setup check.
return this.getShellActivationType(shellType) ?? ACT_TYPE_COMMAND;
}
traceInfo(`Shell startup not supported for ${shellType}, using command activation as fallback`);
return ACT_TYPE_COMMAND;
}
private async autoActivateOnTerminalOpen(terminal: Terminal, environment: PythonEnvironment): Promise<void> {
let actType = getAutoActivationType();
const shellType = identifyTerminalShell(terminal);
if (actType === ACT_TYPE_SHELL) {
await this.handleSetupCheck(shellType);
actType = await this.getEffectiveActivationType(shellType);
}
if (actType === ACT_TYPE_COMMAND) {
if (isActivatableEnvironment(environment)) {
await withProgress(
{
location: ProgressLocation.Window,
title: `${ActivationStrings.activatingEnvironment}: ${environment.environmentPath.fsPath}`,
},
async () => {
await waitForShellIntegration(terminal);
await this.activate(terminal, environment);
},
);
} else {
traceVerbose(`Environment ${environment.environmentPath.fsPath} is not activatable`);
}
} else if (actType === ACT_TYPE_OFF) {
traceInfo(`"python-envs.terminal.autoActivationType" is set to "${actType}", skipping auto activation`);
} else if (actType === ACT_TYPE_SHELL) {
traceInfo(
`"python-envs.terminal.autoActivationType" is set to "${actType}", terminal should be activated by shell startup script`,
);
}
}
public async create(environment: PythonEnvironment, options: PythonTerminalCreateOptions): Promise<Terminal> {
const autoActType = getAutoActivationType();
let envVars = options.env;
if (autoActType === ACT_TYPE_SHELL) {
const vars = await Promise.all(this.startupEnvProviders.map((p) => p.getEnvVariables(environment)));
vars.forEach((varMap) => {
if (varMap) {
varMap.forEach((value, key) => {
envVars = { ...envVars, [key]: value };
});
}
});
}
// Uncomment the code line below after the issue is resolved:
// https://github.com/microsoft/vscode-python-environments/issues/172
// const name = options.name ?? `Python: ${environment.displayName}`;
const newTerminal = createTerminal({
...options,
env: envVars,
});
if (autoActType === ACT_TYPE_COMMAND) {
if (options.disableActivation) {
this.skipActivationOnOpen.add(newTerminal);
return newTerminal;
}
// We add it to skip activation on open to prevent double activation.
// We can activate it ourselves since we are creating it.
this.skipActivationOnOpen.add(newTerminal);
await this.autoActivateOnTerminalOpen(newTerminal, environment);
}
return newTerminal;
}
private dedicatedTerminals = new Map<string, Terminal>();
async getDedicatedTerminal(
terminalKey: Uri,
project: Uri | PythonProject,
environment: PythonEnvironment,
createNew: boolean = false,
): Promise<Terminal> {
const part = terminalKey instanceof Uri ? path.normalize(terminalKey.fsPath) : terminalKey;
const key = `${environment.envId.id}:${part}`;
if (!createNew) {
const terminal = this.dedicatedTerminals.get(key);
if (terminal) {
return terminal;
}
}
const puri = project instanceof Uri ? project : project.uri;
const config = getConfiguration('python', terminalKey);
const projectStat = await fsapi.stat(puri.fsPath);
const projectDir = projectStat.isDirectory() ? puri.fsPath : path.dirname(puri.fsPath);
const uriStat = await fsapi.stat(terminalKey.fsPath);
const uriDir = uriStat.isDirectory() ? terminalKey.fsPath : path.dirname(terminalKey.fsPath);
const cwd = config.get<boolean>('terminal.executeInFileDir', false) ? uriDir : projectDir;
const newTerminal = await this.create(environment, { cwd });
this.dedicatedTerminals.set(key, newTerminal);
const disable = onDidCloseTerminal((terminal) => {
if (terminal === newTerminal) {
this.dedicatedTerminals.delete(key);
disable.dispose();
}
});
return newTerminal;
}
private projectTerminals = new Map<string, Terminal>();
async getProjectTerminal(
project: Uri | PythonProject,
environment: PythonEnvironment,
createNew: boolean = false,
): Promise<Terminal> {
const uri = project instanceof Uri ? project : project.uri;
const key = `${environment.envId.id}:${path.normalize(uri.fsPath)}`;
if (!createNew) {
const terminal = this.projectTerminals.get(key);
if (terminal) {
return terminal;
}
}
const stat = await fsapi.stat(uri.fsPath);
const cwd = stat.isDirectory() ? uri.fsPath : path.dirname(uri.fsPath);
const newTerminal = await this.create(environment, { cwd });
this.projectTerminals.set(key, newTerminal);
const disable = onDidCloseTerminal((terminal) => {
if (terminal === newTerminal) {
this.projectTerminals.delete(key);
disable.dispose();
}
});
return newTerminal;
}
private async activateUsingCommand(api: PythonEnvironmentApi, t: Terminal): Promise<void> {
this.skipActivationOnOpen.add(t);
const env = this.ta.getEnvironment(t) ?? (await getEnvironmentForTerminal(api, t));
if (env && isActivatableEnvironment(env)) {
await this.activate(t, env);
}
}
public async initialize(api: PythonEnvironmentApi): Promise<void> {
const actType = getAutoActivationType();
if (actType === ACT_TYPE_COMMAND) {
await Promise.all(terminals().map(async (t) => this.activateUsingCommand(api, t)));
} else if (actType === ACT_TYPE_SHELL) {
const shells = new Set(
terminals()
.map((t) => identifyTerminalShell(t))
.filter((t) => t !== 'unknown'),
);
if (shells.size > 0) {
await this.handleSetupCheck(shells);
await Promise.all(
terminals().map(async (t) => {
// If the shell is not set up, we activate using command fallback.
if (this.shellSetup.get(identifyTerminalShell(t)) === false) {
await this.activateUsingCommand(api, t);
}
}),
);
}
}
}
public getEnvironment(terminal: Terminal): PythonEnvironment | undefined {
return this.ta.getEnvironment(terminal);
}
public activate(terminal: Terminal, environment: PythonEnvironment): Promise<void> {
return this.ta.activate(terminal, environment);
}
public deactivate(terminal: Terminal): Promise<void> {
return this.ta.deactivate(terminal);
}
isActivated(terminal: Terminal, environment?: PythonEnvironment): boolean {
return this.ta.isActivated(terminal, environment);
}
dispose(): void {
this.disposables.forEach((d) => d.dispose());
}
}