class Tigefa::Site::Cleaner

Handles the cleanup of a site's destination before the site is built.

Public Class Methods

new(site) click to toggle source
# File lib/tigefa/cleaner.rb, line 7
def initialize(site)
  @site = site
end

Public Instance Methods

cleanup!() click to toggle source

Cleans up the site's destination directory

# File lib/tigefa/cleaner.rb, line 12
def cleanup!
  FileUtils.rm_rf(obsolete_files)
end

Private Instance Methods

existing_files() click to toggle source

Private: The list of existing files, except those included in keep_files and hidden files.

Returns a Set with the file paths

# File lib/tigefa/cleaner.rb, line 28
def existing_files
  files = Set.new
  Dir.glob(File.join(@site.dest, "**", "*"), File::FNM_DOTMATCH) do |file|
    files << file unless file =~ /\/\.{1,2}$/ || file =~ keep_file_regex
  end
  files
end
keep_file_regex() click to toggle source

Private: creates a regular expression from the config's keep_files array

Examples

['.git','.svn'] creates the following regex: /\/(\.git|\/.svn)/

Returns the regular expression

# File lib/tigefa/cleaner.rb, line 66
def keep_file_regex
  or_list = @site.keep_files.join("|")
  pattern = "\/(#{or_list.gsub(".", "\.")})"
  Regexp.new pattern
end
new_dirs() click to toggle source

Private: The list of directories to be created when the site is built. These are the parent directories of the files in new_files.

Returns a Set with the directory paths

# File lib/tigefa/cleaner.rb, line 49
def new_dirs
  new_files.map { |file| File.dirname(file) }.to_set
end
new_files() click to toggle source

Private: The list of files to be created when the site is built.

Returns a Set with the file paths

# File lib/tigefa/cleaner.rb, line 39
def new_files
  files = Set.new
  @site.each_site_file { |item| files << item.destination(@site.dest) }
  files
end
obsolete_files() click to toggle source

Private: The list of files and directories to be deleted during the cleanup process

Returns an Array with the file and directory paths

# File lib/tigefa/cleaner.rb, line 21
def obsolete_files
  (existing_files - new_files - new_dirs + replaced_files).to_a
end
replaced_files() click to toggle source

Private: The list of existing files that will be replaced by a directory during build

Returns a Set with the file paths

# File lib/tigefa/cleaner.rb, line 56
def replaced_files
  new_dirs.select { |dir| File.file?(dir) }.to_set
end