From a4fcb4d9399c186fce769ac2ca733ccfc3fe18ea Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Mon, 23 Apr 2012 01:12:52 -0700 Subject: [PATCH 1/2] Fix the file watcher to correctly use relative paths, to ignore files we don't care about, and have a find_new_files method that works like reload_path, but only touches previously-unknown files. This all ends up speeding up the build by a lot. --- .../lib/middleman-core/cli/build.rb | 8 ++++++- .../core_extensions/file_watcher.rb | 22 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/middleman-core/lib/middleman-core/cli/build.rb b/middleman-core/lib/middleman-core/cli/build.rb index 9a1db425..447bca62 100644 --- a/middleman-core/lib/middleman-core/cli/build.rb +++ b/middleman-core/lib/middleman-core/cli/build.rb @@ -196,19 +196,25 @@ module Middleman::Cli sort_order = %w(.png .jpeg .jpg .gif .bmp .svg .svgz .ico .woff .otf .ttf .eot .js .css) # Pre-request CSS to give Compass a chance to build sprites + puts "== Prerendering CSS" if @app.logging? + @app.sitemap.resources.select do |resource| resource.ext == ".css" end.each do |resource| Middleman::Cli::Build.shared_rack.get(resource.destination_path.gsub(/\s/, "%20")) end + puts "== Checking for Compass sprites" if @app.logging? + # Double-check for compass sprites - @app.files.reload_path(File.join(@app.source_dir, @app.images_dir)) + @app.files.find_new_files(File.join(@app.source_dir, @app.images_dir)) # Sort paths to be built by the above order. This is primarily so Compass can # find files in the build folder when it needs to generate sprites for the # css files + puts "== Building files" if @app.logging? + resources = @app.sitemap.resources.sort do |a, b| a_idx = sort_order.index(a.ext) || 100 b_idx = sort_order.index(b.ext) || 100 diff --git a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb index b165a1b1..3563ef35 100644 --- a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb +++ b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb @@ -103,12 +103,14 @@ module Middleman::CoreExtensions::FileWatcher # @param [String] path The path to reload # @return [void] def reload_path(path) - subset = self.known_paths.select { |p| p.match(%r{^#{path}}) } + relative_path = path.sub("#{self.instance.root}/", "") + subset = self.known_paths.select { |p| p.match(%r{^#{relative_path}}) } Find.find(path) do |path| next if File.directory?(path) + next if Middleman::Watcher.ignore_list.any? { |r| path.match(r) } relative_path = path.sub("#{self.instance.root}/", "") - subset.delete(relative_path) if subset.include?(relative_path) + subset.delete(relative_path) self.did_change(relative_path) end if File.exists?(path) @@ -116,6 +118,22 @@ module Middleman::CoreExtensions::FileWatcher self.did_delete(removed_path) end end + + # Like reload_path, but only triggers events on new files + # + # @param [String] path The path to reload + # @return [void] + def find_new_files(path) + relative_path = path.sub("#{self.instance.root}/", "") + subset = self.known_paths.select { |p| p.match(%r{^#{relative_path}}) } + + Find.find(path) do |file| + next if File.directory?(file) + next if Middleman::Watcher.ignore_list.any? { |r| path.match(r) } + relative_path = file.sub("#{self.instance.root}/", "") + self.did_change(relative_path) unless subset.include?(relative_path) + end if File.exists?(path) + end protected # Notify callbacks for a file given an array of callbacks From ab77cb2f3479d6841d82bbcae55cca49939e65ec Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Mon, 23 Apr 2012 01:16:03 -0700 Subject: [PATCH 2/2] Add an "origin" parameter to provides_metadata to allow for replacing metadata callbacks rather than always appending --- .../lib/middleman-core/sitemap/store.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/middleman-core/lib/middleman-core/sitemap/store.rb b/middleman-core/lib/middleman-core/sitemap/store.rb index d6ba9fb5..96faf607 100644 --- a/middleman-core/lib/middleman-core/sitemap/store.rb +++ b/middleman-core/lib/middleman-core/sitemap/store.rb @@ -113,10 +113,22 @@ module Middleman::Sitemap # Register a handler to provide metadata on a url path # @param [Regexp] matcher + # @param [Symbol] origin an indicator of where this metadata came from - only one + # block per [matcher, origin] pair may exist. # @return [Array>] - def provides_metadata_for_path(matcher=nil, &block) + def provides_metadata_for_path(matcher=nil, origin=nil, &block) @_provides_metadata_for_path ||= [] - @_provides_metadata_for_path << [block, matcher] if block_given? + if block_given? + if origin + existing_provider = @_provides_metadata_for_path.find {|b,m,o| o == origin && m == matcher} + end + + if existing_provider + existing_provider[0] = block + else + @_provides_metadata_for_path << [block, matcher, origin] + end + end @_provides_metadata_for_path end @@ -188,4 +200,4 @@ module Middleman::Sitemap path end end -end \ No newline at end of file +end