Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'Logger'
- require 'optparse'
- require_relative 'identity-manager.rb'
- $logger = Logger.new(STDOUT)
- $logger.level = Logger::ERROR
- $logger.formatter = proc do |severity, datetime, progname, message|
- "#{message}\n"
- end
- def find_frameworks_in_app(app_bundle_path)
- fail "Error accessing the specified app bundle" unless File.directory? app_bundle_path
- frameworks_folder_path = File.join(app_bundle_path, "Contents", "Frameworks")
- fail "Error accessing frameworks folder" unless File.directory? frameworks_folder_path
- frameworks_to_return = []
- Dir.glob("#{frameworks_folder_path}/*.framework").each do |first_level_framework_path|
- frameworks = find_recursive_framework_paths(first_level_framework_path)
- frameworks_to_return += frameworks if frameworks
- end
- $logger.info "Found Frameworks: #{frameworks_to_return}"
- frameworks_to_return
- end
- def find_recursive_framework_paths(framework_bundle_path)
- $logger.info "Looking for internal frameworks at #{framework_bundle_path}"
- framework_paths_to_return = []
- fail "Error accessing frameworking bundle path" unless File.directory? framework_bundle_path
- frameworks_folder_path = File.join(framework_bundle_path, "Frameworks")
- if File.directory? frameworks_folder_path
- Dir.glob("#{frameworks_folder_path}/*.framework").each do |framework_path|
- internal_frameworks = find_recursive_framework_paths(framework_path)
- framework_paths_to_return += internal_frameworks if internal_frameworks
- end
- end
- framework_paths_to_return << framework_bundle_path
- end
- def find_matching_signing_identity(identity_search_string = nil, team_identifier = nil, exact_match = true)
- $logger.info "Searching for identity external #{identity_search_string}, team identifier #{team_identifier}, exact match #{exact_match}"
- identity_manager = IdentityManager.new
- identity_manager.logger = $logger
- # First, search for identities without any specified search string or team identifier
- signing_identities = identity_manager.find_identities
- if signing_identities == nil || signing_identities.count == 0
- # No valid identities at all on this machine. Quit.
- fail "No valid signing identities found on this machine."
- elsif signing_identities.count == 1
- # There is exactly one signing identity. We should check whether it matches, or if we can return it anyway because we're not looking for an exact match.
- only_identity = signing_identities.first
- identity_matches_search_string = identity_search_string == nil || identity.full_name_string.contains?(identity_search_string)
- identity_matches_team_identifier = team_identifier == nil || identity.team_identifier == team_identifier
- if (identity_matches_search_string && identity_matches_team_identifier) || exact_match == false
- # We're good. Either we don't need an exact matches, or we have found one with this specific identity.
- signing_identities.first
- else
- # Only one signing identity, and it doesn't match. Quit.
- fail "The only identity on this system: #{only_identity.full_name_string} doesn't match the specified search string or team identifier!"
- end
- else
- # More than one signing idenitty. We need to run a search to narrow them down. If we find a match, return that.
- signing_identities = identity_manager.find_identities(identity_search_string, team_identifier)
- fail "Can't find any signing identities for the specified search string (#{identity_search_string}) and team identifier (#{team_identifier})!" unless signing_identities && signing_identities.count > 0
- fail "Can't disambiguate identities based on search string (#{identity_search_string}) and team identifier (#{team_identifier}). Found #{signing_identities.count} identities! #{signing_identities.collect { |identity| identity.full_name_string }}" unless signing_identities.count == 1
- signing_identities.first
- end
- end
- options = {}
- options[:verbose] = false
- options[:match_exactly] = true
- OptionParser.new do |opts|
- opts.banner = "Usage: #{opts.program_name} [options] path"
- opts.on("-v", "--[no-]verbose", "Run verbosely. False by default.") do |v|
- options[:verbose] = v
- end
- opts.on("-t", "--team-identifier IDENTIFIER", "Match identity by team identifier") do |team_identifier|
- options[:team_identifier] = team_identifier
- end
- opts.on("-m", "--[no-]exact-match", "Match exact identity. Error if we can't disambiguate. True by default.") do |match_exactly|
- options[:match_exactly] = match_exactly
- end
- opts.on("-s", "--search-string STRING", "Match identity by search string") do |search_string|
- options[:search_string] = search_string
- end
- end.parse!
- fail "Target app not specified." if ARGV.count == 0
- fail "Problem parsing arguments. Expected 1 argument (target app path) after prasing. Found #{ARGV.count}. #{ARGV}" if ARGV.count > 1
- target_app_path = ARGV.first
- fail "Invalid target app specified: #{target_app_path}." if not File.directory? target_app_path
- if options[:verbose] == true
- $logger.level= Logger::INFO
- end
- framework_paths = find_frameworks_in_app(target_app_path)
- signing_identity = find_matching_signing_identity(options[:search_string], options[:team_identifier], options[:exact_match])
- fail "Coundn't find identity to sign with!" unless signing_identity
- framework_paths.each do |framework_path|
- signing_identity.sign_bundle(framework_path)
- end
Advertisement
Add Comment
Please, Sign In to add comment