Class: EmergeCLI::XcodePhysicalDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/xcode_physical_device.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_id) ⇒ XcodePhysicalDevice

Returns a new instance of XcodePhysicalDevice.



11
12
13
# File 'lib/utils/xcode_physical_device.rb', line 11

def initialize(device_id)
  @device_id = device_id
end

Instance Attribute Details

#device_idObject (readonly)

Returns the value of attribute device_id.



9
10
11
# File 'lib/utils/xcode_physical_device.rb', line 9

def device_id
  @device_id
end

Instance Method Details

#install_app(ipa_path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/utils/xcode_physical_device.rb', line 15

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

#launch_app(bundle_id) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/utils/xcode_physical_device.rb', line 59

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