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