Skip to content

Commit

Permalink
Merge pull request #498 from nanoc/feature/dotfiles_gh_492
Browse files Browse the repository at this point in the history
Add dotfiles support
  • Loading branch information
denisdefreyne committed Dec 13, 2014
2 parents b5b7b53 + 1782d70 commit 82f9a88
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/nanoc/data_sources/filesystem.rb
Expand Up @@ -179,7 +179,7 @@ def all_split_files_in(dir_name)

# Returns all files in the given directory and directories below it.
def all_files_in(dir_name)
Nanoc::Extra::FilesystemTools.all_files_in(dir_name)
Nanoc::Extra::FilesystemTools.all_files_in(dir_name, config[:extra_files])
end

# Returns the filename for the given base filename and the extension.
Expand Down
2 changes: 1 addition & 1 deletion lib/nanoc/data_sources/static.rb
Expand Up @@ -54,7 +54,7 @@ def items
protected

def all_files_in(dir_name)
Nanoc::Extra::FilesystemTools.all_files_in(dir_name)
Nanoc::Extra::FilesystemTools.all_files_in(dir_name, config[:extra_files])
end
end
end
41 changes: 37 additions & 4 deletions lib/nanoc/extra/filesystem_tools.rb
Expand Up @@ -43,18 +43,22 @@ def initialize(filename)
# @param [String] dir_name The name of the directory whose contents to
# fetch
#
# @param [String, Array, nil] extra_files The list of extra patterns
# to extend the file search for files not found by default, example
# "**/.{htaccess,htpasswd}"
#
# @param [Integer] recursion_limit The maximum number of times to
# recurse into a symlink to a directory
#
# @return [Array<String>] A list of filenames
# @return [Array<String>] A list of file names
#
# @raise [MaxSymlinkDepthExceededError] if too many indirections are
# encountered while resolving symlinks
#
# @raise [UnsupportedFileTypeError] if a file of an unsupported type is
# detected (something other than file, directory or link)
def all_files_in(dir_name, recursion_limit = 10)
Dir[dir_name + '/**/*'].map do |fn|
def all_files_in(dir_name, extra_files, recursion_limit = 10)
all_files_and_dirs_in(dir_name, extra_files).map do |fn|
case File.ftype(fn)
when 'link'
if 0 == recursion_limit
Expand All @@ -64,7 +68,7 @@ def all_files_in(dir_name, recursion_limit = 10)
if File.file?(absolute_target)
fn
else
all_files_in(absolute_target, recursion_limit - 1).map do |sfn|
all_files_in(absolute_target, extra_files, recursion_limit - 1).map do |sfn|
fn + sfn[absolute_target.size..-1]
end
end
Expand All @@ -80,6 +84,35 @@ def all_files_in(dir_name, recursion_limit = 10)
end
module_function :all_files_in

# Returns all files and directories in the given directory and
# directories below it.
#
# @param [String] dir_name The name of the directory whose contents to
# fetch
#
# @param [String, Array, nil] extra_files The list of extra patterns
# to extend the file search for files not found by default, example
# "**/.{htaccess,htpasswd}"
#
# @return [Array<String>] A list of files and directories
#
# @raise [GenericTrivial] when pattern can not be handled
def all_files_and_dirs_in(dir_name, extra_files)
patterns = ["#{dir_name}/**/*"]
case extra_files
when nil
when String
patterns << "#{dir_name}/#{extra_files}"
when Array
patterns.concat(extra_files.map { |extra_file| "#{dir_name}/#{extra_file}" })
else
raise Nanoc::Errors::GenericTrivial,
"Do not know how to handle extra_files: #{extra_files.inspect}"
end
Dir.glob(patterns)
end
module_function :all_files_and_dirs_in

# Resolves the given symlink into an absolute path.
#
# @param [String] filename The filename of the symlink to resolve
Expand Down
2 changes: 1 addition & 1 deletion test/data_sources/test_static.rb
Expand Up @@ -32,7 +32,7 @@ def test_items_with_symlinks

# Check all files
expected_filenames = ['foo/a.png', 'foo/1/b.png', 'foo/c.png'].sort
actual_filenames = Nanoc::Extra::FilesystemTools.all_files_in('foo').sort
actual_filenames = Nanoc::Extra::FilesystemTools.all_files_in('foo', nil).sort
assert_equal expected_filenames, actual_filenames

# Check items
Expand Down
50 changes: 46 additions & 4 deletions test/extra/test_filesystem_tools.rb
Expand Up @@ -32,7 +32,7 @@ def test_all_files_in_follows_symlinks_to_dirs
'dir0/sub/sub/sub/sub/sub/sub/sub/sub/sub/foo.md',
'dir0/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/foo.md'
]
actual_files = Nanoc::Extra::FilesystemTools.all_files_in('dir0').sort
actual_files = Nanoc::Extra::FilesystemTools.all_files_in('dir0', nil).sort
assert_equal expected_files, actual_files
end

Expand All @@ -47,7 +47,7 @@ def test_all_files_in_follows_symlinks_to_dirs_too_many
end

assert_raises Nanoc::Extra::FilesystemTools::MaxSymlinkDepthExceededError do
Nanoc::Extra::FilesystemTools.all_files_in('dir0')
Nanoc::Extra::FilesystemTools.all_files_in('dir0', nil)
end
end

Expand All @@ -61,7 +61,7 @@ def test_all_files_in_relativizes_directory_names
File.symlink('../bar', 'foo/barlink')

expected_files = ['foo/barlink/y.md', 'foo/x.md']
actual_files = Nanoc::Extra::FilesystemTools.all_files_in('foo').sort
actual_files = Nanoc::Extra::FilesystemTools.all_files_in('foo', nil).sort
assert_equal expected_files, actual_files
end

Expand All @@ -74,7 +74,7 @@ def test_all_files_in_follows_symlinks_to_files

# Check
expected_files = ['dir/bar-link', 'dir/foo']
actual_files = Nanoc::Extra::FilesystemTools.all_files_in('dir').sort
actual_files = Nanoc::Extra::FilesystemTools.all_files_in('dir', nil).sort
assert_equal expected_files, actual_files
end

Expand All @@ -100,4 +100,46 @@ def test_resolve_symlink_too_many
Nanoc::Extra::FilesystemTools.resolve_symlink('symlink-7')
end
end

def test_unwanted_dotfiles_not_found
# Write sample files
FileUtils.mkdir_p('dir')
File.open('dir/.DS_Store', 'w') { |io| io.write('o hai') }
File.open('dir/.htaccess', 'w') { |io| io.write('o hai') }

actual_files = Nanoc::Extra::FilesystemTools.all_files_in('dir', nil).sort
assert_equal [], actual_files
end

def test_user_dotfiles_are_valid_items
# Write sample files
FileUtils.mkdir_p('dir')
File.open('dir/.other', 'w') { |io| io.write('o hai') }

actual_files = Nanoc::Extra::FilesystemTools.all_files_in('dir', "**/.other").sort
assert_equal ['dir/.other'], actual_files
end

def test_multiple_user_dotfiles_are_valid_items
# Write sample files
FileUtils.mkdir_p('dir')
File.open('dir/.other', 'w') { |io| io.write('o hai') }
File.open('dir/.DS_Store', 'w') { |io| io.write('o hai') }

actual_files = Nanoc::Extra::FilesystemTools.all_files_in('dir', ["**/.other", "**/.DS_Store"]).sort
assert_equal ['dir/.other', 'dir/.DS_Store'].sort, actual_files.sort
end

def test_unknown_pattern
# Write sample files
FileUtils.mkdir_p('dir')
File.open('dir/.other', 'w') { |io| io.write('o hai') }

pattern = {:dotfiles => "**/.other"}

assert_raises Nanoc::Errors::GenericTrivial, "Do not know how to handle extra_files: #{pattern.inspect}" do
Nanoc::Extra::FilesystemTools.all_files_in('dir0', pattern)
end
end

end

0 comments on commit 82f9a88

Please sign in to comment.