From 62fa17cf1a85240f2ed1b4fe058b5045a88259e1 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Thu, 19 Jul 2012 01:17:50 -0700 Subject: [PATCH] Reduce work converting paths from relative to absolute and back again --- .../lib/middleman-core/cli/build.rb | 9 ++--- .../core_extensions/file_watcher.rb | 40 +++++++++---------- .../lib/middleman-core/preview_server.rb | 4 +- middleman-core/lib/middleman-core/sitemap.rb | 19 ++++----- .../sitemap/extensions/on_disk.rb | 6 +-- .../lib/middleman-core/sitemap/store.rb | 2 +- .../step_definitions/middleman_steps.rb | 6 +-- 7 files changed, 40 insertions(+), 46 deletions(-) diff --git a/middleman-core/lib/middleman-core/cli/build.rb b/middleman-core/lib/middleman-core/cli/build.rb index 2a9eaef1..12451ea1 100644 --- a/middleman-core/lib/middleman-core/cli/build.rb +++ b/middleman-core/lib/middleman-core/cli/build.rb @@ -237,7 +237,7 @@ module Middleman::Cli logger.debug "== Checking for Compass sprites" # Double-check for compass sprites - @app.files.find_new_files(Pathname.new(@app.source_dir) + @app.images_dir) + @app.files.find_new_files((Pathname(@app.source_dir) + @app.images_dir).relative_path_from(@app.root_path)) # 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 @@ -245,11 +245,8 @@ module Middleman::Cli logger.debug "== Building files" - resources = @app.sitemap.resources.sort do |a, b| - a_idx = sort_order.index(a.ext) || 100 - b_idx = sort_order.index(b.ext) || 100 - - a_idx <=> b_idx + resources = @app.sitemap.resources.sort_by do |r| + sort_order.index(r.ext) || 100 end # Loop over all the paths and build them. 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 35bd35c2..33685106 100644 --- a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb +++ b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb @@ -29,12 +29,12 @@ module Middleman # Before parsing config, load the data/ directory app.before_configuration do - files.reload_path(root_path + data_dir) + files.reload_path(data_dir) end # After config, load everything else app.ready do - files.reload_path(root_path) + files.reload_path('.') end end alias :included :registered @@ -89,7 +89,7 @@ module Middleman # @return [void] def did_change(path) return if ignored?(path) - logger.debug "== File Change: #{path.relative_path_from(@app.root_path)}" + logger.debug "== File Change: #{path}" @known_paths << path self.run_callbacks(path, :changed) end @@ -100,7 +100,7 @@ module Middleman # @return [void] def did_delete(path) return if ignored?(path) - logger.debug "== File Deletion: #{path.relative_path_from(@app.root_path)}" + logger.debug "== File Deletion: #{path}" @known_paths.delete(path) self.run_callbacks(path, :deleted) end @@ -111,24 +111,26 @@ module Middleman # @param [Boolean] only_new Whether we only look for new files # @return [void] def reload_path(path, only_new=false) - return unless path.exist? + # chdir into the root directory so Pathname can work with relative paths + Dir.chdir @app.root_path do + path = Pathname(path) + return unless path.exist? - glob = "#{path}**/*" - subset = @known_paths.select { |p| p.fnmatch(glob) } + glob = (path + "**/*").to_s + subset = @known_paths.select { |p| p.fnmatch(glob) } - ::Middleman::Util.all_files_under(path).each do |filepath| - full_path = path + filepath + ::Middleman::Util.all_files_under(path).each do |filepath| + if only_new + next if subset.include?(filepath) + else + subset.delete(filepath) + end - if only_new - next if subset.include?(full_path) - else - subset.delete(full_path) + self.did_change(filepath) end - - self.did_change(full_path) - end - subset.each(&method(:did_delete)) unless only_new + subset.each(&method(:did_delete)) unless only_new + end end # Like reload_path, but only triggers events on new files @@ -144,7 +146,6 @@ module Middleman # @param [Pathname] path # @return [Boolean] def ignored?(path) - path = path.relative_path_from(@app.root_path).to_s if path.is_a? Pathname IGNORE_LIST.any? { |r| path.to_s.match(r) } end @@ -154,9 +155,8 @@ module Middleman # @param [Symbol] callbacks_name The name of the callbacks method # @return [void] def run_callbacks(path, callbacks_name) - path = path.relative_path_from(@app.root_path).to_s if path.is_a? Pathname + path = path.to_s self.send(callbacks_name).each do |callback, matcher| - next if path.match(%r{^#{@app.build_dir}/}) next unless matcher.nil? || path.match(matcher) @app.instance_exec(path, &callback) end diff --git a/middleman-core/lib/middleman-core/preview_server.rb b/middleman-core/lib/middleman-core/preview_server.rb index 42e1cf78..255fe20d 100644 --- a/middleman-core/lib/middleman-core/preview_server.rb +++ b/middleman-core/lib/middleman-core/preview_server.rb @@ -98,7 +98,7 @@ module Middleman # Otherwise forward to Middleman added_and_modified.each do |path| - @app.files.did_change(@app.root_path + path) + @app.files.did_change(path) end end @@ -111,7 +111,7 @@ module Middleman # Otherwise forward to Middleman removed.each do |path| - @app.files.did_delete(@app.root_path + path) + @app.files.did_delete(path) end end end diff --git a/middleman-core/lib/middleman-core/sitemap.rb b/middleman-core/lib/middleman-core/sitemap.rb index 24a1c366..ac430225 100644 --- a/middleman-core/lib/middleman-core/sitemap.rb +++ b/middleman-core/lib/middleman-core/sitemap.rb @@ -25,22 +25,19 @@ module Middleman # Setup callbacks which can exclude paths from the sitemap app.set :ignored_sitemap_matchers, { # dotfiles and folders in the root - :root_dotfiles => proc { |file, path| file.match(/^\./) }, + :root_dotfiles => proc { |file| file.match(%r{^\.}) }, # Files starting with an dot, but not .htaccess - :source_dotfiles => proc { |file, path| - (file.match(/\/\./) && !file.match(/\/\.htaccess/)) + :source_dotfiles => proc { |file| + file.match(%r{/\.}) && !file.match(%r{/\.htaccess}) }, # Files starting with an underscore, but not a double-underscore - :partials => proc { |file, path| (file.match(/\/_/) && !file.match(/\/__/)) }, + :partials => proc { |file| file.match(%r{/_}) && !file.match(%r{/__}) }, - :layout => proc { |file, path| - file.match(/^source\/layout\./) || file.match(/^source\/layouts\//) - }, - - # Files without any output extension (layouts, partials) - # :extensionless => proc { |file, path| !path.match(/\./) }, + :layout => proc { |file| + file.match(%r{^source/layout\.}) || file.match(%r{^source/layouts/}) + } } # Include instance methods @@ -78,4 +75,4 @@ module Middleman end end -end \ No newline at end of file +end diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb b/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb index f1ca99f6..796e52fc 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb @@ -40,13 +40,13 @@ module Middleman # @param [String] file # @return [Boolean] def touch_file(file, rebuild=true) - return false if file == @app.source_dir || File.directory?(file) + return false if File.directory?(file) path = @sitemap.file_to_path(file) return false unless path ignored = @app.ignored_sitemap_matchers.any? do |name, callback| - callback.call(file, path) + callback.call(file) end @file_paths_on_disk << file unless ignored @@ -81,4 +81,4 @@ module Middleman end end end -end \ No newline at end of file +end diff --git a/middleman-core/lib/middleman-core/sitemap/store.rb b/middleman-core/lib/middleman-core/sitemap/store.rb index 1b5f90a7..5f457202 100644 --- a/middleman-core/lib/middleman-core/sitemap/store.rb +++ b/middleman-core/lib/middleman-core/sitemap/store.rb @@ -173,7 +173,7 @@ module Middleman file = File.expand_path(file, @app.root) prefix = @app.source_dir.sub(/\/$/, "") + "/" - return false unless file.include?(prefix) + return false unless file.start_with?(prefix) path = file.sub(prefix, "") diff --git a/middleman-core/lib/middleman-core/step_definitions/middleman_steps.rb b/middleman-core/lib/middleman-core/step_definitions/middleman_steps.rb index 288f089f..70dbcf44 100644 --- a/middleman-core/lib/middleman-core/step_definitions/middleman_steps.rb +++ b/middleman-core/lib/middleman-core/step_definitions/middleman_steps.rb @@ -9,9 +9,9 @@ Then /^the file "([^\"]*)" is removed$/ do |path| end Then /^the file "([^\"]*)" did change$/ do |path| - @server_inst.files.did_change(@server_inst.root_path + path) + @server_inst.files.did_change(path) end Then /^the file "([^\"]*)" did delete$/ do |path| - @server_inst.files.did_delete(@server_inst.root_path + path) -end \ No newline at end of file + @server_inst.files.did_delete(path) +end