<commit_before>require 'xcodeproj/workspace'
require 'xcodeproj/project'

require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/array/conversions'

module Pod
  class Installer

    # The {UserProjectIntegrator} integrates the libraries generated by
    # TargetDefinitions of the {Podfile} with their correspondent user
    # projects.
    #
    class UserProjectIntegrator

      # @return [Podfile] the podfile that should be integrated with the user
      #         projects.
      #
      attr_reader :podfile

      # @return [Project] the pods project which contains the libraries to
      #         integrate.
      #
      # attr_reader :pods_project

      attr_reader :sandbox

      # @return [Pathname] the path of the installation.
      #
      # @todo This is only used to compute the workspace path in case that it
      #       should be inferred by the project. If the workspace should be in
      #       the same dir of the project, this could be removed.
      #
      attr_reader :installation_root

      # @return [Library] the libraries generated by the installer.
      #
      attr_reader :libraries

      # @param  [Podfile]  podfile @see #podfile
      # @param  [Sandbox]  sandbox @see #sandbox
      # @param  [Pathname] installation_root @see #installation_root
      # @param  [Library]  libraries @see #libraries
      #
      # @todo   Too many initialization arguments
      #
      def initialize(podfile, sandbox, installation_root, libraries)
        @podfile = podfile
        @sandbox = sandbox
        @installation_root = installation_root
        @libraries = libraries
      end

      # Integrates the user projects associated with the {TargetDefinitions}
      # with the Pods project and its products.
      #
      # @return [void]
      #
      def integrate!
        create_workspace
        integrate_user_targets
        warn_about_empty_podfile
      end

      #--------------------------------------#

      # @!group Integration steps

      private

      # Creates and saved the workspace containing the Pods project and the
      # user projects, if needed.
      #
      # @note If the workspace alreayd containts the projects it is not saved
      #       to avoid Xcode from diplatying the revert dialog: `Do you want to
      #       keep the Xcode version or revert to the version on disk?`
      #
      # @return [void]
      #
      def create_workspace
        projpaths = [*user_project_paths, sandbox.project_path].map do |path|
          path.relative_path_from(workspace_path.dirname).to_s
        end.uniq

        if workspace_path.exist?
          current_workspace = Xcodeproj::Workspace.new_from_xcworkspace(workspace_path)
          if current_workspace.projpaths.sort != projpaths.sort
            workspace = Xcodeproj::Workspace.new(*projpaths)
            workspace.save_as(workspace_path)
          end

        else
          UI.notice "From now on use `#{workspace_path.basename}'."
          workspace = Xcodeproj::Workspace.new(*projpaths)
          workspace.save_as(workspace_path)<commit_msg>[UserProjectIntegrator] Don't take into account the sorting of the workspace<commit_after>require 'xcodeproj/workspace'
require 'xcodeproj/project'

require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/array/conversions'

module Pod
  class Installer

    # The {UserProjectIntegrator} integrates the libraries generated by
    # TargetDefinitions of the {Podfile} with their correspondent user
    # projects.
    #
    class UserProjectIntegrator

      # @return [Podfile] the podfile that should be integrated with the user
      #         projects.
      #
      attr_reader :podfile

      # @return [Project] the pods project which contains the libraries to
      #         integrate.
      #
      # attr_reader :pods_project

      attr_reader :sandbox

      # @return [Pathname] the path of the installation.
      #
      # @todo This is only used to compute the workspace path in case that it
      #       should be inferred by the project. If the workspace should be in
      #       the same dir of the project, this could be removed.
      #
      attr_reader :installation_root

      # @return [Library] the libraries generated by the installer.
      #
      attr_reader :libraries

      # @param  [Podfile]  podfile @see #podfile
      # @param  [Sandbox]  sandbox @see #sandbox
      # @param  [Pathname] installation_root @see #installation_root
      # @param  [Library]  libraries @see #libraries
      #
      # @todo   Too many initialization arguments
      #
      def initialize(podfile, sandbox, installation_root, libraries)
        @podfile = podfile
        @sandbox = sandbox
        @installation_root = installation_root
        @libraries = libraries
      end

      # Integrates the user projects associated with the {TargetDefinitions}
      # with the Pods project and its products.
      #
      # @return [void]
      #
      def integrate!
        create_workspace
        integrate_user_targets
        warn_about_empty_podfile
      end

      #--------------------------------------#

      # @!group Integration steps

      private

      # Creates and saved the workspace containing the Pods project and the
      # user projects, if needed.
      #
      # @note If the workspace alreayd containts the projects it is not saved
      #       to avoid Xcode from diplatying the revert dialog: `Do you want to
      #       keep the Xcode version or revert to the version on disk?`
      #
      # @return [void]
      #
      def create_workspace
        projpaths = (user_project_paths.dup.push(sandbox.project_path)).map do |path|
          path.relative_path_from(workspace_path.dirname).to_s
        end.uniq

        if workspace_path.exist?
          current_workspace = Xcodeproj::Workspace.new_from_xcworkspace(workspace_path)
          if current_workspace.projpaths != projpaths
            workspace = Xcodeproj::Workspace.new(*projpaths)
            workspace.save_as(workspace_path)
          end

        else
          UI.notice "From now on use `#{workspace_path.basename}'."
          workspace = Xcodeproj::Workspace.new(*projpaths)
          workspace.save_as(workspace_path)
{'fraction_non_alphanumeric': 0.07704527402700556, 'fraction_numerical': 0.0, 'mean_word_length': 2.325937665081881, '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': '6074513', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1709, 'n_words': 531}