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_in(dir_name, 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:

  • 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:



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

def all_files_in(dir_name, recursion_limit = 10)
  Dir[dir_name + '/**/*'].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, 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:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nanoc/extra/filesystem_tools.rb', line 103

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