|
1 | | -import { MarkdownString, window } from 'vscode'; |
| 1 | +import * as fs from 'fs-extra'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { commands, l10n, MarkdownString, QuickInputButtons, Uri, window, workspace } from 'vscode'; |
2 | 4 | import { PythonProject, PythonProjectCreator, PythonProjectCreatorOptions } from '../../api'; |
| 5 | +import { NEW_PROJECT_TEMPLATES_FOLDER } from '../../common/constants'; |
| 6 | +import { showInputBoxWithButtons } from '../../common/window.apis'; |
| 7 | +import { EnvironmentManagers, PythonProjectManager } from '../../internal.api'; |
| 8 | +import { isCopilotInstalled, manageCopilotInstructionsFile, replaceInFilesAndNames } from './creationHelpers'; |
3 | 9 |
|
4 | 10 | export class NewScriptProject implements PythonProjectCreator { |
5 | | - public readonly name = 'newScript'; |
6 | | - public readonly displayName = 'Project'; |
7 | | - public readonly description = 'Create a new Python project'; |
8 | | - public readonly tooltip = new MarkdownString('Create a new Python project'); |
| 11 | + public readonly name = l10n.t('newScript'); |
| 12 | + public readonly displayName = l10n.t('Script'); |
| 13 | + public readonly description = l10n.t('Creates a new script folder in your current workspace with PEP 723 support'); |
| 14 | + public readonly tooltip = new MarkdownString(l10n.t('Create a new Python script')); |
9 | 15 |
|
10 | | - constructor() {} |
| 16 | + constructor( |
| 17 | + private readonly envManagers: EnvironmentManagers, |
| 18 | + private readonly projectManager: PythonProjectManager, |
| 19 | + ) {} |
11 | 20 |
|
12 | | - async create(_options?: PythonProjectCreatorOptions): Promise<PythonProject | undefined> { |
13 | | - // show notification that the script creation was selected than return undefined |
14 | | - window.showInformationMessage('Creating a new Python project...'); |
| 21 | + async create(options?: PythonProjectCreatorOptions): Promise<PythonProject | Uri | undefined> { |
| 22 | + // quick create (needs name, will always create venv and copilot instructions) |
| 23 | + // not quick create |
| 24 | + // ask for script file name |
| 25 | + // ask if they want venv |
| 26 | + let scriptFileName = options?.name; |
| 27 | + let createCopilotInstructions: boolean | undefined; |
| 28 | + if (options?.quickCreate === true) { |
| 29 | + // If quickCreate is true, we should not prompt for any input |
| 30 | + if (!scriptFileName) { |
| 31 | + throw new Error('Script file name is required in quickCreate mode.'); |
| 32 | + } |
| 33 | + createCopilotInstructions = true; |
| 34 | + } else { |
| 35 | + //Prompt as quickCreate is false |
| 36 | + if (!scriptFileName) { |
| 37 | + try { |
| 38 | + scriptFileName = await showInputBoxWithButtons({ |
| 39 | + prompt: l10n.t('What is the name of the script? (e.g. my_script.py)'), |
| 40 | + ignoreFocusOut: true, |
| 41 | + showBackButton: true, |
| 42 | + validateInput: (value) => { |
| 43 | + // Ensure the filename ends with .py and follows valid naming conventions |
| 44 | + if (!value.endsWith('.py')) { |
| 45 | + return l10n.t('Script name must end with ".py".'); |
| 46 | + } |
| 47 | + const baseName = value.replace(/\.py$/, ''); |
| 48 | + // following PyPI (PEP 508) rules for package names |
| 49 | + if (!/^([a-z_]|[a-z0-9_][a-z0-9._-]*[a-z0-9_])$/i.test(baseName)) { |
| 50 | + return l10n.t( |
| 51 | + 'Invalid script name. Use only letters, numbers, underscores, hyphens, or periods. Must start and end with a letter or number.', |
| 52 | + ); |
| 53 | + } |
| 54 | + if (/^[-._0-9]$/i.test(baseName)) { |
| 55 | + return l10n.t('Single-character script names cannot be a number, hyphen, or period.'); |
| 56 | + } |
| 57 | + return null; |
| 58 | + }, |
| 59 | + }); |
| 60 | + } catch (ex) { |
| 61 | + if (ex === QuickInputButtons.Back) { |
| 62 | + await commands.executeCommand('python-envs.createNewProjectFromTemplate'); |
| 63 | + } |
| 64 | + } |
| 65 | + if (!scriptFileName) { |
| 66 | + return undefined; |
| 67 | + } |
| 68 | + if (isCopilotInstalled()) { |
| 69 | + createCopilotInstructions = true; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // 1. Copy template folder |
| 74 | + const newScriptTemplateFile = path.join(NEW_PROJECT_TEMPLATES_FOLDER, 'new723ScriptTemplate', 'script.py'); |
| 75 | + if (!(await fs.pathExists(newScriptTemplateFile))) { |
| 76 | + window.showErrorMessage(l10n.t('Template file does not exist, aborting creation.')); |
| 77 | + return undefined; |
| 78 | + } |
| 79 | + |
| 80 | + // Check if the destination folder is provided, otherwise use the first workspace folder |
| 81 | + let destRoot = options?.rootUri.fsPath; |
| 82 | + if (!destRoot) { |
| 83 | + const workspaceFolders = workspace.workspaceFolders; |
| 84 | + if (!workspaceFolders || workspaceFolders.length === 0) { |
| 85 | + window.showErrorMessage(l10n.t('No workspace folder is open or provided, aborting creation.')); |
| 86 | + return undefined; |
| 87 | + } |
| 88 | + destRoot = workspaceFolders[0].uri.fsPath; |
| 89 | + } |
| 90 | + |
| 91 | + // Check if the destination folder already exists |
| 92 | + const scriptDestination = path.join(destRoot, scriptFileName); |
| 93 | + if (await fs.pathExists(scriptDestination)) { |
| 94 | + window.showErrorMessage( |
| 95 | + l10n.t( |
| 96 | + 'A script file by that name already exists, aborting creation. Please retry with a unique script name given your workspace.', |
| 97 | + ), |
| 98 | + ); |
| 99 | + return undefined; |
| 100 | + } |
| 101 | + await fs.copy(newScriptTemplateFile, scriptDestination); |
| 102 | + |
| 103 | + // 2. Replace 'script_name' in the file using a helper (just script name remove .py) |
| 104 | + await replaceInFilesAndNames(scriptDestination, 'script_name', scriptFileName.replace(/\.py$/, '')); |
| 105 | + |
| 106 | + // 3. add custom github copilot instructions |
| 107 | + if (createCopilotInstructions) { |
| 108 | + const packageInstructionsPath = path.join( |
| 109 | + NEW_PROJECT_TEMPLATES_FOLDER, |
| 110 | + 'copilot-instructions-text', |
| 111 | + 'script-copilot-instructions.md', |
| 112 | + ); |
| 113 | + await manageCopilotInstructionsFile(destRoot, packageInstructionsPath, [ |
| 114 | + { searchValue: '<script_name>', replaceValue: scriptFileName }, |
| 115 | + ]); |
| 116 | + } |
| 117 | + |
| 118 | + return Uri.file(scriptDestination); |
| 119 | + } |
15 | 120 | return undefined; |
16 | 121 | } |
17 | 122 | } |
0 commit comments