Module: Nanoc::Extra::FilesystemTools Private

Defined in:
lib/nanoc/extra/filesystem_tools.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Contains useful functions for managing the filesystem.

Defined Under Namespace

Classes: MaxSymlinkDepthExceededError, UnsupportedFileTypeError

Class Method Summary collapse

Class Method Details

.all_files_and_dirs_in(dir_name, extra_files) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns all files and directories in the given directory and directories below it.

Parameters:

  • dir_name (String)

    The name of the directory whose contents to fetch

  • extra_files (String, Array, nil)

    The list of extra patterns to extend the file search for files not found by default, example “**/.htaccess,htpasswd”

Returns:

Raises:

  • (GenericTrivial)

    when pattern can not be handled



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nanoc/extra/filesystem_tools.rb', line 94

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::Int::Errors::GenericTrivial,
      "Do not know how to handle extra_files: #{extra_files.inspect}",
    )
  end
  Dir.glob(patterns)
end

.all_files_in(dir_name, extra_files, recursion_limit = 10) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns all files in the given directory and directories below it, following symlinks up to a maximum of ‘recursion_limit` times.

Parameters:

  • dir_name (String)

    The name of the directory whose contents to fetch

  • extra_files (String, Array, nil)

    The list of extra patterns to extend the file search for files not found by default, example “**/.htaccess,htpasswd”

  • recursion_limit (Integer) (defaults to: 10)

    The maximum number of times to recurse into a symlink to a directory

Returns:

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/nanoc/extra/filesystem_tools.rb', line 54

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
        raise MaxSymlinkDepthExceededError.new(fn)
      else
        absolute_target = resolve_symlink(fn)
        if File.file?(absolute_target)
          fn
        else
          all_files_in(absolute_target, extra_files, recursion_limit - 1).map do |sfn|
            fn + sfn[absolute_target.size..-1]
          end
        end
      end
    when 'file'
      fn
    when 'directory'
      nil
    else
      raise UnsupportedFileTypeError.new(fn)
    end
  end.compact.flatten
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolves the given symlink into an absolute path.

Parameters:

  • filename (String)

    The filename of the symlink to resolve

  • recursion_limit (Integer) (defaults to: 5)

    The maximum number of times to recurse into a symlink

Returns:

  • (String)

    The absolute resolved filename of the symlink target

Raises:



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/nanoc/extra/filesystem_tools.rb', line 126

def resolve_symlink(filename, recursion_limit = 5)
  target = File.readlink(filename)
  absolute_target = File.expand_path(target, File.dirname(filename))

  case File.ftype(absolute_target)
  when 'link'
    if 0 == recursion_limit
      raise MaxSymlinkDepthExceededError.new(absolute_target)
    else
      resolve_symlink(absolute_target, recursion_limit - 1)
    end
  when 'file', 'directory'
    absolute_target
  else
    raise UnsupportedFileTypeError.new(absolute_target)
  end
end