Module: RunLoop::Core

Defined in:
lib/run_loop/core.rb

Constant Summary collapse

START_DELIMITER =
"OUTPUT_JSON:\n"
END_DELIMITER =
"\nEND_OUTPUT"
SCRIPTS =
{
  :dismiss => 'run_dismiss_location.js',
  :run_loop_host => 'run_loop_host.js',
  :run_loop_fast_uia => 'run_loop_fast_uia.js',
  :run_loop_shared_element => 'run_loop_shared_element.js',
  :run_loop_basic => 'run_loop_basic.js'
}
SCRIPTS_PATH =
File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'scripts'))
READ_SCRIPT_PATH =
File.join(SCRIPTS_PATH, 'read-cmd.sh')
TIMEOUT_SCRIPT_PATH =
File.join(SCRIPTS_PATH, 'timeout3')

Class Method Summary collapse

Class Method Details

.above_or_eql_version?(target_version, xcode_version) ⇒ Boolean

Deprecated.

since 1.0.0

still used extensively in calabash-ios launcher

Returns:

  • (Boolean)

773
774
775
776
777
778
779
780
781
782
783
784
785
786
# File 'lib/run_loop/core.rb', line 773

def self.above_or_eql_version?(target_version, xcode_version)
  if target_version.is_a?(RunLoop::Version)
    target = target_version
  else
    target = RunLoop::Version.new(target_version)
  end

  if xcode_version.is_a?(RunLoop::Version)
    xcode = xcode_version
  else
    xcode = RunLoop::Version.new(xcode_version)
  end
  target >= xcode
end

.automation_template(instruments, candidate = RunLoop::Environment.trace_template) ⇒ Object


712
713
714
715
716
717
# File 'lib/run_loop/core.rb', line 712

def self.automation_template(instruments, candidate=RunLoop::Environment.trace_template)
  unless candidate && File.exist?(candidate)
    candidate = default_tracetemplate(instruments)
  end
  candidate
end

.create_uia_pipe(repl_path) ⇒ Object


548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'lib/run_loop/core.rb', line 548

def self.create_uia_pipe(repl_path)
begin
  Timeout::timeout(5, RunLoop::TimeoutError) do
    loop do
      begin
        FileUtils.rm_f(repl_path)
        return repl_path if system(%Q[mkfifo "#{repl_path}"])
      rescue Errno::EINTR => e
        #retry
        sleep(0.1)
      end
    end
  end
rescue RunLoop::TimeoutError => _
    raise RunLoop::TimeoutError, 'Unable to create pipe (mkfifo failed)'
  end
end

.default_simulator(xcode = RunLoop::Xcode.new) ⇒ Object

Returns the a default simulator to target. This default needs to be one that installed by default in the current Xcode version.

For historical reasons, the most recent non-64b SDK should be used.

Parameters:

  • xcode (RunLoop::Xcode) (defaults to: RunLoop::Xcode.new)

    Used to detect the current xcode version.


475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/run_loop/core.rb', line 475

def self.default_simulator(xcode=RunLoop::Xcode.new)

  if xcode.version_gte_72?
    "iPhone 6s (9.2)"
  elsif xcode.version_gte_71?
    "iPhone 6s (9.1)"
  elsif xcode.version_gte_7?
    "iPhone 5s (9.0)"
  elsif xcode.version_gte_64?
    "iPhone 5s (8.4 Simulator)"
  elsif xcode.version_gte_63?
    "iPhone 5s (8.3 Simulator)"
  elsif xcode.version_gte_62?
    "iPhone 5s (8.2 Simulator)"
  elsif xcode.version_gte_61?
    "iPhone 5s (8.1 Simulator)"
  elsif xcode.version_gte_6?
    "iPhone 5s (8.0 Simulator)"
  else
    "iPhone Retina (4-inch) - Simulator - iOS 7.1"
  end
end

.default_tracetemplate(instruments = RunLoop::Instruments.new) ⇒ Object


719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'lib/run_loop/core.rb', line 719

def self.default_tracetemplate(instruments=RunLoop::Instruments.new)

  templates = instruments.templates

  # xcrun instruments -s templates
  # Xcode >= 6 will return known, Apple defined tracetemplates as names
  #  e.g.  Automation, Zombies, Allocations
  #
  # Xcode < 6 will return known, Apple defined tracetemplates as paths.
  #
  # Xcode 6 Beta versions also return paths, but revert to 'normal'
  # behavior when GM is released.
  #
  # Xcode 7 Beta versions appear to behavior like Xcode 6 Beta versions.
  template = templates.find { |name| name == 'Automation' }
  return template if template

  candidate = templates.find do |path|
    path =~ /\/Automation.tracetemplate/ and path =~ /Xcode/
  end

  if !candidate.nil?
    return candidate.tr("\"", '').strip
  end

  message = ['Expected instruments to report an Automation tracetemplate.',
          'Please report this as bug:  https://github.com/calabash/run_loop/issues',
          "In the bug report, include the output of:\n",
          '$ xcrun xcodebuild -version',
          "$ xcrun instruments -s templates\n"]
  raise message.join("\n")
end

.detect_connected_deviceObject


60
61
62
63
64
65
66
67
68
69
# File 'lib/run_loop/core.rb', line 60

def self.detect_connected_device
  begin
    Timeout::timeout(1, RunLoop::TimeoutError) do
      return `#{File.join(scripts_path, 'udidetect')}`.chomp
    end
  rescue RunLoop::TimeoutError => _
    `killall udidetect &> /dev/null`
  end
  nil
end

.dylib_path_from_options(options) ⇒ String?

Extracts the value of :inject_dylib from options Hash.

Parameters:

Returns:

  • (String, nil)

    If the options contains :inject_dylibs and it is a path to a dylib that exists, return the path. Otherwise return nil or raise an error.

Raises:

  • (RuntimeError)

    If :inject_dylib points to a path that does not exist.

  • (ArgumentError)

    If :inject_dylib is not a String.


455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/run_loop/core.rb', line 455

def self.dylib_path_from_options(options)
  inject_dylib = options.fetch(:inject_dylib, nil)
  return nil if inject_dylib.nil?
  unless inject_dylib.is_a? String
    raise ArgumentError, "Expected :inject_dylib to be a path to a dylib, but found '#{inject_dylib}'"
  end
  dylib_path = File.expand_path(inject_dylib)
  unless File.exist?(dylib_path)
    raise "Cannot load dylib.  The file '#{dylib_path}' does not exist."
  end
  dylib_path
end

.ensure_instruments_not_running!Object

Deprecated.

1.0.5


753
754
755
# File 'lib/run_loop/core.rb', line 753

def self.ensure_instruments_not_running!
  RunLoop::Instruments.new.kill_instruments
end

.escape_host_command(cmd) ⇒ Object


609
610
611
612
# File 'lib/run_loop/core.rb', line 609

def self.escape_host_command(cmd)
  backquote = "\\"
  cmd.gsub(backquote,backquote*4)
end

.expect_compatible_simulator_architecture(launch_options, sim_control) ⇒ Object

Deprecated.

1.5.2 No public replacement.

Note:

This method is implemented for CoreSimulator environments only; for Xcode < 6.0 this method does nothing.

Raise an error if the application binary is not compatible with the target simulator.

Parameters:

  • launch_options (Hash)

    These options need to contain the app bundle path and a udid that corresponds to a simulator name or simulator udid. In practical terms: call this after merging the original launch options with those options that are discovered.

  • sim_control (RunLoop::SimControl)

    A simulator control object.

Raises:


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/run_loop/core.rb', line 89

def self.expect_compatible_simulator_architecture(launch_options, sim_control)
  RunLoop.deprecated('1.5.2', 'No public replacement.')
  logger = launch_options[:logger]
  if sim_control.xcode_version_gte_6?
    sim_identifier = launch_options[:udid]
    simulator = sim_control.simulators.find do |simulator|
      [simulator.instruments_identifier(sim_control.xcode),
       simulator.udid].include?(sim_identifier)
    end

    if simulator.nil?
      raise "Could not find simulator with identifier '#{sim_identifier}'"
    end

    lipo = RunLoop::Lipo.new(launch_options[:bundle_dir_or_bundle_id])
    lipo.expect_compatible_arch(simulator)
    RunLoop::Logging.log_debug(logger, "Simulator instruction set '#{simulator.instruction_set}' is compatible with #{lipo.info}")
    true
  else
    RunLoop::Logging.log_debug(logger, "Xcode #{sim_control.xcode_version} detected; skipping simulator architecture check.")
    false
  end
end

.expect_simulator_compatible_arch(device, app, xcode) ⇒ Object

Note:

This method is implemented for CoreSimulator environments only; for Xcode < 6.0 this method does nothing.

Raise an error if the application binary is not compatible with the target simulator.

Parameters:

Raises:


125
126
127
128
129
130
131
132
133
134
135
# File 'lib/run_loop/core.rb', line 125

def self.expect_simulator_compatible_arch(device, app, xcode)
  if !xcode.version_gte_6?
    RunLoop.log_warn("Checking for compatible arches is only available in CoreSimulator environments")
    return
  end

  lipo = RunLoop::Lipo.new(app.path)
  lipo.expect_compatible_arch(device)

  RunLoop.log_debug("Simulator instruction set '#{device.instruction_set}' is compatible with '#{lipo.info}'")
end

.instruments_pidsObject

Deprecated.

1.0.5


762
763
764
# File 'lib/run_loop/core.rb', line 762

def self.instruments_pids
  RunLoop::Instruments.new.instruments_pids
end

.instruments_running?Boolean

Returns:

  • (Boolean)

757
758
759
# File 'lib/run_loop/core.rb', line 757

def self.instruments_running?
  RunLoop::Instruments.new.instruments_running?
end

.jruby?Boolean

Returns:

  • (Boolean)

566
567
568
# File 'lib/run_loop/core.rb', line 566

def self.jruby?
  RUBY_PLATFORM == 'java'
end

.log_instruments_error(msg) ⇒ Object


614
615
616
617
# File 'lib/run_loop/core.rb', line 614

def self.log_instruments_error(msg)
  $stderr.puts "\033[31m\n\n*** #{msg} ***\n\n\033[0m"
  $stderr.flush
end

.log_run_loop_options(options, xcode) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/run_loop/core.rb', line 34

def self.log_run_loop_options(options, xcode)
  return unless RunLoop::Environment.debug?
  # Ignore :sim_control b/c it is a ruby object; printing is not useful.
  ignored_keys = [:sim_control]
  options_to_log = {}
  options.each_pair do |key, value|
    next if ignored_keys.include?(key)
    options_to_log[key] = value
  end
  # Objects that override '==' cannot be printed by awesome_print
  # https://github.com/michaeldv/awesome_print/issues/154
  # RunLoop::Version overrides '=='
  options_to_log[:xcode] = xcode.version.to_s
  options_to_log[:xcode_path] = xcode.developer_dir
  message = options_to_log.ai({:sort_keys => true})
  logger = options[:logger]
  RunLoop::Logging.log_debug(logger, "\n" + message)
end

.pids_for_run_loop(run_loop, &block) ⇒ Object

Deprecated.

1.0.5


789
790
791
# File 'lib/run_loop/core.rb', line 789

def self.pids_for_run_loop(run_loop, &block)
  RunLoop::Instruments.new.instruments_pids(&block)
end

.prepare_simulator(launch_options, sim_control) ⇒ Object

Prepares the simulator for running.

  1. enabling accessibility and software keyboard

  2. installing / uninstalling apps


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/run_loop/core.rb', line 141

def self.prepare_simulator(launch_options, sim_control)

  xcode = sim_control.xcode

  # Respect option passed from Calabash
  if launch_options[:relaunch_simulator]
    sim_control.quit_sim
  end

  if !xcode.version_gte_6?
    # Xcode 5.1.1

    # Will quit the simulator!
    sim_control.enable_accessibility_on_sims({:verbose => false})
  else

    # CoreSimulator

    app_bundle_path = launch_options[:bundle_dir_or_bundle_id]
    app = RunLoop::App.new(app_bundle_path)

    unless app.valid?
      if !File.exist?(app.path)
        message = "App '#{app_bundle_path}' does not exist."
      else
        message = "App '#{app_bundle_path}' is not a valid .app bundle"
      end
      raise RuntimeError, message
    end

    udid = launch_options[:udid]

    device = sim_control.simulators.find do |sim|
      sim.udid == udid || sim.instruments_identifier(xcode) == udid
    end

    if device.nil?
      raise RuntimeError,
            "Could not find simulator with name or UDID that matches: '#{udid}'"
    end

    # Validate the architecture.
    self.expect_simulator_compatible_arch(device, app, xcode)

    # Quits the simulator.
    core_sim = RunLoop::CoreSimulator.new(device, app)

    # :reset is a legacy variable; has been replaced with :reset_app_sandbox
    if launch_options[:reset] || launch_options[:reset_app_sandbox]
      core_sim.reset_app_sandbox
    end

    # Will quit the simulator if it is running.
    # @todo fix accessibility_enabled? so we don't have to quit the sim
    # SimControl#accessibility_enabled? is always false during Core#prepare_simulator
    # https://github.com/calabash/run_loop/issues/167
    sim_control.ensure_accessibility(device)

    # Will quit the simulator if it is running.
    # @todo fix software_keyboard_enabled? so we don't have to quit the sim
    # SimControl#software_keyboard_enabled? is always false during Core#prepare_simulator
    # https://github.com/calabash/run_loop/issues/168
    sim_control.ensure_software_keyboard(device)

    # Launches the simulator if the app is not installed.
    core_sim.install

    # If CoreSimulator has already launched the simulator, it will not
    # launching it again.
    core_sim.launch_simulator
  end
end

.read_response(run_loop, expected_index, empty_file_timeout = 10, search_for_property = 'index') ⇒ Object


619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'lib/run_loop/core.rb', line 619

def self.read_response(run_loop, expected_index, empty_file_timeout=10, search_for_property='index')
  debug_read = RunLoop::Environment.debug_read?

  log_file = run_loop[:log_file]
  initial_offset = run_loop[:initial_offset] || 0
  offset = initial_offset

  result = nil
  loop do
    unless File.exist?(log_file) && File.size?(log_file)
      sleep(0.2)
      next
    end

    size = File.size(log_file)
    output = File.read(log_file, size-offset, offset)

    if /AXError: Could not auto-register for pid status change/.match(output)
      if /kAXErrorServerNotFound/.match(output)
        self.log_instruments_error('Accessibility is not enabled on device/simulator, please enable it.')
      end
      raise RunLoop::TimeoutError.new('AXError: Could not auto-register for pid status change')
    end

    if /Automation Instrument ran into an exception/.match(output)
      raise RunLoop::TimeoutError.new('Exception while running script')
    end

    if /FBSOpenApplicationErrorDomain error/.match(output)
      msg = "Instruments failed to launch app: 'FBSOpenApplicationErrorDomain error 8"
      if RunLoop::Environment.debug?
        self.log_instruments_error(msg)
      end
      raise RunLoop::TimeoutError.new(msg)
    end

    if /Error: Script threw an uncaught JavaScript error: unknown JavaScript exception/.match(output)
      msg = "Instruments failed to launch: because of an unknown JavaScript exception"
      if RunLoop::Environment.debug?
        self.log_instruments_error(msg)
      end
      raise RunLoop::TimeoutError.new(msg)
    end

    index_if_found = output.index(START_DELIMITER)
    if debug_read
      puts output.gsub('*', '')
      puts "Size #{size}"
      puts "offset #{offset}"
      puts "index_of #{START_DELIMITER}: #{index_if_found}"
    end

    if index_if_found

      offset = offset + index_if_found
      rest = output[index_if_found+START_DELIMITER.size..output.length]
      index_of_json = rest.index("}#{END_DELIMITER}")

      if index_of_json.nil?
        #Wait for rest of json
        sleep(0.1)
        next
      end

      json = rest[0..index_of_json]


      if debug_read
        puts "Index #{index_if_found}, Size: #{size} Offset #{offset}"

        puts ("parse #{json}")
      end

      offset = offset + json.size
      parsed_result = JSON.parse(json)
      if debug_read
        p parsed_result
      end
      json_index_if_present = parsed_result[search_for_property]
      if json_index_if_present && json_index_if_present == expected_index
        result = parsed_result
        break
      end
    else
      sleep(0.1)
    end
  end

  run_loop[:initial_offset] = offset
  RunLoop::HostCache.default.write(run_loop) unless RunLoop::Environment.xtc?
  result
end

.run_with_options(options) ⇒ Object


214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/run_loop/core.rb', line 214

def self.run_with_options(options)
  before = Time.now

  self.prepare(options)

  logger = options[:logger]
  sim_control ||= options[:sim_control] || RunLoop::SimControl.new

  xcode ||= options[:xcode] || sim_control.xcode

  instruments = RunLoop::Instruments.new
  instruments.kill_instruments(xcode)

  device_target = options[:udid] || options[:device_target] || detect_connected_device || 'simulator'
  if device_target && device_target.to_s.downcase == 'device'
    device_target = detect_connected_device
  end

  log_file = options[:log_path]
  timeout = options[:timeout] || 30

  results_dir = options[:results_dir] || RunLoop::DotDir.make_results_dir
  results_dir_trace = File.join(results_dir, 'trace')
  FileUtils.mkdir_p(results_dir_trace)

  dependencies = options[:dependencies] || []
  dependencies << File.join(scripts_path, 'calabash_script_uia.js')
  dependencies.each do |dep|
    FileUtils.cp(dep, results_dir)
  end

  script = File.join(results_dir, '_run_loop.js')

  code = UIAScriptTemplate.new(SCRIPTS_PATH, options[:script]).result
  code = code.gsub(/\$PATH/, results_dir)
  code = code.gsub(/\$READ_SCRIPT_PATH/, READ_SCRIPT_PATH)
  code = code.gsub(/\$TIMEOUT_SCRIPT_PATH/, TIMEOUT_SCRIPT_PATH)
  code = code.gsub(/\$MODE/, 'FLUSH') unless options[:no_flush]

  repl_path = File.join(results_dir, 'repl-cmd.pipe')
  FileUtils.rm_f(repl_path)

  uia_strategy = options[:uia_strategy]

  if uia_strategy == :host
    create_uia_pipe(repl_path)
  else
    FileUtils.touch repl_path
  end

  RunLoop::HostCache.default.clear unless RunLoop::Environment.xtc?

  cal_script = File.join(SCRIPTS_PATH, 'calabash_script_uia.js')
  File.open(script, 'w') do |file|
    if include_calabash_script?(options)
      file.puts IO.read(cal_script)
    end
    file.puts code
  end

  udid = options[:udid]
  bundle_dir_or_bundle_id = options[:bundle_dir_or_bundle_id]

  if !(udid && bundle_dir_or_bundle_id)
    # Compute udid and bundle_dir / bundle_id from options and target depending on Xcode version
    udid, bundle_dir_or_bundle_id = self.udid_and_bundle_for_launcher(device_target, options, sim_control)
  end

  args = options.fetch(:args, [])

  log_file ||= File.join(results_dir, 'run_loop.out')

  discovered_options =
        {
              :udid => udid,
              :results_dir_trace => results_dir_trace,
              :bundle_dir_or_bundle_id => bundle_dir_or_bundle_id,
              :results_dir => results_dir,
              :script => script,
              :log_file => log_file,
              :args => args
        }
  merged_options = options.merge(discovered_options)

  if self.simulator_target?(merged_options)
    self.prepare_simulator(merged_options, sim_control)
  end

  self.log_run_loop_options(merged_options, xcode)

  automation_template = automation_template(instruments)

  RunLoop::Logging.log_header(logger, "Starting on #{device_target} App: #{bundle_dir_or_bundle_id}")

  pid = instruments.spawn(automation_template, merged_options, log_file)

  File.open(File.join(results_dir, 'run_loop.pid'), 'w') do |f|
    f.write pid
  end

  run_loop = {:pid => pid,
              :index => 1,
              :uia_strategy => uia_strategy,
              :udid => udid,
              :app => bundle_dir_or_bundle_id,
              :repl_path => repl_path,
              :log_file => log_file,
              :results_dir => results_dir}

  uia_timeout = options[:uia_timeout] || RunLoop::Environment.uia_timeout || 10

  RunLoop::Logging.log_debug(logger, "Preparation took #{Time.now-before} seconds")

  before_instruments_launch = Time.now

   fifo_retry_on = [RunLoop::Fifo::NoReaderConfiguredError,
                    RunLoop::Fifo::WriteTimedOut]

  begin

    if options[:validate_channel]
      options[:validate_channel].call(run_loop, 0, uia_timeout)
    else

      cmd = "UIALogger.logMessage('Listening for run loop commands')"

      begin

        fifo_timeout = options[:fifo_timeout] || 30
        RunLoop::Fifo.write(repl_path, "0:#{cmd}", timeout: fifo_timeout)

      rescue *fifo_retry_on => e

        message = "Error while writing to fifo. #{e}"
        RunLoop::Logging.log_debug(logger, message)
        raise RunLoop::TimeoutError.new(message)

      end

      Timeout::timeout(timeout, RunLoop::TimeoutError) do
        read_response(run_loop, 0, uia_timeout)
      end

    end
  rescue RunLoop::TimeoutError => e
    RunLoop::Logging.log_debug(logger, "Failed to launch. #{e}: #{e && e.message}")

    message = %Q(

"Timed out waiting for UIAutomation run-loop #{e}.

Logfile: #{log_file}

#{File.read(log_file)}

)
    raise RunLoop::TimeoutError, message
  end

  RunLoop::Logging.log_debug(logger, "Launching took #{Time.now-before_instruments_launch} seconds")

  dylib_path = self.dylib_path_from_options(merged_options)

  if dylib_path
    RunLoop::LLDB.kill_lldb_processes
    app = RunLoop::App.new(options[:app])
    lldb = RunLoop::DylibInjector.new(app.executable_name, dylib_path)
    lldb.retriable_inject_dylib
  end

  RunLoop.log_debug("It took #{Time.now - before} seconds to launch the app")
  run_loop
end

.script_for_key(key) ⇒ Object


53
54
55
56
57
58
# File 'lib/run_loop/core.rb', line 53

def self.script_for_key(key)
  if SCRIPTS[key].nil?
    return nil
  end
  SCRIPTS[key]
end

.scripts_pathObject


30
31
32
# File 'lib/run_loop/core.rb', line 30

def self.scripts_path
  SCRIPTS_PATH
end

.udid_and_bundle_for_launcher(device_target, options, sim_control = RunLoop::SimControl.new) ⇒ Object


498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/run_loop/core.rb', line 498

def self.udid_and_bundle_for_launcher(device_target, options, sim_control=RunLoop::SimControl.new)
  xcode = sim_control.xcode

  bundle_dir_or_bundle_id = options[:app] || RunLoop::Environment.bundle_id || RunLoop::Environment.path_to_app_bundle

  unless bundle_dir_or_bundle_id
    raise 'key :app or environment variable APP_BUNDLE_PATH, BUNDLE_ID or APP must be specified as path to app bundle (simulator) or bundle id (device)'
  end

  udid = nil

  if xcode.version_gte_51?
    if device_target.nil? || device_target.empty? || device_target == 'simulator'
      device_target = self.default_simulator(xcode)
    end
    udid = device_target

    unless self.simulator_target?(options)
      bundle_dir_or_bundle_id = options[:bundle_id] if options[:bundle_id]
    end
  else
    #TODO: this can be removed - Xcode < 5.1.1 not supported.
    if device_target == 'simulator'

      unless File.exist?(bundle_dir_or_bundle_id)
        raise "Unable to find app in directory #{bundle_dir_or_bundle_id} when trying to launch simulator"
      end


      device = options[:device] || :iphone
      device = device && device.to_sym

      plistbuddy='/usr/libexec/PlistBuddy'
      plistfile="#{bundle_dir_or_bundle_id}/Info.plist"
      if device == :iphone
        uidevicefamily=1
      else
        uidevicefamily=2
      end
      system("#{plistbuddy} -c 'Delete :UIDeviceFamily' '#{plistfile}'")
      system("#{plistbuddy} -c 'Add :UIDeviceFamily array' '#{plistfile}'")
      system("#{plistbuddy} -c 'Add :UIDeviceFamily:0 integer #{uidevicefamily}' '#{plistfile}'")
    else
      udid = device_target
      bundle_dir_or_bundle_id = options[:bundle_id] if options[:bundle_id]
    end
  end
  return udid, bundle_dir_or_bundle_id
end

.validate_index_written(run_loop, index, logger) ⇒ Object


596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/run_loop/core.rb', line 596

def self.validate_index_written(run_loop, index, logger)
  begin
    Timeout::timeout(10, RunLoop::TimeoutError) do
      Core.read_response(run_loop, index, 10, 'last_index')
    end
    RunLoop::Logging.log_debug(logger, "validate index written for index #{index} ok")
    return true
  rescue RunLoop::TimeoutError => _
    RunLoop::Logging.log_debug(logger, "validate index written for index #{index} failed. Retrying.")
    return false
  end
end

.write_request(run_loop, cmd, logger = nil) ⇒ Object


570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'lib/run_loop/core.rb', line 570

def self.write_request(run_loop, cmd, logger=nil)
  repl_path = run_loop[:repl_path]
  index = run_loop[:index]
  cmd_str = "#{index}:#{escape_host_command(cmd)}"
  RunLoop::Logging.log_debug(logger, cmd_str)
  write_succeeded = false
  2.times do |i|
    RunLoop::Logging.log_debug(logger, "Trying write of command #{cmd_str} at index #{index}")
    begin
      RunLoop::Fifo.write(repl_path, cmd_str)
      write_succeeded = validate_index_written(run_loop, index, logger)
    rescue RunLoop::Fifo::NoReaderConfiguredError,
           RunLoop::Fifo::WriteTimedOut => e
      RunLoop::Logging.log_debug(logger, "Error while writing command (retry count #{i}). #{e}")
    end
    break if write_succeeded
  end
  unless write_succeeded
    RunLoop::Logging.log_debug(logger, 'Failing...Raising RunLoop::WriteFailedError')
    raise RunLoop::WriteFailedError.new("Trying write of command #{cmd_str} at index #{index}")
  end
  run_loop[:index] = index + 1
  RunLoop::HostCache.default.write(run_loop) unless RunLoop::Environment.xtc?
  index
end

.xcode_version(xcode = RunLoop::Xcode.new) ⇒ Object

Deprecated.

1.0.0 replaced with Xctools#version


767
768
769
# File 'lib/run_loop/core.rb', line 767

def self.xcode_version(xcode=RunLoop::Xcode.new)
  xcode.version
end