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.


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}

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.


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

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


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

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.


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>

Returns The list of the arg files required by this settings file.

Returns:

  • (Set<String>)

    The list of the arg files required by this settings file.


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

def arg_files
  other_linker_flags[:arg_files]
end

#dupConfig

Returns A copy of the receiver.

Returns:

  • (Config)

    A copy of the receiver.


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

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.


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>

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

Returns:

  • (Set<String>)

    The list of the libraries required by this settings file.


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.

Parameters:

  • config (Hash, Config)

    The xcconfig representation to merge.

Returns:

  • (Config)

    the new xcconfig.


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

Parameters:

  • config (Hash, Config)

    The xcconfig representation to merge.


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.

Parameters:

  • pathname (Pathname)

    The file where the data should be written to.


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

Returns:

  • (Hash)

    The hash representation


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\""

Returns:

  • (String)

    The serialized internal data.


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>

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.


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

def weak_frameworks
  other_linker_flags[:weak_frameworks]
end