Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 60 additions & 16 deletions src/lime/tools/XCodeHelper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,49 @@ class XCodeHelper

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)
// Simulator ID's are always:
// XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
var searchPos = 0;

while (true)
{
var startIndex = line.indexOf(")") + 2;
id = line.substring(line.indexOf("(", startIndex) + 1, line.indexOf(")", startIndex));
var openParen = line.indexOf("(", searchPos);
var closeParen = line.indexOf(")", openParen);

if (openParen == -1 || closeParen == -1)
{
// Couldn't find a valid ID
break;
}

var potentialID = line.substring(openParen + 1, closeParen);

// Simulator IDs are always UUID strings exactly 36 chars long
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it really is that simple, we can save a lot of space here.

private static function extractSimulatorID(line:String):String
{
	// Simulator IDs are always UUID strings exactly 36 chars long, in parentheses
	var id:EReg = ~/\(([A-F0-9\-]{36})\)/;
	return id.match(line) ? id.matched(1) : "";
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's much shorter for sure.

if (potentialID.length == 36 && potentialID.indexOf("-") > -1)
{
return potentialID;
}

// Skip past the current set of parentheses and keep looking
searchPos = closeParen + 1;
}

// No valid ID found
return "";
}

return id;
private static function getBootedSimulator():SimulatorInfo
{
var result = System.runProcess("", "xcrun", ["simctl", "list", "devices", "booted"]);
var lines = result.split("\n");
for (line in lines)
{
if (line.indexOf("(Booted)") > -1)
{
return { id: extractSimulatorID(StringTools.trim(line)), name: extractSimulatorFullName(StringTools.trim(line)) };
}
}
return null;
}

public static function getSelectedSimulator(project:HXProject):SimulatorInfo
Expand Down Expand Up @@ -173,22 +207,32 @@ class XCodeHelper

public static function getSimulatorID(project:HXProject):String
{
var simulator = getSelectedSimulator(project);
if (simulator == null)
{
return null;
}
return simulator.id;
var simulator = getPreferredSimulator(project);
return (simulator != null) ? simulator.id : null;
}

public static function getSimulatorName(project:HXProject):String
{
var simulator = getSelectedSimulator(project);
if (simulator == null)
var simulator = getPreferredSimulator(project);
return (simulator != null) ? simulator.name : null;
}

private static function getPreferredSimulator(project:HXProject):SimulatorInfo
{
// try and get the simulator which is currently open and running
// if there are none running then fall back to getting the selected simulator
var simulator = getBootedSimulator();
if (simulator != null)
{
return simulator;
}

simulator = getSelectedSimulator(project);
if (simulator != null)
{
return null;
return simulator;
}
return simulator.name;
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (simulator != null) return simulator else return null can be simplified to return simulator.

}

private static function getSimulators():String
Expand Down