@@ -25,27 +25,30 @@ function couldBeOutput(line: string) {
2525const trace = false ;
2626
2727export 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+
0 commit comments