Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up pruner #969

Merged
merged 1 commit into from Oct 28, 2016
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+21 −6
Diff settings

Always

Just for now

Copy path View file
@@ -17,7 +17,7 @@ class Pruner
def initialize(site, dry_run: false, exclude: [])
@site = site
@dry_run = dry_run
@exclude = exclude
@exclude = Set.new(exclude)
end

# Prunes all output files not managed by Nanoc.
@@ -37,25 +37,40 @@ def run
present_files = []
present_dirs = []
Find.find(site.config[:output_dir] + '/') do |f|
present_files << f if File.file?(f)
present_dirs << f if File.directory?(f)
basename = File.basename(f)

case File.ftype(f)
when 'file'.freeze
unless exclude?(basename)
present_files << f
end
when 'directory'.freeze
if exclude?(basename)
Find.prune
else
present_dirs << f
end
end
end

# Remove stray files
stray_files = (present_files - compiled_files)
stray_files.each do |f|
next if filename_excluded?(f)
delete_file(f)
delete_file(f) unless exclude?(f)
end

# Remove empty directories
present_dirs.reverse_each do |dir|
next if Dir.foreach(dir) { |n| break true if n !~ /\A\.\.?\z/ }
next if filename_excluded?(dir)
next if exclude?(dir)
delete_dir(dir)
end
end

def exclude?(component)
@exclude.include?(component)
end

# @param [String] filename The filename to check
#
# @return [Boolean] true if the given file is excluded, false otherwise
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.