Class: Fastlane::Helper::PlistSurgeonHelper
- Inherits:
-
Object
- Object
- Fastlane::Helper::PlistSurgeonHelper
- Defined in:
- lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb
Class Method Summary collapse
- .find_index_for_key(array, key) ⇒ Object
- .find_prefix_key(plist, parts) ⇒ Object
- .parse_json_if_needed(value) ⇒ Object
- .run(path, key, value) ⇒ Object
- .set_deep_nested_value(plist, keys, value) ⇒ Object
- .set_direct_value(plist, key, value) ⇒ Object
- .set_nested_value(plist, key, value) ⇒ Object
- .set_value(plist, key, value) ⇒ Object
Class Method Details
.find_index_for_key(array, key) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 44 def self.find_index_for_key(array, key) if key.start_with?('(') && key.end_with?(')') search_term = key[1...-1] search_key, search_value = search_term.split('=', 2) if search_key && search_value index = array.index { |item| item.kind_of?(Hash) && item[search_key].to_s == search_value } return index if index end end key.to_i end |
.find_prefix_key(plist, parts) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 81 def self.find_prefix_key(plist, parts) return nil unless plist.kind_of?(Hash) (parts.size - 1).downto(1) do |i| prefix = parts[0...i].join('.') if plist.key?(prefix) return [prefix, parts[i..-1].join('.')] end end nil end |
.parse_json_if_needed(value) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 56 def self.parse_json_if_needed(value) if value.kind_of?(String) && value.start_with?('{', '[') begin require 'json' return JSON.parse(value) rescue JSON::ParserError # Not JSON, keep as string end end value end |
.run(path, key, value) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 7 def self.run(path, key, value) unless File.exist?(path) FastlaneCore::UI.user_error!("File not found at path: #{path}") end plist = Plist.parse_xml(path) if plist.nil? FastlaneCore::UI.user_error!("Failed to parse plist at path: #{path}") end set_value(plist, key, value) plist_content = plist.to_plist File.write(path, plist_content) "#{File.basename(path)} saved with #{key} change." end |
.set_deep_nested_value(plist, keys, value) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 93 def self.set_deep_nested_value(plist, keys, value) last_key = keys.pop current = plist keys.each do |k| if current.kind_of?(Array) index = find_index_for_key(current, k) current = current[index] else current[k] ||= {} current = current[k] end end if current.kind_of?(Array) index = find_index_for_key(current, last_key) current[index] = value else current[last_key] = value end end |
.set_direct_value(plist, key, value) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 35 def self.set_direct_value(plist, key, value) if plist.kind_of?(Array) index = find_index_for_key(plist, key) plist[index] = value else plist[key] = value end end |
.set_nested_value(plist, key, value) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 68 def self.set_nested_value(plist, key, value) # Attempt to find if any prefix of the key exists as a literal key parts = key.split('.') prefix_key = find_prefix_key(plist, parts) if prefix_key prefix, remainder = prefix_key set_value(plist[prefix], remainder, value) else set_deep_nested_value(plist, parts, value) end end |
.set_value(plist, key, value) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 23 def self.set_value(plist, key, value) value = parse_json_if_needed(value) if plist.kind_of?(Hash) && plist.key?(key) plist[key] = value elsif key.include?('.') set_nested_value(plist, key, value) else set_direct_value(plist, key, value) end end |