Class: Nanoc::Extra::Pruner
- Inherits:
-
Object
- Object
- Nanoc::Extra::Pruner
- Defined in:
- lib/nanoc/extra/pruner.rb
Overview
Responsible for finding and deleting files in the site’s output directory that are not managed by nanoc.
Instance Attribute Summary (collapse)
-
- (Nanoc::Site) site
readonly
The site this pruner belongs to.
Instance Method Summary (collapse)
-
- (Boolean) filename_excluded?(filename)
True if the given file is excluded, false otherwise.
-
- (Pruner) initialize(site, params = {})
constructor
A new instance of Pruner.
-
- (void) run
Prunes all output files not managed by nanoc.
Constructor Details
- (Pruner) initialize(site, params = {})
Returns a new instance of Pruner
17 18 19 20 21 |
# File 'lib/nanoc/extra/pruner.rb', line 17 def initialize(site, params = {}) @site = site @dry_run = params.fetch(:dry_run) { false } @exclude = params.fetch(:exclude) { [] } end |
Instance Attribute Details
- (Nanoc::Site) site (readonly)
Returns The site this pruner belongs to
10 11 12 |
# File 'lib/nanoc/extra/pruner.rb', line 10 def site @site end |
Instance Method Details
- (Boolean) filename_excluded?(filename)
Returns true if the given file is excluded, false otherwise
62 63 64 65 |
# File 'lib/nanoc/extra/pruner.rb', line 62 def filename_excluded?(filename) pathname = Pathname.new(filename) @exclude.any? { |e| pathname.include_component?(e) } end |
- (void) run
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 56 57 |
# File 'lib/nanoc/extra/pruner.rb', line 26 def run require 'find' # Get compiled files compiled_files = 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(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 |