|
1 | | -import { window, extensions } from 'vscode'; |
| 1 | +import { window, extensions, Uri } from 'vscode'; |
2 | 2 | import * as fs from 'fs-extra'; |
3 | 3 | import * as path from 'path'; |
| 4 | +import { EnvironmentManagers } from '../../internal.api'; |
| 5 | +import { CreateEnvironmentOptions, EnvironmentManager } from '../../api'; |
| 6 | +import { traceVerbose } from '../../common/logging'; |
4 | 7 |
|
5 | 8 | /** |
6 | 9 | * Prompts the user to choose whether to create a new venv for a package. |
@@ -49,17 +52,56 @@ export async function removeCopilotInstructions(destFolder: string) { |
49 | 52 | } |
50 | 53 | } |
51 | 54 |
|
52 | | -export async function quickCreateNewVenv(destFolder: string) { |
53 | | - const venvPath = path.join(destFolder, '.venv'); |
| 55 | +export async function quickCreateNewVenv(envManagers: EnvironmentManagers, destFolder: string) { |
54 | 56 | try { |
55 | | - // Placeholder: replace with your venv creation logic |
56 | | - // await quickCreateVenv(...) |
57 | | - window.showInformationMessage(`(Placeholder) Would create venv at: ${venvPath}`); |
| 57 | + // get the environment manager for venv |
| 58 | + const envManager: EnvironmentManager | undefined = envManagers.managers.find( |
| 59 | + (m) => m.id === 'ms-python.python:venv', |
| 60 | + ); |
| 61 | + const destUri = Uri.parse(destFolder); |
| 62 | + if (envManager && envManager.create) { |
| 63 | + // with quickCreate enabled, user will not be prompted when creating the environment |
| 64 | + const options: CreateEnvironmentOptions = { quickCreate: false }; |
| 65 | + if (envManager.quickCreateConfig) { |
| 66 | + options.quickCreate = true; |
| 67 | + } |
| 68 | + const pyEnv = await envManager.create(destUri, options); |
| 69 | + // comes back as undefined if this doesn't work |
| 70 | + traceVerbose(`Created venv at: ${pyEnv?.environmentPath} using ${envManager.name}`); |
| 71 | + } else { |
| 72 | + // find an environment manager that supports create |
| 73 | + const envManager = envManagers.managers.find((m) => m.create); |
| 74 | + if (envManager) { |
| 75 | + const pyEnv = await envManager.create(destUri, {}); |
| 76 | + traceVerbose(`Created venv at: ${pyEnv?.environmentPath} using ${envManager.name}`); |
| 77 | + } |
| 78 | + // If no environment manager supports create, show an error message |
| 79 | + window.showErrorMessage( |
| 80 | + `No environment manager found that supports creating a new environment, skipping...`, |
| 81 | + ); |
| 82 | + } |
58 | 83 | } catch (err) { |
59 | 84 | window.showErrorMessage(`Failed to create virtual environment: ${err}`); |
60 | 85 | } |
61 | 86 | } |
62 | 87 |
|
| 88 | +/** |
| 89 | + * Replaces all occurrences of a string in a single file's contents, handling special regex characters in the search value. |
| 90 | + * @param filePath The path to the file to update. |
| 91 | + * @param searchValue The string to search for (will be escaped for regex). |
| 92 | + * @param replaceValue The string to replace with. |
| 93 | + */ |
| 94 | +export async function replaceInFile(filePath: string, searchValue: string, replaceValue: string) { |
| 95 | + // Escape special regex characters in searchValue |
| 96 | + const escapedSearchValue = searchValue.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 97 | + const regex = new RegExp(escapedSearchValue, 'g'); |
| 98 | + let content = await fs.readFile(filePath, 'utf8'); |
| 99 | + if (content.includes(searchValue)) { |
| 100 | + content = content.replace(regex, replaceValue); |
| 101 | + await fs.writeFile(filePath, content, 'utf8'); |
| 102 | + } |
| 103 | +} |
| 104 | + |
63 | 105 | // Helper to recursively replace all occurrences of a string in file/folder names and file contents |
64 | 106 | export async function replaceInFilesAndNames(dir: string, searchValue: string, replaceValue: string) { |
65 | 107 | const entries = await fs.readdir(dir, { withFileTypes: true }); |
|
0 commit comments