-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature predefined and environment variables in config (#655)
* Support workspaceFolder variable in configPath Adds support for predefined VS Code variable "${workspaceFolder}" to setting "configPath" in MPE. This allows for configuring MPE per-workspace if needed. fixes #625 * Support environment variables in configPath setting "configPath" does not support environment variables. Extended to support windows style %ENV_VAR% as well as bash style ${ENV_VAR} and $ENV_VAR environment variables fixes #625 * Refactoring to allow any string to resolve variables Implementation only applies to configPath and polutes the code. Refactors that code and extracts it into a PathResolver class. fixes #625
- Loading branch information
1 parent
eb20c2c
commit 2f71342
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import * as vscode from "vscode"; | ||
|
||
export class PathResolver { | ||
/** | ||
* resolve placeholder in string | ||
*/ | ||
public static resolvePath(path: string) { | ||
let result = path; | ||
|
||
// guard: empty | ||
if (result.trim() == "") { | ||
return path; | ||
} | ||
|
||
// resolve VS Code predefined variables | ||
result = this.resolve_vscode_predefined_variables(result); | ||
|
||
// resolve environment variables | ||
result = this.resolve_environment_variable(result); | ||
|
||
// done | ||
return result; | ||
} | ||
|
||
/** | ||
* resolve vscode predefined variables in string | ||
*/ | ||
public static resolve_vscode_predefined_variables(path: string) { | ||
let result = path; | ||
|
||
// ... workspaceFolder | ||
result = this.resolve_vscode_predefined_variable_workspaceFolder(result); | ||
|
||
// done | ||
return result; | ||
} | ||
|
||
/** | ||
* resolve vscode predefined variables in string | ||
*/ | ||
public static resolve_vscode_predefined_variable_workspaceFolder( | ||
path: string, | ||
) { | ||
let result = path; | ||
|
||
// guard: nothing to do | ||
if (!path.includes("${workspaceFolder}")) return path; | ||
|
||
// guard: error | ||
if (vscode.workspace.workspaceFolders == undefined) { | ||
vscode.window.showErrorMessage( | ||
"Working folder not found, open a folder an try again", | ||
); | ||
|
||
return path; | ||
} | ||
|
||
// determine workspace folder | ||
// let workspaceFolder = vscode.workspace.workspaceFolders[0].uri.path ; | ||
let workspaceFolder = vscode.workspace.workspaceFolders[0].uri.fsPath; | ||
|
||
// replace in configPath | ||
result = path.replace("${workspaceFolder}", workspaceFolder); | ||
|
||
// log | ||
//vscode.window.showInformationMessage( | ||
// `Resolved '${this}' to '${result}'` | ||
//); | ||
|
||
// done | ||
return result; | ||
} | ||
|
||
/** | ||
* resolve environment variables in string | ||
*/ | ||
public static resolve_environment_variable(path: string) { | ||
// try to replace environment variables for windows (%ENV_VAR%) and bash (${ENV_VAR} as well as $ENV_VAR) | ||
// line as suggested in https://stackoverflow.com/questions/21363912/how-to-resolve-a-path-that-includes-an-environment-variable-in-nodejs | ||
return path.replace( | ||
/%([A-Z_]+[A-Z0-9_]*)%|\$([A-Z_]+[A-Z0-9_]*)|\${([A-Z0-9_]*)}/gi, | ||
(_, windows, bash1, bash2) => process.env[windows || bash1 || bash2], | ||
); | ||
} | ||
} |