Skip to content

Commit

Permalink
Reuse iOS simulators (#7)
Browse files Browse the repository at this point in the history
* Reuse iOS simulators

* newline on EOF

* no message
  • Loading branch information
inf2381 authored Dec 12, 2024
1 parent 762b831 commit 3f9a6e0
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions lib/fastlane/plugin/maestro/actions/launch_simulator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,71 @@ module Fastlane
module Actions
class LaunchSimulatorAction < Action
def self.run(params)
device_type = SimCtl.devicetype(name: params[:device_name])
FastlaneCore::PrintTable.print_values(config: params,
title: "Summary for launch_simulator #{Fastlane::Maestro::VERSION}")
device_name = "Maestro - #{params[:device_name]}"
runtime_name = params[:ios_version].nil? || params[:ios_version].empty? ? SimCtl.list_runtimes()[0].name : "iOS #{params[:ios_version]}"
runtime = SimCtl.runtime(name: runtime_name)

runtime_name = ''
if params[:ios_version].nil? || params[:ios_version].empty?
runtime_name = SimCtl.list_runtimes()[0].name
else
runtime_name = "iOS #{params[:ios_version]}"
# Verify runtime identifier
if runtime.nil?
UI.user_error!("Could not find a runtime matching #{runtime_name}")
end
runtime = SimCtl.runtime(name: runtime_name)

# List available devices
available_devices = SimCtl.list_devices

# Manually filter devices
existing_device = available_devices.find { |d| d.name == device_name && d.os == runtime.identifier }
if existing_device
if existing_device.state == :booted
UI.message("Device #{device_name} is already running. Patching settings…")
patch_device_settings(existing_device, params)
return existing_device
elsif existing_device.state == :shutdown
UI.message("Device #{device_name} is shutdown. Booting device…")
existing_device.boot
existing_device.wait { |d| d.state == :booted }
patch_device_settings(existing_device, params)
return existing_device
end
end

# Create and boot the device if it doesn't exist
UI.message("Creating device #{device_name} with runtime #{runtime_name}…")
device_type = SimCtl.devicetype(name: params[:device_name])
device = SimCtl.create_device(device_name, device_type, runtime)

device.boot

UI.message("Waiting for device to boot")
device.wait { |d| d.state == :booted }
patch_device_settings(device, params)

UI.message("Installing app #{params[:app_path]} on the simulator")
device.install(params[:app_path])

return device
end

def self.patch_device_settings(device, params)
unless params[:language].nil? || params[:language].empty?
UI.message("Setting device language to #{params[:language]}")
device.settings.set_language(params[:language])
device.settings.set_locale(params[:language])
end

UI.message("Patching device settings for tests")
time = Time.new(2007, 1, 9, 9, 41, 0)
device.status_bar.clear
device.status_bar.override(
time: time.iso8601,
time: '9:41', # Time.new(2007, 1, 9, 9, 41, 0).iso8601 not possible as of iOS 18.2
dataNetwork: '5g',
wifiMode: 'active',
cellularMode: 'active',
batteryState: 'charging',
batteryLevel: 100
)
device.settings.disable_keyboard_helpers

device.launch

UI.message("Installing app #{params[:app_path]} on the simulator")
device.install(params[:app_path])

return device
end

def self.description
Expand Down Expand Up @@ -95,9 +117,6 @@ def self.available_options
end

def self.is_supported?(platform)
# Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
# See: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
#
[:ios].include?(platform)
end
end
Expand Down

0 comments on commit 3f9a6e0

Please sign in to comment.