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
143 lines (125 loc) · 3.03 KB
/
XCodeHelper.hx
File metadata and controls
143 lines (125 loc) · 3.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
package lime.tools;
import hxp.*;
import lime.tools.HXProject;
class XCodeHelper
{
private static inline var DEFAULT_IPAD_SIMULATOR = "ipad-air";
private static var DEFAULT_IPHONE_SIMULATOR_REGEX = ~/iphone-\d+/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"))
{
currentDevice = devices.get(DEFAULT_IPAD_SIMULATOR);
}
else
{
for (device in devices.keys())
{
if (DEFAULT_IPHONE_SIMULATOR_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;
}