From 5da13d2b6601326e2db4a272f8106d449f1988aa Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Sat, 17 Aug 2013 17:12:27 +0200 Subject: [PATCH 1/6] Refactor `Site#cleanup` Move the cleanup logic out into a separate class, `Jekyll::Cleaner`. --- lib/jekyll.rb | 1 + lib/jekyll/cleaner.rb | 69 +++++++++++++++++++++++++++++++++++++++++++ lib/jekyll/site.rb | 42 ++------------------------ 3 files changed, 73 insertions(+), 39 deletions(-) create mode 100644 lib/jekyll/cleaner.rb diff --git a/lib/jekyll.rb b/lib/jekyll.rb index 3f5758376..5ff25deb4 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -42,6 +42,7 @@ require 'jekyll/filters' require 'jekyll/static_file' require 'jekyll/errors' require 'jekyll/related_posts' +require 'jekyll/cleaner' # extensions require 'jekyll/plugin' diff --git a/lib/jekyll/cleaner.rb b/lib/jekyll/cleaner.rb new file mode 100644 index 000000000..b167f0a73 --- /dev/null +++ b/lib/jekyll/cleaner.rb @@ -0,0 +1,69 @@ +require 'set' + +module Jekyll + # Handles the cleanup of a site's destination before the site is built. + class Cleaner + def initialize(site) + @site = site + end + + # Cleans up the site's destination directory + def cleanup! + obsolete_files = existing_files - new_files - new_dirs + replaced_files + FileUtils.rm_rf(obsolete_files.to_a) + end + + private + + # 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| + if @site.keep_files.length > 0 + files << file unless file =~ /\/\.{1,2}$/ || file =~ keep_file_regex + else + files << file unless file =~ /\/\.{1,2}$/ + end + end + files + 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 \ No newline at end of file diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index f7a8c379f..17cbf7a40 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -1,5 +1,3 @@ -require 'set' - module Jekyll class Site attr_accessor :config, :layouts, :posts, :pages, :static_files, @@ -89,6 +87,8 @@ module Jekyll self.converters = instantiate_subclasses(Jekyll::Converter) self.generators = instantiate_subclasses(Jekyll::Generator) + + @site_cleaner = Cleaner.new(self) end # Internal: Setup the plugin search path @@ -228,43 +228,7 @@ module Jekyll # # Returns nothing. def cleanup - # all files and directories in destination, including hidden ones - dest_files = Set.new - Dir.glob(File.join(self.dest, "**", "*"), File::FNM_DOTMATCH) do |file| - if self.keep_files.length > 0 - dest_files << file unless file =~ /\/\.{1,2}$/ || file =~ keep_file_regex - else - dest_files << file unless file =~ /\/\.{1,2}$/ - end - end - - # files to be written - files = Set.new - each_site_file { |item| files << item.destination(self.dest) } - - # adding files' parent directories - dirs = Set.new - files.each { |file| dirs << File.dirname(file) } - files.merge(dirs) - - # files that are replaced by dirs should be deleted - files_to_delete = Set.new - dirs.each { |dir| files_to_delete << dir if File.file?(dir) } - - obsolete_files = dest_files - files + files_to_delete - FileUtils.rm_rf(obsolete_files.to_a) - end - - # Private: creates a regular expression from the keep_files array - # - # Examples - # ['.git','.svn'] creates the following regex: /\/(\.git|\/.svn)/ - # - # Returns the regular expression - def keep_file_regex - or_list = self.keep_files.join("|") - pattern = "\/(#{or_list.gsub(".", "\.")})" - Regexp.new pattern + @site_cleaner.cleanup! end # Write static files, pages, and posts. From 5d7dba476cd86d72e89d7cbb05ef3cabc5df2897 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Sat, 17 Aug 2013 22:33:01 +0200 Subject: [PATCH 2/6] change site_cleaner to use attr_accessor --- lib/jekyll/site.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 17cbf7a40..51ee135ab 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -3,7 +3,7 @@ module Jekyll attr_accessor :config, :layouts, :posts, :pages, :static_files, :categories, :exclude, :include, :source, :dest, :lsi, :pygments, :permalink_style, :tags, :time, :future, :safe, :plugins, :limit_posts, - :show_drafts, :keep_files, :baseurl + :show_drafts, :keep_files, :baseurl, :site_cleaner attr_accessor :converters, :generators @@ -88,7 +88,7 @@ module Jekyll self.converters = instantiate_subclasses(Jekyll::Converter) self.generators = instantiate_subclasses(Jekyll::Generator) - @site_cleaner = Cleaner.new(self) + self.site_cleaner = Cleaner.new(self) end # Internal: Setup the plugin search path @@ -228,7 +228,7 @@ module Jekyll # # Returns nothing. def cleanup - @site_cleaner.cleanup! + self.site_cleaner.cleanup! end # Write static files, pages, and posts. From 0b3005b3b457a022904c632369cddf76d074f481 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Mon, 19 Aug 2013 20:01:36 +0200 Subject: [PATCH 3/6] make obsolete_files a method (@mattr-) --- lib/jekyll/cleaner.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/jekyll/cleaner.rb b/lib/jekyll/cleaner.rb index b167f0a73..77c353454 100644 --- a/lib/jekyll/cleaner.rb +++ b/lib/jekyll/cleaner.rb @@ -9,12 +9,18 @@ module Jekyll # Cleans up the site's destination directory def cleanup! - obsolete_files = existing_files - new_files - new_dirs + replaced_files - FileUtils.rm_rf(obsolete_files.to_a) + 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 From d28858a9e96447e3fb5058807254cbc4caa9c577 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Mon, 19 Aug 2013 20:15:54 +0200 Subject: [PATCH 4/6] put the Cleaner instance in a `Site#site_cleaner` method --- lib/jekyll/site.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 51ee135ab..7f2cda2ee 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -3,7 +3,7 @@ module Jekyll attr_accessor :config, :layouts, :posts, :pages, :static_files, :categories, :exclude, :include, :source, :dest, :lsi, :pygments, :permalink_style, :tags, :time, :future, :safe, :plugins, :limit_posts, - :show_drafts, :keep_files, :baseurl, :site_cleaner + :show_drafts, :keep_files, :baseurl attr_accessor :converters, :generators @@ -87,8 +87,6 @@ module Jekyll self.converters = instantiate_subclasses(Jekyll::Converter) self.generators = instantiate_subclasses(Jekyll::Generator) - - self.site_cleaner = Cleaner.new(self) end # Internal: Setup the plugin search path @@ -228,7 +226,7 @@ module Jekyll # # Returns nothing. def cleanup - self.site_cleaner.cleanup! + site_cleaner.cleanup! end # Write static files, pages, and posts. @@ -386,5 +384,9 @@ module Jekyll limit = self.posts.length < limit_posts ? self.posts.length : limit_posts self.posts = self.posts[-limit, limit] end + + def site_cleaner + @site_cleaner ||= Cleaner.new(self) + end end end From dbd368f6eec03db666f8a8083b3cf24bca687a8d Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Fri, 23 Aug 2013 20:41:13 +0200 Subject: [PATCH 5/6] remove no longer necessary condition --- lib/jekyll/cleaner.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/jekyll/cleaner.rb b/lib/jekyll/cleaner.rb index 77c353454..46eeffa5f 100644 --- a/lib/jekyll/cleaner.rb +++ b/lib/jekyll/cleaner.rb @@ -27,11 +27,7 @@ module Jekyll def existing_files files = Set.new Dir.glob(File.join(@site.dest, "**", "*"), File::FNM_DOTMATCH) do |file| - if @site.keep_files.length > 0 - files << file unless file =~ /\/\.{1,2}$/ || file =~ keep_file_regex - else - files << file unless file =~ /\/\.{1,2}$/ - end + files << file unless file =~ /\/\.{1,2}$/ || file =~ keep_file_regex end files end From 4c015fc5ff8f2d25eaf681b3d55f520fc5b35138 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Fri, 23 Aug 2013 20:42:27 +0200 Subject: [PATCH 6/6] move Jekyll::Cleaner to Jekyll::Site::Cleaner --- lib/jekyll/cleaner.rb | 122 +++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 60 deletions(-) diff --git a/lib/jekyll/cleaner.rb b/lib/jekyll/cleaner.rb index 46eeffa5f..a933b9e26 100644 --- a/lib/jekyll/cleaner.rb +++ b/lib/jekyll/cleaner.rb @@ -1,71 +1,73 @@ require 'set' module Jekyll - # Handles the cleanup of a site's destination before the site is built. - class Cleaner - def initialize(site) - @site = site - end - - # 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 + class Site + # Handles the cleanup of a site's destination before the site is built. + class Cleaner + def initialize(site) + @site = site end - files - 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 + # Cleans up the site's destination directory + def cleanup! + FileUtils.rm_rf(obsolete_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 - # 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: 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: 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 + # 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 + files + 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 \ No newline at end of file