Class: Nanoc::Core::Pruner
Overview
Responsible for finding and deleting files in the site’s output directory that are not managed by Nanoc.
Instance Method Summary
collapse
enabled?, included, setup_once, warn_about_performance
Constructor Details
#initialize(config, reps, dry_run: false, exclude: []) ⇒ Pruner
Returns a new instance of Pruner.
11
12
13
14
15
16
|
# File 'lib/nanoc/core/pruner.rb', line 11
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
29
30
31
32
|
# File 'lib/nanoc/core/pruner.rb', line 29
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.
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/nanoc/core/pruner.rb', line 79
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
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/nanoc/core/pruner.rb', line 44
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.
67
68
69
70
71
72
73
74
75
|
# File 'lib/nanoc/core/pruner.rb', line 67
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.
58
59
60
61
62
63
|
# File 'lib/nanoc/core/pruner.rb', line 58
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
|
#run ⇒ Object
18
19
20
21
22
23
24
25
26
|
# File 'lib/nanoc/core/pruner.rb', line 18
def run
return unless File.directory?(@config.output_dir)
compiled_files = @reps.flat_map { |r| r.raw_paths.values.flatten }.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
35
36
37
38
39
40
41
|
# File 'lib/nanoc/core/pruner.rb', line 35
def strip_output_dir(filename)
if filename.start_with?(@config.output_dir)
filename[@config.output_dir.size..]
else
filename
end
end
|