Skip to content

Commit

Permalink
Add ios app launcher with options, document code
Browse files Browse the repository at this point in the history
- Add new method  to launch simulator with options
- Keep  for backwards compatibility
- Modify , ,  and  to support options and app arguments
  - Make update safe for backwards compatibility
- Document all the code to get 100 percent documentation coverage (used yardoc)
  • Loading branch information
Maksym Grebenets committed Oct 17, 2013
1 parent f17ce92 commit a4121c1
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 105 deletions.
18 changes: 15 additions & 3 deletions lib/sim_launcher.rb
Expand Up @@ -3,18 +3,24 @@
require 'sim_launcher/simulator'
require 'sim_launcher/sdk_detector'

# Simulator Launcher
module SimLauncher

# Default derived data path
DERIVED_DATA = File.expand_path("~/Library/Developer/Xcode/DerivedData")
# Default derived data info plist
DEFAULT_DERIVED_DATA_INFO = File.expand_path("#{DERIVED_DATA}/*/info.plist")

def self.check_app_path( app_path )
unless File.exists?( app_path )
# Check app path
# @param [String] app_path app path to check
# @return [String] Error message or _nil_ on success
def self.check_app_path(app_path)
unless File.exists?(app_path)
return "The specified app path doesn't seem to exist: #{app_path}"
end

unless File.directory? app_path
file_appears_to_be_a_binary = !!( `file "#{app_path}"` =~ /Mach-O executable/ )
file_appears_to_be_a_binary = !!(`file "#{app_path}"` =~ /Mach-O executable/)
if file_appears_to_be_a_binary
return <<-EOS
The specified app path is a binary executable, rather than a directory. You need to provide the path to the app *bundle*, not the app executable itself.
Expand All @@ -29,6 +35,9 @@ def self.check_app_path( app_path )
nil
end

# Get derived data directory for the project
# @param [String] project_name Name of the project
# @return [String] Derived data directory path
def self.derived_data_dir_for_project_name(project_name)

build_dirs = Dir.glob("#{DERIVED_DATA}/*").find_all do |xc_proj|
Expand Down Expand Up @@ -65,6 +74,9 @@ def self.derived_data_dir_for_project_name(project_name)
end
end

# Get app bundle for path or raise an exception
# @param [String] path Path
# @return [String] App bundle path
def self.app_bundle_or_raise(path)
bundle_path = nil

Expand Down
43 changes: 34 additions & 9 deletions lib/sim_launcher/client.rb
Expand Up @@ -2,30 +2,49 @@
require 'cgi'
require 'net/http'

# Simulator Launcher
module SimLauncher
# Client
class Client
# Default server URI
DEFAULT_SERVER_URI = "http://localhost:8881"

def initialize( app_path, sdk, family )
@app_path = File.expand_path( app_path )
# Initialized
# @param [String] app_path App path
# @param [String] sdk SDK
# @param [String] family Device family
def initialize(app_path, sdk, family)
@app_path = File.expand_path(app_path)
@sdk = sdk
@family = family
self.server_uri = DEFAULT_SERVER_URI
end

def self.for_ipad_app( app_path, sdk = nil )
self.new( app_path, sdk, 'ipad' )
# Client for iPad app
# @param [String] app_path App path
# @param [String] sdk SDK
# @return [Client] Client object for iPad app
def self.for_ipad_app(app_path, sdk = nil)
self.new(app_path, sdk, 'ipad')
end

def self.for_iphone_app( app_path, sdk = nil )
self.new( app_path, sdk, 'iphone' )
# Client for iPhone app
# @param [String] app_path App path
# @param [String] sdk SDK
# @return [Client] Client object for iPhone app
def self.for_iphone_app(app_path, sdk = nil)
self.new(app_path, sdk, 'iphone')
end

# Set server URI
# @param [URI] uri Server URI
def server_uri=(uri)
@server_uri = URI.parse( uri.to_s )
end

def launch(restart=false)
# Launch
# @param [Boolean] restart Restart if this flag is _true_
def launch(restart = false)
begin
full_request_uri = launch_uri(restart)
puts "requesting #{full_request_uri}" if $DEBUG
Expand All @@ -38,14 +57,15 @@ def launch(restart=false)
end
end

# Relaunch
def relaunch
launch(true)
end

# check that there appears to be a server ready for us to send commands to
# Check that there appears to be a server ready for us to send commands to
def ping
# our good-enough solution is just request the list of available iOS sdks and
# check that we get a 200 response
# check that we get a 200 response
begin
uri = list_sdks_uri
Net::HTTP.start( uri.host, uri.port) do |http|
Expand All @@ -59,6 +79,9 @@ def ping

private

# Lauch with URI
# @param [Boolean] requesting_restart Restart request flag
# @return [URI] Full request URI
def launch_uri(requesting_restart)
full_request_uri = @server_uri.dup
full_request_uri.path = "/launch_#{@family}_app"
Expand All @@ -68,6 +91,8 @@ def launch_uri(requesting_restart)
full_request_uri
end

# List SDKs URI
# @param [URI] SDKs URI
def list_sdks_uri
full_request_uri = @server_uri.dup
full_request_uri.path = "/showsdks"
Expand Down
37 changes: 27 additions & 10 deletions lib/sim_launcher/direct_client.rb
@@ -1,35 +1,52 @@
# Simulator Launcher
module SimLauncher
# Direct Client
class DirectClient
def initialize( app_path, sdk, family )
@app_path = File.expand_path( app_path )

# Initialize
# @param [String] app_path App path
# @param [String] sdk SDK
# @param [String] family Device family
def initialize(app_path, sdk, family)
@app_path = File.expand_path(app_path)
@sdk = sdk
@family = family
end

def self.for_ipad_app( app_path, sdk = nil )
self.new( app_path, sdk, 'ipad' )
# Direct client for iPad app
# @param [String] app_path App path
# @param [String] sdk SDK
# @return [DirectClient] Direct client object for iPad app
def self.for_ipad_app(app_path, sdk = nil)
self.new(app_path, sdk, 'ipad')
end

def self.for_iphone_app( app_path, sdk = nil )
self.new( app_path, sdk, 'iphone' )
# Direct client for iPhone app
# @param [String] app_path App path
# @param [String] sdk SDK
# @return [DirectClient] Direct client object for iPhone app
def self.for_iphone_app(app_path, sdk = nil)
self.new(app_path, sdk, 'iphone')
end

# Launch
def launch
SimLauncher::Simulator.new.launch_ios_app( @app_path, @sdk, @family )
SimLauncher::Simulator.new.launch_ios_app(@app_path, @sdk, @family)
end

# Rotate left
def rotate_left
simulator = SimLauncher::Simulator.new
simulator.rotate_left
end


# Rotate right
def rotate_right
simulator = SimLauncher::Simulator.new
simulator.rotate_right
end



# Relaunch
def relaunch
simulator = SimLauncher::Simulator.new
simulator.quit_simulator
Expand Down
8 changes: 8 additions & 0 deletions lib/sim_launcher/sdk_detector.rb
@@ -1,16 +1,24 @@
# Simulator Launcher
module SimLauncher
# SDK Detector
class SdkDetector

# Initialize
# @param [Simulator] simulator Simulator to initialize with
def initialize(simulator = Simulator.new)
@simulator = simulator
end

# Get available SDK versions
# @return [String] SDK versions
def available_sdk_versions
@simulator.showsdks.split("\n").map { |sdk_line|
sdk_line[/\(([\d.]+)\)$/,1] # grab any "(x.x)" part at the end of the line
}.compact
end

# Get latest SDK version
# @return [String] Latest SDK version
def latest_sdk_version
available_sdk_versions.sort.last
end
Expand Down

0 comments on commit a4121c1

Please sign in to comment.