def run
require 'find'
compiled_files = self.site.items.map do |item|
item.reps.map do |rep|
rep.raw_path
end
end.flatten.compact.select { |f| File.file?(f) }
present_files_and_dirs = Set.new
Find.find(self.site.config[:output_dir]) do |f|
present_files_and_dirs << f
end
present_files = present_files_and_dirs.select { |f| File.file?(f) }
present_dirs = present_files_and_dirs.select { |f| File.directory?(f) }
stray_files = (present_files - compiled_files)
stray_files.each do |f|
next if filename_excluded?(f)
self.delete_file(f)
end
present_dirs.sort_by{ |d| -d.length }.each do |dir|
next if Dir.foreach(dir) { |n| break true if n !~ /\A\.\.?\z/ }
next if filename_excluded?(dir)
self.delete_dir(dir)
end
end