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

Returns a new instance of Config.

Parameters:

  • xcconfig_hash_or_file (Hash, File, String) (defaults to: {})

    The initial data.



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

def initialize(xcconfig_hash_or_file = {})
  @attributes = {}
  @includes = []
  @other_linker_flags = {}
  [:simple, :frameworks, :weak_frameworks, :libraries, :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}

Returns The attributes of the settings file excluding frameworks, weak_framework and libraries.

Returns:

  • (Hash{String => String})

    The attributes of the settings file excluding frameworks, weak_framework and libraries.



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

def attributes
  @attributes
end

#includesArray

Returns The list of the configuration files included by this configuration file (‘#include “SomeConfig”`).

Returns:

  • (Array)

    The list of the configuration files included by this configuration file (‘#include “SomeConfig”`).



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

def includes
  @includes
end

#other_linker_flagsHash{Symbol => Set<String>}

Returns The other linker flags by key. Xcodeproj handles them in a dedicated way to prevent duplication of the libraries and of the frameworks.

Returns:

  • (Hash{Symbol => Set<String>})

    The other linker flags by key. Xcodeproj handles them in a dedicated way to prevent duplication of the libraries and of the frameworks.



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

def other_linker_flags
  @other_linker_flags
end

Instance Method Details

#==(other) ⇒ Object



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

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

#dupConfig

Returns A copy of the receiver.

Returns:

  • (Config)

    A copy of the receiver.



237
238
239
# File 'lib/xcodeproj/config.rb', line 237

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

#frameworksSet<String>

Returns The list of the frameworks required by this settings file.

Returns:

  • (Set<String>)

    The list of the frameworks required by this settings file.



163
164
165
# File 'lib/xcodeproj/config.rb', line 163

def frameworks
  other_linker_flags[:frameworks]
end

#inspectObject



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

def inspect
  to_hash.inspect
end

#librariesSet<String>

Returns The list of the libraries required by this settings file.

Returns:

  • (Set<String>)

    The list of the libraries required by this settings file.



177
178
179
# File 'lib/xcodeproj/config.rb', line 177

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.

Parameters:

  • config (Hash, Config)

    The xcconfig representation to merge.

Returns:

  • (Config)

    the new xcconfig.



231
232
233
# File 'lib/xcodeproj/config.rb', line 231

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"' }

Parameters:

  • config (Hash, Config)

    The xcconfig representation to merge.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/xcodeproj/config.rb', line 205

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.

Parameters:

  • pathname (Pathname)

    The file where the data should be written to.



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

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

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`).

Returns:

  • (Hash)

    The hash representation



121
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
# File 'lib/xcodeproj/config.rb', line 121

def to_hash(prefix = nil)
  list = []
  list += other_linker_flags[:simple].to_a.sort
  modifiers = {
    :frameworks => '-framework ',
    :weak_frameworks => '-weak_framework ',
    :libraries => '-l',
    :force_load => '-force_load',
  }
  [:libraries, :frameworks, :weak_frameworks, :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\""

Returns:

  • (String)

    The serialized internal data.



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

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>

Returns The list of the weak frameworks required by this settings file.

Returns:

  • (Set<String>)

    The list of the weak frameworks required by this settings file.



170
171
172
# File 'lib/xcodeproj/config.rb', line 170

def weak_frameworks
  other_linker_flags[:weak_frameworks]
end