Module: HMap::Utils
- Defined in:
- lib/cocoapods-hmap/utils.rb
Overview
A collection of utility functions used throughout cocoapods-hmap.
Constant Summary collapse
- HEADER_EXTENSIONS =
Pod::Sandbox::FileAccessor::HEADER_EXTENSIONS
Class Method Summary collapse
- .chang_xcconfig_header_search_path(xcconfig, hmap_h, use_headermap: true, save_origin: true) ⇒ Object
- .clean_build_setting_to_xcconfig(path, build_setting) ⇒ Object
- .clean_target_build_setting(targets, build_setting) ⇒ Object
- .hash_set_value(hash, *args) ⇒ Object
- .index_of_range(num, range) ⇒ Object
- .magic?(magic) ⇒ Boolean
- .next_power_of_two(num) ⇒ Object
- .power_of_two?(num) ⇒ Boolean
- .safe_encode(string, target_encoding) ⇒ Object
- .save_build_setting_to_xcconfig(path, value, build_setting, save_origin: true) {|xc| ... } ⇒ Object
- .specialize_format(format, swapped) ⇒ Object
- .string_downcase_hash(str) ⇒ Object
- .swapped_magic?(magic, version) ⇒ Boolean
- .target_xcconfig_path(targets) ⇒ Object
- .update_changed_file(path, contents) ⇒ Object
Class Method Details
.chang_xcconfig_header_search_path(xcconfig, hmap_h, use_headermap: true, save_origin: true) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/cocoapods-hmap/utils.rb', line 94 def self.chang_xcconfig_header_search_path(xcconfig, hmap_h, use_headermap: true, save_origin: true) hmap_header_serach_paths = hmap_h.inject('') do |sum, hmap_n| hmap_pod_root_path = "${PODS_ROOT}/Headers/#{HMAP_DIR}/#{hmap_n}" sum + "\"#{hmap_pod_root_path}\" " end save_build_setting_to_xcconfig(xcconfig, hmap_header_serach_paths, HEAD_SEARCH_PATHS, save_origin: save_origin) do |xc| xc.attributes['USE_HEADERMAP'] = 'NO' unless use_headermap end end |
.clean_build_setting_to_xcconfig(path, build_setting) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/cocoapods-hmap/utils.rb', line 122 def self.clean_build_setting_to_xcconfig(path, build_setting) xc = Xcodeproj::Config.new(path) save_origin_build_setting = "SAVE_#{build_setting}" hmap_build_setting = "HMAP_PODS_#{build_setting}" origin_build_setting = xc.attributes[save_origin_build_setting] puts "\t -xcconfig path: #{path}" if origin_build_setting.nil? puts "\t don't have #{save_origin_build_setting} in xcconfig file.".red return end xc.attributes[build_setting] = origin_build_setting xc.attributes.delete(save_origin_build_setting) xc.attributes.delete(hmap_build_setting) xc.attributes['USE_HEADERMAP'] = 'YES' xc.save_as(path) puts "\t clean finish." end |
.clean_target_build_setting(targets, build_setting) ⇒ Object
76 77 78 79 80 |
# File 'lib/cocoapods-hmap/utils.rb', line 76 def self.clean_target_build_setting(targets, build_setting) target_xcconfig_path(targets) do |xc| clean_build_setting_to_xcconfig(xc, build_setting) end end |
.hash_set_value(hash, *args) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/cocoapods-hmap/utils.rb', line 29 def self.hash_set_value(hash, *args) args.each do |arg| hash.merge(arg) end hash end |
.index_of_range(num, range) ⇒ Object
10 11 12 13 |
# File 'lib/cocoapods-hmap/utils.rb', line 10 def self.index_of_range(num, range) num &= range - 1 num end |
.magic?(magic) ⇒ Boolean
62 63 64 |
# File 'lib/cocoapods-hmap/utils.rb', line 62 def self.magic?(magic) magic.eql?(HEADER_CONST[:HMAP_SWAPPED_MAGIC]) || magic.eql?(HEADER_CONST[:HMAP_HEADER_MAGIC_NUMBER]) end |
.next_power_of_two(num) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/cocoapods-hmap/utils.rb', line 19 def self.next_power_of_two(num) num |= (num >> 1) num |= (num >> 2) num |= (num >> 4) num |= (num >> 8) num |= (num >> 16) num |= (num >> 32) num + 1 end |
.power_of_two?(num) ⇒ Boolean
15 16 17 |
# File 'lib/cocoapods-hmap/utils.rb', line 15 def self.power_of_two?(num) num != 0 && (num & (num - 1)).zero? end |
.safe_encode(string, target_encoding) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/cocoapods-hmap/utils.rb', line 66 def self.safe_encode(string, target_encoding) string.encode(target_encoding) rescue Encoding::InvalidByteSequenceError string.force_encoding(target_encoding) rescue Encoding::UndefinedConversionError string.encode(target_encoding, fallback: lambda { |c| c.force_encoding(target_encoding) }) end |
.save_build_setting_to_xcconfig(path, value, build_setting, save_origin: true) {|xc| ... } ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/cocoapods-hmap/utils.rb', line 105 def self.save_build_setting_to_xcconfig(path, value, build_setting, save_origin: true) xc = Xcodeproj::Config.new(path) save_origin_build_setting = "SAVE_#{build_setting}" hmap_build_setting = "HMAP_PODS_#{build_setting}" origin_build_setting = xc.attributes[build_setting] unless !origin_build_setting.nil? && origin_build_setting.include?(hmap_build_setting) xc.attributes[save_origin_build_setting] = origin_build_setting end value = "#{value} ${#{save_origin_build_setting}}" if save_origin xc.attributes[hmap_build_setting] = value xc.attributes[build_setting] = "${#{hmap_build_setting}}" yield(xc) if block_given? xc.save_as(path) end |
.specialize_format(format, swapped) ⇒ Object
36 37 38 39 |
# File 'lib/cocoapods-hmap/utils.rb', line 36 def self.specialize_format(format, swapped) modifier = swapped ? '<' : '>' format.tr('=', modifier) end |
.string_downcase_hash(str) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/cocoapods-hmap/utils.rb', line 41 def self.string_downcase_hash(str) str.downcase.bytes.inject(0) do |sum, value| sum += value * 13 sum end end |
.swapped_magic?(magic, version) ⇒ Boolean
58 59 60 |
# File 'lib/cocoapods-hmap/utils.rb', line 58 def self.swapped_magic?(magic, version) magic.eql?(HEADER_CONST[:HMAP_SWAPPED_MAGIC]) && version.eql?(HEADER_CONST[:HMAP_SWAPPED_VERSION]) end |
.target_xcconfig_path(targets) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/cocoapods-hmap/utils.rb', line 82 def self.target_xcconfig_path(targets) targets.each do |target| raise ClassIncludedError.new(target.class, Pod::Target) unless target.is_a?(Pod::Target) config_h = Pod::Target.instance_method(:build_settings).bind(target).call config_h.each_key do |configuration_name| xcconfig = target.xcconfig_path(configuration_name) yield(xcconfig) if block_given? end end end |
.update_changed_file(path, contents) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/cocoapods-hmap/utils.rb', line 48 def self.update_changed_file(path, contents) if path.exist? content_stream = StringIO.new(contents) identical = File.open(path, 'rb') { |f| FileUtils.compare_stream(f, content_stream) } return if identical end path.dirname.mkpath File.open(path, 'w') { |f| f.write(contents) } end |