Move methods for handling automatic extension discovery and rubygems enumeration into their own file.
This commit is contained in:
parent
ab238c32e5
commit
5fc5e15975
55
middleman-core/lib/middleman-core/auto_gem_extensions.rb
Normal file
55
middleman-core/lib/middleman-core/auto_gem_extensions.rb
Normal file
|
@ -0,0 +1,55 @@
|
|||
# Add module methods to the Middleman module that allow automatically loading
|
||||
# extensions defined in gems based on the existance of a special file. This also
|
||||
# adds a method for iterating over all the Gems on a system.
|
||||
module Middleman
|
||||
class << self
|
||||
# Where to look in gems for extensions to auto-register. Since most extensions are
|
||||
# called out in a Gemfile, this is really only useful for template extensions that get
|
||||
# used by "middleman init".
|
||||
EXTENSION_FILE = File.join('lib', 'middleman_extension.rb') unless const_defined?(:EXTENSION_FILE)
|
||||
|
||||
# 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
|
||||
|
||||
private
|
||||
|
||||
# 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.exist?(full_path)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,4 +1,10 @@
|
|||
require 'middleman-core/extension'
|
||||
|
||||
module Middleman
|
||||
# The Extensions module is used to handle global registration and loading of Middleman Extensions.
|
||||
#
|
||||
# The application-facing extension API (activate, etc) is in Middleman::CoreExtensions::Extensions in
|
||||
# middleman-core/core_extensions/extensions.rb.
|
||||
module Extensions
|
||||
class << self
|
||||
def registered
|
||||
|
@ -66,55 +72,4 @@ module Middleman
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Where to look in gems for extensions to auto-register. Since most extensions are
|
||||
# called out in a Gemfile, this is really only useful for template extensions that get
|
||||
# used by "middleman init".
|
||||
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.exist?(full_path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require 'middleman-core/extension'
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
# Core Pathname library used for traversal
|
||||
require 'pathname'
|
||||
require 'middleman-core/auto_gem_extensions'
|
||||
|
||||
module Middleman
|
||||
class << self
|
||||
|
|
Loading…
Reference in a new issue