forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterpreterPathCommand.ts
More file actions
61 lines (53 loc) · 2.58 KB
/
interpreterPathCommand.ts
File metadata and controls
61 lines (53 loc) · 2.58 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import { Uri, workspace } from 'vscode';
import { IExtensionSingleActivationService } from '../activation/types';
import { Commands } from '../common/constants';
import { IDisposable, IDisposableRegistry } from '../common/types';
import { registerCommand } from '../common/vscodeApis/commandApis';
import { IInterpreterService } from './contracts';
import { useEnvExtension } from '../envExt/api.internal';
@injectable()
export class InterpreterPathCommand implements IExtensionSingleActivationService {
public readonly supportedWorkspaceTypes = { untrustedWorkspace: false, virtualWorkspace: false };
constructor(
@inject(IInterpreterService) private readonly interpreterService: IInterpreterService,
@inject(IDisposableRegistry) private readonly disposables: IDisposable[],
) {}
public async activate(): Promise<void> {
this.disposables.push(
registerCommand(Commands.GetSelectedInterpreterPath, (args) => this._getSelectedInterpreterPath(args)),
);
}
public async _getSelectedInterpreterPath(
args: { workspaceFolder: string; type: string } | string[],
): Promise<string> {
// If `launch.json` is launching this command, `args.workspaceFolder` carries the workspaceFolder
// If `tasks.json` is launching this command, `args[1]` carries the workspaceFolder
let workspaceFolder;
if ('workspaceFolder' in args) {
workspaceFolder = args.workspaceFolder;
} else if (args[1]) {
const [, second] = args;
workspaceFolder = second;
} else if (useEnvExtension() && 'type' in args && args.type === 'debugpy') {
// If using the envsExt and the type is debugpy, we need to add the workspace folder to get the interpreter path.
if (Array.isArray(workspace.workspaceFolders) && workspace.workspaceFolders.length > 0) {
workspaceFolder = workspace.workspaceFolders[0].uri.fsPath;
}
} else {
workspaceFolder = undefined;
}
let workspaceFolderUri;
try {
workspaceFolderUri = workspaceFolder ? Uri.file(workspaceFolder) : undefined;
} catch (ex) {
workspaceFolderUri = undefined;
}
const interpreterPath =
(await this.interpreterService.getActiveInterpreter(workspaceFolderUri))?.path ?? 'python';
return interpreterPath.toCommandArgumentForPythonExt();
}
}