Chris@909: require 'active_support'
Chris@909: require File.join(File.dirname(__FILE__), 'engines/plugin')
Chris@909: require File.join(File.dirname(__FILE__), 'engines/plugin/list')
Chris@909: require File.join(File.dirname(__FILE__), 'engines/plugin/loader')
Chris@909: require File.join(File.dirname(__FILE__), 'engines/plugin/locator')
Chris@909: require File.join(File.dirname(__FILE__), 'engines/assets')
Chris@909: require File.join(File.dirname(__FILE__), 'engines/rails_extensions/rails')
Chris@909:
Chris@909: # == Parameters
Chris@909: #
Chris@909: # The Engines module has a number of public configuration parameters:
Chris@909: #
Chris@909: # [+public_directory+] The directory into which plugin assets should be
Chris@909: # mirrored. Defaults to RAILS_ROOT/public/plugin_assets.
Chris@909: # [+schema_info_table+] The table to use when storing plugin migration
Chris@909: # version information. Defaults to +plugin_schema_info+.
Chris@909: #
Chris@909: # Additionally, there are a few flags which control the behaviour of
Chris@909: # some of the features the engines plugin adds to Rails:
Chris@909: #
Chris@909: # [+disable_application_view_loading+] A boolean flag determining whether
Chris@909: # or not views should be loaded from
Chris@909: # the main app/views directory.
Chris@909: # Defaults to false; probably only
Chris@909: # useful when testing your plugin.
Chris@909: # [+disable_application_code_loading+] A boolean flag determining whether
Chris@909: # or not to load controllers/helpers
Chris@909: # from the main +app+ directory,
Chris@909: # if corresponding code exists within
Chris@909: # a plugin. Defaults to false; again,
Chris@909: # probably only useful when testing
Chris@909: # your plugin.
Chris@909: # [+disable_code_mixing+] A boolean flag indicating whether all plugin
Chris@909: # copies of a particular controller/helper should
Chris@909: # be loaded and allowed to override each other,
Chris@909: # or if the first matching file should be loaded
Chris@909: # instead. Defaults to false.
Chris@909: #
Chris@909: module Engines
Chris@909: # The set of all loaded plugins
Chris@909: mattr_accessor :plugins
Chris@909: self.plugins = Engines::Plugin::List.new
Chris@909:
Chris@909: # List of extensions to load, can be changed in init.rb before calling Engines.init
Chris@909: mattr_accessor :rails_extensions
Chris@909: self.rails_extensions = %w(asset_helpers form_tag_helpers migrations dependencies)
Chris@909:
Chris@909: # The name of the public directory to mirror public engine assets into.
Chris@909: # Defaults to RAILS_ROOT/public/plugin_assets.
Chris@909: mattr_accessor :public_directory
Chris@909: self.public_directory = File.join(RAILS_ROOT, 'public', 'plugin_assets')
Chris@909:
Chris@909: # The table in which to store plugin schema information. Defaults to
Chris@909: # "plugin_schema_info".
Chris@909: mattr_accessor :schema_info_table
Chris@909: self.schema_info_table = "plugin_schema_info"
Chris@909:
Chris@909: #--
Chris@909: # These attributes control the behaviour of the engines extensions
Chris@909: #++
Chris@909:
Chris@909: # Set this to true if views should *only* be loaded from plugins
Chris@909: mattr_accessor :disable_application_view_loading
Chris@909: self.disable_application_view_loading = false
Chris@909:
Chris@909: # Set this to true if controller/helper code shouldn't be loaded
Chris@909: # from the application
Chris@909: mattr_accessor :disable_application_code_loading
Chris@909: self.disable_application_code_loading = false
Chris@909:
Chris@909: # Set this to true if code should not be mixed (i.e. it will be loaded
Chris@909: # from the first valid path on $LOAD_PATH)
Chris@909: mattr_accessor :disable_code_mixing
Chris@909: self.disable_code_mixing = false
Chris@909:
Chris@909: # This is used to determine which files are candidates for the "code
Chris@909: # mixing" feature that the engines plugin provides, where classes from
Chris@909: # plugins can be loaded, and then code from the application loaded
Chris@909: # on top of that code to override certain methods.
Chris@909: mattr_accessor :code_mixing_file_types
Chris@909: self.code_mixing_file_types = %w(controller helper)
Chris@909:
Chris@909: class << self
Chris@909: def init(initializer)
Chris@909: load_extensions
Chris@909: Engines::Assets.initialize_base_public_directory
Chris@909: end
Chris@909:
Chris@909: def logger
Chris@909: RAILS_DEFAULT_LOGGER
Chris@909: end
Chris@909:
Chris@909: def load_extensions
Chris@909: rails_extensions.each { |name| require "engines/rails_extensions/#{name}" }
Chris@909: # load the testing extensions, if we are in the test environment.
Chris@909: require "engines/testing" if RAILS_ENV == "test"
Chris@909: end
Chris@909:
Chris@909: def select_existing_paths(paths)
Chris@909: paths.select { |path| File.directory?(path) }
Chris@909: end
Chris@909:
Chris@909: # The engines plugin will, by default, mix code from controllers and helpers,
Chris@909: # allowing application code to override specific methods in the corresponding
Chris@909: # controller or helper classes and modules. However, if other file types should
Chris@909: # also be mixed like this, they can be added by calling this method. For example,
Chris@909: # if you want to include "things" within your plugin and override them from
Chris@909: # your applications, you should use the following layout:
Chris@909: #
Chris@909: # app/
Chris@909: # +-- things/
Chris@909: # | +-- one_thing.rb
Chris@909: # | +-- another_thing.rb
Chris@909: # ...
Chris@909: # vendor/
Chris@909: # +-- plugins/
Chris@909: # +-- my_plugin/
Chris@909: # +-- app/
Chris@909: # +-- things/
Chris@909: # +-- one_thing.rb
Chris@909: # +-- another_thing.rb
Chris@909: #
Chris@909: # The important point here is that your "things" are named _thing.rb,
Chris@909: # and that they are placed within plugin/app/things (the pluralized form of 'thing').
Chris@909: #
Chris@909: # It's important to note that you'll also want to ensure that the "things" are
Chris@909: # on your load path by including them in Rails load path mechanism, e.g. in init.rb:
Chris@909: #
Chris@909: # ActiveSupport::Dependencies.load_paths << File.join(File.dirname(__FILE__), 'app', 'things'))
Chris@909: #
Chris@909: def mix_code_from(*types)
Chris@909: self.code_mixing_file_types += types.map { |x| x.to_s.singularize }
Chris@909: end
Chris@909:
Chris@909: # A general purpose method to mirror a directory (+source+) into a destination
Chris@909: # directory, including all files and subdirectories. Files will not be mirrored
Chris@909: # if they are identical already (checked via FileUtils#identical?).
Chris@909: def mirror_files_from(source, destination)
Chris@909: return unless File.directory?(source)
Chris@909:
Chris@909: # TODO: use Rake::FileList#pathmap?
Chris@909: source_files = Dir[source + "/**/*"]
Chris@909: source_dirs = source_files.select { |d| File.directory?(d) }
Chris@909: source_files -= source_dirs
Chris@909:
Chris@909: unless source_files.empty?
Chris@909: base_target_dir = File.join(destination, File.dirname(source_files.first).gsub(source, ''))
Chris@909: FileUtils.mkdir_p(base_target_dir)
Chris@909: end
Chris@909:
Chris@909: source_dirs.each do |dir|
Chris@909: # strip down these paths so we have simple, relative paths we can
Chris@909: # add to the destination
Chris@909: target_dir = File.join(destination, dir.gsub(source, ''))
Chris@909: begin
Chris@909: FileUtils.mkdir_p(target_dir)
Chris@909: rescue Exception => e
Chris@909: raise "Could not create directory #{target_dir}: \n" + e
Chris@909: end
Chris@909: end
Chris@909:
Chris@909: source_files.each do |file|
Chris@909: begin
Chris@909: target = File.join(destination, file.gsub(source, ''))
Chris@909: unless File.exist?(target) && FileUtils.identical?(file, target)
Chris@909: FileUtils.cp(file, target)
Chris@909: end
Chris@909: rescue Exception => e
Chris@909: raise "Could not copy #{file} to #{target}: \n" + e
Chris@909: end
Chris@909: end
Chris@909: end
Chris@909: end
Chris@909: end