Skip to content

Commit

Permalink
add support for envFile
Browse files Browse the repository at this point in the history
fix #258
  • Loading branch information
GitMensch committed May 10, 2022
1 parent db00740 commit 79c9a8c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 23 deletions.
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@
"description": "Environment overriding the gdb (and in turn also the process) environment",
"default": null
},
"envFile": {
"type": "string",
"description": "path to a file containing environment variable definitions",
"default": null
},
"debugger_args": {
"type": "array",
"description": "Additional arguments to pass to GDB",
Expand Down Expand Up @@ -342,6 +347,11 @@
"description": "Environment overriding the gdb (and in turn also the process) environment",
"default": null
},
"envFile": {
"type": "string",
"description": "path to a file containing environment variable definitions",
"default": null
},
"debugger_args": {
"type": "array",
"description": "Additional arguments to pass to GDB",
Expand Down Expand Up @@ -629,6 +639,11 @@
"description": "Environment overriding the lldb-mi (and in turn also the process) environment",
"default": null
},
"envFile": {
"type": "string",
"description": "path to a file containing environment variable definitions",
"default": null
},
"debugger_args": {
"type": "array",
"description": "Additional arguments to pass to LLDB",
Expand Down Expand Up @@ -797,6 +812,11 @@
"description": "Environment overriding the lldb-mi (and in turn also the process) environment",
"default": null
},
"envFile": {
"type": "string",
"description": "path to a file containing environment variable definitions",
"default": null
},
"debugger_args": {
"type": "array",
"description": "Additional arguments to pass to LLDB",
Expand Down Expand Up @@ -948,6 +968,11 @@
"description": "Environment overriding the mago-mi (and in turn also the process) environment",
"default": null
},
"envFile": {
"type": "string",
"description": "path to a file containing environment variable definitions",
"default": null
},
"debugger_args": {
"type": "array",
"description": "Additional arguments to pass to mago",
Expand Down Expand Up @@ -1023,6 +1048,11 @@
"description": "Environment overriding the mago-mi (and in turn also the process) environment",
"default": null
},
"envFile": {
"type": "string",
"description": "path to a file containing environment variable definitions",
"default": null
},
"debugger_args": {
"type": "array",
"description": "Additional arguments to pass to mago",
Expand Down
66 changes: 49 additions & 17 deletions src/backend/mi2/mi2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,30 @@ function couldBeOutput(line: string) {
const trace = false;

export class MI2 extends EventEmitter implements IBackend {
constructor(public application: string, public preargs: string[], public extraargs: string[], procEnv: any, public extraCommands: string[] = []) {
constructor(public application: string, public preargs: string[], public extraargs: string[], procEnv: any, envFile: string, public extraCommands: string[] = []) {
super();

if (!(envFile || procEnv))
return;

// FIXME: if actuall debugged process is Win32, which we only know _after_ connect,
// then the keys must be read via hashmap using only upper-case for hashing
// ... or entered/compared in upper-case --> possibly postpone this
const env = {};
// Duplicate process.env so we don't override it
for (const key in process.env)
if (process.env.hasOwnProperty(key))
env[key] = process.env[key];

// Overwrite with user specified variables
if (envFile) {
mergeEnv(env, readEnvFile(envFile));
}
if (procEnv) {
const env = {};
// Duplicate process.env so we don't override it
for (const key in process.env)
if (process.env.hasOwnProperty(key))
env[key] = process.env[key];

// Overwrite with user specified variables
for (const key in procEnv) {
if (procEnv.hasOwnProperty(key)) {
if (procEnv === undefined)
delete env[key];
else
env[key] = procEnv[key];
}
}
this.procEnv = env;
mergeEnv(env, procEnv);
}

this.procEnv = env;
}

load(cwd: string, target: string, procArgs: string, separateConsole: string): Thenable<any> {
Expand Down Expand Up @@ -886,3 +889,32 @@ export class MI2 extends EventEmitter implements IBackend {
protected stream;
protected sshConn;
}
function readEnvFile(filename: string): { [key: string]: string } {
const env = {};
if (!fs.existsSync(filename)) {
return env;
}
const buffer = fs.readFileSync(filename, 'utf8');
buffer.split('\n').forEach( line => {
// using a match which will automatically ignore "wrong" lines including
// lines that are empty or start with a "#", or //, or ...
const m = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
if (m != undefined) {
env[m[1]] = m[2] || '';
}
});

throw new Error("Function not implemented.");
}

function mergeEnv(env: {}, buffer: { [key: string]: string; }) {
for (const key in buffer) {
if (buffer.hasOwnProperty(key)) {
if (buffer === undefined)
delete env[key];
else
env[key] = buffer[key];
}
}
}

6 changes: 4 additions & 2 deletions src/gdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArgum
target: string;
gdbpath: string;
env: any;
envFile: string;
debugger_args: string[];
pathSubstitutions: { [index: string]: string };
arguments: string;
Expand All @@ -26,6 +27,7 @@ export interface AttachRequestArguments extends DebugProtocol.AttachRequestArgum
target: string;
gdbpath: string;
env: any;
envFile: string;
debugger_args: string[];
pathSubstitutions: { [index: string]: string };
executable: string;
Expand Down Expand Up @@ -53,7 +55,7 @@ class GDBDebugSession extends MI2DebugSession {
}

protected launchRequest(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): void {
this.miDebugger = new MI2(args.gdbpath || "gdb", ["-q", "--interpreter=mi2"], args.debugger_args, args.env);
this.miDebugger = new MI2(args.gdbpath || "gdb", ["-q", "--interpreter=mi2"], args.debugger_args, args.env, args.envFile);
this.setPathSubstitutions(args.pathSubstitutions);
this.initDebugger();
this.quit = false;
Expand Down Expand Up @@ -102,7 +104,7 @@ class GDBDebugSession extends MI2DebugSession {
}

protected attachRequest(response: DebugProtocol.AttachResponse, args: AttachRequestArguments): void {
this.miDebugger = new MI2(args.gdbpath || "gdb", ["-q", "--interpreter=mi2"], args.debugger_args, args.env);
this.miDebugger = new MI2(args.gdbpath || "gdb", ["-q", "--interpreter=mi2"], args.debugger_args, args.env, args.envFile);
this.setPathSubstitutions(args.pathSubstitutions);
this.initDebugger();
this.quit = false;
Expand Down
6 changes: 4 additions & 2 deletions src/lldb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArgum
target: string;
lldbmipath: string;
env: any;
envFile: string;
debugger_args: string[];
pathSubstitutions: { [index: string]: string };
arguments: string;
Expand All @@ -25,6 +26,7 @@ export interface AttachRequestArguments extends DebugProtocol.AttachRequestArgum
target: string;
lldbmipath: string;
env: any;
envFile: string;
debugger_args: string[];
pathSubstitutions: { [index: string]: string };
executable: string;
Expand All @@ -48,7 +50,7 @@ class LLDBDebugSession extends MI2DebugSession {
}

protected launchRequest(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): void {
this.miDebugger = new MI2_LLDB(args.lldbmipath || "lldb-mi", [], args.debugger_args, args.env);
this.miDebugger = new MI2_LLDB(args.lldbmipath || "lldb-mi", [], args.debugger_args, args.env, args.envFile);
this.setPathSubstitutions(args.pathSubstitutions);
this.initDebugger();
this.quit = false;
Expand Down Expand Up @@ -93,7 +95,7 @@ class LLDBDebugSession extends MI2DebugSession {
}

protected attachRequest(response: DebugProtocol.AttachResponse, args: AttachRequestArguments): void {
this.miDebugger = new MI2_LLDB(args.lldbmipath || "lldb-mi", [], args.debugger_args, args.env);
this.miDebugger = new MI2_LLDB(args.lldbmipath || "lldb-mi", [], args.debugger_args, args.env, args.envFile);
this.setPathSubstitutions(args.pathSubstitutions);
this.initDebugger();
this.quit = false;
Expand Down
6 changes: 4 additions & 2 deletions src/mago.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArgum
target: string;
magomipath: string;
env: any;
envFile: string;
debugger_args: string[];
arguments: string;
autorun: string[];
Expand All @@ -22,6 +23,7 @@ export interface AttachRequestArguments extends DebugProtocol.AttachRequestArgum
target: string;
magomipath: string;
env: any;
envFile: string;
debugger_args: string[];
executable: string;
autorun: string[];
Expand Down Expand Up @@ -50,7 +52,7 @@ class MagoDebugSession extends MI2DebugSession {
}

protected launchRequest(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): void {
this.miDebugger = new MI2_Mago(args.magomipath || "mago-mi", ["-q"], args.debugger_args, args.env);
this.miDebugger = new MI2_Mago(args.magomipath || "mago-mi", ["-q"], args.debugger_args, args.env, args.envFile);
this.initDebugger();
this.quit = false;
this.attached = false;
Expand All @@ -71,7 +73,7 @@ class MagoDebugSession extends MI2DebugSession {
}

protected attachRequest(response: DebugProtocol.AttachResponse, args: AttachRequestArguments): void {
this.miDebugger = new MI2_Mago(args.magomipath || "mago-mi", [], args.debugger_args, args.env);
this.miDebugger = new MI2_Mago(args.magomipath || "mago-mi", [], args.debugger_args, args.env, args.envFile);
this.initDebugger();
this.quit = false;
this.attached = true;
Expand Down

0 comments on commit 79c9a8c

Please sign in to comment.