Class: HMap::XCConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/hmap/xc/target/xcconfig.rb

Overview

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

Instance Attribute Summary collapse

Serialization collapse

Merging collapse

Instance Method Summary collapse

Constructor Details

#initialize(xcconfig_hash_or_file = {}) ⇒ Hash{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.



49
50
51
52
53
54
55
# File 'lib/hmap/xc/target/xcconfig.rb', line 49

def initialize(xcconfig_hash_or_file = {})
  @attributes = {}
  @includes = []
  @include_attributes = {}
  merge!(extract_hash(xcconfig_hash_or_file))
  @includes_paths = @includes.map { |i| File.expand_path(i, @filepath.dirname) }
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.



38
39
40
# File 'lib/hmap/xc/target/xcconfig.rb', line 38

def attributes
  @attributes
end

#includes_pathsArray

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



43
44
45
# File 'lib/hmap/xc/target/xcconfig.rb', line 43

def includes_paths
  @includes_paths
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
# File 'lib/hmap/xc/target/xcconfig.rb', line 61

def ==(other)
  other.attributes == attributes && other.includes_paths = @includes_paths
end

#dupConfig

Returns A copy of the receiver.

Returns:

  • (Config)

    A copy of the receiver.



157
158
159
# File 'lib/hmap/xc/target/xcconfig.rb', line 157

def dup
  HMap::XCConfig.new(to_hash.dup)
end

#inspectObject



57
58
59
# File 'lib/hmap/xc/target/xcconfig.rb', line 57

def inspect
  to_hash.inspect
end

#merge(config) ⇒ Config

Creates a new #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.



151
152
153
# File 'lib/hmap/xc/target/xcconfig.rb', line 151

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.

Parameters:

  • config (Hash, Config)

    The xcconfig representation to merge.



134
135
136
137
138
139
140
# File 'lib/hmap/xc/target/xcconfig.rb', line 134

def merge!(xcconfig)
  if xcconfig.is_a? XCConfig
    merge_attributes!(xcconfig.attributes)
  else
    merge_attributes!(xcconfig.to_hash)
  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.



92
93
94
95
96
# File 'lib/hmap/xc/target/xcconfig.rb', line 92

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

  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



107
108
109
110
111
112
113
114
115
# File 'lib/hmap/xc/target/xcconfig.rb', line 107

def to_hash(prefix = nil)
  result = attributes.dup
  result.reject! { |_, v| INHERITED.any? { |i| i == v.to_s.strip } }
  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.



78
79
80
81
82
# File 'lib/hmap/xc/target/xcconfig.rb', line 78

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