middleman/middleman-core/lib/middleman-core/extensions.rb
2014-01-01 15:10:49 -08:00

79 lines
2.1 KiB
Ruby

require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/module/delegation'
module Middleman
module Extensions
class << self
def registered
@_registered ||= {}
end
# Register a new extension. Choose a name which will be
# used to activate the extension in config.rb, like this:
#
# activate :my_extension
#
# Provide your extension module either as the namespace
# parameter, or return it from the block:
#
# @param [Symbol] name The name of the extension
# @param [Module] namespace The extension module
def register(name, namespace)
if !registered.has_key?(name.to_sym)
registered[name.to_sym] = namespace
end
end
end
end
# Where to look in gems for extensions to auto-register
EXTENSION_FILE = File.join('lib', 'middleman_extension.rb') unless const_defined?(:EXTENSION_FILE)
class << self
# Automatically load extensions from available RubyGems
# which contain the EXTENSION_FILE
#
# @private
def load_extensions_in_path
require 'rubygems'
extensions = rubygems_latest_specs.select do |spec|
spec_has_file?(spec, EXTENSION_FILE)
end
extensions.each do |spec|
require spec.name
end
end
# Backwards compatible means of finding all the latest gemspecs
# available on the system
#
# @private
# @return [Array] Array of latest Gem::Specification
def rubygems_latest_specs
# If newer Rubygems
if ::Gem::Specification.respond_to? :latest_specs
::Gem::Specification.latest_specs(true)
else
::Gem.source_index.latest_specs
end
end
# Where a given Gem::Specification has a specific file. Used
# to discover extensions.
#
# @private
# @param [Gem::Specification] spec
# @param [String] path Path to look for
# @return [Boolean] Whether the file exists
def spec_has_file?(spec, path)
full_path = File.join(spec.full_gem_path, path)
File.exists?(full_path)
end
end
end
require 'middleman-core/extension'