-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathenvironments.ts
More file actions
190 lines (175 loc) · 6.2 KB
/
environments.ts
File metadata and controls
190 lines (175 loc) · 6.2 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
import { Uri, ThemeIcon, QuickPickItem, QuickPickItemKind, ProgressLocation, QuickInputButtons } from 'vscode';
import { IconPath, PythonEnvironment, PythonProject } from '../../api';
import { InternalEnvironmentManager } from '../../internal.api';
import { Common, Interpreter, Pickers } from '../localize';
import { showQuickPickWithButtons, showQuickPick, showOpenDialog, withProgress } from '../window.apis';
import { traceError } from '../logging';
import { pickEnvironmentManager } from './managers';
import { handlePythonPath } from '../utils/pythonPath';
import { isWindows } from '../utils/platformUtils';
type QuickPickIcon =
| Uri
| {
light: Uri;
dark: Uri;
}
| ThemeIcon
| undefined;
function getIconPath(i: IconPath | undefined): QuickPickIcon {
if (i === undefined || i instanceof ThemeIcon || i instanceof Uri) {
return i;
}
if (typeof i === 'string') {
return Uri.file(i);
}
return {
light: i.light instanceof Uri ? i.light : Uri.file(i.light),
dark: i.dark instanceof Uri ? i.dark : Uri.file(i.dark),
};
}
interface EnvironmentPickOptions {
recommended?: PythonEnvironment;
showBackButton?: boolean;
projects: PythonProject[];
}
async function browseForPython(
managers: InternalEnvironmentManager[],
projectEnvManagers: InternalEnvironmentManager[],
): Promise<PythonEnvironment | undefined> {
const filters = isWindows() ? { python: ['exe'] } : undefined;
const uris = await showOpenDialog({
canSelectFiles: true,
canSelectFolders: false,
canSelectMany: false,
filters,
title: Pickers.Environments.selectExecutable,
});
if (!uris || uris.length === 0) {
return;
}
const uri = uris[0];
const environment = await withProgress(
{
location: ProgressLocation.Notification,
cancellable: false,
},
async (reporter, token) => {
const env = await handlePythonPath(uri, managers, projectEnvManagers, reporter, token);
return env;
},
);
return environment;
}
async function createEnvironment(
managers: InternalEnvironmentManager[],
projectEnvManagers: InternalEnvironmentManager[],
options: EnvironmentPickOptions,
): Promise<PythonEnvironment | undefined> {
const managerId = await pickEnvironmentManager(
managers.filter((m) => m.supportsCreate),
projectEnvManagers.filter((m) => m.supportsCreate),
);
const manager = managers.find((m) => m.id === managerId);
if (manager) {
try {
const env = await manager.create(
options.projects.map((p) => p.uri),
undefined,
);
return env;
} catch (ex) {
if (ex === QuickInputButtons.Back) {
return createEnvironment(managers, projectEnvManagers, options);
}
traceError(`Failed to create environment using ${manager.id}`, ex);
throw ex;
}
}
}
async function pickEnvironmentImpl(
items: (QuickPickItem | (QuickPickItem & { result: PythonEnvironment }))[],
managers: InternalEnvironmentManager[],
projectEnvManagers: InternalEnvironmentManager[],
options: EnvironmentPickOptions,
): Promise<PythonEnvironment | undefined> {
const selected = await showQuickPickWithButtons(items, {
placeHolder: Pickers.Environments.selectEnvironment,
ignoreFocusOut: true,
showBackButton: options?.showBackButton,
});
if (selected && !Array.isArray(selected)) {
if (selected.label === Interpreter.browsePath) {
return browseForPython(managers, projectEnvManagers);
} else if (selected.label === Interpreter.createVirtualEnvironment) {
return createEnvironment(managers, projectEnvManagers, options);
}
return (selected as { result: PythonEnvironment })?.result;
}
return undefined;
}
export async function pickEnvironment(
managers: InternalEnvironmentManager[],
projectEnvManagers: InternalEnvironmentManager[],
options: EnvironmentPickOptions,
): Promise<PythonEnvironment | undefined> {
const items: (QuickPickItem | (QuickPickItem & { result: PythonEnvironment }))[] = [
{
label: Interpreter.browsePath,
iconPath: new ThemeIcon('folder'),
},
{
label: '',
kind: QuickPickItemKind.Separator,
},
{
label: Interpreter.createVirtualEnvironment,
iconPath: new ThemeIcon('add'),
},
];
if (options?.recommended) {
items.push(
{
label: Common.recommended,
kind: QuickPickItemKind.Separator,
},
{
label: options.recommended.displayName,
description: options.recommended.description || options.recommended.displayPath,
result: options.recommended,
iconPath: getIconPath(options.recommended.iconPath),
},
);
}
for (const manager of managers) {
items.push({
label: manager.displayName,
kind: QuickPickItemKind.Separator,
});
const envs = await manager.getEnvironments('all');
items.push(
...envs.map((e) => {
return {
label: e.displayName ?? e.name,
description: e.description || e.displayPath,
result: e,
manager: manager,
iconPath: getIconPath(e.iconPath),
};
}),
);
}
return pickEnvironmentImpl(items, managers, projectEnvManagers, options);
}
export async function pickEnvironmentFrom(environments: PythonEnvironment[]): Promise<PythonEnvironment | undefined> {
const items = environments.map((e) => ({
label: e.displayName ?? e.name,
description: e.description || e.displayPath,
e: e,
iconPath: getIconPath(e.iconPath),
}));
const selected = await showQuickPick(items, {
placeHolder: Pickers.Environments.selectEnvironment,
ignoreFocusOut: true,
});
return (selected as { e: PythonEnvironment })?.e;
}