Class: AstroSubframeOrganizer::Utils::EmptyDirectoryCleaner

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/astro_subframe_organizer/utils/empty_directory_cleaner.rb

Overview

A cleanup utility that removes empty directories from ‘path` and all its subdirectories. Useful after organizing and moving subframes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(path = Dir.pwd) ⇒ EmptyDirectoryCleaner

Returns a new instance of EmptyDirectoryCleaner.



12
13
14
# File 'lib/astro_subframe_organizer/utils/empty_directory_cleaner.rb', line 12

def initialize(path = Dir.pwd)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/astro_subframe_organizer/utils/empty_directory_cleaner.rb', line 10

def path
  @path
end

Instance Method Details

#cleanup(dry_run: false, verbose: false) ⇒ Object

Removes empty directories under the given directory.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/astro_subframe_organizer/utils/empty_directory_cleaner.rb', line 17

def cleanup(dry_run: false, verbose: false)
  logger.info 'Cleaning up empty directories...'

  Dir.glob('**//*/', base: path).reverse_each do |dir|
    full_path = File.join(path, dir)
    entries   = Dir.entries(full_path) - ['.', '..', '.DS_Store']

    next unless entries.empty?

    unless dry_run
      ds_store = File.join(full_path, '.DS_Store')
      FileUtils.rm_f(ds_store, verbose: verbose)
      FileUtils.rmdir(full_path, verbose: verbose)
    end
    logger.debug "rmdir #{full_path}"
  end
end