From 79c9a8cefa856026f194ed9b2761a957167ee0ea Mon Sep 17 00:00:00 2001 From: Simon Sobisch Date: Tue, 10 May 2022 15:09:20 +0200 Subject: [PATCH] add support for envFile fix #258 --- package.json | 30 +++++++++++++++++++ src/backend/mi2/mi2.ts | 66 +++++++++++++++++++++++++++++++----------- src/gdb.ts | 6 ++-- src/lldb.ts | 6 ++-- src/mago.ts | 6 ++-- 5 files changed, 91 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index e3b10c08..4d2a38b2 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", diff --git a/src/backend/mi2/mi2.ts b/src/backend/mi2/mi2.ts index 52a1c494..9b334b55 100644 --- a/src/backend/mi2/mi2.ts +++ b/src/backend/mi2/mi2.ts @@ -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 { @@ -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]; + } + } +} + diff --git a/src/gdb.ts b/src/gdb.ts index 26eef1ec..8c82901c 100644 --- a/src/gdb.ts +++ b/src/gdb.ts @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/src/lldb.ts b/src/lldb.ts index 012e57a0..3b526cd3 100644 --- a/src/lldb.ts +++ b/src/lldb.ts @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/src/mago.ts b/src/mago.ts index 313181ac..6a0bb2ab 100644 --- a/src/mago.ts +++ b/src/mago.ts @@ -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[]; @@ -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[]; @@ -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; @@ -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;