Class: JekyllImgFlow::GeneratedFileCleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-imgflow/generated_file_cleaner.rb

Instance Method Summary collapse

Constructor Details

#initialize(site, config) ⇒ GeneratedFileCleaner

Returns a new instance of GeneratedFileCleaner.



7
8
9
10
# File 'lib/jekyll-imgflow/generated_file_cleaner.rb', line 7

def initialize(site, config)
  @site = site
  @config = config
end

Instance Method Details

#clearObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jekyll-imgflow/generated_file_cleaner.rb', line 28

def clear
  roots = [@site.source, @site.dest].map { |base_path| output_root(base_path) }
  valid = roots.zip([@site.source, @site.dest]).all? do |directory, base_path|
    base = File.expand_path(base_path)
    directory.start_with?("#{base}#{File::SEPARATOR}")
  end
  raise "Refusing to delete optimized directory outside configured roots" unless valid

  unregister_output_tree(roots.first)
  files = roots.flat_map do |directory|
    Dir.glob(File.join(directory, "**", "*")).select { |path| File.file?(path) }
  end
  files = files.uniq
  files.each { |path| File.delete(path) }
  files.length
end

#delete(output_path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jekyll-imgflow/generated_file_cleaner.rb', line 12

def delete(output_path)
  return false unless output_path

  unregister_static_file(output_path)
  deleted = false
  [@site.source, @site.dest].each do |base_path|
    path = safe_generated_path(base_path, output_path)
    next unless path && File.file?(path)

    File.delete(path)
    remove_empty_output_dirs(File.dirname(path), output_root(base_path))
    deleted = true
  end
  deleted
end