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

+ (Array<String>) all_files_and_dirs_in(dir_name, extra_files)

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:

  • extra_files (String, Array, nil)

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

  • dir_name (String)

    The name of the directory whose contents to fetch

Returns:

Raises:

  • (GenericTrivial)

    when pattern can not be handled



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/nanoc/extra/filesystem_tools.rb', line 100

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

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

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:

  • 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

  • dir_name (String)

    The name of the directory whose contents to fetch

Returns:

Raises:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/nanoc/extra/filesystem_tools.rb', line 60

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:

  • recursion_limit (Integer) (defaults to: 5)

    The maximum number of times to recurse into a symlink

  • filename (String)

    The filename of the symlink to resolve

Returns:

  • (String)

    The absolute resolved filename of the symlink target

Raises:



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/nanoc/extra/filesystem_tools.rb', line 130

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