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
15 16 17 18 19 |
# File 'lib/nanoc/extra/pruner.rb', line 15 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
8 9 10 |
# File 'lib/nanoc/extra/pruner.rb', line 8 def site @site end |
Instance Method Details
- (Boolean) filename_excluded?(filename)
Returns true if the given file is excluded, false otherwise
59 60 61 62 |
# File 'lib/nanoc/extra/pruner.rb', line 59 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.
24 25 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 |
# File 'lib/nanoc/extra/pruner.rb', line 24 def run require 'find' # Get compiled files all_raw_paths = site.items.map do |item| item.reps.map(&:raw_path) end 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 |