Skip to content

Instantly share code, notes, and snippets.

@pmairoldi
Last active August 29, 2015 14:18
Show Gist options
  • Save pmairoldi/0e03ae973d912b5e98cc to your computer and use it in GitHub Desktop.
Save pmairoldi/0e03ae973d912b5e98cc to your computer and use it in GitHub Desktop.
module Fastlane
module Actions
module SharedValues
DEVICES_LIST_CUSTOM_VALUE = :DEVICES_LIST_CUSTOM_VALUE
end
class ::String
def split_reject(pattern)
self.split(pattern).reject{|c|c.empty?}
end
end
class DevicesListAction
def self.device_array(input)
rawArray = input.split_reject(/^--.*.--$\s/).map{ |a| a.gsub(/^\s+/, "").gsub(/\)\s\(.*.\)$/, "").split_reject(/\r?\n|\r/) }
devices = rawArray.collect{ |a| a.map{ |b| Hash[[:name, :id].zip(b.split_reject(/\s\(/))] } }
return devices
end
def self.version_array(input)
versions = input.scan(/^--.*.--$\s/).map{|a|a.gsub(/(^--\s|\s--$|(\r?\n|\r))/, "")}.map{|b|b.scan(/\d/) * "."}
devices = device_array(input)
device_map = Hash[versions.zip(devices)]
device_map.each do |key, value|
value.each do |device|
device[:version] = key
end
end
return device_map.values.flatten
end
def self.run(params)
rawDeviceList = `xcrun simctl list devices`
availableDevices = rawDeviceList.gsub(/== Devices ==\s/,"")
removedUnavailable = availableDevices.gsub(/^.*\b[Uu]navailable\b.*$\s/, "")
versions = version_array(removedUnavailable)
return_value = Array.new
if params = params.first
has_version = params.key? :version
has_name = params.key? :name
if has_version
requested_version = params[:version].to_s
return_value = versions.select { |device| device[:version] == requested_version }
end
if has_name
requested_name = params[:name]
if has_version
return_value = return_value.select { |device| device[:name] == requested_name }
else
return_value = versions.select { |device| device[:name] == requested_name }
end
end
end
return return_value
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment