Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dotfiles support #498

Merged
merged 8 commits into from Dec 13, 2014
@@ -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.
@@ -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
@@ -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

This comment has been minimized.

Copy link
@ddfreyne

ddfreyne Nov 29, 2014

Member

The addition of the new extra_files param makes it not backwards compatible. Could this be converted into a params = {} defaulting to []?

This comment has been minimized.

Copy link
@mpapis

mpapis Nov 29, 2014

Author Member

https://github.com/nanoc/nanoc/blob/feature/dotfiles_gh_492/lib/nanoc/extra/filesystem_tools.rb#L7 :

# @api private

wouldn't that mean we do not have to care about backwards compatibility? ... not that I can't do that change, just curious.

This comment has been minimized.

Copy link
@ddfreyne

ddfreyne Nov 29, 2014

Member

You’re right. I didn’t spot the @api private. So, never mind :)

#
# @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
@@ -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
@@ -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}" })

This comment has been minimized.

Copy link
@mpapis

mpapis Dec 6, 2014

Author Member

now, is << and concat ok, or should I change << to concat([...])?

This comment has been minimized.

Copy link
@ddfreyne

ddfreyne Dec 13, 2014

Member

Yup, that’s OK. It doesn’t make sense to use #concat when appending only a single element.

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
@@ -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
@@ -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

@@ -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

@@ -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

@@ -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

@@ -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
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.