Skip to content

Commit

Permalink
Moved replacePlaceholders closer to caller
Browse files Browse the repository at this point in the history
  • Loading branch information
sisoe24 committed Jul 7, 2024
1 parent eb471e9 commit 524bd88
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions src/launch_executable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,6 @@ export class ExecutablePath {
}
}

/**
* Replace placeholders in a string with their corresponding values.
*
* @param value The string to replace placeholders in
* @returns The string with placeholders replaced
*/
function replacePlaceholders(value: string): string {
let workspaceFolder = vscode.workspace.workspaceFolders?.[0].uri.fsPath || "";

// on windows we need to convert the path to a unix-like path
if (IS_WINDOWS && isUnixShell()) {
workspaceFolder = workspaceFolder.replace(/\\/g, "/");
// Convert the drive letter to lowercase and add a leading slash (e.g. C: -> /c)
workspaceFolder = workspaceFolder.replace(/^([a-zA-Z]):/, (_, driveLetter) => {
return `/${driveLetter.toLowerCase()}`;
});
}

// always escape the backslashes in the placeholder
const placeholders = {
"\\$\\{workspaceFolder\\}": workspaceFolder,
};

for (const [placeholder, replacement] of Object.entries(placeholders)) {
value = value.replace(new RegExp(placeholder, "g"), replacement);
}

return value;
}

/**
* Concatenate the user's environment variables with the system's environment variables.
*
Expand Down Expand Up @@ -146,6 +116,36 @@ function stringifyEnv(env: EnvVars): string {
return envString;
}

/**
* Replace placeholders in a string with their corresponding values.
*
* @param value The string to replace placeholders in
* @returns The string with placeholders replaced
*/
function replacePlaceholders(value: string): string {
let workspaceFolder = vscode.workspace.workspaceFolders?.[0].uri.fsPath || "";

// on windows we need to convert the path to a unix-like path
if (IS_WINDOWS && isUnixShell()) {
workspaceFolder = workspaceFolder.replace(/\\/g, "/");
// Convert the drive letter to lowercase and add a leading slash (e.g. C: -> /c)
workspaceFolder = workspaceFolder.replace(/^([a-zA-Z]):/, (_, driveLetter) => {
return `/${driveLetter.toLowerCase()}`;
});
}

// always escape the backslashes in the placeholder
const placeholders = {
"\\$\\{workspaceFolder\\}": workspaceFolder,
};

for (const [placeholder, replacement] of Object.entries(placeholders)) {
value = value.replace(new RegExp(placeholder, "g"), replacement);
}

return value;
}

/**
* Execute the command in the terminal. Before executing the command, if restartInstance
* is enabled, will dispose of the previous terminal instance.
Expand Down

0 comments on commit 524bd88

Please sign in to comment.