Reduce work converting paths from relative to absolute and back again

This commit is contained in:
Ben Hollis 2012-07-19 01:17:50 -07:00
parent 1f22184fc7
commit 62fa17cf1a
7 changed files with 40 additions and 46 deletions

View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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
end

View file

@ -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
end

View file

@ -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, "")

View file

@ -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
@server_inst.files.did_delete(path)
end