<commit_before>module Thrust
  module IOS
    class XCodeTools
      ProvisioningProfileNotFound = Class.new(StandardError)
      ProvisioningProfileNotEmbedded = Class.new(StandardError)

      def initialize(thrust_executor, out, build_configuration, build_directory, options = {})
        @thrust_executor = thrust_executor
        @out = out
        @git = Thrust::Git.new(@out, @thrust_executor)
        @build_configuration = build_configuration
        @build_directory = build_directory
        @project_name = options[:project_name]
        @workspace_name = options[:workspace_name]
        raise "project_name OR workspace_name required" unless @project_name.nil? ^ @workspace_name.nil?
      end

      def cleanly_create_ipa(target, app_name, signing_identity, provision_search_query = nil)
        clean_build
        kill_simulator
        build_scheme_or_target(target, 'iphoneos')
        ipa_name = create_ipa(app_name, signing_identity, provision_search_query)
        verify_provision(app_name, provision_search_query)

        return ipa_name
      end

      def build_configuration_directory
        "#{@build_directory}/#{@build_configuration}-iphoneos"
      end

      def clean_build
        @out.puts 'Cleaning...'
        FileUtils.rm_rf(@build_directory)
      end

      def build_scheme_or_target(scheme_or_target, build_sdk, architecture=nil)
        @out.puts "Building..."
        run_xcode(build_sdk, scheme_or_target, architecture)
      end

      def test(scheme, build_configuration, os_version, device_name, timeout, build_dir)
        destination = "OS=#{os_version},name=#{device_name}"
        timeout ||= "30"

        cmd = [
          "xcodebuild",
          "test",
          "-scheme #{scheme}",
          "-configuration #{build_configuration}",
          "-destination '#{destination}'",
          "-destination-timeout '#{timeout}'",
          "SYMROOT='#{build_dir}'"
        ].join(' ')

        @thrust_executor.check_command_for_failure(cmd)
      end

      def kill_simulator
        @out.puts('Killing simulator...')
        @thrust_executor.system %q[killall -m -KILL "gdb"]
        @thrust_executor.system %q[killall -m -KILL "otest"]
        @thrust_executor.system %q[killall -m -KILL "iPhone Simulator"]
      end

      private

      def provision_path(provision_search_query)
        provision_search_path = File.expand_path("~/Library/MobileDevice/Provisioning Profiles/")
        command = %Q(grep -rl "#{provision_search_query}" "#{provision_search_path}")
        paths = `#{command}`.split("\n")
        paths.first or raise(ProvisioningProfileNotFound, "\nCouldn't find provisioning profiles matching #{provision_search_query}.\n\nThe command used was:\n\n#{command}")
      end

      def create_ipa(app_name, signing_identity, provision_search_query)
        @out.puts 'Packaging...'
        app_filepath = "#{build_configuration_directory}/#{app_name}.app"
        ipa_filepath = "#{build_configuration_directory}/#{app_name}.ipa"
        package_command = [
          "xcrun",
          "-sdk iphoneos",
          "-v PackageApplication",
          "'#{app_filepath}'",
          "-o '#{ipa_filepath}'",
          "--embed '#{provision_path(provision_search_query)}'"
        ].join(' ')
        @thrust_executor.system_or_exit(package_command)

        @thrust_executor.system_or_exit("cd '#{build_configuration_directory}' && unzip '#{app_name}.ipa'")
        @thrust_executor.system_or_exit("/usr/bin/codesign -f -s '#{signing_identity}' '#{build_configuration_directory}/Payload/#{app_name}.app'")
        @thrust_executor.system_or_exit("cd '#{build_configuration_directory}' && zip -qr '#{app_name}.ipa' 'Payload'")<commit_msg>use the most recent Provisioning Profile when searching with provision_search_query [finishes #64005834]<commit_after>module Thrust
  module IOS
    class XCodeTools
      ProvisioningProfileNotFound = Class.new(StandardError)
      ProvisioningProfileNotEmbedded = Class.new(StandardError)

      def initialize(thrust_executor, out, build_configuration, build_directory, options = {})
        @thrust_executor = thrust_executor
        @out = out
        @git = Thrust::Git.new(@out, @thrust_executor)
        @build_configuration = build_configuration
        @build_directory = build_directory
        @project_name = options[:project_name]
        @workspace_name = options[:workspace_name]
        raise "project_name OR workspace_name required" unless @project_name.nil? ^ @workspace_name.nil?
      end

      def cleanly_create_ipa(target, app_name, signing_identity, provision_search_query = nil)
        clean_build
        kill_simulator
        build_scheme_or_target(target, 'iphoneos')
        ipa_name = create_ipa(app_name, signing_identity, provision_search_query)
        verify_provision(app_name, provision_search_query)

        return ipa_name
      end

      def build_configuration_directory
        "#{@build_directory}/#{@build_configuration}-iphoneos"
      end

      def clean_build
        @out.puts 'Cleaning...'
        FileUtils.rm_rf(@build_directory)
      end

      def build_scheme_or_target(scheme_or_target, build_sdk, architecture=nil)
        @out.puts "Building..."
        run_xcode(build_sdk, scheme_or_target, architecture)
      end

      def test(scheme, build_configuration, os_version, device_name, timeout, build_dir)
        destination = "OS=#{os_version},name=#{device_name}"
        timeout ||= "30"

        cmd = [
          "xcodebuild",
          "test",
          "-scheme #{scheme}",
          "-configuration #{build_configuration}",
          "-destination '#{destination}'",
          "-destination-timeout '#{timeout}'",
          "SYMROOT='#{build_dir}'"
        ].join(' ')

        @thrust_executor.check_command_for_failure(cmd)
      end

      def kill_simulator
        @out.puts('Killing simulator...')
        @thrust_executor.system %q[killall -m -KILL "gdb"]
        @thrust_executor.system %q[killall -m -KILL "otest"]
        @thrust_executor.system %q[killall -m -KILL "iPhone Simulator"]
      end

      private

      def provision_path(provision_search_query)
        provision_search_path = File.expand_path("~/Library/MobileDevice/Provisioning Profiles")
        command = %Q(ls -t '#{provision_search_path}' | grep '#{provision_search_query}')
        provisioning_profile = `#{command}`.split("\n").first
        if !provisioning_profile
          raise(ProvisioningProfileNotFound, "\nCouldn't find provisioning profiles matching #{provision_search_query}.\n\nThe command used was:\n\n#{command}")
        end
        "#{provision_search_path}/#{provisioning_profile}"
      end

      def create_ipa(app_name, signing_identity, provision_search_query)
        @out.puts 'Packaging...'
        app_filepath = "#{build_configuration_directory}/#{app_name}.app"
        ipa_filepath = "#{build_configuration_directory}/#{app_name}.ipa"
        package_command = [
          "xcrun",
          "-sdk iphoneos",
          "-v PackageApplication",
          "'#{app_filepath}'",
          "-o '#{ipa_filepath}'",
          "--embed '#{provision_path(provision_search_query)}'"
        ].join(' ')
        @thrust_executor.system_or_exit(package_command)

        @thrust_executor.system_or_exit("cd '#{build_configuration_directory}' && unzip '#{app_name}.ipa'")
        @thrust_executor.system_or_exit("/usr/bin/codesign -f -s '#{signing_identity}' '#{build_configuration_directory}/Payload/#{app_name}.app'")
        @thrust_executor.system_or_exit("cd '#{build_configuration_directory}' && zip -qr '#{app_name}.ipa' 'Payload'")
{'fraction_non_alphanumeric': 0.11867296092315761, 'fraction_numerical': 0.0015735641227380016, 'mean_word_length': 3.158669574700109, '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': '1660400', 'n_tokens_mistral': 2312, 'n_tokens_neox': 2300, 'n_words': 393}