forked from probablycorey/WaxSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulator.m
123 lines (94 loc) · 3.84 KB
/
Simulator.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
//
// Sim
//
// Created by ProbablyInteractive on 7/28/09.
// Copyright 2009 Probably Interactive. All rights reserved.
//
#import "Simulator.h"
#include <sys/param.h>
#include <objc/runtime.h>
#define WaxLog(format, args...) \
printf("%s\n", [[NSString stringWithFormat:(format), ## args] UTF8String])
@implementation Simulator
@synthesize session=_session;
- (id)initWithAppPath:(NSString *)appPath sdk:(NSString *)sdk args:(NSArray *)args {
self = [super init];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![appPath isAbsolutePath]) {
appPath = [[fileManager currentDirectoryPath] stringByAppendingPathComponent:appPath];
}
_appPath = [appPath retain];
if (![fileManager fileExistsAtPath:_appPath]) {
WaxLog(@"App path '%@' does not exist!", _appPath);
exit(EXIT_FAILURE);
}
if (!sdk) _sdk = [[DTiPhoneSimulatorSystemRoot defaultRoot] retain];
else {
_sdk = [[DTiPhoneSimulatorSystemRoot rootWithSDKVersion:sdk] retain];
}
if (!_sdk) {
WaxLog(@"Unknown sdk '%@'", sdk);
WaxLog(@"Available sdks are...");
for (id root in [DTiPhoneSimulatorSystemRoot knownRoots]) {
WaxLog(@" %@", [root sdkVersion]);
}
exit(EXIT_FAILURE);
}
_args = [args retain];
return self;
}
+ (NSArray *)availableSDKs {
NSMutableArray *sdks = [NSMutableArray array];
for (id root in [DTiPhoneSimulatorSystemRoot knownRoots]) {
[sdks addObject:[root sdkVersion]];
}
return sdks;
}
- (int)launch {
WaxLog(@"Launching '%@' on'%@'", _appPath, [_sdk sdkDisplayName]);
DTiPhoneSimulatorApplicationSpecifier *appSpec = [DTiPhoneSimulatorApplicationSpecifier specifierWithApplicationPath:_appPath];
if (!appSpec) {
WaxLog(@"Could not load application specifier for '%@'", _appPath);
return EXIT_FAILURE;
}
DTiPhoneSimulatorSystemRoot *sdkRoot = [DTiPhoneSimulatorSystemRoot defaultRoot];
DTiPhoneSimulatorSessionConfig *config = [[DTiPhoneSimulatorSessionConfig alloc] init];
[config setApplicationToSimulateOnStart:appSpec];
[config setSimulatedSystemRoot:sdkRoot];
[config setSimulatedApplicationShouldWaitForDebugger:NO];
[config setSimulatedApplicationLaunchArgs:_args];
[config setSimulatedApplicationLaunchEnvironment:[[NSProcessInfo processInfo] environment]];
[config setLocalizedClientName:@"WaxSim"];
// Make the simulator output to the current STDOUT & STDERR
char path[MAXPATHLEN];
fcntl(STDOUT_FILENO, F_GETPATH, &path);
[config setSimulatedApplicationStdOutPath:[NSString stringWithUTF8String:path]];
fcntl(STDERR_FILENO, F_GETPATH, &path);
[config setSimulatedApplicationStdErrPath:[NSString stringWithUTF8String:path]];
_session = [[DTiPhoneSimulatorSession alloc] init];
[_session setDelegate:self];
[_session setSimulatedApplicationPID:[NSNumber numberWithInt:35]];
NSError *error;
if (![_session requestStartWithConfig:config timeout:30 error:&error]) {
WaxLog(@"Could not start simulator session: %@", [error localizedDescription]);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
- (void)end {
[_session requestEndWithTimeout:0];
}
// DTiPhoneSimulatorSession Delegate
// ---------------------------------
- (void)session:(DTiPhoneSimulatorSession *)session didStart:(BOOL)started withError:(NSError *)error {
if (!started) {
WaxLog(@"Session failed to start. %@", [error localizedDescription]);
exit(EXIT_FAILURE);
}
}
- (void)session:(DTiPhoneSimulatorSession *)session didEndWithError:(NSError *)error {
WaxLog(@"Session ended with error. %@", [error localizedDescription]);
if ([error code] != 2) exit(EXIT_FAILURE); // if it is a timeout error, that's cool. We are probably rebooting
}
@end