Extract the ruby script for finding iOS simulators to a separate file

This commit is contained in:
Thomas Goyne 2017-09-26 10:35:30 -07:00
parent 9a31febc4c
commit d278649fee
2 changed files with 54 additions and 32 deletions

View File

@ -0,0 +1,22 @@
#!/usr/bin/ruby
require 'json'
ios_sim_default_device_type = ENV["IOS_SIM_DEVICE_TYPE"] or "iPhone 5s"
ios_sim_default_ios_version = ENV["IOS_SIM_OS"] or "iOS 10.1"
mode = ARGV[0]
devices = JSON.parse(%x{xcrun simctl list devices --json})['devices']
.each { |os, group| group.each{ |dev| dev['os'] = os } }
.flat_map { |x| x[1] }
if mode == "booted" then
device = devices.select{|x| x['state'] == 'Booted'}
else
device = devices
.select{ |x| x['availability'] == '(available)' }
.each { |x| x['score'] = (x['name'] == '$ios_sim_default_device_type' ? 1 : 0) + (x['os'] == '$ios_sim_default_ios_version' ? 1 : 0) }
.sort_by! { |x| [x['score'], x['name']] }
.reverse!
end
puts device[0]['udid']

View File

@ -14,8 +14,6 @@ if echo "$CONFIGURATION" | grep -i "^Debug$" > /dev/null ; then
fi
IOS_SIM_DEVICE=${IOS_SIM_DEVICE:-} # use preferentially, otherwise will be set and re-exported
ios_sim_default_device_type=${IOS_SIM_DEVICE_TYPE:-iPhone 5s}
ios_sim_default_ios_version=${IOS_SIM_OS:-iOS 10.1}
PATH="/opt/android-sdk-linux/platform-tools:$PATH"
SRCROOT=$(cd "$(dirname "$0")/.." && pwd)
@ -193,14 +191,15 @@ setup_ios_simulator() {
# - Choose a device, if it has not already been chosen
local deadline=$((SECONDS+5))
while [ -z "$IOS_SIM_DEVICE" ] && [ $SECONDS -lt $deadline ]; do
IOS_DEVICE=$(ruby -rjson -e "puts JSON.parse(%x{xcrun simctl list devices --json})['devices'].each{|os,group| group.each{|dev| dev['os'] = os}}.flat_map{|x| x[1]}.select{|x| x['availability'] == '(available)'}.each{|x| x['score'] = (x['name'] == '$ios_sim_default_device_type' ? 1 : 0) + (x['os'] == '$ios_sim_default_ios_version' ? 1 : 0)}.sort_by!{|x| [x['score'], x['name']]}.reverse![0]['udid']")
export IOS_SIM_DEVICE=$IOS_DEVICE
IOS_DEVICE=""
while [ -z "$IOS_DEVICE" ] && [ $SECONDS -lt $deadline ]; do
IOS_DEVICE="$(ruby $SRCROOT/scripts/find-ios-device.rb best)"
done
if [ -z "$IOS_SIM_DEVICE" ]; then
if [ -z "$IOS_DEVICE" ]; then
echo "*** Failed to determine the iOS Simulator device to use ***"
exit 1
fi
export IOS_SIM_DEVICE=$IOS_DEVICE
# - Reset the device we will be using if running in CI
xcrun simctl shutdown "$IOS_SIM_DEVICE" 1>/dev/null 2>/dev/null || true # sometimes simctl gets confused
@ -219,14 +218,15 @@ setup_ios_simulator() {
# - Select the first device booted in the simulator, since it will boot something for us
local deadline=$((SECONDS+10))
while [ -z "$IOS_SIM_DEVICE" ] && [ $SECONDS -lt $deadline ]; do
IOS_DEVICE=$(ruby -rjson -e "puts JSON.parse(%x{xcrun simctl list devices --json})['devices'].each{|os,group| group.each{|dev| dev['os'] = os}}.flat_map{|x| x[1]}.select{|x| x['state'] == 'Booted'}[0]['udid']")
export IOS_SIM_DEVICE=$IOS_DEVICE
IOS_DEVICE=""
while [ -z "$IOS_DEVICE" ] && [ $SECONDS -lt $deadline ]; do
IOS_DEVICE="$(ruby $SRCROOT/scripts/find-ios-device.rb booted)"
done
if [ -z "$IOS_SIM_DEVICE" ]; then
if [ -z "$IOS_DEVICE" ]; then
echo "*** Failed to determine the iOS Simulator device in use ***"
exit 1
fi
export IOS_SIM_DEVICE=$IOS_DEVICE
fi
# Wait until the boot completes