-
Notifications
You must be signed in to change notification settings - Fork 1
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
Overhaul how iOS build distribution installation works #49
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
386685e
progress
trevor-e 164b755
progress
trevor-e 397e08b
progress
trevor-e 27be4ef
progress
trevor-e 1ba7f14
progress
trevor-e c6ed273
progress
trevor-e d946a8b
progress
trevor-e 771f5fd
progress
trevor-e 9515e37
progress
trevor-e 8f398a9
Check whether the device type is supported
trevor-e 0bd6735
Unused
trevor-e 0313f31
Add tests
trevor-e a9c76cf
cop
trevor-e 31cd0b6
test
trevor-e d9ca2ed
rename
trevor-e a3eac5e
test
trevor-e 2164a96
huh
trevor-e fc8eac2
lint
trevor-e faf5c10
lint
trevor-e 553e3e4
log tweaks
trevor-e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module EmergeCLI | ||
class Environment | ||
def execute_command(command) | ||
`#{command}` | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
require 'json' | ||
require_relative 'xcode_simulator' | ||
require 'zip' | ||
require 'cfpropertylist' | ||
|
||
module EmergeCLI | ||
class XcodeDeviceManager | ||
class DeviceType | ||
VIRTUAL = :virtual | ||
PHYSICAL = :physical | ||
ANY = :any | ||
end | ||
|
||
def initialize(environment: Environment.new) | ||
@environment = environment | ||
end | ||
|
||
class << self | ||
def get_supported_platforms(ipa_path) | ||
return [] unless ipa_path&.end_with?('.ipa') | ||
|
||
Zip::File.open(ipa_path) do |zip_file| | ||
app_entry = zip_file.glob('**/*.app/').first || | ||
zip_file.glob('**/*.app').first || | ||
zip_file.find { |entry| entry.name.end_with?('.app/') || entry.name.end_with?('.app') } | ||
|
||
raise 'No .app found in .ipa file' unless app_entry | ||
|
||
app_dir = app_entry.name.end_with?('/') ? app_entry.name.chomp('/') : app_entry.name | ||
info_plist_path = "#{app_dir}/Info.plist" | ||
info_plist_entry = zip_file.find_entry(info_plist_path) | ||
raise 'Info.plist not found in app bundle' unless info_plist_entry | ||
|
||
info_plist_content = info_plist_entry.get_input_stream.read | ||
plist = CFPropertyList::List.new(data: info_plist_content) | ||
info_plist = CFPropertyList.native_types(plist.value) | ||
|
||
info_plist['CFBundleSupportedPlatforms'] || [] | ||
end | ||
end | ||
end | ||
|
||
def find_device_by_id(device_id) | ||
Logger.debug "Looking for device with ID: #{device_id}" | ||
devices_json = execute_command('xcrun xcdevice list') | ||
devices_data = JSON.parse(devices_json) | ||
|
||
found_device = devices_data.find { |device| device['identifier'] == device_id } | ||
raise "No device found with ID: #{device_id}" unless found_device | ||
|
||
device_type = found_device['simulator'] ? 'simulator' : 'physical' | ||
Logger.info "✅ Found device: #{found_device['name']} " \ | ||
"(#{found_device['identifier']}, #{device_type})" | ||
if found_device['simulator'] | ||
XcodeSimulator.new(found_device['identifier']) | ||
else | ||
XcodePhysicalDevice.new(found_device['identifier']) | ||
end | ||
end | ||
|
||
def find_device_by_type(device_type, ipa_path) | ||
case device_type | ||
when DeviceType::VIRTUAL | ||
find_and_boot_most_recently_used_simulator | ||
when DeviceType::PHYSICAL | ||
find_connected_device | ||
when DeviceType::ANY | ||
# Check supported platforms in Info.plist to make intelligent choice | ||
supported_platforms = self.class.get_supported_platforms(ipa_path) | ||
Logger.debug "Build supports platforms: #{supported_platforms.join(', ')}" | ||
|
||
if supported_platforms.include?('iPhoneOS') | ||
device = find_connected_device | ||
return device if device | ||
|
||
# Only fall back to simulator if it's also supported | ||
unless supported_platforms.include?('iPhoneSimulator') | ||
raise 'Build only supports physical devices, but no device is connected' | ||
end | ||
Logger.info 'No physical device found, falling back to simulator since build supports both' | ||
find_and_boot_most_recently_used_simulator | ||
|
||
elsif supported_platforms.include?('iPhoneSimulator') | ||
find_and_boot_most_recently_used_simulator | ||
else | ||
raise "Build doesn't support either physical devices or simulators" | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def execute_command(command) | ||
@environment.execute_command(command) | ||
end | ||
|
||
def find_connected_device | ||
Logger.info 'Finding connected device...' | ||
devices_json = execute_command('xcrun xcdevice list') | ||
Logger.debug "Device list output: #{devices_json}" | ||
|
||
devices_data = JSON.parse(devices_json) | ||
physical_devices = devices_data | ||
.select do |device| | ||
device['simulator'] == false && | ||
device['ignored'] == false && | ||
device['available'] == true && | ||
device['platform'] == 'com.apple.platform.iphoneos' | ||
end | ||
|
||
Logger.debug "Found physical devices: #{physical_devices}" | ||
|
||
if physical_devices.empty? | ||
Logger.info 'No physical connected device found' | ||
return nil | ||
end | ||
|
||
device = physical_devices.first | ||
Logger.info "Found connected physical device: #{device['name']} (#{device['identifier']})" | ||
XcodePhysicalDevice.new(device['identifier']) | ||
end | ||
|
||
def find_and_boot_most_recently_used_simulator | ||
Logger.info 'Finding and booting most recently used simulator...' | ||
simulators_json = execute_command('xcrun simctl list devices --json') | ||
Logger.debug "Simulators JSON: #{simulators_json}" | ||
|
||
simulators_data = JSON.parse(simulators_json) | ||
|
||
simulators = simulators_data['devices'].flat_map do |runtime, devices| | ||
next [] unless runtime.include?('iOS') # Only include iOS devices | ||
|
||
devices.select do |device| | ||
(device['name'].start_with?('iPhone', 'iPad') && | ||
device['isAvailable'] && | ||
!device['isDeleted']) | ||
end.map do |device| | ||
version = runtime.match(/iOS-(\d+)-(\d+)/)&.captures&.join('.').to_f | ||
last_booted = device['lastBootedAt'] ? Time.parse(device['lastBootedAt']) : Time.at(0) | ||
[device['udid'], device['state'], version, last_booted] | ||
end | ||
end.sort_by { |_, _, _, last_booted| last_booted }.reverse | ||
|
||
Logger.debug "Simulators: #{simulators}" | ||
|
||
raise 'No available simulator found' unless simulators.any? | ||
|
||
simulator_id, simulator_state, version, last_booted = simulators.first | ||
version_str = version.zero? ? '' : " (#{version})" | ||
last_booted_str = last_booted == Time.at(0) ? 'never' : last_booted.strftime('%Y-%m-%d %H:%M:%S') | ||
Logger.info "Found simulator #{simulator_id}#{version_str} (#{simulator_state}, last booted: #{last_booted_str})" | ||
|
||
simulator = XcodeSimulator.new(simulator_id, environment: @environment) | ||
simulator.boot unless simulator_state == 'Booted' | ||
simulator | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
require 'English' | ||
require 'timeout' | ||
require 'zip' | ||
require 'cfpropertylist' | ||
require 'fileutils' | ||
|
||
module EmergeCLI | ||
class XcodePhysicalDevice | ||
attr_reader :device_id | ||
|
||
def initialize(device_id) | ||
@device_id = device_id | ||
end | ||
|
||
def install_app(ipa_path) | ||
raise "Non-IPA file provided: #{ipa_path}" unless ipa_path.end_with?('.ipa') | ||
|
||
Logger.info "Installing app to device #{@device_id}..." | ||
|
||
begin | ||
# Set a timeout since I've noticed xcrun devicectl can occasionally hang for invalid apps | ||
Timeout.timeout(60) do | ||
command = "xcrun devicectl device install app --device #{@device_id} \"#{ipa_path}\"" | ||
Logger.debug "Running command: #{command}" | ||
|
||
output = `#{command} 2>&1` | ||
Logger.debug "Install command output: #{output}" | ||
|
||
if output.include?('ERROR:') || output.include?('error:') | ||
if output.include?('This provisioning profile cannot be installed on this device') | ||
bundle_id = extract_bundle_id_from_error(output) | ||
raise "Failed to install app: The provisioning profile for #{bundle_id} is not " \ | ||
"valid for this device. Make sure the device's UDID is included in the " \ | ||
'provisioning profile.' | ||
elsif output.include?('Unable to Install') | ||
error_message = output.match(/Unable to Install.*\n.*NSLocalizedRecoverySuggestion = ([^\n]+)/)&.[](1) | ||
check_device_compatibility(ipa_path) | ||
raise "Failed to install app: #{error_message || 'Unknown error'}" | ||
else | ||
check_device_compatibility(ipa_path) | ||
raise "Failed to install app: #{output}" | ||
end | ||
end | ||
|
||
success = $CHILD_STATUS.success? | ||
unless success | ||
check_device_compatibility(ipa_path) | ||
raise "Installation failed with exit code #{$CHILD_STATUS.exitstatus}" | ||
end | ||
end | ||
rescue Timeout::Error | ||
raise 'Installation timed out after 30 seconds. The device might be locked or ' \ | ||
'installation might be stuck. Try unlocking the device and trying again.' | ||
end | ||
|
||
true | ||
end | ||
|
||
def launch_app(bundle_id) | ||
command = "xcrun devicectl device process launch --device #{@device_id} #{bundle_id}" | ||
Logger.debug "Running command: #{command}" | ||
|
||
begin | ||
Timeout.timeout(30) do | ||
output = `#{command} 2>&1` | ||
success = $CHILD_STATUS.success? | ||
|
||
unless success | ||
Logger.debug "Launch command output: #{output}" | ||
if output.include?('The operation couldn\'t be completed. Application is restricted') | ||
raise 'Failed to launch app: The app is restricted. Make sure the device is ' \ | ||
'unlocked and the app is allowed to run.' | ||
elsif output.include?('The operation couldn\'t be completed. Unable to launch') | ||
raise 'Failed to launch app: Unable to launch. The app might be in a bad state - ' \ | ||
'try uninstalling and reinstalling.' | ||
else | ||
raise "Failed to launch app #{bundle_id} on device: #{output}" | ||
end | ||
end | ||
end | ||
rescue Timeout::Error | ||
raise 'Launch timed out after 30 seconds. The device might be locked. ' \ | ||
'Try unlocking the device and trying again.' | ||
end | ||
|
||
true | ||
end | ||
|
||
private | ||
|
||
def check_device_compatibility(ipa_path) | ||
supported_platforms = XcodeDeviceManager.get_supported_platforms(ipa_path) | ||
Logger.debug "Supported platforms: #{supported_platforms.join(', ')}" | ||
|
||
unless supported_platforms.include?('iPhoneOS') | ||
raise 'This build is not compatible with physical devices. Please use a simulator ' \ | ||
'or make your build compatible with physical devices.' | ||
end | ||
|
||
Logger.debug 'Build is compatible with physical devices' | ||
end | ||
|
||
def extract_bundle_id_from_error(output) | ||
# Extract bundle ID from error message like "...profile for com.emerge.hn.Hacker-News :" | ||
output.match(/profile for ([\w\.-]+) :/)&.[](1) || 'unknown bundle ID' | ||
end | ||
end | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went back and forth on the naming here and whether I should just use
IosDeviceManager
but didn't want to lock us into iOS platform