Merge pull request #545 from bhollis/optimize

Reduce work converting paths from relative to absolute and back again
This commit is contained in:
Thomas Reynolds 2012-07-19 10:58:46 -07:00
commit 923f7e8408
7 changed files with 42 additions and 50 deletions

View file

@ -97,9 +97,7 @@ module Middleman::Cli
def shared_rack def shared_rack
@_shared_rack ||= begin @_shared_rack ||= begin
mock = ::Rack::MockSession.new(shared_server.to_rack_app) mock = ::Rack::MockSession.new(shared_server.to_rack_app)
sess = ::Rack::Test::Session.new(mock) ::Rack::Test::Session.new(mock)
response = sess.get("__middleman__")
sess
end end
end end
@ -239,7 +237,7 @@ module Middleman::Cli
logger.debug "== Checking for Compass sprites" logger.debug "== Checking for Compass sprites"
# Double-check 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 # 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 # find files in the build folder when it needs to generate sprites for the
@ -247,11 +245,8 @@ module Middleman::Cli
logger.debug "== Building files" logger.debug "== Building files"
resources = @app.sitemap.resources.sort do |a, b| resources = @app.sitemap.resources.sort_by do |r|
a_idx = sort_order.index(a.ext) || 100 sort_order.index(r.ext) || 100
b_idx = sort_order.index(b.ext) || 100
a_idx <=> b_idx
end end
# Loop over all the paths and build them. # Loop over all the paths and build them.
@ -279,4 +274,4 @@ class ::Thor::Actions::CreateFile
block.call unless pretend? block.call unless pretend?
end end
end end
end end

View file

@ -29,12 +29,12 @@ module Middleman
# Before parsing config, load the data/ directory # Before parsing config, load the data/ directory
app.before_configuration do app.before_configuration do
files.reload_path(root_path + data_dir) files.reload_path(data_dir)
end end
# After config, load everything else # After config, load everything else
app.ready do app.ready do
files.reload_path(root_path) files.reload_path('.')
end end
end end
alias :included :registered alias :included :registered
@ -89,7 +89,7 @@ module Middleman
# @return [void] # @return [void]
def did_change(path) def did_change(path)
return if ignored?(path) return if ignored?(path)
logger.debug "== File Change: #{path.relative_path_from(@app.root_path)}" logger.debug "== File Change: #{path}"
@known_paths << path @known_paths << path
self.run_callbacks(path, :changed) self.run_callbacks(path, :changed)
end end
@ -100,7 +100,7 @@ module Middleman
# @return [void] # @return [void]
def did_delete(path) def did_delete(path)
return if ignored?(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) @known_paths.delete(path)
self.run_callbacks(path, :deleted) self.run_callbacks(path, :deleted)
end end
@ -111,24 +111,26 @@ module Middleman
# @param [Boolean] only_new Whether we only look for new files # @param [Boolean] only_new Whether we only look for new files
# @return [void] # @return [void]
def reload_path(path, only_new=false) 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}**/*" glob = (path + "**/*").to_s
subset = @known_paths.select { |p| p.fnmatch(glob) } subset = @known_paths.select { |p| p.fnmatch(glob) }
::Middleman::Util.all_files_under(path).each do |filepath| ::Middleman::Util.all_files_under(path).each do |filepath|
full_path = path + filepath if only_new
next if subset.include?(filepath)
else
subset.delete(filepath)
end
if only_new self.did_change(filepath)
next if subset.include?(full_path)
else
subset.delete(full_path)
end 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 end
# Like reload_path, but only triggers events on new files # Like reload_path, but only triggers events on new files
@ -144,7 +146,6 @@ module Middleman
# @param [Pathname] path # @param [Pathname] path
# @return [Boolean] # @return [Boolean]
def ignored?(path) 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) } IGNORE_LIST.any? { |r| path.to_s.match(r) }
end end
@ -154,9 +155,8 @@ module Middleman
# @param [Symbol] callbacks_name The name of the callbacks method # @param [Symbol] callbacks_name The name of the callbacks method
# @return [void] # @return [void]
def run_callbacks(path, callbacks_name) 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| self.send(callbacks_name).each do |callback, matcher|
next if path.match(%r{^#{@app.build_dir}/})
next unless matcher.nil? || path.match(matcher) next unless matcher.nil? || path.match(matcher)
@app.instance_exec(path, &callback) @app.instance_exec(path, &callback)
end end

View file

@ -98,7 +98,7 @@ module Middleman
# Otherwise forward to Middleman # Otherwise forward to Middleman
added_and_modified.each do |path| added_and_modified.each do |path|
@app.files.did_change(@app.root_path + path) @app.files.did_change(path)
end end
end end
@ -111,7 +111,7 @@ module Middleman
# Otherwise forward to Middleman # Otherwise forward to Middleman
removed.each do |path| removed.each do |path|
@app.files.did_delete(@app.root_path + path) @app.files.did_delete(path)
end end
end end
end end

View file

@ -25,22 +25,19 @@ module Middleman
# Setup callbacks which can exclude paths from the sitemap # Setup callbacks which can exclude paths from the sitemap
app.set :ignored_sitemap_matchers, { app.set :ignored_sitemap_matchers, {
# dotfiles and folders in the root # 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 # Files starting with an dot, but not .htaccess
:source_dotfiles => proc { |file, path| :source_dotfiles => proc { |file|
(file.match(/\/\./) && !file.match(/\/\.htaccess/)) file.match(%r{/\.}) && !file.match(%r{/\.htaccess})
}, },
# Files starting with an underscore, but not a double-underscore # 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| :layout => proc { |file|
file.match(/^source\/layout\./) || file.match(/^source\/layouts\//) file.match(%r{^source/layout\.}) || file.match(%r{^source/layouts/})
}, }
# Files without any output extension (layouts, partials)
# :extensionless => proc { |file, path| !path.match(/\./) },
} }
# Include instance methods # Include instance methods
@ -78,4 +75,4 @@ module Middleman
end end
end end
end end

View file

@ -40,13 +40,13 @@ module Middleman
# @param [String] file # @param [String] file
# @return [Boolean] # @return [Boolean]
def touch_file(file, rebuild=true) 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) path = @sitemap.file_to_path(file)
return false unless path return false unless path
ignored = @app.ignored_sitemap_matchers.any? do |name, callback| ignored = @app.ignored_sitemap_matchers.any? do |name, callback|
callback.call(file, path) callback.call(file)
end end
@file_paths_on_disk << file unless ignored @file_paths_on_disk << file unless ignored
@ -81,4 +81,4 @@ module Middleman
end end
end end
end end
end end

View file

@ -173,7 +173,7 @@ module Middleman
file = File.expand_path(file, @app.root) file = File.expand_path(file, @app.root)
prefix = @app.source_dir.sub(/\/$/, "") + "/" prefix = @app.source_dir.sub(/\/$/, "") + "/"
return false unless file.include?(prefix) return false unless file.start_with?(prefix)
path = file.sub(prefix, "") path = file.sub(prefix, "")

View file

@ -9,9 +9,9 @@ Then /^the file "([^\"]*)" is removed$/ do |path|
end end
Then /^the file "([^\"]*)" did change$/ do |path| Then /^the file "([^\"]*)" did change$/ do |path|
@server_inst.files.did_change(@server_inst.root_path + path) @server_inst.files.did_change(path)
end end
Then /^the file "([^\"]*)" did delete$/ do |path| Then /^the file "([^\"]*)" did delete$/ do |path|
@server_inst.files.did_delete(@server_inst.root_path + path) @server_inst.files.did_delete(path)
end end