forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.ts
More file actions
160 lines (151 loc) · 7.14 KB
/
helper.ts
File metadata and controls
160 lines (151 loc) · 7.14 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { inject, injectable, multiInject, named } from 'inversify';
import { Terminal, Uri } from 'vscode';
import { ICondaService, IInterpreterService, InterpreterType, PythonInterpreter } from '../../interpreter/contracts';
import { sendTelemetryEvent } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { ITerminalManager } from '../application/types';
import '../extensions';
import { traceDecorators, traceError } from '../logger';
import { IPlatformService } from '../platform/types';
import { IConfigurationService, Resource } from '../types';
import { OSType } from '../utils/platform';
import { ShellDetector } from './shellDetector';
import {
IShellDetector,
ITerminalActivationCommandProvider,
ITerminalHelper,
TerminalActivationProviders,
TerminalShellType
} from './types';
@injectable()
export class TerminalHelper implements ITerminalHelper {
private readonly shellDetector: ShellDetector;
constructor(
@inject(IPlatformService) private readonly platform: IPlatformService,
@inject(ITerminalManager) private readonly terminalManager: ITerminalManager,
@inject(ICondaService) private readonly condaService: ICondaService,
@inject(IInterpreterService) readonly interpreterService: IInterpreterService,
@inject(IConfigurationService) private readonly configurationService: IConfigurationService,
@inject(ITerminalActivationCommandProvider)
@named(TerminalActivationProviders.conda)
private readonly conda: ITerminalActivationCommandProvider,
@inject(ITerminalActivationCommandProvider)
@named(TerminalActivationProviders.bashCShellFish)
private readonly bashCShellFish: ITerminalActivationCommandProvider,
@inject(ITerminalActivationCommandProvider)
@named(TerminalActivationProviders.commandPromptAndPowerShell)
private readonly commandPromptAndPowerShell: ITerminalActivationCommandProvider,
@inject(ITerminalActivationCommandProvider)
@named(TerminalActivationProviders.pyenv)
private readonly pyenv: ITerminalActivationCommandProvider,
@inject(ITerminalActivationCommandProvider)
@named(TerminalActivationProviders.pipenv)
private readonly pipenv: ITerminalActivationCommandProvider,
@multiInject(IShellDetector) shellDetectors: IShellDetector[]
) {
this.shellDetector = new ShellDetector(this.platform, shellDetectors);
}
public createTerminal(title?: string): Terminal {
return this.terminalManager.createTerminal({ name: title });
}
public identifyTerminalShell(terminal?: Terminal): TerminalShellType {
return this.shellDetector.identifyTerminalShell(terminal);
}
public buildCommandForTerminal(terminalShellType: TerminalShellType, command: string, args: string[]) {
const isPowershell =
terminalShellType === TerminalShellType.powershell ||
terminalShellType === TerminalShellType.powershellCore;
const commandPrefix = isPowershell ? '& ' : '';
return `${commandPrefix}${command.fileToCommandArgument()} ${args.join(' ')}`.trim();
}
public async getEnvironmentActivationCommands(
terminalShellType: TerminalShellType,
resource?: Uri,
interpreter?: PythonInterpreter
): Promise<string[] | undefined> {
const providers = [this.pipenv, this.pyenv, this.bashCShellFish, this.commandPromptAndPowerShell];
const promise = this.getActivationCommands(resource || undefined, interpreter, terminalShellType, providers);
this.sendTelemetry(
terminalShellType,
EventName.PYTHON_INTERPRETER_ACTIVATION_FOR_TERMINAL,
interpreter,
promise
).ignoreErrors();
return promise;
}
public async getEnvironmentActivationShellCommands(
resource: Resource,
shell: TerminalShellType,
interpreter?: PythonInterpreter
): Promise<string[] | undefined> {
if (this.platform.osType === OSType.Unknown) {
return;
}
const providers = [this.bashCShellFish, this.commandPromptAndPowerShell];
const promise = this.getActivationCommands(resource, interpreter, shell, providers);
this.sendTelemetry(
shell,
EventName.PYTHON_INTERPRETER_ACTIVATION_FOR_RUNNING_CODE,
interpreter,
promise
).ignoreErrors();
return promise;
}
@traceDecorators.error('Failed to capture telemetry')
protected async sendTelemetry(
terminalShellType: TerminalShellType,
eventName: EventName,
interpreter: PythonInterpreter | undefined,
promise: Promise<string[] | undefined>
): Promise<void> {
let hasCommands = false;
let failed = false;
try {
const cmds = await promise;
hasCommands = Array.isArray(cmds) && cmds.length > 0;
} catch (ex) {
failed = true;
traceError('Failed to get activation commands', ex);
}
const pythonVersion = interpreter && interpreter.version ? interpreter.version.raw : undefined;
const interpreterType = interpreter ? interpreter.type : InterpreterType.Unknown;
const data = { failed, hasCommands, interpreterType, terminal: terminalShellType, pythonVersion };
sendTelemetryEvent(eventName, undefined, data);
}
protected async getActivationCommands(
resource: Resource,
interpreter: PythonInterpreter | undefined,
terminalShellType: TerminalShellType,
providers: ITerminalActivationCommandProvider[]
): Promise<string[] | undefined> {
const settings = this.configurationService.getSettings(resource);
const activateEnvironment = settings.terminal.activateEnvironment;
if (!activateEnvironment) {
return;
}
// If we have a conda environment, then use that.
const isCondaEnvironment = interpreter
? interpreter.type === InterpreterType.Conda
: await this.condaService.isCondaEnvironment(settings.pythonPath);
if (isCondaEnvironment) {
const activationCommands = interpreter
? await this.conda.getActivationCommandsForInterpreter(interpreter.path, terminalShellType)
: await this.conda.getActivationCommands(resource, terminalShellType);
if (Array.isArray(activationCommands)) {
return activationCommands;
}
}
// Search from the list of providers.
const supportedProviders = providers.filter(provider => provider.isShellSupported(terminalShellType));
for (const provider of supportedProviders) {
const activationCommands = interpreter
? await provider.getActivationCommandsForInterpreter(interpreter.path, terminalShellType)
: await provider.getActivationCommands(resource, terminalShellType);
if (Array.isArray(activationCommands) && activationCommands.length > 0) {
return activationCommands;
}
}
}
}