class Fastlane::Helper::RemoveSettingHelper

Public Class Methods

remove_setting(project, bundle_name, file, key) click to toggle source

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

# 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