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

.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)
  plist
end

.set_nested_value(plist, key, value) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fastlane/plugin/plist_surgeon/helper/plist_surgeon_helper.rb', line 40

def self.set_nested_value(plist, key, value)
  keys = key.split('.')
  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_value(plist, key, value) ⇒ Object



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

def self.set_value(plist, key, value)
  if value.kind_of?(String) && value.start_with?('{', '[')
    begin
      require 'json'
      value = JSON.parse(value)
    rescue JSON::ParserError
      # Not JSON, keep as string
    end
  end

  if key.include?('.')
    set_nested_value(plist, key, value)
  else
    plist[key] = value
  end
end