Class: SimLauncher::Simulator

Inherits:
Object
  • Object
show all
Defined in:
lib/sim_launcher/simulator.rb

Instance Method Summary collapse

Constructor Details

#initialize(iphonesim_path_external = nil) ⇒ Simulator

Returns a new instance of Simulator.



5
6
7
# File 'lib/sim_launcher/simulator.rb', line 5

def initialize( iphonesim_path_external = nil )
  @iphonesim_path = iphonesim_path_external || iphonesim_path
end

Instance Method Details

#cmd_line_with_args(args) ⇒ Object



87
88
89
90
# File 'lib/sim_launcher/simulator.rb', line 87

def cmd_line_with_args( args )
  cmd_sections = [@iphonesim_path] + args.map{ |x| "\"#{x.to_s}\"" } << '2>&1'
  cmd_sections.join(' ')
end

#iphonesim_pathObject



101
102
103
104
105
106
107
108
109
# File 'lib/sim_launcher/simulator.rb', line 101

def iphonesim_path
  installed = `which ios-sim`
  if installed =~ /(.*ios-sim)/
    puts "Using installed ios-sim at #{$1}" if $DEBUG
    return $1
  end

  raise "Didn't find the ios-sim binary in your $PATH.\n\nPlease install, e.g. using Homebrew:\n\tbrew install ios-sim\n\n"
end

#launch_ios_app(app_path, sdk_version, device_family, app_args = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/sim_launcher/simulator.rb', line 48

def launch_ios_app(app_path, sdk_version, device_family, app_args = nil)
  if problem = SimLauncher.check_app_path( app_path )
    bangs = '!'*80
    raise "\n#{bangs}\nENCOUNTERED A PROBLEM WITH THE SPECIFIED APP PATH:\n\n#{problem}\n#{bangs}"
  end
  sdk_version ||= SdkDetector.new(self).latest_sdk_version
  args = ["--args"] + app_args.flatten if app_args
  run_synchronous_command( :launch, app_path, '--sdk', sdk_version, '--family', device_family, '--exit', *args )
end

#launch_ipad_app(app_path, sdk) ⇒ Object



58
59
60
# File 'lib/sim_launcher/simulator.rb', line 58

def launch_ipad_app( app_path, sdk )
  launch_ios_app( app_path, sdk, 'ipad' )
end

#launch_ipad_app_with_name(app_name, sdk) ⇒ Object



62
63
64
65
# File 'lib/sim_launcher/simulator.rb', line 62

def launch_ipad_app_with_name( app_name, sdk )
  app_path = SimLauncher.app_bundle_or_raise(app_name)
  launch_ios_app( app_path, sdk, 'iphone' )
end

#launch_iphone_app(app_path, sdk) ⇒ Object



67
68
69
# File 'lib/sim_launcher/simulator.rb', line 67

def launch_iphone_app( app_path, sdk )
  launch_ios_app( app_path, sdk, 'iphone' )
end

#launch_iphone_app_with_name(app_name, sdk) ⇒ Object



71
72
73
74
# File 'lib/sim_launcher/simulator.rb', line 71

def launch_iphone_app_with_name( app_name, sdk )
  app_path = SimLauncher.app_bundle_or_raise(app_name)
  launch_ios_app( app_path, sdk, 'iphone' )
end

#quit_simulatorObject



76
77
78
# File 'lib/sim_launcher/simulator.rb', line 76

def quit_simulator
  `echo 'application "iPhone Simulator" quit' | osascript`
end

#reset(sdks = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sim_launcher/simulator.rb', line 31

def reset(sdks=nil)
  script_dir = File.join(File.dirname(__FILE__),"..","..","scripts")
  reset_script = File.expand_path("#{script_dir}/reset_simulator.applescript")

  sdks ||= SimLauncher::SdkDetector.new(self).available_sdk_versions

  sdks.each do |sdk_path_str|
    start_simulator(sdk_path_str,"iphone")
    system("osascript #{reset_script}")
    start_simulator(sdk_path_str,"ipad")
    system("osascript #{reset_script}")
  end

  quit_simulator

end

#rotate_leftObject



19
20
21
22
23
# File 'lib/sim_launcher/simulator.rb', line 19

def rotate_left
  script_dir = File.join(File.dirname(__FILE__),"..","..","scripts")
  rotate_script = File.expand_path("#{script_dir}/rotate_simulator_left.applescript")
  system("osascript #{rotate_script}")
end

#rotate_rightObject



25
26
27
28
29
# File 'lib/sim_launcher/simulator.rb', line 25

def rotate_right
  script_dir = File.join(File.dirname(__FILE__),"..","..","scripts")
  rotate_script = File.expand_path("#{script_dir}/rotate_simulator_right.applescript")
  system("osascript #{rotate_script}")
end

#run_synchronous_command(*args) ⇒ Object



80
81
82
83
84
85
# File 'lib/sim_launcher/simulator.rb', line 80

def run_synchronous_command( *args )
  args.compact!
  cmd = cmd_line_with_args( args )
  puts "executing #{cmd}" if $DEBUG
  `#{cmd}`
end

#showsdksObject



9
10
11
# File 'lib/sim_launcher/simulator.rb', line 9

def showsdks
  run_synchronous_command( 'showsdks' )
end

#start_simulator(sdk_version = nil, device_family = "iphone") ⇒ Object



13
14
15
16
# File 'lib/sim_launcher/simulator.rb', line 13

def start_simulator(sdk_version=nil, device_family="iphone")
  sdk_version ||= SdkDetector.new(self).latest_sdk_version
  run_synchronous_command( :start, '--sdk', sdk_version, '--family', device_family, '--exit' )
end

#xcode_versionObject



92
93
94
95
96
97
98
99
# File 'lib/sim_launcher/simulator.rb', line 92

def xcode_version
  version_out = `xcodebuild -version`
  begin
    Float(version_out[/([0-9]\.[0-9])/, 1])
  rescue => ex
    raise "Cannot determine xcode version: #{ex}"
  end
end