<commit_before>require 'fiddle'

module Xcodeproj
  class Project
    module XCProjHelper
      class << self
        # @return [Bool] Whether the xcproj tool is available.
        #
        def available?
          true
        end

        # Touches the project at the given path if the xcproj tool is
        # available.
        #
        # @return [void]
        #
        def touch(path)
          if available?
            command = "xcproj --project \"#{path}\" touch"
            success, output = execute(command)
            unless success
              message = 'The `xcproj` tool has failed to touch the project. ' \
                        'Check whether your installation of `xcproj` is ' \
                        "functional.\n\n"
              message << command << "\n"
              message << output
              UI.warn(message)
            end
          project = PBXProject.new(path)
          project.writeToFileSystemProjectFile
        end
      end

      private

      # @!group Private Helpers
      #---------------------------------------------------------------------#

      def self.silence_stderr
        begin
          orig_stderr = $stderr.clone
          $stderr.reopen File.new('/dev/null', 'w')
          retval = yield
        ensure
          $stderr.reopen orig_stderr
        end
        retval
      end

      # rubocop:disable Style/MethodName
      # rubocop:disable Style/VariableName

      class PBXProject
        public

        def initialize(path)
          XCProjHelper.silence_stderr do
            CoreFoundation.IDEInitialize(1, CoreFoundation::NULL)
            CoreFoundation.XCInitializeCoreIfNeeded(1)
          end

          projectWithFile = PBXProject.objc_msgSend([CoreFoundation::VoidPointer])
          @project = projectWithFile.call(
            PBXProject.objc_class,
            CoreFoundation.NSSelectorFromString(CoreFoundation.RubyStringToCFString('projectWithFile:')),
            CoreFoundation.RubyStringToCFString(path))
        end

        def writeToFileSystemProjectFile
          writeToFile = PBXProject.objc_msgSend([CoreFoundation::Boolean, CoreFoundation::Boolean, CoreFoundation::Boolean], CoreFoundation::Boolean)
          writeToFile.call(
            @project,
            CoreFoundation.NSSelectorFromString(CoreFoundation.RubyStringToCFString('writeToFileSystemProjectFile:userFile:checkNeedsRevert:')),
            1,
            0,
            0)
        end

        private

        def self.objc_class
          @objc_class ||= CoreFoundation.objc_getClass('PBXProject')
        end

        XCODE_PATH = Pathname.new(`xcode-select -p`).join('../..')

        def self.image
          Fiddle.dlopen(XCODE_PATH.join('SharedFrameworks/DVTFoundation.framework/DVTFoundation').to_s)
          Fiddle.dlopen(XCODE_PATH.join('SharedFrameworks/DVTSourceControl.framework/DVTSourceControl').to_s)
          Fiddle.dlopen(XCODE_PATH.join('Frameworks/IDEFoundation.framework/IDEFoundation').to_s)
          @image ||= Fiddle.dlopen(XCODE_PATH.join('PlugIns/Xcode3Core.ideplugin/Contents/MacOS/Xcode3Core').to_s)
        end

        def self.extern(symbol, parameter_types, return_type)
          CoreFoundation.extern_image(image, symbol, parameter_types, return_type)
        end

        def self.objc_msgSend(args, return_type = CoreFoundation::VoidPointer)
          arguments = [CoreFoundation::VoidPointer, CoreFoundation::VoidPointer] + args

          Fiddle::Function.new(image['objc_msgSend'], arguments, return_type)<commit_msg>Fix determining the Xcode path.<commit_after>require 'fiddle'

module Xcodeproj
  class Project
    module XCProjHelper
      class << self
        # @return [Bool] Whether the xcproj tool is available.
        #
        def available?
          true
        end

        # Touches the project at the given path if the xcproj tool is
        # available.
        #
        # @return [void]
        #
        def touch(path)
          if available?
            command = "xcproj --project \"#{path}\" touch"
            success, output = execute(command)
            unless success
              message = 'The `xcproj` tool has failed to touch the project. ' \
                        'Check whether your installation of `xcproj` is ' \
                        "functional.\n\n"
              message << command << "\n"
              message << output
              UI.warn(message)
            end
          project = PBXProject.new(path)
          project.writeToFileSystemProjectFile
        end
      end

      private

      # @!group Private Helpers
      #---------------------------------------------------------------------#

      def self.silence_stderr
        begin
          orig_stderr = $stderr.clone
          $stderr.reopen File.new('/dev/null', 'w')
          retval = yield
        ensure
          $stderr.reopen orig_stderr
        end
        retval
      end

      # rubocop:disable Style/MethodName
      # rubocop:disable Style/VariableName

      class PBXProject
        public

        def initialize(path)
          XCProjHelper.silence_stderr do
            CoreFoundation.IDEInitialize(1, CoreFoundation::NULL)
            CoreFoundation.XCInitializeCoreIfNeeded(1)
          end

          projectWithFile = PBXProject.objc_msgSend([CoreFoundation::VoidPointer])
          @project = projectWithFile.call(
            PBXProject.objc_class,
            CoreFoundation.NSSelectorFromString(CoreFoundation.RubyStringToCFString('projectWithFile:')),
            CoreFoundation.RubyStringToCFString(path))
        end

        def writeToFileSystemProjectFile
          writeToFile = PBXProject.objc_msgSend([CoreFoundation::Boolean, CoreFoundation::Boolean, CoreFoundation::Boolean], CoreFoundation::Boolean)
          writeToFile.call(
            @project,
            CoreFoundation.NSSelectorFromString(CoreFoundation.RubyStringToCFString('writeToFileSystemProjectFile:userFile:checkNeedsRevert:')),
            1,
            0,
            0)
        end

        private

        def self.objc_class
          @objc_class ||= CoreFoundation.objc_getClass('PBXProject')
        end

        XCODE_PATH = Pathname.new(`xcrun xcode-select -p`.strip).dirname

        def self.image
          Fiddle.dlopen(XCODE_PATH.join('SharedFrameworks/DVTFoundation.framework/DVTFoundation').to_s)
          Fiddle.dlopen(XCODE_PATH.join('SharedFrameworks/DVTSourceControl.framework/DVTSourceControl').to_s)
          Fiddle.dlopen(XCODE_PATH.join('Frameworks/IDEFoundation.framework/IDEFoundation').to_s)
          @image ||= Fiddle.dlopen(XCODE_PATH.join('PlugIns/Xcode3Core.ideplugin/Contents/MacOS/Xcode3Core').to_s)
        end

        def self.extern(symbol, parameter_types, return_type)
          CoreFoundation.extern_image(image, symbol, parameter_types, return_type)
        end

        def self.objc_msgSend(args, return_type = CoreFoundation::VoidPointer)
          arguments = [CoreFoundation::VoidPointer, CoreFoundation::VoidPointer] + args

          Fiddle::Function.new(image['objc_msgSend'], arguments, return_type)
{'fraction_non_alphanumeric': 0.10383206537052692, 'fraction_numerical': 0.0019723865877712033, 'mean_word_length': 2.0454740454740454, 'pattern_counts': {'":': 0, '<': 19, '<?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': '5885356', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1867, 'n_words': 354}