<commit_before>require 'singleton'
require 'fileutils'

module Illuminator

  # Convenience functions for command-line actions done in Xcode
  class XcodeUtils
    include Singleton

    def initialize
      @xcode_path              = `/usr/bin/xcode-select -print-path`.chomp.sub(/^\s+/, '')
      @xcode_version           = nil
      @sdk_path                = nil
      @instruments_path        = nil
      @_simulator_devices_text = nil
      @simulator_device_types  = nil
      @simulator_runtimes      = nil
    end

    def get_xcode_path
      @xcode_path
    end

    def get_xcode_app_path
      Illuminator::HostUtils.realpath(File.join(@xcode_path, "../../"))
    end

    def get_xcode_simctl_path
      Illuminator::HostUtils.realpath(File.join(@xcode_path, "/usr/bin/simctl"))
    end

    def get_xcode_version
      if @xcode_version.nil?
        xcode_version = `xcodebuild -version`
        needle = 'Xcode (.*)'
        match = xcode_version.match(needle)
        @xcode_version = match.captures[0]
      end
      @xcode_version
    end

    def is_xcode_major_version ver
      # should update this with 'version' gem
      needle = '(\d+)\.?(\d+)?'
      match = get_xcode_version.match(needle)
      return match.captures[0].to_i == ver
    end

    # Get the path to the SDK
    def get_sdk_path
      @sdk_path ||= `/usr/bin/xcodebuild -version -sdk iphoneos | grep PlatformPath`.split(':')[1].chomp.sub(/^\s+/, '')
    end

    # Get the path to the instruments bundle
    def get_instruments_path
      if @instruments_path.nil?
        if File.directory? "#{@xcode_path}/../Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/"
          @instruments_path = "AutomationInstrument.xrplugin";
        else
          #fallback to old instruments bundle (pre Xcode6)
          @instruments_path = "AutomationInstrument.bundle";
        end
      end
      @instruments_path
    end

    # Get the path to the instruments template
    def get_instruments_template_path
      File.join(@xcode_path,
                "../Applications/Instruments.app/Contents/PlugIns",
                get_instruments_path,
                "Contents/Resources/Automation.tracetemplate")
    end

    def _get_all_simulator_info
      info = `#{get_xcode_simctl_path} list`.split("\n")

      output = {"devices" => [], "runtimes" => []}
      pointer = nil
      needle = nil

      info.each do |line|
        case line
        when "== Device Types =="
          # all data we want is followed by " (" e.g. "iPhone 5 (com.apple.C......."
          pointer = "devices"
          needle = '([^(]*) '
        when "== Runtimes =="
          # all data we want is in the form "iOS 7.0 (7.0.3 - ........." and we want the 7.0
          pointer = "runtimes"
          needle = 'iOS ([^(]*) '
        when "== Devices =="
          pointer = nil
          needle = nil
        else
          unless pointer.nil?
            match = line.match(needle)
            if match
              output[pointer] << match.captures[0]
            end
          end
        end
      end

      @simulator_device_types = output["devices"]
      @simulator_runtimes = output["runtimes"]
    end

    def get_simulator_device_types
      if @simulator_device_types.nil?
        _get_all_simulator_info
      end
      @simulator_device_types
    end

    def get_simulator_runtimes
      if @simulator_runtimes.nil?
        _get_all_simulator_info
      end
      @simulator_runtimes
    end

    def get_simulator_devices
      if @_simulator_devices_text.nil?
        @_simulator_devices_text = `instruments -s devices`
      end
      @_simulator_devices_text
    end

    # Based on the desired device and version, get the ID of the simulator that will be passed to instruments<commit_msg>[xcode] workaround for xcode 7 hang<commit_after>require 'singleton'
require 'fileutils'

module Illuminator

  # Convenience functions for command-line actions done in Xcode
  class XcodeUtils
    include Singleton

    def initialize
      @xcode_path              = `/usr/bin/xcode-select -print-path`.chomp.sub(/^\s+/, '')
      @xcode_version           = nil
      @sdk_path                = nil
      @instruments_path        = nil
      @_simulator_devices_text = nil
      @simulator_device_types  = nil
      @simulator_runtimes      = nil
    end

    def get_xcode_path
      @xcode_path
    end

    def get_xcode_app_path
      Illuminator::HostUtils.realpath(File.join(@xcode_path, "../../"))
    end

    def get_xcode_simctl_path
      Illuminator::HostUtils.realpath(File.join(@xcode_path, "/usr/bin/simctl"))
    end

    def get_xcode_version
      if @xcode_version.nil?
        xcode_version = `xcodebuild -version`
        needle = 'Xcode (.*)'
        match = xcode_version.match(needle)
        @xcode_version = match.captures[0]
      end
      @xcode_version
    end

    def is_xcode_major_version ver
      # should update this with 'version' gem
      needle = '(\d+)\.?(\d+)?'
      match = get_xcode_version.match(needle)
      return match.captures[0].to_i == ver
    end

    # Get the path to the SDK
    def get_sdk_path
      @sdk_path ||= `/usr/bin/xcodebuild -version -sdk iphoneos | grep PlatformPath`.split(':')[1].chomp.sub(/^\s+/, '')
    end

    # Get the path to the instruments bundle
    def get_instruments_path
      if @instruments_path.nil?
        if File.directory? "#{@xcode_path}/../Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/"
          @instruments_path = "AutomationInstrument.xrplugin";
        else
          #fallback to old instruments bundle (pre Xcode6)
          @instruments_path = "AutomationInstrument.bundle";
        end
      end
      @instruments_path
    end

    # Get the path to the instruments template
    def get_instruments_template_path
      File.join(@xcode_path,
                "../Applications/Instruments.app/Contents/PlugIns",
                get_instruments_path,
                "Contents/Resources/Automation.tracetemplate")
    end

    def _get_all_simulator_info
      info = `#{get_xcode_simctl_path} list`.split("\n")

      output = {"devices" => [], "runtimes" => []}
      pointer = nil
      needle = nil

      info.each do |line|
        case line
        when "== Device Types =="
          # all data we want is followed by " (" e.g. "iPhone 5 (com.apple.C......."
          pointer = "devices"
          needle = '([^(]*) '
        when "== Runtimes =="
          # all data we want is in the form "iOS 7.0 (7.0.3 - ........." and we want the 7.0
          pointer = "runtimes"
          needle = 'iOS ([^(]*) '
        when "== Devices =="
          pointer = nil
          needle = nil
        else
          unless pointer.nil?
            match = line.match(needle)
            if match
              output[pointer] << match.captures[0]
            end
          end
        end
      end

      @simulator_device_types = output["devices"]
      @simulator_runtimes = output["runtimes"]
    end

    def get_simulator_device_types
      if @simulator_device_types.nil?
        _get_all_simulator_info
      end
      @simulator_device_types
    end

    def get_simulator_runtimes
      if @simulator_runtimes.nil?
        _get_all_simulator_info
      end
      @simulator_runtimes
    end

    def get_simulator_devices
      if @_simulator_devices_text.nil?
        @_simulator_devices_text = `script -t 0.1 -q /dev/null instruments -s devices`
      end
      @_simulator_devices_text
    end

    # Based on the desired device and version, get the ID of the simulator that will be passed to instruments
{'fraction_non_alphanumeric': 0.10074480595844766, 'fraction_numerical': 0.003789363648242519, 'mean_word_length': 2.0987854251012146, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7538084', 'n_tokens_mistral': 2444, 'n_tokens_neox': 2365, 'n_words': 545}