Skip to content

Commit

Permalink
Parse envFile and pass to debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianPfliegel committed Jul 23, 2018
1 parent 28645b2 commit 735a5f3
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,11 @@
"description": "Environment variables passed to the program.",
"default": {}
},
"envFile": {
"type": "string",
"description": "Environment variables passed to the program by a file.",
"default": "${workspaceFolder}/.env"
},
"console": {
"type": "string",
"enum": [
Expand Down Expand Up @@ -1876,6 +1881,11 @@
"description": "Environment variables passed to the program.",
"default": {}
},
"envFile": {
"type": "string",
"description": "Environment variables passed to the program by a file.",
"default": "${workspaceFolder}/.env"
},
"console": {
"type": "string",
"enum": [
Expand Down
68 changes: 68 additions & 0 deletions src/configurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,79 @@ export class CSharpConfigurationProvider implements vscode.DebugConfigurationPro
});
}

/**
* Parse envFile and add to config.env
*/
private parseEnvFile(envFile: string, config: vscode.DebugConfiguration): vscode.DebugConfiguration {
if(envFile) {
try {
let content: string = fs.readFileSync(envFile, "utf8");
let parseErrors: string[] = [];

// Remove UTF-8 BOM if present
if(content.charAt(0) === '\uFEFF') {
content = content.substr(1);
}

content.split("\n").forEach(line => {
const r: RegExpMatchArray = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);

if (r !== null) {
const key: string = r[1];
let value: string = r[2] || "";
if ((value.length > 0) && (value.charAt(0) === '"') && (value.charAt(value.length - 1) === '"')) {
value = value.replace(/\\n/gm, "\n");
}

value = value.replace(/(^['"]|['"]$)/g, "");

if(!config.env) {
config.env = {};
}
config.env[key] = value;
}
else {
// Blank lines and lines starting with # are no parse errors
const comments: RegExp = new RegExp(/^\s*(#|$)/);
if (!comments.test(line)) {
parseErrors.push(line);
}
}
});

// show error message if single lines cannot get parsed
if(parseErrors.length !== 0) {
let parseError: string = "Ignoring non-parseable lines in envFile " + envFile + ": ";
parseErrors.forEach(function (value, idx, array) {
parseError += "\"" + value + "\"" + ((idx !== array.length - 1) ? ", " : ".");
});
showErrorMessage(vscode, parseError);
}
}
catch (e) {
throw new Error("Can't parse envFile " + envFile);
}
}

// remove envFile from config after parsing
if(config.envFile) {
delete config.envFile;
}

return config;
}

/**
* Try to add all missing attributes to the debug configuration being launched.
*/
resolveDebugConfiguration(folder: vscode.WorkspaceFolder | undefined, config: vscode.DebugConfiguration, token?: vscode.CancellationToken): vscode.ProviderResult<vscode.DebugConfiguration> {
// vsdbg does the error checking

// read from envFile and set config.env
if(config.envFile) {
config = this.parseEnvFile(config.envFile.replace(/\${workspaceFolder}/g, folder.uri.path), config);
}

return config;
}
}
5 changes: 5 additions & 0 deletions src/tools/OptionsSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@
"description": "Environment variables passed to the program.",
"default": {}
},
"envFile": {
"type": "string",
"description": "Environment variables passed to the program by a file.",
"default": "${workspaceFolder}/.env"
},
"console": {
"type": "string",
"enum": [ "internalConsole", "integratedTerminal", "externalTerminal" ],
Expand Down

0 comments on commit 735a5f3

Please sign in to comment.