@@ -218,6 +218,44 @@ function getDeviceFromDeviceTypeId(devicetypeid) {
218218 return ret_obj ;
219219}
220220
221+ // Parses array of KEY=Value strings into map of strings
222+ // If fixsymctl == true, updates variables for correct usage with simctl
223+ function parseEnvironmentVariables ( envVariables , fixsymctl ) {
224+ envVariables = envVariables || [ ] ;
225+ fixsymctl = typeof fixsymctl != 'undefined' ? fixsymctl : true ;
226+
227+ var envMap = { } ;
228+ envVariables . forEach ( function ( variable ) {
229+ var envPair = variable . split ( '=' , 2 ) ;
230+ if ( envPair . length == 2 ) {
231+ var key = envPair [ 0 ] ;
232+ var value = envPair [ 1 ] ;
233+ if ( fixsymctl ) {
234+ key = 'SIMCTL_CHILD_' + key ;
235+ }
236+ envMap [ key ] = value ;
237+ }
238+ } ) ;
239+ return envMap ;
240+ }
241+
242+ // Injects specified environt variables to the process and then runs action
243+ // returns environment variables back to original state after action completes
244+ function withInjectedEnvironmentVariablesToProcess ( process , envVariables , action ) {
245+ var oldVariables = util . _extend ( { } , process . env ) ;
246+
247+ // Inject additional environment variables to process
248+ for ( var key in envVariables ) {
249+ var value = envVariables [ key ] ;
250+ process . env [ key ] = value ;
251+ }
252+
253+ action ( ) ;
254+
255+ // restore old envs
256+ process . env = oldVariables ;
257+ }
258+
221259// replace hyphens in iPad Pro name which differ in 'Device Types' and 'Devices'
222260function filterDeviceName ( deviceName ) {
223261 // replace hyphens in iPad Pro name which differ in 'Device Types' and 'Devices'
@@ -305,7 +343,7 @@ var lib = {
305343 } ,
306344 //jscs:enable disallowUnusedParams
307345
308- launch : function ( app_path , devicetypeid , log , exit , argv ) {
346+ launch : function ( app_path , devicetypeid , log , exit , setenv , argv ) {
309347 var wait_for_debugger = false ;
310348 var info_plist_path ;
311349 var app_identifier ;
@@ -334,22 +372,27 @@ var lib = {
334372 }
335373
336374 argv = argv || [ ] ;
337-
338- // get the deviceid from --devicetypeid
339- // --devicetypeid is a string in the form "devicetype, runtime_version" (optional: runtime_version)
340- var device = getDeviceFromDeviceTypeId ( devicetypeid ) ;
341-
342- // so now we have the deviceid, we can proceed
343- simctl . extensions . start ( device . id ) ;
344- simctl . install ( device . id , app_path ) ;
345- simctl . launch ( wait_for_debugger , device . id , app_identifier , argv ) ;
346- simctl . extensions . log ( device . id , log ) ;
347- if ( log ) {
348- console . log ( util . format ( 'logPath: %s' , path . resolve ( log ) ) ) ;
349- }
350- if ( exit ) {
351- process . exit ( 0 ) ;
352- }
375+ setenv = setenv || [ ] ;
376+
377+ var environmentVariables = parseEnvironmentVariables ( setenv ) ;
378+
379+ withInjectedEnvironmentVariablesToProcess ( process , environmentVariables , function ( ) {
380+ // get the deviceid from --devicetypeid
381+ // --devicetypeid is a string in the form "devicetype, runtime_version" (optional: runtime_version)
382+ var device = getDeviceFromDeviceTypeId ( devicetypeid ) ;
383+
384+ // so now we have the deviceid, we can proceed
385+ simctl . extensions . start ( device . id ) ;
386+ simctl . install ( device . id , app_path ) ;
387+ simctl . launch ( wait_for_debugger , device . id , app_identifier , argv ) ;
388+ simctl . extensions . log ( device . id , log ) ;
389+ if ( log ) {
390+ console . log ( util . format ( 'logPath: %s' , path . resolve ( log ) ) ) ;
391+ }
392+ if ( exit ) {
393+ process . exit ( 0 ) ;
394+ }
395+ } ) ;
353396 } ) ;
354397 } ,
355398
@@ -399,7 +442,10 @@ var lib = {
399442 }
400443
401444 simctl . extensions . start ( device . id ) ;
402- }
445+ } ,
446+
447+ _parseEnvironmentVariables : parseEnvironmentVariables
448+
403449} ;
404450
405451module . exports = lib ;
0 commit comments