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.



44
45
46
47
48
# File 'lib/hmap/xc/target/xcconfig.rb', line 44

def initialize(xcconfig_hash_or_file = {})
  @attributes = {}
  @includes = []
  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.



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

def attributes
  @attributes
end

Instance Method Details

#==(other) ⇒ Object



54
55
56
# File 'lib/hmap/xc/target/xcconfig.rb', line 54

def ==(other)
  other.attributes == attributes
end

#dupConfig

Returns A copy of the receiver.

Returns:

  • (Config)

    A copy of the receiver.



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

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

#inspectObject



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

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.



146
147
148
# File 'lib/hmap/xc/target/xcconfig.rb', line 146

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.



129
130
131
132
133
134
135
# File 'lib/hmap/xc/target/xcconfig.rb', line 129

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.



85
86
87
88
89
# File 'lib/hmap/xc/target/xcconfig.rb', line 85

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



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hmap/xc/target/xcconfig.rb', line 100

def to_hash(prefix = nil)
  list = []

  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.



71
72
73
74
75
# File 'lib/hmap/xc/target/xcconfig.rb', line 71

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