Class: EmergeCLI::XcodeSimulator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_id, environment: Environment.new) ⇒ XcodeSimulator

Returns a new instance of XcodeSimulator.



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

def initialize(device_id, environment: Environment.new)
  @device_id = device_id
  @environment = environment
end

Instance Attribute Details

#device_idObject (readonly)

Returns the value of attribute device_id.



8
9
10
# File 'lib/utils/xcode_simulator.rb', line 8

def device_id
  @device_id
end

Instance Method Details

#bootObject



15
16
17
18
19
# File 'lib/utils/xcode_simulator.rb', line 15

def boot
  Logger.info "Booting simulator #{@device_id}..."
  output = @environment.execute_command("xcrun simctl boot #{@device_id}")
  raise 'Failed to boot simulator' if output.include?('error') || output.include?('failed')
end

#install_app(ipa_path) ⇒ Object



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
58
59
60
# File 'lib/utils/xcode_simulator.rb', line 21

def install_app(ipa_path)
  raise "Non-IPA file provided: #{ipa_path}" unless ipa_path.end_with?('.ipa')

  Dir.mktmpdir do |tmp_dir|
    Logger.debug "Extracting .app from .ipa in temporary directory: #{tmp_dir}"

    Zip::File.open(ipa_path) do |zip_file|
      # Debug: List all entries to see what's in the IPA
      Logger.debug 'IPA contents:'
      zip_file.each do |entry|
        Logger.debug "  #{entry.name}"
      end

      # Try different patterns to find the .app directory
      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
      Logger.debug "Found app entry: #{app_entry.name}"

      # Extract the .app directory and its contents
      app_dir = app_entry.name.end_with?('/') ? app_entry.name.chomp('/') : app_entry.name
      pattern = "#{File.dirname(app_dir)}/#{File.basename(app_dir)}/**/*"
      Logger.debug "Using glob pattern: #{pattern}"

      zip_file.glob(pattern).each do |entry|
        entry_path = File.join(tmp_dir, entry.name)
        FileUtils.mkdir_p(File.dirname(entry_path))
        zip_file.extract(entry, entry_path) unless File.exist?(entry_path)
      end

      extracted_app = Dir.glob(File.join(tmp_dir, '**/*.app')).first
      raise 'Failed to extract .app from .ipa' unless extracted_app
      Logger.debug "Extracted app at: #{extracted_app}"

      install_extracted_app(extracted_app)
    end
  end
end

#launch_app(bundle_id) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/utils/xcode_simulator.rb', line 62

def launch_app(bundle_id)
  command = "xcrun simctl launch #{@device_id} #{bundle_id}"
  Logger.debug "Running command: #{command}"

  output = `#{command} 2>&1`
  success = $CHILD_STATUS.success?

  unless success
    Logger.debug "Launch command output: #{output}"
    raise "Failed to launch app #{bundle_id} on simulator"
  end

  true
end