forked from openfl/lime
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathXCodeHelper.hx
More file actions
204 lines (186 loc) · 5.03 KB
/
XCodeHelper.hx
File metadata and controls
204 lines (186 loc) · 5.03 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package lime.tools;
import hxp.*;
import lime.tools.HXProject;
class XCodeHelper
{
// different computers may have different sets of simulators installed
// so there isn't necessarily a reasonable default for ipads
private static var DEFAULT_IPAD_SIMULATOR_NAMES = [
"ipad",
"ipad-air"
];
private static var DEFAULT_IPAD_AIR_SIMULATOR_FALLBACK_REGEX = ~/ipad-air-.+/g;
private static var DEFAULT_IPAD_SIMULATOR_FALLBACK_REGEX = ~/ipad-.+/g;
// this should be a standard iPhone of a particular generation
private static var DEFAULT_IPHONE_SIMULATOR_REGEX = ~/iphone-\d+/g;
private static var DEFAULT_IPHONE_SIMULATOR_FALLBACK_REGEX = ~/iphone-.+/g;
private static function extractSimulatorFlagName(line:String):String
{
var device = line.substring(0, line.indexOf("(") - 1);
device = device.toLowerCase();
device = StringTools.replace(device, " ", "-");
return device;
}
private static function extractSimulatorFullName(line:String):String
{
var name = "";
if (line.indexOf("inch") > -1 || line.indexOf("generation") > -1)
{
name = line.substring(0, line.indexOf(")") + 1);
}
else
{
name = line.substring(0, line.indexOf("(") - 1);
}
return name;
}
private static function extractSimulatorID(line:String):String
{
var id = line.substring(line.indexOf("(") + 1, line.indexOf(")"));
if (id.indexOf("inch") > -1 || id.indexOf("generation") > -1)
{
var startIndex = line.indexOf(")") + 2;
id = line.substring(line.indexOf("(", startIndex) + 1, line.indexOf(")", startIndex));
}
return id;
}
public static function getSelectedSimulator(project:HXProject):SimulatorInfo
{
var output = getSimulators();
var lines = output.split("\n");
var foundSection = false;
var device = "";
var deviceID = "";
var deviceName = "";
var devices = new Map<String, SimulatorInfo>();
var currentDevice:SimulatorInfo = null;
for (line in lines)
{
if (StringTools.startsWith(line, "--"))
{
if (line.indexOf("Unavailable") > -1)
{
foundSection = false;
}
else if (line.indexOf("iOS") > -1)
{
foundSection = true;
}
else if (foundSection)
{
break;
}
}
else if (foundSection)
{
deviceName = extractSimulatorFullName(StringTools.trim(line));
deviceID = extractSimulatorID(StringTools.trim(line));
device = extractSimulatorFlagName(StringTools.trim(line));
devices.set(device, {id: deviceID, name: deviceName});
if (project.targetFlags.exists(device))
{
currentDevice = devices.get(device);
break;
}
}
}
if (currentDevice == null)
{
if (project.targetFlags.exists("ipad") || project.config.getString("ios.device", "universal") == "ipad")
{
for (device in DEFAULT_IPAD_SIMULATOR_NAMES)
{
// try to find a relatively standard ipad simulator
currentDevice = devices.get(device);
if (currentDevice != null)
{
break;
}
}
// if we couldn't find one of the default names, let's try to
// find any iPad Air, which should be a reasonable default
if (currentDevice == null)
{
for (device in devices.keys())
{
if (DEFAULT_IPAD_AIR_SIMULATOR_FALLBACK_REGEX.match(device))
{
currentDevice = devices.get(device);
break;
}
}
}
// worst case, if we still haven't found a good name, choose the
// first ipad that we find. it could be a mini or pro, which
// might not necessarily be ideal, but it's better than nothing.
if (currentDevice == null)
{
for (device in devices.keys())
{
if (DEFAULT_IPAD_SIMULATOR_FALLBACK_REGEX.match(device))
{
currentDevice = devices.get(device);
break;
}
}
}
}
else
{
for (device in devices.keys())
{
// try to find a standard iphone, which should have an name
// like iphone-15 or iphone-16
if (DEFAULT_IPHONE_SIMULATOR_REGEX.match(device))
{
currentDevice = devices.get(device);
break;
}
}
// of we can't find a standard iphone for some reason, choose
// the first iphone- name that we find. it could be a plus, pro,
// se, or something that might not necessarily be ideal, but
// it's better than nothing at all.
if (currentDevice == null)
{
for (device in devices.keys())
{
if (DEFAULT_IPHONE_SIMULATOR_FALLBACK_REGEX.match(device))
{
currentDevice = devices.get(device);
break;
}
}
}
}
}
return currentDevice;
}
public static function getSimulatorID(project:HXProject):String
{
var simulator = getSelectedSimulator(project);
if (simulator == null)
{
return null;
}
return simulator.id;
}
public static function getSimulatorName(project:HXProject):String
{
var simulator = getSelectedSimulator(project);
if (simulator == null)
{
return null;
}
return simulator.name;
}
private static function getSimulators():String
{
return System.runProcess("", "xcrun", ["simctl", "list", "devices"]);
}
}
private typedef SimulatorInfo =
{
public var id:String;
public var name:String;
}