move Jekyll::Cleaner to Jekyll::Site::Cleaner
This commit is contained in:
parent
dbd368f6ee
commit
4c015fc5ff
|
@ -1,71 +1,73 @@
|
||||||
require 'set'
|
require 'set'
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
# Handles the cleanup of a site's destination before the site is built.
|
class Site
|
||||||
class Cleaner
|
# Handles the cleanup of a site's destination before the site is built.
|
||||||
def initialize(site)
|
class Cleaner
|
||||||
@site = site
|
def initialize(site)
|
||||||
end
|
@site = site
|
||||||
|
|
||||||
# Cleans up the site's destination directory
|
|
||||||
def cleanup!
|
|
||||||
FileUtils.rm_rf(obsolete_files)
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
# Private: The list of files and directories to be deleted during the cleanup process
|
|
||||||
#
|
|
||||||
# Returns an Array with the file and directory paths
|
|
||||||
def obsolete_files
|
|
||||||
(existing_files - new_files - new_dirs + replaced_files).to_a
|
|
||||||
end
|
|
||||||
|
|
||||||
# Private: The list of existing files, except those included in keep_files and hidden files.
|
|
||||||
#
|
|
||||||
# Returns a Set with the file paths
|
|
||||||
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
|
end
|
||||||
files
|
|
||||||
end
|
|
||||||
|
|
||||||
# Private: The list of files to be created when the site is built.
|
# Cleans up the site's destination directory
|
||||||
#
|
def cleanup!
|
||||||
# Returns a Set with the file paths
|
FileUtils.rm_rf(obsolete_files)
|
||||||
def new_files
|
end
|
||||||
files = Set.new
|
|
||||||
@site.each_site_file { |item| files << item.destination(@site.dest) }
|
|
||||||
files
|
|
||||||
end
|
|
||||||
|
|
||||||
# Private: The list of directories to be created when the site is built.
|
private
|
||||||
# These are the parent directories of the files in #new_files.
|
|
||||||
#
|
|
||||||
# Returns a Set with the directory paths
|
|
||||||
def new_dirs
|
|
||||||
new_files.map { |file| File.dirname(file) }.to_set
|
|
||||||
end
|
|
||||||
|
|
||||||
# Private: The list of existing files that will be replaced by a directory during build
|
# Private: The list of files and directories to be deleted during the cleanup process
|
||||||
#
|
#
|
||||||
# Returns a Set with the file paths
|
# Returns an Array with the file and directory paths
|
||||||
def replaced_files
|
def obsolete_files
|
||||||
new_dirs.select { |dir| File.file?(dir) }.to_set
|
(existing_files - new_files - new_dirs + replaced_files).to_a
|
||||||
end
|
end
|
||||||
|
|
||||||
# Private: creates a regular expression from the config's keep_files array
|
# Private: The list of existing files, except those included in keep_files and hidden files.
|
||||||
#
|
#
|
||||||
# Examples
|
# Returns a Set with the file paths
|
||||||
# ['.git','.svn'] creates the following regex: /\/(\.git|\/.svn)/
|
def existing_files
|
||||||
#
|
files = Set.new
|
||||||
# Returns the regular expression
|
Dir.glob(File.join(@site.dest, "**", "*"), File::FNM_DOTMATCH) do |file|
|
||||||
def keep_file_regex
|
files << file unless file =~ /\/\.{1,2}$/ || file =~ keep_file_regex
|
||||||
or_list = @site.keep_files.join("|")
|
end
|
||||||
pattern = "\/(#{or_list.gsub(".", "\.")})"
|
files
|
||||||
Regexp.new pattern
|
end
|
||||||
|
|
||||||
|
# Private: The list of files to be created when the site is built.
|
||||||
|
#
|
||||||
|
# Returns a Set with the file paths
|
||||||
|
def new_files
|
||||||
|
files = Set.new
|
||||||
|
@site.each_site_file { |item| files << item.destination(@site.dest) }
|
||||||
|
files
|
||||||
|
end
|
||||||
|
|
||||||
|
# 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
|
||||||
|
def new_dirs
|
||||||
|
new_files.map { |file| File.dirname(file) }.to_set
|
||||||
|
end
|
||||||
|
|
||||||
|
# Private: The list of existing files that will be replaced by a directory during build
|
||||||
|
#
|
||||||
|
# Returns a Set with the file paths
|
||||||
|
def replaced_files
|
||||||
|
new_dirs.select { |dir| File.file?(dir) }.to_set
|
||||||
|
end
|
||||||
|
|
||||||
|
# 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
|
||||||
|
def keep_file_regex
|
||||||
|
or_list = @site.keep_files.join("|")
|
||||||
|
pattern = "\/(#{or_list.gsub(".", "\.")})"
|
||||||
|
Regexp.new pattern
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Reference in New Issue