class Xcodeproj::Config
provides support for serialization.
This class holds the data for a Xcode build settings file (xcconfig) and
def ==(other)
def ==(other) other.respond_to?(:to_hash) && other.to_hash == self.to_hash end
def dup
-
(Config)- A copy of the receiver.
def dup Xcodeproj::Config.new(self.to_hash.dup) end
def extract_hash(argument)
-
(Hash)-
Parameters:
-
argument(String, Pathname, Hash) --
def extract_hash(argument) if argument.respond_to? :read hash_from_file_content(argument.read) elsif File.readable?(argument.to_s) hash_from_file_content(File.read(argument)) else argument end end
def extract_include(line)
-
(Nil)- if no include was found in the line. -
(String)- the included file.
Parameters:
-
line(String) --
def extract_include(line) regexp = /#include\s*"(.+)"/ match = line.match(regexp) match[1] if match end
def extract_key_value(line)
-
(Array)- A tuple where the first entry is the key and the second
Parameters:
-
line(String) --
def extract_key_value(line) key, value = line.split('=', 2) if key && value [key.strip, value.strip] else [] end end
def hash_from_file_content(string)
-
(Hash)- the hash containing the xcconfig data.
Parameters:
-
string(String) --
def hash_from_file_content(string) hash = {} string.split("\n").each do |line| uncommented_line = strip_comment(line) if include = extract_include(uncommented_line) @includes.push include else key, value = extract_key_value(uncommented_line) hash[key] = value if key end end hash end
def initialize(xcconfig_hash_or_file = {})
-
xcconfig_hash_or_file(Hash, File, String) --
def initialize(xcconfig_hash_or_file = {}) @attributes = {} @includes = [] @frameworks, @weak_frameworks, @libraries = Set.new, Set.new, Set.new merge!(extract_hash(xcconfig_hash_or_file)) end
def inspect
def inspect to_hash.inspect end
def merge(config)
-
(Config)- the new xcconfig.
Parameters:
-
config(Hash, Config) --
def merge(config) self.dup.tap { |x| x.merge!(config) } end
def merge!(xcconfig)
-
(void)-
Other tags:
- Todo: - The logic to normalize an hash should be extracted and the
Parameters:
-
config(Hash, Config) --
Other tags:
- Note: - If a key in the given hash already exists in the internal data
def merge!(xcconfig) if xcconfig.is_a? Config merge_attributes!(xcconfig.attributes) @libraries.merge(xcconfig.libraries) @frameworks.merge(xcconfig.frameworks) @weak_frameworks.merge(xcconfig.weak_frameworks) else merge_attributes!(xcconfig.to_hash) # Parse frameworks and libraries. Then remove them from the linker # flags flags = @attributes['OTHER_LDFLAGS'] return unless flags frameworks = flags.scan(/-framework\s+([^\s]+)/).map { |m| m[0] } weak_frameworks = flags.scan(/-weak_framework\s+([^\s]+)/).map { |m| m[0] } libraries = flags.scan(/-l ?([^\s]+)/).map { |m| m[0] } @frameworks.merge frameworks @weak_frameworks.merge weak_frameworks @libraries.merge libraries new_flags = flags.dup frameworks.each {|f| new_flags.gsub!("-framework #{f}", "") } weak_frameworks.each {|f| new_flags.gsub!("-weak_framework #{f}", "") } libraries.each {|l| new_flags.gsub!("-l#{l}", ""); new_flags.gsub!("-l #{l}", "") } @attributes['OTHER_LDFLAGS'] = new_flags.gsub("\w*", ' ').strip end end
def merge_attributes!(attributes)
-
(void)-
Parameters:
-
attributes(Hash) --
def merge_attributes!(attributes) @attributes.merge!(attributes) do |_, v1, v2| v1, v2 = v1.strip, v2.strip existing = v1.strip.shellsplit existing.include?(v2) ? v1 : "#{v1} #{v2}" end end
def normalized_xcconfig_path(path)
-
(String)- The normalized path.
Parameters:
-
path(String) --
def normalized_xcconfig_path(path) if File.extname(path) == '.xcconfig' path else "#{path}.xcconfig" end end
def save_as(pathname, prefix = nil)
-
(void)-
Parameters:
-
pathname(Pathname) --
def save_as(pathname, prefix = nil) pathname.open('w') { |file| file << to_s(prefix) } end
def strip_comment(line)
-
(String)- the uncommented line.
Parameters:
-
line(String) --
def strip_comment(line) line.partition('//').first end
def to_hash(prefix = nil)
-
(Hash)- The hash representation
Other tags:
- Note: - All the values are sorted to have a consistent output in Ruby
def to_hash(prefix = nil) hash = @attributes.dup flags = hash['OTHER_LDFLAGS'] || '' flags = flags.dup.strip flags << libraries.to_a.sort.reduce('') {| memo, l | memo << " -l#{l}" } flags << frameworks.to_a.sort.reduce('') {| memo, f | memo << " -framework #{f}" } flags << weak_frameworks.to_a.sort.reduce('') {| memo, f | memo << " -weak_framework #{f}" } hash['OTHER_LDFLAGS'] = flags.strip hash.delete('OTHER_LDFLAGS') if flags.strip.empty? if prefix Hash[hash.map {|k, v| [prefix + k, v]}] else hash end end
def to_s(prefix = nil)
-
(String)- The serialized internal data.
def to_s(prefix = nil) include_lines = includes.map { |path| "#include \"#{normalized_xcconfig_path(path)}\""} settings = to_hash(prefix).sort_by(&:first).map { |k, v| "#{k} = #{v}" } [include_lines + settings].join("\n") end