<commit_before>#--
# Copyright (c) 2005 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++

require 'ruby_extensions'
require 'dependencies_extensions'
require 'action_view_extensions'
require 'action_mailer_extensions'



# Holds the Rails Engine loading logic and default constants
module ::Engines
  
  # An array of active engines (actually paths to active engines)
  ActiveEngines = []
  
  # The root directory for engines
  config :root, File.join(RAILS_ROOT, "vendor", "plugins")
  
  # The name of the public folder under which engine files are copied
  config :public_dir, "engine_files"
  
  class << self
  
    # Initializes a Rails Engine by loading the engine's init.rb file and
    # ensuring that any engine controllers are added to the load path.
    # This will also copy any files in a directory named 'public'
    # into the public webserver directory. Example usage:
    #
    #   Engines.start :login
    #   Engines.start :login_engine  # equivalent
    #
    # A list of engine names can be specified:
    #
    #   Engines.start :login, :user, :wiki
    #
    # The engines will be loaded in the order given.
    # If no engine names are given, all engines will be started.
    #
    # Options can include:
    # * :copy_files => true | false
    # * :engine_name => the name within the plugins directory this engine resides, if
    #   different from the first parameter
    #
    # Note that if a list of engines is given, the options will apply to ALL engines.
    def start(*args)
      
      options = (args.last.is_a? Hash) ? args.pop : {}
      
      if args.empty?
        start_all
        return
      else
        args.each do |engine_name|
          start_engine(engine_name, options)
        end
      end
    end

    # Starts all available engines. Plugins are considered engines if they
    # include an init_engine.rb file, or they are named <something>_engine.
    def start_all()
      plugins = Dir[File.join(config(:root), "*")]
      RAILS_DEFAULT_LOGGER.debug "considering plugins: #{plugins.inspect}"
      plugins.each { |plugin|
        engine_name = File.basename(plugin)
        if File.exist?(File.join(plugin, "init_engine.rb")) or
           (engine_name =~ /_engine$/)
          # start the engine...
          start(engine_name)
        end
      }
    end

    def start_engine(engine_name, options={})

      current_engine = Engine.new
      current_engine.name = options[:engine_name] || engine_name
      current_engine.root = get_engine_dir(engine_name)
      
      #engine_name = options[:engine_name] || engine
      #engine_dir = get_engine_dir(engine_name)

      RAILS_DEFAULT_LOGGER.debug "Trying to start engine '#{current_engine.name}' from '#{File.expand_path(current_engine.root)}'"

      # put this engine at the front of the ActiveEngines list
      Engines::ActiveEngines.unshift current_engine #engine_dir

      # add the code directories of this engine to the load path
      add_engine_to_load_path(current_engine) #engine_dir)

      # load the engine's init.rb file
      startup_file = File.join(current_engine.root, "init_engine.rb")
      if File.exist?(startup_file)
        eval(IO.read(startup_file), binding, startup_file)
        #require startup_file
      else
        RAILS_DEFAULT_LOGGER.warn "WARNING: No init_engines.rb file found for engine '#{current_engine.name}'..."
      end

      # add the controller path to the Dependency system
      Controllers.add_path(File.join(current_engine.root, 'app', 'controllers'))

      # copy the files unless indicated otherwise
      if options[:copy_files] != false
        copy_engine_files(current_engine)
      end
    end

    # Adds all directories in the /app and /lib directories within the engine
    # to the load path
    def add_engine_to_load_path(engine)
      # Add ALL paths under the engine root to the load path
      app_dirs = [engine.root + "/app/controllers", engine.root + "/app/models",
                  engine.root + "/app/helpers"]
      lib_dirs = Dir[engine.root + "/lib/**/*"] + [engine.root, "lib"]
      load_paths = (app_dirs + lib_dirs).select { |d| 
        File.directory?(d)
      }

      # Remove other engines from the $LOAD_PATH bby matching against the engine.root values
      # in ActiveEngines. Store the removed engines in the order they came off.
      #
      # This is a hack - if Ticket http://dev.rubyonrails.com/ticket/2817 is accepted, then 
      # a new Engines system can be developed which only modifies load paths in one sweep,
      # thus avoiding this.
      #
      
      old_plugin_paths = []
      # assumes that all engines are at the bottom of the $LOAD_PATH
      while (File.expand_path($LOAD_PATH.last).index(File.expand_path(Engines.config(:root))) == 0) do
        puts "unshifting: " + $LOAD_PATH.last
        old_plugin_paths.unshift($LOAD_PATH.pop)
      end

<commit_msg>Removed errant puts from engines.rb<commit_after>#--
# Copyright (c) 2005 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++

require 'ruby_extensions'
require 'dependencies_extensions'
require 'action_view_extensions'
require 'action_mailer_extensions'



# Holds the Rails Engine loading logic and default constants
module ::Engines
  
  # An array of active engines (actually paths to active engines)
  ActiveEngines = []
  
  # The root directory for engines
  config :root, File.join(RAILS_ROOT, "vendor", "plugins")
  
  # The name of the public folder under which engine files are copied
  config :public_dir, "engine_files"
  
  class << self
  
    # Initializes a Rails Engine by loading the engine's init.rb file and
    # ensuring that any engine controllers are added to the load path.
    # This will also copy any files in a directory named 'public'
    # into the public webserver directory. Example usage:
    #
    #   Engines.start :login
    #   Engines.start :login_engine  # equivalent
    #
    # A list of engine names can be specified:
    #
    #   Engines.start :login, :user, :wiki
    #
    # The engines will be loaded in the order given.
    # If no engine names are given, all engines will be started.
    #
    # Options can include:
    # * :copy_files => true | false
    # * :engine_name => the name within the plugins directory this engine resides, if
    #   different from the first parameter
    #
    # Note that if a list of engines is given, the options will apply to ALL engines.
    def start(*args)
      
      options = (args.last.is_a? Hash) ? args.pop : {}
      
      if args.empty?
        start_all
        return
      else
        args.each do |engine_name|
          start_engine(engine_name, options)
        end
      end
    end

    # Starts all available engines. Plugins are considered engines if they
    # include an init_engine.rb file, or they are named <something>_engine.
    def start_all()
      plugins = Dir[File.join(config(:root), "*")]
      RAILS_DEFAULT_LOGGER.debug "considering plugins: #{plugins.inspect}"
      plugins.each { |plugin|
        engine_name = File.basename(plugin)
        if File.exist?(File.join(plugin, "init_engine.rb")) or
           (engine_name =~ /_engine$/)
          # start the engine...
          start(engine_name)
        end
      }
    end

    def start_engine(engine_name, options={})

      current_engine = Engine.new
      current_engine.name = options[:engine_name] || engine_name
      current_engine.root = get_engine_dir(engine_name)
      
      #engine_name = options[:engine_name] || engine
      #engine_dir = get_engine_dir(engine_name)

      RAILS_DEFAULT_LOGGER.debug "Trying to start engine '#{current_engine.name}' from '#{File.expand_path(current_engine.root)}'"

      # put this engine at the front of the ActiveEngines list
      Engines::ActiveEngines.unshift current_engine #engine_dir

      # add the code directories of this engine to the load path
      add_engine_to_load_path(current_engine) #engine_dir)

      # load the engine's init.rb file
      startup_file = File.join(current_engine.root, "init_engine.rb")
      if File.exist?(startup_file)
        eval(IO.read(startup_file), binding, startup_file)
        #require startup_file
      else
        RAILS_DEFAULT_LOGGER.warn "WARNING: No init_engines.rb file found for engine '#{current_engine.name}'..."
      end

      # add the controller path to the Dependency system
      Controllers.add_path(File.join(current_engine.root, 'app', 'controllers'))

      # copy the files unless indicated otherwise
      if options[:copy_files] != false
        copy_engine_files(current_engine)
      end
    end

    # Adds all directories in the /app and /lib directories within the engine
    # to the load path
    def add_engine_to_load_path(engine)
      # Add ALL paths under the engine root to the load path
      app_dirs = [engine.root + "/app/controllers", engine.root + "/app/models",
                  engine.root + "/app/helpers"]
      lib_dirs = Dir[engine.root + "/lib/**/*"] + [engine.root, "lib"]
      load_paths = (app_dirs + lib_dirs).select { |d| 
        File.directory?(d)
      }

      # Remove other engines from the $LOAD_PATH bby matching against the engine.root values
      # in ActiveEngines. Store the removed engines in the order they came off.
      #
      # This is a hack - if Ticket http://dev.rubyonrails.com/ticket/2817 is accepted, then 
      # a new Engines system can be developed which only modifies load paths in one sweep,
      # thus avoiding this.
      #
      
      old_plugin_paths = []
      # assumes that all engines are at the bottom of the $LOAD_PATH
      while (File.expand_path($LOAD_PATH.last).index(File.expand_path(Engines.config(:root))) == 0) do
        old_plugin_paths.unshift($LOAD_PATH.pop)
      end


{'fraction_non_alphanumeric': 0.0812849635531446, 'fraction_numerical': 0.0015256823190371248, 'mean_word_length': 3.018732970027248, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3896105', 'n_tokens_mistral': 3373, 'n_tokens_neox': 3278, 'n_words': 1237}