Skip to content

Commit 79c9a8c

Browse files
committed
add support for envFile
fix #258
1 parent db00740 commit 79c9a8c

File tree

5 files changed

+91
-23
lines changed

5 files changed

+91
-23
lines changed

package.json

+30
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@
168168
"description": "Environment overriding the gdb (and in turn also the process) environment",
169169
"default": null
170170
},
171+
"envFile": {
172+
"type": "string",
173+
"description": "path to a file containing environment variable definitions",
174+
"default": null
175+
},
171176
"debugger_args": {
172177
"type": "array",
173178
"description": "Additional arguments to pass to GDB",
@@ -342,6 +347,11 @@
342347
"description": "Environment overriding the gdb (and in turn also the process) environment",
343348
"default": null
344349
},
350+
"envFile": {
351+
"type": "string",
352+
"description": "path to a file containing environment variable definitions",
353+
"default": null
354+
},
345355
"debugger_args": {
346356
"type": "array",
347357
"description": "Additional arguments to pass to GDB",
@@ -629,6 +639,11 @@
629639
"description": "Environment overriding the lldb-mi (and in turn also the process) environment",
630640
"default": null
631641
},
642+
"envFile": {
643+
"type": "string",
644+
"description": "path to a file containing environment variable definitions",
645+
"default": null
646+
},
632647
"debugger_args": {
633648
"type": "array",
634649
"description": "Additional arguments to pass to LLDB",
@@ -797,6 +812,11 @@
797812
"description": "Environment overriding the lldb-mi (and in turn also the process) environment",
798813
"default": null
799814
},
815+
"envFile": {
816+
"type": "string",
817+
"description": "path to a file containing environment variable definitions",
818+
"default": null
819+
},
800820
"debugger_args": {
801821
"type": "array",
802822
"description": "Additional arguments to pass to LLDB",
@@ -948,6 +968,11 @@
948968
"description": "Environment overriding the mago-mi (and in turn also the process) environment",
949969
"default": null
950970
},
971+
"envFile": {
972+
"type": "string",
973+
"description": "path to a file containing environment variable definitions",
974+
"default": null
975+
},
951976
"debugger_args": {
952977
"type": "array",
953978
"description": "Additional arguments to pass to mago",
@@ -1023,6 +1048,11 @@
10231048
"description": "Environment overriding the mago-mi (and in turn also the process) environment",
10241049
"default": null
10251050
},
1051+
"envFile": {
1052+
"type": "string",
1053+
"description": "path to a file containing environment variable definitions",
1054+
"default": null
1055+
},
10261056
"debugger_args": {
10271057
"type": "array",
10281058
"description": "Additional arguments to pass to mago",

src/backend/mi2/mi2.ts

+49-17
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,30 @@ function couldBeOutput(line: string) {
2525
const trace = false;
2626

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

31+
if (!(envFile || procEnv))
32+
return;
33+
34+
// FIXME: if actuall debugged process is Win32, which we only know _after_ connect,
35+
// then the keys must be read via hashmap using only upper-case for hashing
36+
// ... or entered/compared in upper-case --> possibly postpone this
37+
const env = {};
38+
// Duplicate process.env so we don't override it
39+
for (const key in process.env)
40+
if (process.env.hasOwnProperty(key))
41+
env[key] = process.env[key];
42+
43+
// Overwrite with user specified variables
44+
if (envFile) {
45+
mergeEnv(env, readEnvFile(envFile));
46+
}
3147
if (procEnv) {
32-
const env = {};
33-
// Duplicate process.env so we don't override it
34-
for (const key in process.env)
35-
if (process.env.hasOwnProperty(key))
36-
env[key] = process.env[key];
37-
38-
// Overwrite with user specified variables
39-
for (const key in procEnv) {
40-
if (procEnv.hasOwnProperty(key)) {
41-
if (procEnv === undefined)
42-
delete env[key];
43-
else
44-
env[key] = procEnv[key];
45-
}
46-
}
47-
this.procEnv = env;
48+
mergeEnv(env, procEnv);
4849
}
50+
51+
this.procEnv = env;
4952
}
5053

5154
load(cwd: string, target: string, procArgs: string, separateConsole: string): Thenable<any> {
@@ -886,3 +889,32 @@ export class MI2 extends EventEmitter implements IBackend {
886889
protected stream;
887890
protected sshConn;
888891
}
892+
function readEnvFile(filename: string): { [key: string]: string } {
893+
const env = {};
894+
if (!fs.existsSync(filename)) {
895+
return env;
896+
}
897+
const buffer = fs.readFileSync(filename, 'utf8');
898+
buffer.split('\n').forEach( line => {
899+
// using a match which will automatically ignore "wrong" lines including
900+
// lines that are empty or start with a "#", or //, or ...
901+
const m = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
902+
if (m != undefined) {
903+
env[m[1]] = m[2] || '';
904+
}
905+
});
906+
907+
throw new Error("Function not implemented.");
908+
}
909+
910+
function mergeEnv(env: {}, buffer: { [key: string]: string; }) {
911+
for (const key in buffer) {
912+
if (buffer.hasOwnProperty(key)) {
913+
if (buffer === undefined)
914+
delete env[key];
915+
else
916+
env[key] = buffer[key];
917+
}
918+
}
919+
}
920+

src/gdb.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArgum
99
target: string;
1010
gdbpath: string;
1111
env: any;
12+
envFile: string;
1213
debugger_args: string[];
1314
pathSubstitutions: { [index: string]: string };
1415
arguments: string;
@@ -26,6 +27,7 @@ export interface AttachRequestArguments extends DebugProtocol.AttachRequestArgum
2627
target: string;
2728
gdbpath: string;
2829
env: any;
30+
envFile: string;
2931
debugger_args: string[];
3032
pathSubstitutions: { [index: string]: string };
3133
executable: string;
@@ -53,7 +55,7 @@ class GDBDebugSession extends MI2DebugSession {
5355
}
5456

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

104106
protected attachRequest(response: DebugProtocol.AttachResponse, args: AttachRequestArguments): void {
105-
this.miDebugger = new MI2(args.gdbpath || "gdb", ["-q", "--interpreter=mi2"], args.debugger_args, args.env);
107+
this.miDebugger = new MI2(args.gdbpath || "gdb", ["-q", "--interpreter=mi2"], args.debugger_args, args.env, args.envFile);
106108
this.setPathSubstitutions(args.pathSubstitutions);
107109
this.initDebugger();
108110
this.quit = false;

src/lldb.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArgum
99
target: string;
1010
lldbmipath: string;
1111
env: any;
12+
envFile: string;
1213
debugger_args: string[];
1314
pathSubstitutions: { [index: string]: string };
1415
arguments: string;
@@ -25,6 +26,7 @@ export interface AttachRequestArguments extends DebugProtocol.AttachRequestArgum
2526
target: string;
2627
lldbmipath: string;
2728
env: any;
29+
envFile: string;
2830
debugger_args: string[];
2931
pathSubstitutions: { [index: string]: string };
3032
executable: string;
@@ -48,7 +50,7 @@ class LLDBDebugSession extends MI2DebugSession {
4850
}
4951

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

9597
protected attachRequest(response: DebugProtocol.AttachResponse, args: AttachRequestArguments): void {
96-
this.miDebugger = new MI2_LLDB(args.lldbmipath || "lldb-mi", [], args.debugger_args, args.env);
98+
this.miDebugger = new MI2_LLDB(args.lldbmipath || "lldb-mi", [], args.debugger_args, args.env, args.envFile);
9799
this.setPathSubstitutions(args.pathSubstitutions);
98100
this.initDebugger();
99101
this.quit = false;

src/mago.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArgum
99
target: string;
1010
magomipath: string;
1111
env: any;
12+
envFile: string;
1213
debugger_args: string[];
1314
arguments: string;
1415
autorun: string[];
@@ -22,6 +23,7 @@ export interface AttachRequestArguments extends DebugProtocol.AttachRequestArgum
2223
target: string;
2324
magomipath: string;
2425
env: any;
26+
envFile: string;
2527
debugger_args: string[];
2628
executable: string;
2729
autorun: string[];
@@ -50,7 +52,7 @@ class MagoDebugSession extends MI2DebugSession {
5052
}
5153

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

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

0 commit comments

Comments
 (0)