./lib/shenzhen/xcodebuild.rb in shenzhen-0.2.1 vs ./lib/shenzhen/xcodebuild.rb in shenzhen-0.2.2
- old
+ new
@@ -1,63 +1,97 @@
require 'ostruct'
+require 'shellwords'
module Shenzhen::XcodeBuild
class Info < OpenStruct; end
+ class Settings < OpenStruct
+ include Enumerable
+ def initialize(hash = {})
+ super
+ self.targets = hash.keys
+ end
+
+ def members
+ self.targets
+ end
+
+ def each
+ members.each do |target|
+ yield target, send(target)
+ end
+
+ self
+ end
+ end
+
class Error < StandardError; end
class NilOutputError < Error; end
class << self
- def info
- output = `xcodebuild -list 2> /dev/null`
+ def info(*args)
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ output = `xcodebuild -list #{Shellwords.join(args + args_from_options(options))} 2>&1`
raise Error.new $1 if /^xcodebuild\: error\: (.+)$/ === output
raise NilOutputError unless /\S/ === output
lines = output.split(/\n/)
- hash = {}
- group = nil
+ info, group = {}, nil
- hash[:project] = lines.shift.match(/\"(.+)\"\:/)[1]
+ info[:project] = lines.shift.match(/\"(.+)\"\:/)[1]
lines.each do |line|
if /\:$/ === line
group = line.strip[0...-1].downcase.gsub(/\s+/, '_')
- hash[group] = []
+ info[group] = []
next
end
unless group.nil? or /\.$/ === line
- hash[group] << line.strip
+ info[group] << line.strip
end
end
- hash.each do |group, values|
+ info.each do |group, values|
next unless Array === values
values.delete("") and values.uniq!
end
- Info.new(hash)
+ Info.new(info)
end
- def settings(flags = [])
- output = `xcodebuild #{flags.join(' ')} -showBuildSettings 2> /dev/null`
+ def settings(*args)
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ output = `xcodebuild #{(args + args_from_options(options)).join(" ")} -showBuildSettings 2> /dev/null`
raise Error.new $1 if /^xcodebuild\: error\: (.+)$/ === output
raise NilOutputError unless /\S/ === output
lines = output.split(/\n/)
lines.shift
- hash = {}
+ settings, target = {}, nil
lines.each do |line|
- key, value = line.split(/\=/).collect(&:strip)
- hash[key] = value
+ case line
+ when /Build settings for action build and target (\w+)/
+ target = $1
+ settings[target] = {}
+ else
+ key, value = line.split(/\=/).collect(&:strip)
+ settings[target][key] = value if target
+ end
end
- hash
+ Settings.new(settings)
end
def version
output = `xcodebuild -version`
- output.scan(/([\d\.?]+)/).flatten.first rescue nil
+ output.scan(/([\d+\.?]+)/).flatten.first rescue nil
+ end
+
+ private
+
+ def args_from_options(options = {})
+ options.reject{|key, value| value.nil?}.collect{|key, value| "-#{key} #{value}"}
end
end
end