Skip to content

Commit

Permalink
Merge pull request #685 from edeustace/keep_files_feature
Browse files Browse the repository at this point in the history
Keep files feature
  • Loading branch information
parkr committed Jan 11, 2013
2 parents 6253f79 + 0fa5541 commit 418ef41
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
4 changes: 4 additions & 0 deletions bin/jekyll
Expand Up @@ -145,6 +145,10 @@ opts = OptionParser.new do |opts|
puts "Jekyll " + Jekyll::VERSION puts "Jekyll " + Jekyll::VERSION
exit 0 exit 0
end end

opts.on( "--keep-files filename1,filename2", Array, "Whether to keep files that match the filename (default: .git)") do |names|
options['keep_files'] = names
end
end end


# Read command line options into `options` hash # Read command line options into `options` hash
Expand Down
1 change: 1 addition & 0 deletions lib/jekyll.rb
Expand Up @@ -60,6 +60,7 @@ module Jekyll
'destination' => File.join(Dir.pwd, '_site'), 'destination' => File.join(Dir.pwd, '_site'),
'plugins' => File.join(Dir.pwd, '_plugins'), 'plugins' => File.join(Dir.pwd, '_plugins'),
'layouts' => '_layouts', 'layouts' => '_layouts',
'keep_files' => ['.git','.svn'],


'future' => true, 'future' => true,
'lsi' => false, 'lsi' => false,
Expand Down
25 changes: 21 additions & 4 deletions lib/jekyll/site.rb
Expand Up @@ -5,7 +5,8 @@ module Jekyll
class Site class Site
attr_accessor :config, :layouts, :posts, :pages, :static_files, attr_accessor :config, :layouts, :posts, :pages, :static_files,
:categories, :exclude, :include, :source, :dest, :lsi, :pygments, :categories, :exclude, :include, :source, :dest, :lsi, :pygments,
:permalink_style, :tags, :time, :future, :safe, :plugins, :limit_posts :permalink_style, :tags, :time, :future, :safe, :plugins, :limit_posts,
:keep_files


attr_accessor :converters, :generators attr_accessor :converters, :generators


Expand All @@ -26,6 +27,7 @@ def initialize(config)
self.include = config['include'] || [] self.include = config['include'] || []
self.future = config['future'] self.future = config['future']
self.limit_posts = config['limit_posts'] || nil self.limit_posts = config['limit_posts'] || nil
self.keep_files = config['keep_files'] || []


self.reset self.reset
self.setup self.setup
Expand Down Expand Up @@ -222,8 +224,12 @@ def render
def cleanup def cleanup
# all files and directories in destination, including hidden ones # all files and directories in destination, including hidden ones
dest_files = Set.new dest_files = Set.new
Dir.glob(File.join(self.dest, "**", "*")) do |file| Dir.glob(File.join(self.dest, "**", "*"), File::FNM_DOTMATCH) do |file|
dest_files << 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 end


# files to be written # files to be written
Expand All @@ -244,10 +250,21 @@ def cleanup
files.merge(dirs) files.merge(dirs)


obsolete_files = dest_files - files obsolete_files = dest_files - files

FileUtils.rm_rf(obsolete_files.to_a) FileUtils.rm_rf(obsolete_files.to_a)
end 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
end

# Write static files, pages, and posts. # Write static files, pages, and posts.
# #
# Returns nothing. # Returns nothing.
Expand Down
26 changes: 26 additions & 0 deletions test/test_site.rb
Expand Up @@ -200,19 +200,45 @@ class TestSite < Test::Unit::TestCase
File.open(dest_dir('qux/obsolete.html'), 'w') File.open(dest_dir('qux/obsolete.html'), 'w')
# empty directory # empty directory
FileUtils.mkdir(dest_dir('quux')) FileUtils.mkdir(dest_dir('quux'))
FileUtils.mkdir(dest_dir('.git'))
FileUtils.mkdir(dest_dir('.svn'))
FileUtils.mkdir(dest_dir('.hg'))
# single file in repository
File.open(dest_dir('.git/HEAD'), 'w')
File.open(dest_dir('.svn/HEAD'), 'w')
File.open(dest_dir('.hg/HEAD'), 'w')
end end


teardown do teardown do
FileUtils.rm_f(dest_dir('obsolete.html')) FileUtils.rm_f(dest_dir('obsolete.html'))
FileUtils.rm_rf(dest_dir('qux')) FileUtils.rm_rf(dest_dir('qux'))
FileUtils.rm_f(dest_dir('quux')) FileUtils.rm_f(dest_dir('quux'))
FileUtils.rm_rf(dest_dir('.git'))
FileUtils.rm_rf(dest_dir('.svn'))
FileUtils.rm_rf(dest_dir('.hg'))
end end


should 'remove orphaned files in destination' do should 'remove orphaned files in destination' do
@site.process @site.process
assert !File.exist?(dest_dir('obsolete.html')) assert !File.exist?(dest_dir('obsolete.html'))
assert !File.exist?(dest_dir('qux')) assert !File.exist?(dest_dir('qux'))
assert !File.exist?(dest_dir('quux')) assert !File.exist?(dest_dir('quux'))
assert File.exist?(dest_dir('.git'))
assert File.exist?(dest_dir('.git/HEAD'))
end

should 'remove orphaned files in destination - keep_files .svn' do
config = Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'keep_files' => ['.svn']})
@site = Site.new(config)
@site.process
assert !File.exist?(dest_dir('.htpasswd'))
assert !File.exist?(dest_dir('obsolete.html'))
assert !File.exist?(dest_dir('qux'))
assert !File.exist?(dest_dir('quux'))
assert !File.exist?(dest_dir('.git'))
assert !File.exist?(dest_dir('.git/HEAD'))
assert File.exist?(dest_dir('.svn'))
assert File.exist?(dest_dir('.svn/HEAD'))
end end


end end
Expand Down

0 comments on commit 418ef41

Please sign in to comment.