@@ -7,11 +7,11 @@ import {
7
7
promptForCopilotInstructions ,
8
8
isCopilotInstalled ,
9
9
quickCreateNewVenv ,
10
- removeCopilotInstructions ,
11
10
replaceInFilesAndNames ,
12
- replaceInFile ,
11
+ manageCopilotInstructionsFile ,
12
+ manageLaunchJsonFile ,
13
13
} from './creationHelpers' ;
14
- import { EXTENSION_ROOT_DIR } from '../../common/constants' ;
14
+ import { NEW_PROJECT_TEMPLATES_FOLDER } from '../../common/constants' ;
15
15
import { EnvironmentManagers } from '../../internal.api' ;
16
16
import { showInputBoxWithButtons } from '../../common/window.apis' ;
17
17
@@ -23,13 +23,16 @@ export class NewPackageProject implements PythonProjectCreator {
23
23
24
24
constructor ( private readonly envManagers : EnvironmentManagers ) { }
25
25
26
- async create ( _options ?: PythonProjectCreatorOptions ) : Promise < PythonProject | undefined > {
27
- // Prompt for package name (TODO: this doesn't make sense if the _options is already being passed in )
28
- const packageName = await showInputBoxWithButtons ( {
29
- prompt : 'What is the name of the package? (e.g. my_package)' ,
30
- ignoreFocusOut : true ,
31
- showBackButton : true ,
32
- } ) ;
26
+ async create ( options ?: PythonProjectCreatorOptions ) : Promise < PythonProject | undefined > {
27
+ // Prompt for package name if not provided
28
+ let packageName = options ?. name ;
29
+ if ( ! packageName ) {
30
+ packageName = await showInputBoxWithButtons ( {
31
+ prompt : 'What is the name of the package? (e.g. my_package)' ,
32
+ ignoreFocusOut : true ,
33
+ showBackButton : true ,
34
+ } ) ;
35
+ }
33
36
if ( ! packageName ) {
34
37
return undefined ;
35
38
}
@@ -55,102 +58,73 @@ export class NewPackageProject implements PythonProjectCreator {
55
58
) ;
56
59
57
60
// 1. Copy template folder
58
- const templateFolder = path . join (
59
- EXTENSION_ROOT_DIR ,
60
- 'src' ,
61
- 'features' ,
62
- 'creators' ,
63
- 'templates' ,
64
- 'newPackageTemplate' ,
65
- ) ;
66
- if ( ! ( await fs . pathExists ( templateFolder ) ) ) {
67
- window . showErrorMessage ( 'Template folder does not exist.' ) ;
61
+ const newPackageTemplateFolder = path . join ( NEW_PROJECT_TEMPLATES_FOLDER , 'newPackageTemplate' ) ;
62
+ if ( ! ( await fs . pathExists ( newPackageTemplateFolder ) ) ) {
63
+ window . showErrorMessage ( 'Template folder does not exist, aborting creation.' ) ;
68
64
return undefined ;
69
- // might need another check or error handling here
70
65
}
71
- const workspaceFolders = workspace . workspaceFolders ;
72
- if ( ! workspaceFolders || workspaceFolders . length === 0 ) {
73
- window . showErrorMessage ( 'No workspace folder is open.' ) ;
74
- return undefined ;
66
+
67
+ // Check if the destination folder is provided, otherwise use the first workspace folder
68
+ let destRoot = options ?. uri ?. fsPath ;
69
+ if ( ! destRoot ) {
70
+ const workspaceFolders = workspace . workspaceFolders ;
71
+ if ( ! workspaceFolders || workspaceFolders . length === 0 ) {
72
+ window . showErrorMessage ( 'No workspace folder is open or provided, aborting creation.' ) ;
73
+ return undefined ;
74
+ }
75
+ destRoot = workspaceFolders [ 0 ] . uri . fsPath ;
75
76
}
76
- const destRoot = workspaceFolders [ 0 ] . uri . fsPath ; // this doesn't seem right...
77
+
77
78
// Check if the destination folder already exists
78
- const destFolder = path . join ( destRoot , `${ packageName } _project` ) ;
79
- if ( await fs . pathExists ( destFolder ) ) {
80
- window . showErrorMessage ( 'A project folder by that name already exists, aborting.' ) ;
79
+ const projectDestinationFolder = path . join ( destRoot , `${ packageName } _project` ) ;
80
+ if ( await fs . pathExists ( projectDestinationFolder ) ) {
81
+ window . showErrorMessage (
82
+ 'A project folder by that name already exists, aborting creation. Please retry with a unique package name given your workspace.' ,
83
+ ) ;
81
84
return undefined ;
82
85
}
83
- await fs . copy ( templateFolder , destFolder ) ;
84
-
85
- // custom instructions
86
- const instructionsTextPath = path . join (
87
- EXTENSION_ROOT_DIR ,
88
- 'src' ,
89
- 'features' ,
90
- 'creators' ,
91
- 'templates' ,
92
- 'copilot-instructions-text' ,
93
- 'package-copilot-instructions.md' ,
94
- ) ;
95
- const instructionsText = `\n \n` + ( await fs . readFile ( instructionsTextPath , 'utf-8' ) ) ;
96
-
97
- // check to see if .github folder exists
98
- const githubFolderPath = path . join ( destRoot , '.github' ) ;
99
- const customInstructionsPath = path . join ( githubFolderPath , 'copilot-instructions.md' ) ;
100
- const ghFolder = await fs . pathExists ( githubFolderPath ) ;
101
- if ( ghFolder ) {
102
- const customInstructions = await fs . pathExists ( customInstructionsPath ) ;
103
- if ( customInstructions ) {
104
- // Append to the existing file
105
- await fs . appendFile ( customInstructionsPath , instructionsText ) ;
106
- } else {
107
- // Create the file if it doesn't exist
108
- await fs . writeFile ( customInstructionsPath , instructionsText ) ;
109
- }
110
- } else {
111
- // Create the .github folder and the file
112
- await fs . mkdir ( githubFolderPath ) ;
113
- await fs . writeFile ( customInstructionsPath , instructionsText ) ;
114
- }
86
+ await fs . copy ( newPackageTemplateFolder , projectDestinationFolder ) ;
115
87
116
- // 2. Replace <package_name> in all files and file/folder names using helper
117
- await replaceInFilesAndNames ( destFolder , 'package_name' , packageName ) ;
118
-
119
- // 3. Remove Copilot instructions folder if needed
120
- if ( ! createCopilotInstructions ) {
121
- await removeCopilotInstructions ( destFolder ) ;
122
- }
88
+ // 2. Replace 'package_name' in all files and file/folder names using a helper
89
+ await replaceInFilesAndNames ( projectDestinationFolder , 'package_name' , packageName ) ;
123
90
124
91
// 4. Create virtual environment if requested
125
92
if ( createVenv ) {
126
- await quickCreateNewVenv ( this . envManagers , destFolder ) ;
93
+ await quickCreateNewVenv ( this . envManagers , projectDestinationFolder ) ;
127
94
}
128
95
129
96
// 5. Get the Python environment for the destination folder
130
- // could be either the one created in step 4 or an existing one
131
- const pythonEnvironment = await this . envManagers . getEnvironment ( Uri . parse ( destFolder ) ) ;
97
+ // could be either the one created in an early step or an existing one
98
+ const pythonEnvironment = await this . envManagers . getEnvironment ( Uri . parse ( projectDestinationFolder ) ) ;
132
99
133
- // 6. Replace <run_exec> and <activation_command> in README.md
134
- // const readmeFilePath = path.join(destFolder, 'README.md');
135
100
if ( ! pythonEnvironment ) {
136
101
window . showErrorMessage ( 'Python environment not found.' ) ;
137
102
return undefined ;
138
103
}
139
- const execInfo = pythonEnvironment . execInfo ;
140
- if ( execInfo . run ) {
141
- // const { executable, args = [] } = execInfo.run;
142
- // const execRunStr = [executable, ...args].join(' ');
143
- // TODO: check this as I don't think I need this anymore
144
- await replaceInFile ( customInstructionsPath , '<package_name>' , packageName ) ;
104
+
105
+ // add custom github copilot instructions
106
+ if ( createCopilotInstructions ) {
107
+ const packageInstructionsPath = path . join (
108
+ NEW_PROJECT_TEMPLATES_FOLDER ,
109
+ 'copilot-instructions-text' ,
110
+ 'package-copilot-instructions.md' ,
111
+ ) ;
112
+ await manageCopilotInstructionsFile ( destRoot , packageName , packageInstructionsPath ) ;
145
113
}
146
114
147
- // TODO: insert copilot instructions text into the copilot instructions file
148
- // TODO: insert configs into existing launch.json file
115
+ // update launch.json file with config for the package
116
+ const launchJsonConfig = {
117
+ name : `Python Package: ${ packageName } ` ,
118
+ type : 'debugpy' ,
119
+ request : 'launch' ,
120
+ module : packageName ,
121
+ } ;
122
+ await manageLaunchJsonFile ( destRoot , JSON . stringify ( launchJsonConfig ) ) ;
149
123
150
124
// Return a PythonProject OR Uri (if no venv was created)
151
125
return {
152
126
name : packageName ,
153
- uri : Uri . file ( destFolder ) ,
127
+ uri : Uri . file ( projectDestinationFolder ) ,
154
128
} ;
155
129
}
156
130
}
0 commit comments