|
| 1 | +import { Uri, ThemeIcon, QuickPickItem, QuickPickItemKind, ProgressLocation, QuickInputButtons } from 'vscode'; |
| 2 | +import { IconPath, PythonEnvironment, PythonProject } from '../../api'; |
| 3 | +import { InternalEnvironmentManager } from '../../internal.api'; |
| 4 | +import { Common, Interpreter } from '../localize'; |
| 5 | +import { showQuickPickWithButtons, showQuickPick, showOpenDialog, withProgress } from '../window.apis'; |
| 6 | +import { isWindows } from '../../managers/common/utils'; |
| 7 | +import { traceError } from '../logging'; |
| 8 | +import { pickEnvironmentManager } from './managers'; |
| 9 | +import { handlePythonPath } from '../utils/pythonPath'; |
| 10 | + |
| 11 | +type QuickPickIcon = |
| 12 | + | Uri |
| 13 | + | { |
| 14 | + light: Uri; |
| 15 | + dark: Uri; |
| 16 | + } |
| 17 | + | ThemeIcon |
| 18 | + | undefined; |
| 19 | + |
| 20 | +function getIconPath(i: IconPath | undefined): QuickPickIcon { |
| 21 | + if (i === undefined || i instanceof ThemeIcon) { |
| 22 | + return i; |
| 23 | + } |
| 24 | + |
| 25 | + if (i instanceof Uri) { |
| 26 | + return i.fsPath.endsWith('__icon__.py') ? undefined : i; |
| 27 | + } |
| 28 | + |
| 29 | + if (typeof i === 'string') { |
| 30 | + return Uri.file(i); |
| 31 | + } |
| 32 | + |
| 33 | + return { |
| 34 | + light: i.light instanceof Uri ? i.light : Uri.file(i.light), |
| 35 | + dark: i.dark instanceof Uri ? i.dark : Uri.file(i.dark), |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +interface EnvironmentPickOptions { |
| 40 | + recommended?: PythonEnvironment; |
| 41 | + showBackButton?: boolean; |
| 42 | + projects: PythonProject[]; |
| 43 | +} |
| 44 | +async function browseForPython( |
| 45 | + managers: InternalEnvironmentManager[], |
| 46 | + projectEnvManagers: InternalEnvironmentManager[], |
| 47 | +): Promise<PythonEnvironment | undefined> { |
| 48 | + const filters = isWindows() ? { python: ['exe'] } : undefined; |
| 49 | + const uris = await showOpenDialog({ |
| 50 | + canSelectFiles: true, |
| 51 | + canSelectFolders: false, |
| 52 | + canSelectMany: false, |
| 53 | + filters, |
| 54 | + title: 'Select Python executable', |
| 55 | + }); |
| 56 | + if (!uris || uris.length === 0) { |
| 57 | + return; |
| 58 | + } |
| 59 | + const uri = uris[0]; |
| 60 | + |
| 61 | + const environment = await withProgress( |
| 62 | + { |
| 63 | + location: ProgressLocation.Notification, |
| 64 | + cancellable: false, |
| 65 | + }, |
| 66 | + async (reporter, token) => { |
| 67 | + const env = await handlePythonPath(uri, managers, projectEnvManagers, reporter, token); |
| 68 | + return env; |
| 69 | + }, |
| 70 | + ); |
| 71 | + return environment; |
| 72 | +} |
| 73 | + |
| 74 | +async function createEnvironment( |
| 75 | + managers: InternalEnvironmentManager[], |
| 76 | + projectEnvManagers: InternalEnvironmentManager[], |
| 77 | + options: EnvironmentPickOptions, |
| 78 | +): Promise<PythonEnvironment | undefined> { |
| 79 | + const managerId = await pickEnvironmentManager( |
| 80 | + managers.filter((m) => m.supportsCreate), |
| 81 | + projectEnvManagers.filter((m) => m.supportsCreate), |
| 82 | + ); |
| 83 | + |
| 84 | + const manager = managers.find((m) => m.id === managerId); |
| 85 | + if (manager) { |
| 86 | + try { |
| 87 | + const env = await manager.create(options.projects.map((p) => p.uri)); |
| 88 | + return env; |
| 89 | + } catch (ex) { |
| 90 | + if (ex === QuickInputButtons.Back) { |
| 91 | + return createEnvironment(managers, projectEnvManagers, options); |
| 92 | + } |
| 93 | + traceError(`Failed to create environment using ${manager.id}`, ex); |
| 94 | + throw ex; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +async function pickEnvironmentImpl( |
| 100 | + items: (QuickPickItem | (QuickPickItem & { result: PythonEnvironment }))[], |
| 101 | + managers: InternalEnvironmentManager[], |
| 102 | + projectEnvManagers: InternalEnvironmentManager[], |
| 103 | + options: EnvironmentPickOptions, |
| 104 | +): Promise<PythonEnvironment | undefined> { |
| 105 | + const selected = await showQuickPickWithButtons(items, { |
| 106 | + placeHolder: `Select a Python Environment`, |
| 107 | + ignoreFocusOut: true, |
| 108 | + showBackButton: options?.showBackButton, |
| 109 | + }); |
| 110 | + |
| 111 | + if (selected && !Array.isArray(selected)) { |
| 112 | + if (selected.label === Interpreter.browsePath) { |
| 113 | + return browseForPython(managers, projectEnvManagers); |
| 114 | + } else if (selected.label === Interpreter.createVirtualEnvironment) { |
| 115 | + return createEnvironment(managers, projectEnvManagers, options); |
| 116 | + } |
| 117 | + return (selected as { result: PythonEnvironment })?.result; |
| 118 | + } |
| 119 | + return undefined; |
| 120 | +} |
| 121 | + |
| 122 | +export async function pickEnvironment( |
| 123 | + managers: InternalEnvironmentManager[], |
| 124 | + projectEnvManagers: InternalEnvironmentManager[], |
| 125 | + options: EnvironmentPickOptions, |
| 126 | +): Promise<PythonEnvironment | undefined> { |
| 127 | + const items: (QuickPickItem | (QuickPickItem & { result: PythonEnvironment }))[] = [ |
| 128 | + { |
| 129 | + label: Interpreter.browsePath, |
| 130 | + iconPath: new ThemeIcon('folder'), |
| 131 | + }, |
| 132 | + { |
| 133 | + label: '', |
| 134 | + kind: QuickPickItemKind.Separator, |
| 135 | + }, |
| 136 | + { |
| 137 | + label: Interpreter.createVirtualEnvironment, |
| 138 | + iconPath: new ThemeIcon('add'), |
| 139 | + }, |
| 140 | + ]; |
| 141 | + |
| 142 | + if (options?.recommended) { |
| 143 | + items.push( |
| 144 | + { |
| 145 | + label: Common.recommended, |
| 146 | + kind: QuickPickItemKind.Separator, |
| 147 | + }, |
| 148 | + { |
| 149 | + label: options.recommended.displayName, |
| 150 | + description: options.recommended.description, |
| 151 | + result: options.recommended, |
| 152 | + iconPath: getIconPath(options.recommended.iconPath), |
| 153 | + }, |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + for (const manager of managers) { |
| 158 | + items.push({ |
| 159 | + label: manager.displayName, |
| 160 | + kind: QuickPickItemKind.Separator, |
| 161 | + }); |
| 162 | + const envs = await manager.getEnvironments('all'); |
| 163 | + items.push( |
| 164 | + ...envs.map((e) => { |
| 165 | + return { |
| 166 | + label: e.displayName ?? e.name, |
| 167 | + description: e.description, |
| 168 | + e: { selected: e, manager: manager }, |
| 169 | + iconPath: getIconPath(e.iconPath), |
| 170 | + }; |
| 171 | + }), |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + return pickEnvironmentImpl(items, managers, projectEnvManagers, options); |
| 176 | +} |
| 177 | + |
| 178 | +export async function pickEnvironmentFrom(environments: PythonEnvironment[]): Promise<PythonEnvironment | undefined> { |
| 179 | + const items = environments.map((e) => ({ |
| 180 | + label: e.displayName ?? e.name, |
| 181 | + description: e.description, |
| 182 | + e: e, |
| 183 | + iconPath: getIconPath(e.iconPath), |
| 184 | + })); |
| 185 | + const selected = await showQuickPick(items, { |
| 186 | + placeHolder: 'Select Python Environment', |
| 187 | + ignoreFocusOut: true, |
| 188 | + }); |
| 189 | + return (selected as { e: PythonEnvironment })?.e; |
| 190 | +} |
0 commit comments