From 5fc5e159758acab6b96ea5625c65c436818e5b33 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 10 May 2014 19:06:51 -0700 Subject: [PATCH] Move methods for handling automatic extension discovery and rubygems enumeration into their own file. --- .../lib/middleman-core/auto_gem_extensions.rb | 55 ++++++++++++++++++ .../lib/middleman-core/extensions.rb | 57 ++----------------- .../lib/middleman-core/load_paths.rb | 1 + 3 files changed, 62 insertions(+), 51 deletions(-) create mode 100644 middleman-core/lib/middleman-core/auto_gem_extensions.rb diff --git a/middleman-core/lib/middleman-core/auto_gem_extensions.rb b/middleman-core/lib/middleman-core/auto_gem_extensions.rb new file mode 100644 index 00000000..430106b2 --- /dev/null +++ b/middleman-core/lib/middleman-core/auto_gem_extensions.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/extensions.rb b/middleman-core/lib/middleman-core/extensions.rb index f775b99e..47157122 100644 --- a/middleman-core/lib/middleman-core/extensions.rb +++ b/middleman-core/lib/middleman-core/extensions.rb @@ -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' diff --git a/middleman-core/lib/middleman-core/load_paths.rb b/middleman-core/lib/middleman-core/load_paths.rb index 68defc69..36c32704 100644 --- a/middleman-core/lib/middleman-core/load_paths.rb +++ b/middleman-core/lib/middleman-core/load_paths.rb @@ -1,5 +1,6 @@ # Core Pathname library used for traversal require 'pathname' +require 'middleman-core/auto_gem_extensions' module Middleman class << self