<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[179130] trunk/Tools</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/179130">179130</a></dd>
<dt>Author</dt> <dd>dbates@webkit.org</dd>
<dt>Date</dt> <dd>2015-01-26 11:45:48 -0800 (Mon, 26 Jan 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Teach run-webkit-app --simulator how to install custom built app
https://bugs.webkit.org/show_bug.cgi?id=140691

Reviewed by David Kilzer.

We should teach run-webkit-app --simulator how to install a custom built app.

Currently run-webkit-app --simulator can only run a system app or an app that
was installed using the simctl command line tool. For convenience we should
teach run-webkit-app --simulator how to install a custom built app.

As a side effect of this change run-safari --simulator will install and run
a custom built of MobileSafari (if it exists).

* Scripts/webkitdirs.pm:
(iOSSimulatorDevices): Added FIXME comment to decouple device representation in
Perl from the structure of the device.plist file.
(plistPathFromBundle): Fix if-statement condition so that we actually perform a
file system check to determine the plist path for a Mac app bundle.
(appIdentifierFromBundle): Pass absolute files system path to defaults(1). Otherwise,
it will complain that it cannot find the file: &quot;Domain ... does not exist&quot;.
(appDisplayNameFromBundle): Ditto.
(waitUntilIOSSimulatorDeviceIsInState): Added; helper function that does not return
until a simulator device is the specified state.
(relaunchIOSSimulator): Renamed; formerly named openIOSSimulator. Quits iOS Simulator
(if it's open) before opening it again so as to ensure that the iOS Simulator boots
the specified device.
(quitIOSSimulator): Added optional parameter, $waitForShutdownOfSimulatedDeviceUDID.
As implied by its name, this function will not return until the specified simulator
device UDID is in the shutdown state.
(iosSimulatorDeviceByUDID): Added; returns the device dictionary object for the simulator
device with the specified UDID.
(isIOSSimulatorSystemInstalledApp): Resolve symbolic links in iosSimulatorApplicationsPath()
before using it as part of a prefix match to avoid a mismatch. In the public iOS 8.1 SDK the
return value of iosSimulatorApplicationsPath(), which returns a result analogous to the shell
expression echo `xcrun --sdk iphonesimulator --show-sdk-path`/Applications/, contains a
symbolic link. Specifically, echo `xcrun --sdk iphonesimulator --show-sdk-path` returns
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.1.sdk,
which is a symbolic link to /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk.
(hasUserInstalledAppInSimulatorDevice): Added.
(isSimulatorDeviceBooted): Added; returns whether the specified simulator device is in the
&quot;Booted&quot; state.
(runIOSWebKitAppInSimulator): Modified to install a custom built app (if needed). For now,
switching between a system installed app and a custom built version of the same app (e.g.
custom build of MobileSafari) will erase all contents and settings in the simulator device.
(eraseIOSSimulatorDevice): Deleted; inline implementation into runIOSWebKitAppInSimulator()
as that was the only caller of this function.
(bootedIOSSimulatorDevice): Deleted.
(openIOSSimulator): Deleted.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsScriptswebkitdirspm">trunk/Tools/Scripts/webkitdirs.pm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (179129 => 179130)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog        2015-01-26 19:44:09 UTC (rev 179129)
+++ trunk/Tools/ChangeLog        2015-01-26 19:45:48 UTC (rev 179130)
</span><span class="lines">@@ -1,3 +1,55 @@
</span><ins>+2015-01-26  Daniel Bates  &lt;dabates@apple.com&gt;
+
+        Teach run-webkit-app --simulator how to install custom built app
+        https://bugs.webkit.org/show_bug.cgi?id=140691
+
+        Reviewed by David Kilzer.
+
+        We should teach run-webkit-app --simulator how to install a custom built app.
+
+        Currently run-webkit-app --simulator can only run a system app or an app that
+        was installed using the simctl command line tool. For convenience we should
+        teach run-webkit-app --simulator how to install a custom built app.
+
+        As a side effect of this change run-safari --simulator will install and run
+        a custom built of MobileSafari (if it exists).
+
+        * Scripts/webkitdirs.pm:
+        (iOSSimulatorDevices): Added FIXME comment to decouple device representation in
+        Perl from the structure of the device.plist file.
+        (plistPathFromBundle): Fix if-statement condition so that we actually perform a
+        file system check to determine the plist path for a Mac app bundle.
+        (appIdentifierFromBundle): Pass absolute files system path to defaults(1). Otherwise,
+        it will complain that it cannot find the file: &quot;Domain ... does not exist&quot;.
+        (appDisplayNameFromBundle): Ditto.
+        (waitUntilIOSSimulatorDeviceIsInState): Added; helper function that does not return
+        until a simulator device is the specified state.
+        (relaunchIOSSimulator): Renamed; formerly named openIOSSimulator. Quits iOS Simulator
+        (if it's open) before opening it again so as to ensure that the iOS Simulator boots
+        the specified device.
+        (quitIOSSimulator): Added optional parameter, $waitForShutdownOfSimulatedDeviceUDID.
+        As implied by its name, this function will not return until the specified simulator
+        device UDID is in the shutdown state.
+        (iosSimulatorDeviceByUDID): Added; returns the device dictionary object for the simulator
+        device with the specified UDID.
+        (isIOSSimulatorSystemInstalledApp): Resolve symbolic links in iosSimulatorApplicationsPath()
+        before using it as part of a prefix match to avoid a mismatch. In the public iOS 8.1 SDK the
+        return value of iosSimulatorApplicationsPath(), which returns a result analogous to the shell
+        expression echo `xcrun --sdk iphonesimulator --show-sdk-path`/Applications/, contains a
+        symbolic link. Specifically, echo `xcrun --sdk iphonesimulator --show-sdk-path` returns
+        /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.1.sdk,
+        which is a symbolic link to /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk.
+        (hasUserInstalledAppInSimulatorDevice): Added.
+        (isSimulatorDeviceBooted): Added; returns whether the specified simulator device is in the
+        &quot;Booted&quot; state.
+        (runIOSWebKitAppInSimulator): Modified to install a custom built app (if needed). For now,
+        switching between a system installed app and a custom built version of the same app (e.g.
+        custom build of MobileSafari) will erase all contents and settings in the simulator device.
+        (eraseIOSSimulatorDevice): Deleted; inline implementation into runIOSWebKitAppInSimulator()
+        as that was the only caller of this function.
+        (bootedIOSSimulatorDevice): Deleted.
+        (openIOSSimulator): Deleted.
+
</ins><span class="cx"> 2015-01-26  Alexey Proskuryakov  &lt;ap@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Update bot assignments.
</span></span></pre></div>
<a id="trunkToolsScriptswebkitdirspm"></a>
<div class="modfile"><h4>Modified: trunk/Tools/Scripts/webkitdirs.pm (179129 => 179130)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/Scripts/webkitdirs.pm        2015-01-26 19:44:09 UTC (rev 179129)
+++ trunk/Tools/Scripts/webkitdirs.pm        2015-01-26 19:45:48 UTC (rev 179130)
</span><span class="lines">@@ -37,11 +37,13 @@
</span><span class="cx"> use Digest::MD5 qw(md5_hex);
</span><span class="cx"> use FindBin;
</span><span class="cx"> use File::Basename;
</span><ins>+use File::Find;
</ins><span class="cx"> use File::Path qw(mkpath rmtree);
</span><span class="cx"> use File::Spec;
</span><span class="cx"> use File::stat;
</span><span class="cx"> use List::Util;
</span><span class="cx"> use POSIX;
</span><ins>+use Time::HiRes qw(usleep);
</ins><span class="cx"> use VCSUtils;
</span><span class="cx"> 
</span><span class="cx"> BEGIN {
</span><span class="lines">@@ -66,11 +68,11 @@
</span><span class="cx">        &amp;findOrCreateSimulatorForIOSDevice
</span><span class="cx">        &amp;iosSimulatorDeviceByName
</span><span class="cx">        &amp;nmPath
</span><del>-       &amp;openIOSSimulator
</del><span class="cx">        &amp;passedConfiguration
</span><span class="cx">        &amp;printHelpAndExitForRunAndDebugWebKitAppIfNeeded
</span><span class="cx">        &amp;productDir
</span><span class="cx">        &amp;quitIOSSimulator
</span><ins>+       &amp;relaunchIOSSimulator
</ins><span class="cx">        &amp;runIOSWebKitApp
</span><span class="cx">        &amp;runMacWebKitApp
</span><span class="cx">        &amp;safariPath
</span><span class="lines">@@ -86,6 +88,7 @@
</span><span class="cx"> 
</span><span class="cx"> use constant USE_OPEN_COMMAND =&gt; 1; # Used in runMacWebKitApp().
</span><span class="cx"> use constant INCLUDE_OPTIONS_FOR_DEBUGGING =&gt; 1;
</span><ins>+use constant SIMULATOR_DEVICE_STATE_SHUTDOWN =&gt; &quot;1&quot;;
</ins><span class="cx"> use constant SIMULATOR_DEVICE_STATE_BOOTED =&gt; &quot;3&quot;;
</span><span class="cx"> 
</span><span class="cx"> our @EXPORT_OK;
</span><span class="lines">@@ -1150,6 +1153,9 @@
</span><span class="cx">     } readdir(DEVICES);
</span><span class="cx">     close(DEVICES);
</span><span class="cx"> 
</span><ins>+    # FIXME: We should parse the device.plist file ourself and map the dictionary keys in it to known
+    #        dictionary keys so as to decouple our representation of the plist from the actual structure
+    #        of the plist, which may change.
</ins><span class="cx">     my @devices = map {
</span><span class="cx">         Foundation::perlRefFromObjectRef(NSDictionary-&gt;dictionaryWithContentsOfFile_(&quot;$devicesPath/$_/device.plist&quot;));
</span><span class="cx">     } @udids;
</span><span class="lines">@@ -1180,23 +1186,6 @@
</span><span class="cx">     die &quot;Device $name $deviceTypeId $runtimeId wasn't found in &quot; . iOSSimulatorDevicesPath();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-sub eraseIOSSimulatorDevice($)
-{
-    my $udid = shift;
-    return exitStatus(system(&quot;xcrun&quot;, &quot;--sdk&quot;, &quot;iphonesimulator&quot;, &quot;simctl&quot;, &quot;erase&quot;, $udid)) == 0;
-}
-
-sub bootedIOSSimulatorDevice()
-{
-    my @devices = iOSSimulatorDevices();
-    for my $device (@devices) {
-        if ($device-&gt;{state} eq SIMULATOR_DEVICE_STATE_BOOTED) {
-            return $device;
-        }
-    }
-    return undef;
-}
-
</del><span class="cx"> sub willUseIOSDeviceSDKWhenBuilding()
</span><span class="cx"> {
</span><span class="cx">     return xcodeSDKPlatformName() eq &quot;iphoneos&quot;;
</span><span class="lines">@@ -2063,14 +2052,14 @@
</span><span class="cx"> {
</span><span class="cx">     my ($appBundle) = @_;
</span><span class="cx">     return &quot;$appBundle/Info.plist&quot; if -f &quot;$appBundle/Info.plist&quot;; # iOS app bundle
</span><del>-    return &quot;$appBundle/Contents/Info.plist&quot; if &quot;$appBundle/Contents/Info.plist&quot;; # Mac app bundle
</del><ins>+    return &quot;$appBundle/Contents/Info.plist&quot; if -f &quot;$appBundle/Contents/Info.plist&quot;; # Mac app bundle
</ins><span class="cx">     return &quot;&quot;;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> sub appIdentifierFromBundle($)
</span><span class="cx"> {
</span><span class="cx">     my ($appBundle) = @_;
</span><del>-    my $plistPath = plistPathFromBundle($appBundle);
</del><ins>+    my $plistPath = File::Spec-&gt;rel2abs(plistPathFromBundle($appBundle)); # defaults(1) will complain if the specified path is not absolute.
</ins><span class="cx">     chomp(my $bundleIdentifier = `defaults read '$plistPath' CFBundleIdentifier 2&gt; /dev/null`);
</span><span class="cx">     return $bundleIdentifier;
</span><span class="cx"> }
</span><span class="lines">@@ -2078,28 +2067,45 @@
</span><span class="cx"> sub appDisplayNameFromBundle($)
</span><span class="cx"> {
</span><span class="cx">     my ($appBundle) = @_;
</span><del>-    my $plistPath = plistPathFromBundle($appBundle);
</del><ins>+    my $plistPath = File::Spec-&gt;rel2abs(plistPathFromBundle($appBundle)); # defaults(1) will complain if the specified path is not absolute.
</ins><span class="cx">     chomp(my $bundleDisplayName = `defaults read '$plistPath' CFBundleDisplayName 2&gt; /dev/null`);
</span><span class="cx">     return $bundleDisplayName;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-sub openIOSSimulator($)
</del><ins>+sub waitUntilIOSSimulatorDeviceIsInState($$)
</ins><span class="cx"> {
</span><ins>+    my ($deviceUDID, $waitUntilState) = @_;
+    my $device = iosSimulatorDeviceByUDID($deviceUDID);
+    while ($device-&gt;{state} ne $waitUntilState) {
+        usleep(500 * 1000); # Waiting 500ms between file system polls does not make script run-safari feel sluggish.
+        $device = iosSimulatorDeviceByUDID($deviceUDID);
+    }
+}
+
+sub relaunchIOSSimulator($)
+{
</ins><span class="cx">     my ($simulatedDevice) = @_;
</span><ins>+    quitIOSSimulator($simulatedDevice-&gt;{UDID});
+
</ins><span class="cx">     chomp(my $developerDirectory = $ENV{DEVELOPER_DIR} || `xcode-select --print-path`);
</span><span class="cx">     my $iosSimulatorPath = File::Spec-&gt;catfile($developerDirectory, &quot;Applications&quot;, &quot;iOS Simulator.app&quot;);
</span><span class="cx"> 
</span><span class="cx">     system(&quot;open&quot;, &quot;-a&quot;, $iosSimulatorPath, &quot;--args&quot;, &quot;-CurrentDeviceUDID&quot;, $simulatedDevice-&gt;{UDID}) == 0 or die &quot;Failed to open $iosSimulatorPath: $!&quot;;
</span><del>-    my $device;
-    do {
-        $device = iosSimulatorDeviceByName($simulatedDevice-&gt;{name});
-        sleep(2);
-    } while ($device-&gt;{state} ne SIMULATOR_DEVICE_STATE_BOOTED);
</del><ins>+    waitUntilIOSSimulatorDeviceIsInState($simulatedDevice-&gt;{UDID}, SIMULATOR_DEVICE_STATE_BOOTED);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-sub quitIOSSimulator()
</del><ins>+sub quitIOSSimulator(;$)
</ins><span class="cx"> {
</span><del>-    return system {&quot;osascript&quot;} &quot;osascript&quot;, &quot;-e&quot;, 'tell application &quot;iOS Simulator&quot; to quit';
</del><ins>+    my ($waitForShutdownOfSimulatedDeviceUDID) = @_;
+    exitStatus(system {&quot;osascript&quot;} &quot;osascript&quot;, &quot;-e&quot;, 'tell application &quot;iOS Simulator&quot; to quit') == 0 or die &quot;Failed to quit iOS Simulator: $!&quot;;
+    if (!defined($waitForShutdownOfSimulatedDeviceUDID)) {
+        return;
+    }
+    # FIXME: We assume that $waitForShutdownOfSimulatedDeviceUDID was not booted using the simctl command line tool.
+    #        Otherwise we will spin indefinitely since quiting the iOS Simulator will not shutdown this device. We
+    #        should add a maximum time limit to wait for a device to shutdown and either return an error or die()
+    #        on expiration of the time limit.
+    waitUntilIOSSimulatorDeviceIsInState($waitForShutdownOfSimulatedDeviceUDID, SIMULATOR_DEVICE_STATE_SHUTDOWN);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> sub iosSimulatorDeviceByName($)
</span><span class="lines">@@ -2115,6 +2121,20 @@
</span><span class="cx">     return undef;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+sub iosSimulatorDeviceByUDID($)
+{
+    my ($simulatedDeviceUDID) = @_;
+    my $devicePlistPath = File::Spec-&gt;catfile(iOSSimulatorDevicesPath(), $simulatedDeviceUDID, &quot;device.plist&quot;);
+    if (!-f $devicePlistPath) {
+        return;
+    }
+    # FIXME: We should parse the device.plist file ourself and map the dictionary keys in it to known
+    #        dictionary keys so as to decouple our representation of the plist from the actual structure
+    #        of the plist, which may change.
+    eval &quot;require Foundation&quot;;
+    return Foundation::perlRefFromObjectRef(NSDictionary-&gt;dictionaryWithContentsOfFile_($devicePlistPath));
+}
+
</ins><span class="cx"> sub iosSimulatorRuntime()
</span><span class="cx"> {
</span><span class="cx">     my $xcodeSDKVersion = xcodeSDKVersion();
</span><span class="lines">@@ -2142,21 +2162,85 @@
</span><span class="cx"> sub isIOSSimulatorSystemInstalledApp($)
</span><span class="cx"> {
</span><span class="cx">     my ($appBundle) = @_;
</span><del>-    my $simulatorApplicationsPath = iosSimulatorApplicationsPath();
</del><ins>+    my $simulatorApplicationsPath = realpath(iosSimulatorApplicationsPath());
</ins><span class="cx">     return substr(realpath($appBundle), 0, length($simulatorApplicationsPath)) eq $simulatorApplicationsPath;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+sub hasUserInstalledAppInSimulatorDevice($$)
+{
+    my ($appIdentifier, $simulatedDeviceUDID) = @_;
+    my $userInstalledAppPath = File::Spec-&gt;catfile($ENV{HOME}, &quot;Library&quot;, &quot;Developer&quot;, &quot;CoreSimulator&quot;, &quot;Devices&quot;, $simulatedDeviceUDID, &quot;data&quot;, &quot;Containers&quot;, &quot;Bundle&quot;, &quot;Application&quot;);
+    if (!$userInstalledAppPath) {
+        return 0; # No user installed apps.
+    }
+    local @::userInstalledAppBundles;
+    sub wanted {
+        my $file = $_;
+
+        # Ignore hidden files and directories.
+        if ($file =~ /^\../) {
+            $File::Find::prune = 1;
+            return;
+        }
+
+        return if !-d $file || $file !~ /\.app$/;
+        push @::userInstalledAppBundles, $File::Find::name;
+        $File::Find::prune = 1; # Do not traverse contents of app bundle.
+    }
+    find(\&amp;wanted, $userInstalledAppPath);
+    for my $userInstalledAppBundle (@::userInstalledAppBundles) {
+        if (appIdentifierFromBundle($userInstalledAppBundle) eq $appIdentifier) {
+            return 1; # Has user installed app.
+        }
+    }
+    return 0; # Does not have user installed app.
+}
+
+sub isSimulatorDeviceBooted($)
+{
+    my ($simulatedDeviceUDID) = @_;
+    my $device = iosSimulatorDeviceByUDID($simulatedDeviceUDID);
+    return $device &amp;&amp; $device-&gt;{state} eq SIMULATOR_DEVICE_STATE_BOOTED;
+}
+
</ins><span class="cx"> sub runIOSWebKitAppInSimulator($;$)
</span><span class="cx"> {
</span><span class="cx">     my ($appBundle, $simulatorOptions) = @_;
</span><span class="cx">     my $productDir = productDir();
</span><del>-    my $appDisplayName = appDisplayNameFromBundle($appBundle);;
</del><ins>+    my $appDisplayName = appDisplayNameFromBundle($appBundle);
+    my $appIdentifier = appIdentifierFromBundle($appBundle);
</ins><span class="cx">     my $simulatedDevice = findOrCreateSimulatorForIOSDevice(&quot;For WebKit Development&quot;);
</span><ins>+    my $simulatedDeviceUDID = $simulatedDevice-&gt;{UDID};
</ins><span class="cx"> 
</span><del>-    my $bootedSimulatorDevice = bootedIOSSimulatorDevice();
-    if (!$bootedSimulatorDevice || $bootedSimulatorDevice-&gt;{UDID} ne $simulatedDevice-&gt;{UDID}) {
-        quitIOSSimulator();
-        openIOSSimulator($simulatedDevice);
</del><ins>+    my $willUseSystemInstalledApp = isIOSSimulatorSystemInstalledApp($appBundle);
+    if ($willUseSystemInstalledApp) {
+        if (hasUserInstalledAppInSimulatorDevice($appIdentifier, $simulatedDeviceUDID)) {
+            # Restore the system-installed app in the simulator device corresponding to $appBundle as it
+            # was previously overwritten with a custom built version of the app.
+            # FIXME: Only restore the system-installed version of the app instead of erasing all contents and settings.
+            print &quot;Quitting iOS Simulator...\n&quot;;
+            quitIOSSimulator($simulatedDeviceUDID);
+            print &quot;Erasing contents and settings for simulator device \&quot;$simulatedDevice-&gt;{name}\&quot;.\n&quot;;
+            exitStatus(system(&quot;xcrun&quot;, &quot;--sdk&quot;, &quot;iphonesimulator&quot;, &quot;simctl&quot;, &quot;erase&quot;, $simulatedDeviceUDID)) == 0 or die;
+        }
+        # FIXME: We assume that if $simulatedDeviceUDID is not booted then iOS Simulator is not open. However
+        #        $simulatedDeviceUDID may have been booted using the simctl command line tool. If $simulatedDeviceUDID
+        #        was booted using simctl then we should shutdown the device and launch iOS Simulator to boot it again.
+        if (!isSimulatorDeviceBooted($simulatedDeviceUDID)) {
+            print &quot;Launching iOS Simulator...\n&quot;;
+            relaunchIOSSimulator($simulatedDevice);
+        }
+    } else {
+        # FIXME: We should killall(1) any running instances of $appBundle before installing it to ensure
+        #        that simctl launch opens the latest installed version of the app. For now we quit and
+        #        launch the iOS Simulator again to ensure there are no running instances of $appBundle.
+        print &quot;Quitting and launching iOS Simulator...\n&quot;;
+        relaunchIOSSimulator($simulatedDevice);
+
+        print &quot;Installing $appBundle.\n&quot;;
+        # Install custom built app, overwriting an app with the same app identifier if one exists.
+        exitStatus(system(&quot;xcrun&quot;, &quot;--sdk&quot;, &quot;iphonesimulator&quot;, &quot;simctl&quot;, &quot;install&quot;, $simulatedDeviceUDID, $appBundle)) == 0 or die;
+
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     $simulatorOptions = {} unless $simulatorOptions;
</span><span class="lines">@@ -2176,15 +2260,8 @@
</span><span class="cx">         $ENV{&quot;SIMCTL_CHILD_$key&quot;} = $simulatorENV{$key};
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    # FIXME: We should also erase the iOS Simulator device when alternating between running a
-    # custom-built MobileSafari and the system-installed MobileSafari.
-    if (!isIOSSimulatorSystemInstalledApp($appBundle)) {
-        eraseIOSSimulatorDevice($simulatedDevice-&gt;{UDID}) or die;
-    }
-
-    my $appIdentifier = appIdentifierFromBundle($appBundle);
</del><span class="cx">     print &quot;Starting $appDisplayName with DYLD_FRAMEWORK_PATH set to point to built WebKit in $productDir.\n&quot;;
</span><del>-    return exitStatus(system(&quot;xcrun&quot;, &quot;--sdk&quot;, &quot;iphonesimulator&quot;, &quot;simctl&quot;, &quot;launch&quot;, $simulatedDevice-&gt;{UDID}, $appIdentifier, @$applicationArguments));
</del><ins>+    return exitStatus(system(&quot;xcrun&quot;, &quot;--sdk&quot;, &quot;iphonesimulator&quot;, &quot;simctl&quot;, &quot;launch&quot;, $simulatedDeviceUDID, $appIdentifier, @$applicationArguments));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> sub runIOSWebKitApp($)
</span></span></pre>
</div>
</div>

</body>
</html>