@@ -7,7 +7,11 @@ import Future = require("fibers/future");
7
7
import path = require( "path" ) ;
8
8
import util = require( "util" ) ;
9
9
10
+ import errors = require( "./errors" ) ;
10
11
import options = require( "./options" ) ;
12
+ import utils = require( "./utils" ) ;
13
+ import xcode6SimulatorLib = require( "./iphone-simulator-xcode-6" ) ;
14
+ import xcode5SimulatorLib = require( "./iphone-simulator-xcode-5" ) ;
11
15
12
16
var $ = require ( "NodObjC" ) ;
13
17
@@ -23,46 +27,55 @@ export class iPhoneSimulator implements IiPhoneSimulator {
23
27
private static SIMULATOR_FRAMEWORK_RELATIVE_PATH = "../SharedFrameworks/DVTiPhoneSimulatorRemoteClient.framework" ;
24
28
25
29
public run ( appPath : string ) : IFuture < void > {
26
- return ( ( ) => {
27
- if ( ! fs . existsSync ( appPath ) ) {
28
- throw new Error ( util . format ( "Path does not exist " , appPath ) ) ;
29
- }
30
+ if ( ! fs . existsSync ( appPath ) ) {
31
+ errors . fail ( "Path does not exist " , appPath ) ;
32
+ }
33
+
34
+ return this . execute ( this . launch , { canRunMainLoop : true , appPath : appPath } ) ;
35
+ }
36
+
37
+ public printDeviceTypes ( ) : IFuture < void > {
38
+
39
+ var action = ( ) => {
40
+ var simulator = this . createSimulator ( ) ;
41
+ _ . each ( simulator . validDeviceIdentifiers , ( identifier : any ) => console . log ( identifier ) ) ;
42
+ }
30
43
44
+ return this . execute ( action , { canRunMainLoop : false } ) ;
45
+ }
46
+
47
+ private execute ( action : ( appPath ?: string ) => any , opts : IExecuteOptions ) : IFuture < void > {
48
+ return ( ( ) => {
31
49
$ . importFramework ( iPhoneSimulator . FOUNDATION_FRAMEWORK_NAME ) ;
32
50
$ . importFramework ( iPhoneSimulator . APPKIT_FRAMEWORK_NAME ) ;
33
51
34
52
var pool = $ . NSAutoreleasePool ( "alloc" ) ( "init" ) ;
35
53
36
54
var developerDirectoryPath = this . findDeveloperDirectory ( ) . wait ( ) ;
37
55
if ( ! developerDirectoryPath ) {
38
- throw new Error ( "Unable to find developer directory" ) ;
56
+ errors . fail ( "Unable to find developer directory" ) ;
39
57
}
40
58
41
59
this . loadFrameworks ( developerDirectoryPath ) ;
42
- this . launch ( developerDirectoryPath , appPath ) ;
43
60
44
- $ . NSRunLoop ( "mainRunLoop" ) ( "run" ) ;
61
+ action . apply ( this , [ opts . appPath ] ) ;
62
+
63
+ if ( opts . canRunMainLoop ) {
64
+ $ . NSRunLoop ( "mainRunLoop" ) ( "run" ) ;
65
+ }
45
66
46
67
pool ( "release" ) ;
47
68
} ) . future < void > ( ) ( ) ;
48
69
}
49
70
50
- private launch ( developerDirectoryPath : string , appPath : string ) : void {
51
- this . loadFrameworks ( developerDirectoryPath ) ;
52
-
71
+ private launch ( appPath : string ) : void {
53
72
var sessionDelegate = $ . NSObject . extend ( "DTiPhoneSimulatorSessionDelegate" ) ;
54
73
sessionDelegate . addMethod ( "session:didEndWithError:" , "v@:@@" , function ( self : any , sel : any , sess : any , error : any ) {
55
- console . log ( "Session ended with error: " ) ;
56
- console . log ( error ) ;
57
- process . exit ( 1 ) ;
74
+ iPhoneSimulator . logSessionInfo ( error , "Session ended without errors." , "Session ended with error " ) ;
75
+ process . exit ( 0 ) ;
58
76
} ) ;
59
- sessionDelegate . addMethod ( "session:didStart:withError:" , "v@:@c@" , function ( self : any , sel : any , sess : any , did : any , err :any ) {
60
- if ( err ) {
61
- console . log ( "Session started with error " , err ) ;
62
- process . exit ( 1 ) ;
63
- } else {
64
- console . log ( "Session started without errors" ) ;
65
- }
77
+ sessionDelegate . addMethod ( "session:didStart:withError:" , "v@:@c@" , function ( self : any , sel : any , sess : any , did : any , error :any ) {
78
+ iPhoneSimulator . logSessionInfo ( error , "Session started without errors." , "Session started with error " ) ;
66
79
} ) ;
67
80
sessionDelegate . register ( ) ;
68
81
@@ -73,22 +86,14 @@ export class iPhoneSimulator implements IiPhoneSimulator {
73
86
var sdkRoot = options . sdkRoot ? $ ( options . sdkRoot ) : this . getClassByName ( "DTiPhoneSimulatorSystemRoot" ) ( "defaultRoot" ) ;
74
87
config ( "setSimulatedSystemRoot" , sdkRoot ) ;
75
88
76
- var family = 1 ;
77
- if ( options . family ) {
78
- if ( options . family . toLowerCase ( ) === "ipad" ) {
79
- family = 2 ;
89
+ var simulator = this . createSimulator ( config ) ;
90
+ if ( options . device ) {
91
+ var validDeviceIdentifiers = simulator . validDeviceIdentifiers ;
92
+ if ( ! _ . contains ( validDeviceIdentifiers , options . device ) ) {
93
+ errors . fail ( "Invalid device identifier %s. Valid device identifiers are %s." , options . device , utils . stringify ( validDeviceIdentifiers ) ) ;
80
94
}
81
95
}
82
- config ( "setSimulatedDeviceFamily" , $ . NSNumber ( "numberWithInt" , family ) ) ;
83
-
84
- if ( options . env ) {
85
- var env = $ . NSMutableDictionary ( "dictionary" ) ;
86
- Object . keys ( env ) . forEach ( key => {
87
- env ( "setObject" , $ ( env [ key ] ) , "forKey" , $ ( key ) ) ;
88
- } ) ;
89
-
90
- config ( "setSimulatedApplicationLaunchEnvironment" , env ) ;
91
- }
96
+ simulator . setSimulatedDevice ( config ) ;
92
97
93
98
config ( "setLocalizedClientName" , $ ( "ios-sim-portable" ) ) ;
94
99
@@ -101,7 +106,7 @@ export class iPhoneSimulator implements IiPhoneSimulator {
101
106
session ( "setDelegate" , delegate ) ;
102
107
103
108
if ( ! session ( "requestStartWithConfig" , config , "timeout" , timeout , "error" , sessionError ) ) {
104
- throw new Error ( util . format ( "Could not start simulator session " , sessionError ) ) ;
109
+ errors . fail ( "Could not start simulator session " , sessionError ) ;
105
110
}
106
111
}
107
112
@@ -116,7 +121,7 @@ export class iPhoneSimulator implements IiPhoneSimulator {
116
121
var platformsError : string = null ;
117
122
var dvtPlatformClass = this . getClassByName ( "DVTPlatform" ) ;
118
123
if ( ! dvtPlatformClass ( "loadAllPlatformsReturningError" , platformsError ) ) {
119
- throw new Error ( util . format ( "Unable to loadAllPlatformsReturningError " , platformsError ) ) ;
124
+ errors . fail ( "Unable to loadAllPlatformsReturningError " , platformsError ) ;
120
125
}
121
126
122
127
var simulatorFrameworkPath = path . join ( developerDirectoryPath , iPhoneSimulator . SIMULATOR_FRAMEWORK_RELATIVE_PATH_LEGACY ) ;
@@ -129,7 +134,7 @@ export class iPhoneSimulator implements IiPhoneSimulator {
129
134
private loadFramework ( frameworkPath : string ) {
130
135
var bundle = $ . NSBundle ( "bundleWithPath" , $ ( frameworkPath ) ) ;
131
136
if ( ! bundle ( "load" ) ) {
132
- throw new Error ( util . format ( "Unable to load " , frameworkPath ) ) ;
137
+ errors . fail ( "Unable to load " , frameworkPath ) ;
133
138
}
134
139
}
135
140
@@ -167,4 +172,28 @@ export class iPhoneSimulator implements IiPhoneSimulator {
167
172
private getClassByName ( className : string ) : any {
168
173
return $ . classDefinition . getClassByName ( className ) ;
169
174
}
175
+
176
+ private static logSessionInfo ( error : any , successfulMessage : string , errorMessage : string ) : void {
177
+ if ( error ) {
178
+ console . log ( util . format ( "%s %s" , errorMessage , error ) ) ;
179
+ process . exit ( 1 ) ;
180
+ }
181
+
182
+ console . log ( successfulMessage ) ;
183
+ }
184
+
185
+ private createSimulator ( config ?: any ) : ISimulator {
186
+ if ( ! config ) {
187
+ config = this . getClassByName ( "DTiPhoneSimulatorSessionConfig" ) ( "alloc" ) ( "init" ) ( "autorelease" ) ;
188
+ }
189
+
190
+ var simulator : ISimulator ;
191
+ if ( _ . contains ( config . methods ( ) , "setDevice:" ) ) {
192
+ simulator = new xcode6SimulatorLib . XCode6Simulator ( ) ;
193
+ } else {
194
+ simulator = new xcode5SimulatorLib . XCode5Simulator ( ) ;
195
+ }
196
+
197
+ return simulator ;
198
+ }
170
199
}
0 commit comments