Skip to content

Prefer currently booted iOS Simulator #1938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
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
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;
}

private static function getSimulators():String
Expand Down