|
1 | | -import { ExtensionContext, extensions, Uri, workspace } from 'vscode'; |
| 1 | +import { ExtensionContext, extensions, QuickInputButtons, Uri, window, workspace } from 'vscode'; |
2 | 2 | import { PythonEnvironment, PythonEnvironmentApi } from './api'; |
3 | 3 | import { traceError, traceInfo, traceWarn } from './common/logging'; |
4 | 4 | import { normalizePath } from './common/utils/pathUtils'; |
| 5 | +import { isWindows } from './common/utils/platformUtils'; |
| 6 | +import { createTerminal, showInputBoxWithButtons } from './common/window.apis'; |
5 | 7 | import { getConfiguration } from './common/workspace.apis'; |
6 | 8 | import { getAutoActivationType } from './features/terminal/utils'; |
7 | 9 | import { EnvironmentManagers, PythonProjectManager } from './internal.api'; |
8 | | -import { NativeEnvInfo, NativePythonFinder } from './managers/common/nativePythonFinder'; |
| 10 | +import { getNativePythonToolsPath, NativeEnvInfo, NativePythonFinder } from './managers/common/nativePythonFinder'; |
9 | 11 |
|
10 | 12 | /** |
11 | 13 | * Collects relevant Python environment information for issue reporting |
@@ -137,6 +139,90 @@ export function getUserConfiguredSetting<T>(section: string, key: string): T | u |
137 | 139 | return undefined; |
138 | 140 | } |
139 | 141 |
|
| 142 | +/** |
| 143 | + * Runs the Python Environment Tool (PET) in a terminal window, allowing users to |
| 144 | + * execute various PET commands like finding all Python environments or resolving |
| 145 | + * the details of a specific environment. |
| 146 | + * |
| 147 | + * |
| 148 | + * @returns A Promise that resolves when the PET command has been executed or cancelled |
| 149 | + */ |
| 150 | +export async function runPetInTerminalImpl(): Promise<void> { |
| 151 | + const petPath = await getNativePythonToolsPath(); |
| 152 | + |
| 153 | + // Show quick pick menu for PET operation selection |
| 154 | + const selectedOption = await window.showQuickPick( |
| 155 | + [ |
| 156 | + { |
| 157 | + label: 'Find All Environments', |
| 158 | + description: 'Finds all environments and reports them to the standard output', |
| 159 | + detail: 'Runs: pet find --verbose', |
| 160 | + }, |
| 161 | + { |
| 162 | + label: 'Resolve Environment...', |
| 163 | + description: 'Resolves & reports the details of the environment to the standard output', |
| 164 | + detail: 'Runs: pet resolve <path>', |
| 165 | + }, |
| 166 | + ], |
| 167 | + { |
| 168 | + placeHolder: 'Select a Python Environment Tool (PET) operation', |
| 169 | + ignoreFocusOut: true, |
| 170 | + }, |
| 171 | + ); |
| 172 | + |
| 173 | + if (!selectedOption) { |
| 174 | + return; // User cancelled |
| 175 | + } |
| 176 | + |
| 177 | + if (selectedOption.label === 'Find All Environments') { |
| 178 | + // Create and show terminal immediately for 'Find All Environments' option |
| 179 | + const terminal = createTerminal({ |
| 180 | + name: 'Python Environment Tool (PET)', |
| 181 | + }); |
| 182 | + terminal.show(); |
| 183 | + |
| 184 | + // Run pet find --verbose |
| 185 | + terminal.sendText(`"${petPath}" find --verbose`, true); |
| 186 | + traceInfo(`Running PET find command: ${petPath} find --verbose`); |
| 187 | + } else if (selectedOption.label === 'Resolve Environment...') { |
| 188 | + try { |
| 189 | + // Show input box for path with back button |
| 190 | + const placeholder = isWindows() ? 'C:\\path\\to\\python\\executable' : '/path/to/python/executable'; |
| 191 | + const inputPath = await showInputBoxWithButtons({ |
| 192 | + prompt: 'Enter the path to the Python executable to resolve', |
| 193 | + placeHolder: placeholder, |
| 194 | + ignoreFocusOut: true, |
| 195 | + showBackButton: true, |
| 196 | + validateInput: (value) => { |
| 197 | + if (!value || value.trim().length === 0) { |
| 198 | + return 'Please enter a valid path'; |
| 199 | + } |
| 200 | + return null; |
| 201 | + }, |
| 202 | + }); |
| 203 | + |
| 204 | + if (inputPath) { |
| 205 | + // Only create and show terminal after path has been entered |
| 206 | + const terminal = createTerminal({ |
| 207 | + name: 'Python Environment Tool (PET)', |
| 208 | + }); |
| 209 | + terminal.show(); |
| 210 | + |
| 211 | + // Run pet resolve with the provided path |
| 212 | + terminal.sendText(`"${petPath}" resolve "${inputPath.trim()}"`, true); |
| 213 | + traceInfo(`Running PET resolve command: ${petPath} resolve "${inputPath.trim()}"`); |
| 214 | + } |
| 215 | + } catch (ex) { |
| 216 | + if (ex === QuickInputButtons.Back) { |
| 217 | + // If back button was clicked, restart the flow |
| 218 | + await runPetInTerminalImpl(); |
| 219 | + return; |
| 220 | + } |
| 221 | + throw ex; // Re-throw other errors |
| 222 | + } |
| 223 | + } |
| 224 | +} |
| 225 | + |
140 | 226 | /** |
141 | 227 | * Sets the default Python interpreter for the workspace if the user has not explicitly set 'defaultEnvManager'. |
142 | 228 | * @param nativeFinder - used to resolve interpreter paths. |
|
0 commit comments