Skip to content

Commit 1d2a93e

Browse files
committed
minor edits
1 parent aa65199 commit 1d2a93e

2 files changed

Lines changed: 37 additions & 16 deletions

File tree

src/features/creators/creationHelpers.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { CreateEnvironmentOptions, EnvironmentManager } from '../../api';
66
import { traceVerbose } from '../../common/logging';
77

88
/**
9-
* Prompts the user to choose whether to create a new venv for a package.
10-
* Returns true if the user selects 'Yes', false if 'No', and undefined if cancelled.
9+
* Prompts the user to choose whether to create a new virtual environment (venv) for a package.
10+
* @returns {Promise<boolean | undefined>} Resolves to true if 'Yes' is selected, false if 'No', or undefined if cancelled.
1111
*/
1212
export async function promptForVenv(): Promise<boolean | undefined> {
1313
const venvChoice = await window.showQuickPick(['Yes', 'No'], {
@@ -21,15 +21,16 @@ export async function promptForVenv(): Promise<boolean | undefined> {
2121
}
2222

2323
/**
24-
* Returns true if GitHub Copilot extension is installed, false otherwise.
24+
* Checks if the GitHub Copilot extension is installed in the current VS Code environment.
25+
* @returns {boolean} True if Copilot is installed, false otherwise.
2526
*/
2627
export function isCopilotInstalled(): boolean {
2728
return !!extensions.getExtension('GitHub.copilot');
2829
}
2930

3031
/**
3132
* Prompts the user to choose whether to create a Copilot instructions file, only if Copilot is installed.
32-
* Returns true if the user selects 'Yes', false if 'No', and undefined if cancelled or Copilot not installed.
33+
* @returns {Promise<boolean | undefined>} Resolves to true if 'Yes' is selected, false if 'No', or undefined if cancelled or Copilot is not installed.
3334
*/
3435
export async function promptForCopilotInstructions(): Promise<boolean | undefined> {
3536
if (!isCopilotInstalled()) {
@@ -45,13 +46,25 @@ export async function promptForCopilotInstructions(): Promise<boolean | undefine
4546
return copilotChoice === 'Yes';
4647
}
4748

49+
/**
50+
* Removes the .github Copilot instructions folder from the specified destination folder, if it exists.
51+
* @param destFolder - The absolute path to the destination folder where the .github folder may exist.
52+
* @returns {Promise<void>} Resolves when the folder is removed or if it does not exist.
53+
*/
4854
export async function removeCopilotInstructions(destFolder: string) {
4955
const copilotFolder = path.join(destFolder, '.github');
5056
if (await fs.pathExists(copilotFolder)) {
5157
await fs.remove(copilotFolder);
5258
}
5359
}
5460

61+
/**
62+
* Quickly creates a new Python virtual environment (venv) in the specified destination folder using the available environment managers.
63+
* Attempts to use the venv manager if available, otherwise falls back to any manager that supports environment creation.
64+
* @param envManagers - The collection of available environment managers.
65+
* @param destFolder - The absolute path to the destination folder where the environment should be created.
66+
* @returns {Promise<void>} Resolves when the environment is created or an error is shown.
67+
*/
5568
export async function quickCreateNewVenv(envManagers: EnvironmentManagers, destFolder: string) {
5669
try {
5770
// get the environment manager for venv
@@ -86,10 +99,11 @@ export async function quickCreateNewVenv(envManagers: EnvironmentManagers, destF
8699
}
87100

88101
/**
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.
102+
* Replaces all occurrences of a string in a single file's contents, safely handling special regex characters in the search value.
103+
* @param filePath - The absolute path to the file to update.
104+
* @param searchValue - The string to search for (will be escaped for regex).
105+
* @param replaceValue - The string to replace with.
106+
* @returns {Promise<void>} Resolves when the file has been updated.
93107
*/
94108
export async function replaceInFile(filePath: string, searchValue: string, replaceValue: string) {
95109
// Escape special regex characters in searchValue
@@ -102,7 +116,13 @@ export async function replaceInFile(filePath: string, searchValue: string, repla
102116
}
103117
}
104118

105-
// Helper to recursively replace all occurrences of a string in file/folder names and file contents
119+
/**
120+
* Recursively replaces all occurrences of a string in file and folder names, as well as file contents, within a directory tree.
121+
* @param dir - The root directory to start the replacement from.
122+
* @param searchValue - The string to search for in names and contents.
123+
* @param replaceValue - The string to replace with.
124+
* @returns {Promise<void>} Resolves when all replacements are complete.
125+
*/
106126
export async function replaceInFilesAndNames(dir: string, searchValue: string, replaceValue: string) {
107127
const entries = await fs.readdir(dir, { withFileTypes: true });
108128
for (const entry of entries) {

src/features/creators/newPackageProject.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class NewPackageProject implements PythonProjectCreator {
7171
window.showErrorMessage('No workspace folder is open.');
7272
return undefined;
7373
}
74-
const destRoot = workspaceFolders[0].uri.fsPath;
74+
const destRoot = workspaceFolders[0].uri.fsPath; // this doesn't seem right...
7575
const destFolder = path.join(destRoot, `${packageName}_project`);
7676
await fs.copy(templateFolder, destFolder);
7777

@@ -88,19 +88,20 @@ export class NewPackageProject implements PythonProjectCreator {
8888
await quickCreateNewVenv(this.envManagers, destFolder);
8989
}
9090

91-
// 5. Replace <run_exec> and <activation_command> in README.md
92-
const readmeFilePath = path.join(destFolder, 'README.md');
91+
// 5. Get the Python environment for the destination folder
92+
// could be either the one created in step 4 or an existing one
9393
const pythonEnvironment = await this.envManagers.getEnvironment(Uri.parse(destFolder));
94+
95+
// 6. Replace <run_exec> and <activation_command> in README.md
96+
const readmeFilePath = path.join(destFolder, 'README.md');
9497
if (!pythonEnvironment) {
9598
window.showErrorMessage('Python environment not found.');
9699
return undefined;
97100
}
98101
const execInfo = pythonEnvironment.execInfo;
99102
if (execInfo.run) {
100-
let execRunStr = execInfo.run.executable;
101-
if (execInfo.run.args) {
102-
execRunStr += ` ${execInfo.run.args.join(' ')}`;
103-
}
103+
const { executable, args = [] } = execInfo.run;
104+
const execRunStr = [executable, ...args].join(' ');
104105
await replaceInFile(readmeFilePath, '<run_exec>', execRunStr);
105106
}
106107
// TODO: replace <activation_command> in README.md ?

0 commit comments

Comments
 (0)