Parent

Class/Module Index [+]

Quicksearch

Nanoc::Extra::Pruner

Responsible for finding and deleting files in the site’s output directory that are not managed by nanoc.

Attributes

site[R]

@return [Nanoc::Site] The site this pruner belongs to

Public Class Methods

new(site, params={}) click to toggle source

@param [Nanoc::Site] site The site for which a pruner is created

@option params [Boolean] :dry_run (false) true if the files to be deleted

should only be printed instead of actually deleted, false if the files
should actually be deleted.
# File lib/nanoc/extra/pruner.rb, line 16
def initialize(site, params={})
  @site    = site
  @dry_run = params.fetch(:dry_run) { false }
  @exclude = params.fetch(:exclude) { [] }
end

Public Instance Methods

filename_excluded?(filename) click to toggle source

@param [String] filename The filename to check

@return [Boolean] true if the given file is excluded, false otherwise

# File lib/nanoc/extra/pruner.rb, line 61
def filename_excluded?(filename)
  pathname = Pathname.new(filename)
  @exclude.any? { |e| pathname.include_component?(e) }
end
run() click to toggle source

Prunes all output files not managed by nanoc.

@return [void]

# File lib/nanoc/extra/pruner.rb, line 25
def run
  require 'find'

  # Get compiled files
  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) }

  # Get present files and dirs
  present_files = []
  present_dirs = []
  Find.find(self.site.config[:output_dir] + '/') do |f|
    present_files << f if File.file?(f)
    present_dirs  << f if File.directory?(f)
  end

  # Remove stray files
  stray_files = (present_files - compiled_files)
  stray_files.each do |f|
    next if filename_excluded?(f)
    self.delete_file(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)
    self.delete_dir(dir)
  end
end

Protected Instance Methods

delete_dir(dir) click to toggle source
# File lib/nanoc/extra/pruner.rb, line 77
def delete_dir(dir)
  if @dry_run
    puts dir
  else
    Nanoc::CLI::Logger.instance.file(:high, :delete, dir)
    Dir.rmdir(dir)
  end
end
delete_file(file) click to toggle source
# File lib/nanoc/extra/pruner.rb, line 68
def delete_file(file)
  if @dry_run
    puts file
  else
    Nanoc::CLI::Logger.instance.file(:high, :delete, file)
    FileUtils.rm(file)
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.