Class: Nanoc::Extra::Pruner Private

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc/extra/pruner.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, dry_run: false, exclude: []) ⇒ Pruner

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Pruner.

Parameters:

  • site (Nanoc::Int::Site)

    The site for which a pruner is created

  • dry_run (Boolean) (defaults to: false)

    true if the files to be deleted should only be printed instead of actually deleted, false if the files should actually be deleted.

  • exclude (Enumerable<String>) (defaults to: [])


17
18
19
20
21
# File 'lib/nanoc/extra/pruner.rb', line 17

def initialize(site, dry_run: false, exclude: [])
  @site    = site
  @dry_run = dry_run
  @exclude = exclude
end

Instance Attribute Details

#siteNanoc::Int::Site (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The site this pruner belongs to.

Returns:



8
9
10
# File 'lib/nanoc/extra/pruner.rb', line 8

def site
  @site
end

Instance Method Details

#filename_excluded?(filename) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if the given file is excluded, false otherwise.

Parameters:

  • filename (String)

    The filename to check

Returns:

  • (Boolean)

    true if the given file is excluded, false otherwise



60
61
62
63
# File 'lib/nanoc/extra/pruner.rb', line 60

def filename_excluded?(filename)
  pathname = Pathname.new(filename)
  @exclude.any? { |e| pathname.__nanoc_include_component?(e) }
end

#runvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Prunes all output files not managed by Nanoc.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nanoc/extra/pruner.rb', line 26

def run
  require 'find'

  # Get compiled files
  # FIXME: requires #build_reps to have been called
  all_raw_paths = site.compiler.reps.flat_map { |r| r.raw_paths.values }
  compiled_files = all_raw_paths.flatten.compact.select { |f| File.file?(f) }

  # Get present files and dirs
  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)
  end

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