Class: Nanoc::Core::Pruner

Inherits:
Object
  • Object
show all
Includes:
ContractsSupport
Defined in:
lib/nanoc/core/pruner.rb

Overview

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

Instance Method Summary collapse

Methods included from ContractsSupport

enabled?, included, setup_once, warn_about_performance

Constructor Details

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

Returns a new instance of Pruner.



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

def initialize(config, reps, dry_run: false, exclude: [])
  @config  = config
  @reps    = reps
  @dry_run = dry_run
  @exclude = Set.new(exclude)
end

Instance Method Details

#filename_excluded?(filename) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/nanoc/core/pruner.rb', line 37

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

#files_and_dirs_in(dir) ⇒ Object

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.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nanoc/core/pruner.rb', line 87

def files_and_dirs_in(dir)
  present_files = []
  present_dirs = []

  expanded_dir = File.expand_path(dir)

  Find.find(dir) do |f|
    case File.ftype(f)
    when 'file'
      unless filename_excluded?(f)
        present_files << f
      end
    when 'directory'
      if filename_excluded?(f)
        Find.prune
      elsif expanded_dir != File.expand_path(f)
        present_dirs << f
      end
    end
  end

  [present_files, present_dirs]
end

#pathname_components(pathname) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nanoc/core/pruner.rb', line 52

def pathname_components(pathname)
  components = []
  tmp = pathname
  loop do
    old = tmp
    components << File.basename(tmp)
    tmp = File.dirname(tmp)
    break if old == tmp
  end
  components.reverse
end

#remove_empty_directories(present_dirs) ⇒ Object

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.



75
76
77
78
79
80
81
82
83
# File 'lib/nanoc/core/pruner.rb', line 75

def remove_empty_directories(present_dirs)
  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
  self
end

#remove_stray_files(present_files, compiled_files) ⇒ Object

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.



66
67
68
69
70
71
# File 'lib/nanoc/core/pruner.rb', line 66

def remove_stray_files(present_files, compiled_files)
  (present_files - compiled_files).each do |f|
    delete_file(f) unless filename_excluded?(f)
  end
  self
end

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nanoc/core/pruner.rb', line 23

def run
  return unless File.directory?(@config.output_dir)

  compiled_files = @reps.flat_map do |r|
    r.raw_paths.values.flatten
  end.compact
  present_files, present_dirs =
    files_and_dirs_in("#{@config.output_dir}/")

  remove_stray_files(present_files, compiled_files)
  remove_empty_directories(present_dirs)
end

#strip_output_dir(filename) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/nanoc/core/pruner.rb', line 43

def strip_output_dir(filename)
  if filename.start_with?(@config.output_dir)
    filename[@config.output_dir.size..]
  else
    filename
  end
end