Class: Fastlane::Helper::PlistSurgeonHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb

Class Method Summary collapse

Class Method Details

.delete(path, key) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 23

def self.delete(path, key)
  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

  delete_key(plist, key)
  plist_content = plist.to_plist
  File.write(path, plist_content)
  "#{File.basename(path)} saved after deleting #{key}."
end

.delete_deep_nested_value(plist, keys) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 161

def self.delete_deep_nested_value(plist, keys)
  last_key = keys.pop
  current = navigate_to_container(plist, keys)

  return unless current

  if current.kind_of?(Array)
    index = find_index_for_key(current, last_key)
    current.delete_at(index) if index
  elsif current.kind_of?(Hash)
    current.delete(last_key)
  end
end

.delete_direct_value(plist, key) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 140

def self.delete_direct_value(plist, key)
  if plist.kind_of?(Array)
    index = find_index_for_key(plist, key)
    plist.delete_at(index) if index
  else
    plist.delete(key)
  end
end

.delete_key(plist, key) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 130

def self.delete_key(plist, key)
  if plist.kind_of?(Hash) && plist.key?(key)
    plist.delete(key)
  elsif key.include?('.')
    delete_nested_value(plist, key)
  else
    delete_direct_value(plist, key)
  end
end

.delete_nested_value(plist, key) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 149

def self.delete_nested_value(plist, key)
  parts = key.split('.')
  prefix_key = find_prefix_key(plist, parts)

  if prefix_key
    prefix, remainder = prefix_key
    delete_key(plist[prefix], remainder)
  else
    delete_deep_nested_value(plist, parts)
  end
end

.find_index_for_key(array, key) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 60

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



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 97

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


175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 175

def self.navigate_to_container(plist, keys)
  current = plist
  keys.each do |k|
    current = if current.kind_of?(Array)
                index = find_index_for_key(current, k)
                current[index]
              else
                current[k]
              end
    break unless current
  end
  current
end

.parse_json_if_needed(value) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 72

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 109

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



51
52
53
54
55
56
57
58
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 51

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



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 84

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



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 39

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