diff --git a/middleman-core/lib/middleman-core/sources.rb b/middleman-core/lib/middleman-core/sources.rb index ca58f656..cd3eab17 100644 --- a/middleman-core/lib/middleman-core/sources.rb +++ b/middleman-core/lib/middleman-core/sources.rb @@ -1,4 +1,5 @@ require 'middleman-core/contracts' +require 'backports/2.0.0/enumerable/lazy' module Middleman # The standard "record" that contains information about a file on disk. @@ -168,6 +169,7 @@ module Middleman Contract Symbol, String, Maybe[Bool] => Maybe[SourceFile] def find(type, path, glob=false) watchers + .lazy .select { |d| d.type == type } .map { |d| d.find(path, glob) } .reject { |d| d.nil? } @@ -182,6 +184,7 @@ module Middleman Contract Symbol, String => Bool def exists?(type, path) watchers + .lazy .select { |d| d.type == type } .any? { |d| d.exists?(path) } end diff --git a/middleman-core/lib/middleman-core/sources/source_watcher.rb b/middleman-core/lib/middleman-core/sources/source_watcher.rb index 9fb05475..bfc0878f 100644 --- a/middleman-core/lib/middleman-core/sources/source_watcher.rb +++ b/middleman-core/lib/middleman-core/sources/source_watcher.rb @@ -1,7 +1,7 @@ # Watcher Library require 'listen' - require 'middleman-core/contracts' +require 'backports/2.0.0/enumerable/lazy' module Middleman # The default source watcher implementation. Watches a directory on disk @@ -211,25 +211,31 @@ module Middleman Contract ArrayOf[Pathname], ArrayOf[Pathname] => Any def update(updated_paths, removed_paths) valid_updates = updated_paths + .lazy .map(&method(:path_to_source_file)) .select(&method(:valid?)) - - valid_updates.each do |f| - add_file_to_cache(f) - logger.debug "== Change (#{f[:type]}): #{f[:relative_path]}" - end + .to_a + .each do |f| + add_file_to_cache(f) + logger.debug "== Change (#{f[:type]}): #{f[:relative_path]}" + end valid_removes = removed_paths + .lazy .select(&@files.method(:key?)) .map(&@files.method(:[])) .select(&method(:valid?)) + .to_a + .each do |f| + remove_file_from_cache(f) + logger.debug "== Deletion (#{f[:type]}): #{f[:relative_path]}" + end - valid_removes.each do |f| - remove_file_from_cache(f) - logger.debug "== Deletion (#{f[:type]}): #{f[:relative_path]}" - end - - run_callbacks(@on_change_callbacks, valid_updates, valid_removes) unless valid_updates.empty? && valid_removes.empty? + run_callbacks( + @on_change_callbacks, + valid_updates, + valid_removes + ) unless valid_updates.empty? && valid_removes.empty? end def add_file_to_cache(f) @@ -260,7 +266,7 @@ module Middleman Contract Pathname => IsA['Middleman::SourceFile'] def path_to_source_file(path) ::Middleman::SourceFile.new( - path.relative_path_from(@directory), path, @directory, @type) + path.relative_path_from(@directory), path, @directory, @type) end # Notify callbacks for a file given an array of callbacks diff --git a/middleman-core/middleman-core.gemspec b/middleman-core/middleman-core.gemspec index f8735917..397928dc 100644 --- a/middleman-core/middleman-core.gemspec +++ b/middleman-core/middleman-core.gemspec @@ -20,6 +20,7 @@ Gem::Specification.new do |s| # Core s.add_dependency('bundler', ['~> 1.1']) + s.add_dependency('backports', ['~> 3.6']) s.add_dependency('rack', ['>= 1.4.5', '< 2.0']) s.add_dependency('tilt', ['~> 1.4.1']) s.add_dependency('erubis')