forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.legacy.ts
More file actions
152 lines (139 loc) · 5.67 KB
/
api.legacy.ts
File metadata and controls
152 lines (139 loc) · 5.67 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { Terminal, Uri } from 'vscode';
import { getEnvExtApi, getEnvironment } from './api.internal';
import { EnvironmentType, PythonEnvironment as PythonEnvironmentLegacy } from '../pythonEnvironments/info';
import { PythonEnvironment, PythonTerminalOptions } from './types';
import { Architecture } from '../common/utils/platform';
import { parseVersion } from '../pythonEnvironments/base/info/pythonVersion';
import { PythonEnvType } from '../pythonEnvironments/base/info';
import { traceError } from '../logging';
import { reportActiveInterpreterChanged } from '../environmentApi';
import { getWorkspaceFolder, getWorkspaceFolders } from '../common/vscodeApis/workspaceApis';
function toEnvironmentType(pythonEnv: PythonEnvironment): EnvironmentType {
if (pythonEnv.envId.managerId.toLowerCase().endsWith('system')) {
return EnvironmentType.System;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('venv')) {
return EnvironmentType.Venv;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('virtualenv')) {
return EnvironmentType.VirtualEnv;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('conda')) {
return EnvironmentType.Conda;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('pipenv')) {
return EnvironmentType.Pipenv;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('poetry')) {
return EnvironmentType.Poetry;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('pyenv')) {
return EnvironmentType.Pyenv;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('hatch')) {
return EnvironmentType.Hatch;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('pixi')) {
return EnvironmentType.Pixi;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('virtualenvwrapper')) {
return EnvironmentType.VirtualEnvWrapper;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('activestate')) {
return EnvironmentType.ActiveState;
}
return EnvironmentType.Unknown;
}
function getEnvType(kind: EnvironmentType): PythonEnvType | undefined {
switch (kind) {
case EnvironmentType.Pipenv:
case EnvironmentType.VirtualEnv:
case EnvironmentType.Pyenv:
case EnvironmentType.Venv:
case EnvironmentType.Poetry:
case EnvironmentType.Hatch:
case EnvironmentType.Pixi:
case EnvironmentType.VirtualEnvWrapper:
case EnvironmentType.ActiveState:
return PythonEnvType.Virtual;
case EnvironmentType.Conda:
return PythonEnvType.Conda;
case EnvironmentType.MicrosoftStore:
case EnvironmentType.Global:
case EnvironmentType.System:
default:
return undefined;
}
}
function toLegacyType(env: PythonEnvironment): PythonEnvironmentLegacy {
const ver = parseVersion(env.version);
const envType = toEnvironmentType(env);
return {
id: env.environmentPath.fsPath,
displayName: env.displayName,
detailedDisplayName: env.name,
envType,
envPath: env.sysPrefix,
type: getEnvType(envType),
path: env.environmentPath.fsPath,
version: {
raw: env.version,
major: ver.major,
minor: ver.minor,
patch: ver.micro,
build: [],
prerelease: [],
},
sysVersion: env.version,
architecture: Architecture.x64,
sysPrefix: env.sysPrefix,
};
}
const previousEnvMap = new Map<string, PythonEnvironment | undefined>();
export async function getActiveInterpreterLegacy(resource?: Uri): Promise<PythonEnvironmentLegacy | undefined> {
const api = await getEnvExtApi();
const uri = resource ? api.getPythonProject(resource)?.uri : undefined;
const pythonEnv = await getEnvironment(resource);
const oldEnv = previousEnvMap.get(uri?.fsPath || '');
const newEnv = pythonEnv ? toLegacyType(pythonEnv) : undefined;
const folders = getWorkspaceFolders() ?? [];
const shouldReport =
(folders.length === 0 && resource === undefined) || (folders.length > 0 && resource !== undefined);
if (shouldReport && newEnv && oldEnv?.envId.id !== pythonEnv?.envId.id) {
reportActiveInterpreterChanged({
resource: getWorkspaceFolder(resource),
path: newEnv.path,
});
previousEnvMap.set(uri?.fsPath || '', pythonEnv);
}
return pythonEnv ? toLegacyType(pythonEnv) : undefined;
}
export async function setInterpreterLegacy(pythonPath: string, uri: Uri | undefined): Promise<void> {
const api = await getEnvExtApi();
const pythonEnv = await api.resolveEnvironment(Uri.file(pythonPath));
if (!pythonEnv) {
traceError(`EnvExt: Failed to resolve environment for ${pythonPath}`);
return;
}
await api.setEnvironment(uri, pythonEnv);
}
export async function resetInterpreterLegacy(uri: Uri | undefined): Promise<void> {
const api = await getEnvExtApi();
await api.setEnvironment(uri, undefined);
}
export async function ensureTerminalLegacy(
resource: Uri | undefined,
options?: PythonTerminalOptions,
): Promise<Terminal> {
const api = await getEnvExtApi();
const pythonEnv = await api.getEnvironment(resource);
const project = resource ? api.getPythonProject(resource) : undefined;
if (pythonEnv && project) {
const fixedOptions = options ? { ...options } : { cwd: project.uri };
const terminal = await api.createTerminal(pythonEnv, fixedOptions);
return terminal;
}
throw new Error('Invalid arguments to create terminal');
}