|
1 | 1 | import * as fs from 'fs-extra'; |
2 | 2 | import * as path from 'path'; |
3 | | -import { MarkdownString, Uri, window, workspace } from 'vscode'; |
| 3 | +import { commands, MarkdownString, QuickInputButtons, Uri, window, workspace } from 'vscode'; |
4 | 4 | import { PythonProject, PythonProjectCreator, PythonProjectCreatorOptions } from '../../api'; |
5 | 5 | import { NEW_PROJECT_TEMPLATES_FOLDER } from '../../common/constants'; |
6 | 6 | import { showInputBoxWithButtons } from '../../common/window.apis'; |
@@ -37,101 +37,114 @@ export class NewPackageProject implements PythonProjectCreator { |
37 | 37 | } else { |
38 | 38 | //Prompt as quickCreate is false |
39 | 39 | if (!packageName) { |
40 | | - packageName = await showInputBoxWithButtons({ |
41 | | - prompt: 'What is the name of the package? (e.g. my_package)', |
42 | | - ignoreFocusOut: true, |
43 | | - showBackButton: true, |
44 | | - }); |
45 | | - } |
46 | | - if (!packageName) { |
47 | | - return undefined; |
| 40 | + try { |
| 41 | + packageName = await showInputBoxWithButtons({ |
| 42 | + prompt: 'What is the name of the package? (e.g. my_package)', |
| 43 | + ignoreFocusOut: true, |
| 44 | + showBackButton: true, |
| 45 | + }); |
| 46 | + } catch (ex) { |
| 47 | + if (ex === QuickInputButtons.Back) { |
| 48 | + await commands.executeCommand('python-envs.createNewProjectFromTemplate'); |
| 49 | + } |
| 50 | + } |
| 51 | + if (!packageName) { |
| 52 | + return undefined; |
| 53 | + } |
| 54 | + // Use helper to prompt for virtual environment creation |
| 55 | + const callback = () => { |
| 56 | + if (options) { |
| 57 | + return this.create(options); |
| 58 | + } else { |
| 59 | + return this.create({ name: packageName } as PythonProjectCreatorOptions); |
| 60 | + } |
| 61 | + }; |
| 62 | + createVenv = await promptForVenv(callback); |
| 63 | + if (createVenv === undefined) { |
| 64 | + return undefined; |
| 65 | + } |
| 66 | + if (isCopilotInstalled()) { |
| 67 | + const copilotResult = await promptForCopilotInstructions(); |
| 68 | + if (copilotResult === undefined) { |
| 69 | + return undefined; |
| 70 | + } |
| 71 | + createCopilotInstructions = copilotResult === true; |
| 72 | + } |
48 | 73 | } |
49 | | - // Use helper to prompt for virtual environment creation |
50 | | - createVenv = await promptForVenv(); |
51 | | - if (createVenv === undefined) { |
| 74 | + |
| 75 | + window.showInformationMessage( |
| 76 | + `Creating a new Python project: ${packageName}\nvenv: ${createVenv}\nCopilot instructions: ${createCopilotInstructions}`, |
| 77 | + ); |
| 78 | + |
| 79 | + // 1. Copy template folder |
| 80 | + const newPackageTemplateFolder = path.join(NEW_PROJECT_TEMPLATES_FOLDER, 'newPackageTemplate'); |
| 81 | + if (!(await fs.pathExists(newPackageTemplateFolder))) { |
| 82 | + window.showErrorMessage('Template folder does not exist, aborting creation.'); |
52 | 83 | return undefined; |
53 | 84 | } |
54 | | - if (isCopilotInstalled()) { |
55 | | - const copilotResult = await promptForCopilotInstructions(); |
56 | | - if (copilotResult === undefined) { |
| 85 | + |
| 86 | + // Check if the destination folder is provided, otherwise use the first workspace folder |
| 87 | + let destRoot = options?.rootUri.fsPath; |
| 88 | + if (!destRoot) { |
| 89 | + const workspaceFolders = workspace.workspaceFolders; |
| 90 | + if (!workspaceFolders || workspaceFolders.length === 0) { |
| 91 | + window.showErrorMessage('No workspace folder is open or provided, aborting creation.'); |
57 | 92 | return undefined; |
58 | 93 | } |
59 | | - createCopilotInstructions = copilotResult === true; |
| 94 | + destRoot = workspaceFolders[0].uri.fsPath; |
60 | 95 | } |
61 | | - } |
62 | | - |
63 | | - window.showInformationMessage( |
64 | | - `Creating a new Python project: ${packageName}\nvenv: ${createVenv}\nCopilot instructions: ${createCopilotInstructions}`, |
65 | | - ); |
66 | 96 |
|
67 | | - // 1. Copy template folder |
68 | | - const newPackageTemplateFolder = path.join(NEW_PROJECT_TEMPLATES_FOLDER, 'newPackageTemplate'); |
69 | | - if (!(await fs.pathExists(newPackageTemplateFolder))) { |
70 | | - window.showErrorMessage('Template folder does not exist, aborting creation.'); |
71 | | - return undefined; |
72 | | - } |
73 | | - |
74 | | - // Check if the destination folder is provided, otherwise use the first workspace folder |
75 | | - let destRoot = options?.rootUri.fsPath; |
76 | | - if (!destRoot) { |
77 | | - const workspaceFolders = workspace.workspaceFolders; |
78 | | - if (!workspaceFolders || workspaceFolders.length === 0) { |
79 | | - window.showErrorMessage('No workspace folder is open or provided, aborting creation.'); |
| 97 | + // Check if the destination folder already exists |
| 98 | + const projectDestinationFolder = path.join(destRoot, `${packageName}_project`); |
| 99 | + if (await fs.pathExists(projectDestinationFolder)) { |
| 100 | + window.showErrorMessage( |
| 101 | + 'A project folder by that name already exists, aborting creation. Please retry with a unique package name given your workspace.', |
| 102 | + ); |
80 | 103 | return undefined; |
81 | 104 | } |
82 | | - destRoot = workspaceFolders[0].uri.fsPath; |
83 | | - } |
84 | | - |
85 | | - // Check if the destination folder already exists |
86 | | - const projectDestinationFolder = path.join(destRoot, `${packageName}_project`); |
87 | | - if (await fs.pathExists(projectDestinationFolder)) { |
88 | | - window.showErrorMessage( |
89 | | - 'A project folder by that name already exists, aborting creation. Please retry with a unique package name given your workspace.', |
90 | | - ); |
91 | | - return undefined; |
92 | | - } |
93 | | - await fs.copy(newPackageTemplateFolder, projectDestinationFolder); |
| 105 | + await fs.copy(newPackageTemplateFolder, projectDestinationFolder); |
94 | 106 |
|
95 | | - // 2. Replace 'package_name' in all files and file/folder names using a helper |
96 | | - await replaceInFilesAndNames(projectDestinationFolder, 'package_name', packageName); |
| 107 | + // 2. Replace 'package_name' in all files and file/folder names using a helper |
| 108 | + await replaceInFilesAndNames(projectDestinationFolder, 'package_name', packageName); |
97 | 109 |
|
98 | | - // 4. Create virtual environment if requested |
99 | | - if (createVenv) { |
100 | | - await quickCreateNewVenv(this.envManagers, projectDestinationFolder); |
101 | | - } |
| 110 | + // 4. Create virtual environment if requested |
| 111 | + if (createVenv) { |
| 112 | + await quickCreateNewVenv(this.envManagers, projectDestinationFolder); |
| 113 | + } |
102 | 114 |
|
103 | | - // 5. Get the Python environment for the destination folder |
104 | | - // could be either the one created in an early step or an existing one |
105 | | - const pythonEnvironment = await this.envManagers.getEnvironment(Uri.parse(projectDestinationFolder)); |
| 115 | + // 5. Get the Python environment for the destination folder |
| 116 | + // could be either the one created in an early step or an existing one |
| 117 | + const pythonEnvironment = await this.envManagers.getEnvironment(Uri.parse(projectDestinationFolder)); |
106 | 118 |
|
107 | | - if (!pythonEnvironment) { |
108 | | - window.showErrorMessage('Python environment not found.'); |
109 | | - return undefined; |
110 | | - } |
| 119 | + if (!pythonEnvironment) { |
| 120 | + window.showErrorMessage('Python environment not found.'); |
| 121 | + return undefined; |
| 122 | + } |
111 | 123 |
|
112 | | - // add custom github copilot instructions |
113 | | - if (createCopilotInstructions) { |
114 | | - const packageInstructionsPath = path.join( |
115 | | - NEW_PROJECT_TEMPLATES_FOLDER, |
116 | | - 'copilot-instructions-text', |
117 | | - 'package-copilot-instructions.md', |
118 | | - ); |
119 | | - await manageCopilotInstructionsFile(destRoot, packageName, packageInstructionsPath); |
120 | | - } |
| 124 | + // add custom github copilot instructions |
| 125 | + if (createCopilotInstructions) { |
| 126 | + const packageInstructionsPath = path.join( |
| 127 | + NEW_PROJECT_TEMPLATES_FOLDER, |
| 128 | + 'copilot-instructions-text', |
| 129 | + 'package-copilot-instructions.md', |
| 130 | + ); |
| 131 | + await manageCopilotInstructionsFile(destRoot, packageName, packageInstructionsPath); |
| 132 | + } |
121 | 133 |
|
122 | | - // update launch.json file with config for the package |
123 | | - const launchJsonConfig = { |
124 | | - name: `Python Package: ${packageName}`, |
125 | | - type: 'debugpy', |
126 | | - request: 'launch', |
127 | | - module: packageName, |
128 | | - }; |
129 | | - await manageLaunchJsonFile(destRoot, JSON.stringify(launchJsonConfig)); |
| 134 | + // update launch.json file with config for the package |
| 135 | + const launchJsonConfig = { |
| 136 | + name: `Python Package: ${packageName}`, |
| 137 | + type: 'debugpy', |
| 138 | + request: 'launch', |
| 139 | + module: packageName, |
| 140 | + }; |
| 141 | + await manageLaunchJsonFile(destRoot, JSON.stringify(launchJsonConfig)); |
130 | 142 |
|
131 | | - // Return a PythonProject OR Uri (if no venv was created) |
132 | | - return { |
133 | | - name: packageName, |
134 | | - uri: Uri.file(projectDestinationFolder), |
135 | | - }; |
| 143 | + // Return a PythonProject OR Uri (if no venv was created) |
| 144 | + return { |
| 145 | + name: packageName, |
| 146 | + uri: Uri.file(projectDestinationFolder), |
| 147 | + }; |
| 148 | + } |
136 | 149 | } |
137 | 150 | } |
0 commit comments