<commit_before>require 'shellwords'

module Sigh
  # Resigns an existing ipa file
  class Resign
    def run(options, args)
      # get the command line inputs and parse those into the vars we need...
      ipa, signing_identity, provisioning_profiles = get_inputs(options, args)

      # ... then invoke out programmatic interface with these vars
      resign(ipa, signing_identity, provisioning_profiles)
    end

    def self.resign(ipa, signing_identity, provisioning_profiles)
      self.new.resign(ipa, signing_identity, provisioning_profiles)
    end

    def resign(ipa, signing_identity, provisioning_profiles)
      resign_path = find_resign_path
      signing_identity = find_signing_identity(signing_identity)

      unless provisioning_profiles.kind_of?(Enumerable)
        provisioning_profiles = [provisioning_profiles]
      end

      # validate that we have valid values for all these params, we don't need to check signing_identity because `find_signing_identity` will only ever return a valid value
      validate_params(resign_path, ipa, provisioning_profiles)

      provisioning_options = provisioning_profiles.map { |fst, snd| "-p #{[fst, snd].compact.join('=')}" }.join(' ')

      command = [
        resign_path.shellescape,
        ipa.shellescape,
        signing_identity.shellescape,
        provisioning_options,
        ipa.shellescape
      ].join(' ')

      puts command.magenta
      puts `#{command}`

      if $?.to_i == 0
        Helper.log.info "Successfully signed #{ipa}!".green
        true
      else
        Helper.log.fatal "Something went wrong while code signing #{ipa}".red
        false
      end
    end

    def get_inputs(options, args)
      ipa = args.first || find_ipa || ask('Path to ipa file: ')
      signing_identity = options.signing_identity || ask_for_signing_identity
      provisioning_profiles = options.provisioning_profile || find_provisioning_profile || ask('Path to provisioning file: ')

      return ipa, signing_identity, provisioning_profiles
    end

    def find_resign_path
      File.join(Helper.gem_path('sigh'), 'lib', 'assets', 'resign.sh')<commit_msg>Escaping provisioning profile params when resigning<commit_after>require 'shellwords'

module Sigh
  # Resigns an existing ipa file
  class Resign
    def run(options, args)
      # get the command line inputs and parse those into the vars we need...
      ipa, signing_identity, provisioning_profiles = get_inputs(options, args)

      # ... then invoke out programmatic interface with these vars
      resign(ipa, signing_identity, provisioning_profiles)
    end

    def self.resign(ipa, signing_identity, provisioning_profiles)
      self.new.resign(ipa, signing_identity, provisioning_profiles)
    end

    def resign(ipa, signing_identity, provisioning_profiles)
      resign_path = find_resign_path
      signing_identity = find_signing_identity(signing_identity)

      unless provisioning_profiles.kind_of?(Enumerable)
        provisioning_profiles = [provisioning_profiles]
      end

      # validate that we have valid values for all these params, we don't need to check signing_identity because `find_signing_identity` will only ever return a valid value
      validate_params(resign_path, ipa, provisioning_profiles)

      provisioning_options = provisioning_profiles.map { |fst, snd| "-p #{[fst, snd].compact.join('=')}" }.join(' ')

      command = [
        resign_path.shellescape,
        ipa.shellescape,
        signing_identity.shellescape,
        provisioning_options.shellescape,
        ipa.shellescape
      ].join(' ')

      puts command.magenta
      puts `#{command}`

      if $?.to_i == 0
        Helper.log.info "Successfully signed #{ipa}!".green
        true
      else
        Helper.log.fatal "Something went wrong while code signing #{ipa}".red
        false
      end
    end

    def get_inputs(options, args)
      ipa = args.first || find_ipa || ask('Path to ipa file: ')
      signing_identity = options.signing_identity || ask_for_signing_identity
      provisioning_profiles = options.provisioning_profile || find_provisioning_profile || ask('Path to provisioning file: ')

      return ipa, signing_identity, provisioning_profiles
    end

    def find_resign_path
      File.join(Helper.gem_path('sigh'), 'lib', 'assets', 'resign.sh')
{'fraction_non_alphanumeric': 0.08499305233904586, 'fraction_numerical': 0.0004631773969430292, 'mean_word_length': 3.297512437810945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6467024', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1273, 'n_words': 330}