@@ -25,299 +25,41 @@ THE SOFTWARE.
25
25
var path = require ( 'path' ) ,
26
26
fs = require ( 'fs' ) ,
27
27
help = require ( './help' ) ,
28
+ lib = require ( './lib' ) ;
28
29
util = require ( 'util' ) ,
29
30
simctl ,
30
31
bplist ;
31
32
32
- function findFirstAvailableDevice ( list ) {
33
- /*
34
- // Example result:
35
- {
36
- name : 'iPhone 6',
37
- id : 'A1193D97-F5EE-468D-9DBA-786F403766E6',
38
- runtime : 'iOS 8.3'
39
- }
40
- */
41
-
42
- // the object to return
43
- var ret_obj = {
44
- name : null ,
45
- id : null ,
46
- runtime : null
47
- } ;
48
-
49
- var available_runtimes = { } ;
50
-
51
- list . runtimes . forEach ( function ( runtime ) {
52
- if ( runtime . available ) {
53
- available_runtimes [ runtime . name ] = true ;
54
- }
55
- } ) ;
56
-
57
-
58
- list . devices . some ( function ( deviceGroup ) {
59
- deviceGroup . devices . some ( function ( device ) {
60
- if ( available_runtimes [ deviceGroup . runtime ] ) {
61
- ret_obj = {
62
- name : device . name ,
63
- id : device . id ,
64
- runtime : deviceGroup . runtime
65
- } ;
66
- return true ;
67
- }
68
- return false ;
69
- } ) ;
70
- return false ;
71
- } ) ;
72
-
73
- return ret_obj ;
74
- }
75
-
76
- function findRuntimesGroupByDeviceProperty ( list , deviceProperty , availableOnly ) {
77
- /*
78
- // Example result:
79
- {
80
- "iPhone 6" : [ "iOS 8.2", "iOS 8.3"],
81
- "iPhone 6 Plus" : [ "iOS 8.2", "iOS 8.3"]
82
- }
83
- */
84
-
85
- var runtimes = { } ;
86
- var available_runtimes = { } ;
87
-
88
- list . runtimes . forEach ( function ( runtime ) {
89
- if ( runtime . available ) {
90
- available_runtimes [ runtime . name ] = true ;
91
- }
92
- } ) ;
93
-
94
- list . devices . forEach ( function ( deviceGroup ) {
95
- deviceGroup . devices . forEach ( function ( device ) {
96
- var devicePropertyValue = device [ deviceProperty ] ;
97
-
98
- if ( ! runtimes [ devicePropertyValue ] ) {
99
- runtimes [ devicePropertyValue ] = [ ] ;
100
- }
101
- if ( availableOnly ) {
102
- if ( available_runtimes [ deviceGroup . runtime ] ) {
103
- runtimes [ devicePropertyValue ] . push ( deviceGroup . runtime ) ;
104
- }
105
- } else {
106
- runtimes [ devicePropertyValue ] . push ( deviceGroup . runtime ) ;
107
- }
108
- } ) ;
109
- } ) ;
110
-
111
- return runtimes ;
112
- }
113
-
114
- function findAvailableRuntime ( list , device_name ) {
115
-
116
- var all_druntimes = findRuntimesGroupByDeviceProperty ( list , "name" , true ) ;
117
- var druntime = all_druntimes [ device_name ] ;
118
- var runtime_found = druntime && druntime . length > 0 ;
119
-
120
- if ( ! runtime_found ) {
121
- console . error ( util . format ( 'No available runtimes could be found for "%s".' , device_name ) ) ;
122
- process . exit ( 1 ) ;
123
- }
124
-
125
- // return most modern runtime
126
- return druntime . sort ( ) . pop ( ) ;
127
- }
128
-
129
- function getDeviceFromDeviceTypeId ( devicetypeid ) {
130
- /*
131
- // Example result:
132
- {
133
- name : 'iPhone 6',
134
- id : 'A1193D97-F5EE-468D-9DBA-786F403766E6',
135
- runtime : 'iOS 8.3'
136
- }
137
- */
138
-
139
- // the object to return
140
- var ret_obj = {
141
- name : null ,
142
- id : null ,
143
- runtime : null
144
- } ;
145
-
146
- var options = { 'silent' : true } ;
147
- var list = simctl . list ( options ) . json ;
148
-
149
- var arr = [ ] ;
150
- if ( devicetypeid ) {
151
- arr = devicetypeid . split ( ',' ) ;
152
- }
153
-
154
- // get the devicetype from --devicetypeid
155
- // --devicetypeid is a string in the form "devicetype, runtime_version" (optional: runtime_version)
156
- var devicetype = null ;
157
- if ( arr . length < 1 ) {
158
- var dv = findFirstAvailableDevice ( list ) ;
159
- console . error ( util . format ( '--devicetypeid was not specified, using first available device: %s.' , dv . name ) ) ;
160
- return dv ;
161
- } else {
162
- devicetype = arr [ 0 ] . trim ( ) ;
163
- if ( arr . length > 1 ) {
164
- ret_obj . runtime = arr [ 1 ] . trim ( ) ;
165
- }
166
- }
167
-
168
- // check whether devicetype has the "com.apple.CoreSimulator.SimDeviceType." prefix, if not, add it
169
- var prefix = 'com.apple.CoreSimulator.SimDeviceType.' ;
170
- if ( devicetype . indexOf ( prefix ) != 0 ) {
171
- devicetype = prefix + devicetype ;
172
- }
173
-
174
- // now find the devicename from the devicetype
175
- var devicename_found = list . devicetypes . some ( function ( deviceGroup ) {
176
- if ( deviceGroup . id === devicetype ) {
177
- ret_obj . name = deviceGroup . name ;
178
- return true ;
179
- }
180
-
181
- return false ;
182
- } ) ;
183
-
184
- // device name not found, exit
185
- if ( ! devicename_found ) {
186
- console . error ( util . format ( 'Device type "%s" could not be found.' , devicetype ) ) ;
187
- process . exit ( 1 ) ;
188
- }
189
-
190
- // if runtime_version was not specified, we use a default. Use first available that has the device
191
- if ( ! ret_obj . runtime ) {
192
- ret_obj . runtime = findAvailableRuntime ( list , ret_obj . name ) ;
193
- }
194
-
195
- // prepend iOS to runtime version, if necessary
196
- if ( ret_obj . runtime . indexOf ( 'iOS' ) === - 1 ) {
197
- ret_obj . runtime = util . format ( 'iOS %s' , ret_obj . runtime ) ;
198
- }
199
-
200
- // now find the deviceid (by runtime and devicename)
201
- var deviceid_found = list . devices . some ( function ( deviceGroup ) {
202
- if ( deviceGroup . runtime === ret_obj . runtime ) { // found the runtime, now find the actual device matching devicename
203
- return deviceGroup . devices . some ( function ( device ) {
204
- if ( device . name === ret_obj . name ) {
205
- ret_obj . id = device . id ;
206
- return true ;
207
- }
208
- return false ;
209
- } ) ;
210
- }
211
- return false ;
212
- } ) ;
213
-
214
- if ( ! deviceid_found ) {
215
- console . error ( util . format ( 'Device id for device name "%s" and runtime "%s" could not be found, or is not available.' , ret_obj . name , ret_obj . runtime ) ) ;
216
- process . exit ( 1 ) ;
217
- }
218
-
219
- return ret_obj ;
220
- }
221
-
222
33
var command_lib = {
223
34
224
35
init : function ( ) {
225
- if ( ! simctl ) {
226
- simctl = require ( 'simctl' ) ;
227
- }
228
- var output = simctl . check_prerequisites ( ) ;
229
- if ( output . code !== 0 ) {
230
- console . error ( output . output ) ;
231
- process . exit ( 2 ) ;
232
- }
233
-
234
- if ( ! bplist ) {
235
- bplist = require ( 'bplist-parser' ) ;
236
- }
36
+ lib . init ( ) ;
237
37
} ,
238
38
239
39
showsdks : function ( args ) {
240
- var options = { silent : true , runtimes : true } ;
241
- var list = simctl . list ( options ) . json ;
242
-
243
- console . log ( "Simulator SDK Roots:" ) ;
244
- list . runtimes . forEach ( function ( runtime ) {
245
- if ( runtime . available ) {
246
- console . log ( util . format ( "'%s' (%s)" , runtime . name , runtime . build ) ) ;
247
- console . log ( util . format ( "\t(unknown)" ) ) ;
248
- }
249
- } ) ;
40
+ lib . showsdks ( ) ;
250
41
} ,
251
42
252
43
showdevicetypes : function ( args ) {
253
- var options = { silent : true } ;
254
- var list = simctl . list ( options ) . json ;
255
-
256
- var druntimes = findRuntimesGroupByDeviceProperty ( list , "name" , true ) ;
257
- var name_id_map = { } ;
258
-
259
- list . devicetypes . forEach ( function ( device ) {
260
- name_id_map [ device . name ] = device . id ;
261
- } ) ;
262
-
263
- for ( var deviceName in druntimes ) {
264
- var runtimes = druntimes [ deviceName ] ;
265
- runtimes . forEach ( function ( runtime ) {
266
- // remove "iOS" prefix in runtime, remove prefix "com.apple.CoreSimulator.SimDeviceType." in id
267
- console . log ( util . format ( "%s, %s" , name_id_map [ deviceName ] . replace ( / ^ c o m .a p p l e .C o r e S i m u l a t o r .S i m D e v i c e T y p e ./ , '' ) , runtime . replace ( / ^ i O S / , '' ) ) ) ;
268
- } ) ;
269
- }
44
+ lib . showdevicetypes ( ) ;
270
45
} ,
271
46
272
47
launch : function ( args ) {
273
48
var wait_for_debugger = false ,
274
- app_identifier ,
275
- argv ,
276
- app_path ,
277
- info_plist_path ;
278
-
49
+ app_path ;
50
+
279
51
if ( args . argv . remain . length < 2 ) {
280
52
help ( ) ;
281
53
process . exit ( 1 ) ;
282
54
}
283
55
284
56
app_path = args . argv . remain [ 1 ] ;
285
- info_plist_path = path . join ( app_path , 'Info.plist' ) ;
286
- if ( ! fs . existsSync ( info_plist_path ) ) {
287
- console . error ( info_plist_path + " file not found." ) ;
288
- process . exit ( 1 ) ;
289
- }
290
57
291
- bplist . parseFile ( info_plist_path , function ( err , obj ) {
292
-
293
- if ( err ) {
294
- throw err ;
295
- }
296
-
297
- app_identifier = obj [ 0 ] . CFBundleIdentifier ;
298
- argv = args . args || [ ] ;
299
-
300
- // get the deviceid from --devicetypeid
301
- // --devicetypeid is a string in the form "devicetype, runtime_version" (optional: runtime_version)
302
- var device = getDeviceFromDeviceTypeId ( args . devicetypeid ) ;
303
-
304
- // so now we have the deviceid, we can proceed
305
- simctl . extensions . start ( device . id ) ;
306
- simctl . install ( device . id , app_path ) ;
307
- simctl . launch ( wait_for_debugger , device . id , app_identifier , argv ) ;
308
- simctl . extensions . log ( device . id , args . log ) ;
309
- if ( args . log ) {
310
- console . log ( util . format ( "logPath: %s" , path . resolve ( args . log ) ) ) ;
311
- }
312
- if ( args . exit ) {
313
- process . exit ( 0 ) ;
314
- }
315
- } ) ;
58
+ lib . launch ( app_path , devicetypeid , log , exit , args . args ) ;
316
59
} ,
317
60
318
61
install : function ( args ) {
319
- var wait_for_debugger = false ,
320
- app_identifier ,
62
+ var app_identifier ,
321
63
argv ,
322
64
app_path ,
323
65
info_plist_path ;
@@ -328,48 +70,12 @@ var command_lib = {
328
70
}
329
71
330
72
app_path = args . argv . remain [ 1 ] ;
331
- info_plist_path = path . join ( app_path , 'Info.plist' ) ;
332
- if ( ! fs . existsSync ( info_plist_path ) ) {
333
- console . error ( info_plist_path + " file not found." ) ;
334
- process . exit ( 1 ) ;
335
- }
336
-
337
- bplist . parseFile ( info_plist_path , function ( err , obj ) {
338
-
339
- if ( err ) {
340
- throw err ;
341
- }
342
-
343
- app_identifier = obj [ 0 ] . CFBundleIdentifier ;
344
- argv = args . args || [ ] ;
345
73
346
- // get the deviceid from --devicetypeid
347
- // --devicetypeid is a string in the form "devicetype, runtime_version" (optional: runtime_version)
348
- var device = getDeviceFromDeviceTypeId ( args . devicetypeid ) ;
349
-
350
- // so now we have the deviceid, we can proceed
351
- simctl . extensions . start ( device . id ) ;
352
- simctl . install ( device . id , app_path ) ;
353
-
354
- simctl . extensions . log ( device . id , args . log ) ;
355
- if ( args . log ) {
356
- console . log ( util . format ( "logPath: %s" , path . resolve ( args . log ) ) ) ;
357
- }
358
- if ( args . exit ) {
359
- process . exit ( 0 ) ;
360
- }
361
- } ) ;
74
+ lib . install ( app_path , devicetypeid , log , exit ) ;
362
75
} ,
363
76
364
77
start : function ( args ) {
365
- var device = { } ;
366
- try {
367
- device = getDeviceFromDeviceTypeId ( args . devicetypeid ) ;
368
- } catch ( e ) {
369
- console . error ( e ) ;
370
- }
371
-
372
- simctl . extensions . start ( device . id ) ;
78
+ lib . start ( args . devicetypeid ) ;
373
79
}
374
80
}
375
81
0 commit comments