Class: Xcodeproj::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/xcodeproj/config.rb,
lib/xcodeproj/config/other_linker_flags_parser.rb

Overview

This class holds the data for a Xcode build settings file (xcconfig) and provides support for serialization.

Defined Under Namespace

Modules: OtherLinkerFlagsParser

Instance Attribute Summary collapse

Serialization collapse

Merging collapse

Instance Method Summary collapse

Constructor Details

#initialize(xcconfig_hash_or_file = {}) ⇒ Config



58
59
60
61
62
63
64
65
66
# File 'lib/xcodeproj/config.rb', line 58

def initialize(xcconfig_hash_or_file = {})
  @attributes = {}
  @includes = []
  @other_linker_flags = {}
  [:simple, :frameworks, :weak_frameworks, :libraries, :arg_files, :force_load].each do |key|
    @other_linker_flags[key] = Set.new
  end
  merge!(extract_hash(xcconfig_hash_or_file))
end

Instance Attribute Details

#attributesHash{String => String}



42
43
44
# File 'lib/xcodeproj/config.rb', line 42

def attributes
  @attributes
end

#includesArray



53
54
55
# File 'lib/xcodeproj/config.rb', line 53

def includes
  @includes
end

#other_linker_flagsHash{Symbol => Set<String>}



48
49
50
# File 'lib/xcodeproj/config.rb', line 48

def other_linker_flags
  @other_linker_flags
end

Instance Method Details

#==(other) ⇒ Object



72
73
74
# File 'lib/xcodeproj/config.rb', line 72

def ==(other)
  other.attributes == attributes && other.other_linker_flags == other_linker_flags && other.includes == includes
end

#arg_filesSet<String>



188
189
190
# File 'lib/xcodeproj/config.rb', line 188

def arg_files
  other_linker_flags[:arg_files]
end

#dupConfig



248
249
250
# File 'lib/xcodeproj/config.rb', line 248

def dup
  Xcodeproj::Config.new(to_hash.dup)
end

#frameworksSet<String>



167
168
169
# File 'lib/xcodeproj/config.rb', line 167

def frameworks
  other_linker_flags[:frameworks]
end

#inspectObject



68
69
70
# File 'lib/xcodeproj/config.rb', line 68

def inspect
  to_hash.inspect
end

#librariesSet<String>



181
182
183
# File 'lib/xcodeproj/config.rb', line 181

def libraries
  other_linker_flags[:libraries]
end

#merge(config) ⇒ Config

Creates a new #Xcodeproj::Config with the data of the receiver merged with the given xcconfig representation.



242
243
244
# File 'lib/xcodeproj/config.rb', line 242

def merge(config)
  dup.tap { |x| x.merge!(config) }
end

#merge!(xcconfig) ⇒ void Also known as: <<

TODO:

The logic to normalize an hash should be extracted and the initializer should not call this method.

Note:

If a key in the given hash already exists in the internal data then its value is appended.

This method returns an undefined value.

Merges the given xcconfig representation in the receiver.

Examples:


config = Config.new('PODS_ROOT' => '"$(SRCROOT)/Pods"', 'OTHER_LDFLAGS' => '-lxml2')
config.merge!('OTHER_LDFLAGS' => '-lz', 'HEADER_SEARCH_PATHS' => '"$(PODS_ROOT)/Headers"')
config.to_hash # => { 'PODS_ROOT' => '"$(SRCROOT)/Pods"', 'OTHER_LDFLAGS' => '-lxml2 -lz', 'HEADER_SEARCH_PATHS' => '"$(PODS_ROOT)/Headers"' }


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/xcodeproj/config.rb', line 216

def merge!(xcconfig)
  if xcconfig.is_a? Config
    merge_attributes!(xcconfig.attributes)
    other_linker_flags.keys.each do |key|
      other_linker_flags[key].merge(xcconfig.other_linker_flags[key])
    end
  else
    merge_attributes!(xcconfig.to_hash)
    if flags = attributes.delete('OTHER_LDFLAGS')
      flags_by_key = OtherLinkerFlagsParser.parse(flags)
      other_linker_flags.keys.each do |key|
        other_linker_flags[key].merge(flags_by_key[key])
      end
    end
  end
end

#save_as(pathname, prefix = nil) ⇒ void

This method returns an undefined value.

Writes the serialized representation of the internal data to the given path.



105
106
107
108
109
110
111
# File 'lib/xcodeproj/config.rb', line 105

def save_as(pathname, prefix = nil)
  if File.exist?(pathname)
    return if Config.new(pathname) == self
  end

  pathname.open('w') { |file| file << to_s(prefix) }
end

#to_hash(prefix = nil) ⇒ Hash Also known as: to_h

Note:

All the values are sorted to have a consistent output in Ruby 1.8.7.

The hash representation of the xcconfig. The hash includes the frameworks, the weak frameworks, the libraries and the simple other linker flags in the ‘Other Linker Flags` (OTHER_LDFLAGS).



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/xcodeproj/config.rb', line 122

def to_hash(prefix = nil)
  list = []
  list += other_linker_flags[:simple].to_a.sort
  modifiers = {
    :frameworks => '-framework ',
    :weak_frameworks => '-weak_framework ',
    :libraries => '-l',
    :arg_files => '@',
    :force_load => '-force_load',
  }
  [:libraries, :frameworks, :weak_frameworks, :arg_files, :force_load].each do |key|
    modifier = modifiers[key]
    sorted = other_linker_flags[key].to_a.sort
    if key == :force_load
      list += sorted.map { |l| %(#{modifier} #{l}) }
    else
      list += sorted.map { |l| %(#{modifier}"#{l}") }
    end
  end

  result = attributes.dup
  result['OTHER_LDFLAGS'] = list.join(' ') unless list.empty?
  result.reject! { |_, v| INHERITED.any? { |i| i == v.to_s.strip } }

  result = @includes.map do |incl|
    path = File.expand_path(incl, @filepath.dirname)
    if File.readable? path
      Xcodeproj::Config.new(path).to_hash
    else
      {}
    end
  end.inject(&:merge).merge(result) unless @filepath.nil? || @includes.empty?

  if prefix
    Hash[result.map { |k, v| [prefix + k, v] }]
  else
    result
  end
end

#to_s(prefix = nil) ⇒ String

Sorts the internal data by setting name and serializes it in the xcconfig format.

Examples:


config = Config.new('PODS_ROOT' => '"$(SRCROOT)/Pods"', 'OTHER_LDFLAGS' => '-lxml2')
config.to_s # => "OTHER_LDFLAGS = -lxml2\nPODS_ROOT = \"$(SRCROOT)/Pods\""


91
92
93
94
95
# File 'lib/xcodeproj/config.rb', line 91

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}".strip }
  (include_lines + settings).join("\n") << "\n"
end

#weak_frameworksSet<String>



174
175
176
# File 'lib/xcodeproj/config.rb', line 174

def weak_frameworks
  other_linker_flags[:weak_frameworks]
end