Class: Fastlane::Helper::RemoveSettingHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/remove_setting/helper/remove_setting_helper.rb

Class Method Summary collapse

Class Method Details

.remove_setting(project, bundle_name, file, key) ⇒ Object

Takes an open Xcodeproj::Project, extracts the settings bundle and removes the specified setting key in the specified file. Raises on error.

:project: An open Xcodeproj::Project, obtained from Xcodeproj::Project.open, e.g. :bundle_name: (String) Regex to identify the bundle to look for, usually Settings.bundle. :file: A settings plist file in the Settings.bundle, usually “Root.plist” :key: A valid NSUserDefaults key in the Settings.bundle



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fastlane/plugin/remove_setting/helper/remove_setting_helper.rb', line 35

def remove_setting(project, bundle_name, file, key)
  settings_bundle = project.files.find { |f| f.path =~ /#{bundle_name}/ }

  raise "#{bundle_name} not found in project" if settings_bundle.nil?

  # The #real_path method returns the full resolved path to the Settings.bundle
  settings_bundle_path = settings_bundle.real_path

  plist_path = File.join settings_bundle_path, file

  # raises IOError
  settings_plist = File.open(plist_path) { |f| Plist.parse_xml f }

  raise "Could not parse #{plist_path}" if settings_plist.nil?

  preference_specifiers = settings_plist['PreferenceSpecifiers']
  raise "#{file} is not a settings plist file" if preference_specifiers.nil?

  original_count = preference_specifiers.length

  raise "#{file} is not a settings plist file" if preference_specifiers.nil?

  # Remove the specifier matching the supplied key
  settings_plist['PreferenceSpecifiers'] = preference_specifiers.reject do |specifier|
    specifier['Key'] == key
  end

  raise "preference specifier for key #{key} not found in #{file}" if settings_plist['PreferenceSpecifiers'].length == original_count

  # Save (raises)
  Plist::Emit.save_plist settings_plist, plist_path
end