require 'securerandom' module Fastlane module Actions module SharedValues PREFIX_SCHEMA = :PREFIX_SCHEMA end class AddPrefixSchemaAction < Action def self.run(config) Fastlane::Polidea.session.action_launched("add_prefix_schema", config) prefix_schema = generate_url_scheme info_plists = Dir.glob(File.join(config[:path], "**/*.plist")) UI.user_error!("There isn't any Info.plist in this directory") if info_plists.empty? info_plists.each do |info_plist| update_plist(info_plist, prefix_schema) end Actions.lane_context[SharedValues::PREFIX_SCHEMA] = prefix_schema Fastlane::Polidea.session.action_completed("add_prefix_schema") prefix_schema end def self.generate_url_scheme Base64.urlsafe_encode64(SecureRandom.hex)[0..11] end def self.update_plist(info_plist_path, prefix_schema) modify_plist(info_plist_path, "Add :CFBundleURLTypes array") modify_plist(info_plist_path, "Add :CFBundleURLTypes:0 dict") modify_plist(info_plist_path, "Add :CFBundleURLTypes:0:CFBundleURLName string 'com.polideastore.\\$(PRODUCT_NAME)'") modify_plist(info_plist_path, "Add :CFBundleURLTypes:0:CFBundleURLSchemes array") modify_plist(info_plist_path, "Add :CFBundleURLTypes:0:CFBundleURLSchemes:0 string '#{prefix_schema}'") modify_plist(info_plist_path, "Save") UI.success("Added custom url scheme: #{prefix_schema} to plist file: #{info_plist_path}") end def self.modify_plist(info_plist_path, command) system "/usr/libexec/PlistBuddy -c \"#{command}\" \"#{info_plist_path}\"" end def self.description "Add prefix schema for Polidea Store" end def self.available_options [ FastlaneCore::ConfigItem.new(key: :path, env_name: "", description: "Path where search for Info.plist begins", default_value: File.absolute_path("."), optional: true) ] end def self.output [ ['PREFIX_SCHEMA', 'Prefix schema added to Info.plist'] ] end def self.author "Piotrek Dubiel" end def self.is_supported?(platform) platform == :ios end end end end