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

.find_prefix_key(plist, parts) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 68

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



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 43

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



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

def self.set_deep_nested_value(plist, keys, value)
  last_key = keys.pop
  current = plist
  keys.each do |k|
    if current.kind_of?(Array)
      current = current[k.to_i]
    else
      current[k] ||= {}
      current = current[k]
    end
  end

  if current.kind_of?(Array)
    current[last_key.to_i] = value
  else
    current[last_key] = value
  end
end

.set_direct_value(plist, key, value) ⇒ Object



35
36
37
38
39
40
41
# 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)
    plist[key.to_i] = value
  else
    plist[key] = value
  end
end

.set_nested_value(plist, key, value) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 55

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