module Racket::Utils::FSM

Utility functions for filesystem.

Public Class Methods

dir_or_nil(path) click to toggle source

Return a Pathname for a directory if the directory is readable, otherwise returns nil.

@param [String] path @return [Pathname|nil]

# File lib/racket/utils/file_system.rb, line 104
def self.dir_or_nil(path)
  return nil unless path
  path = Pathname.new(path)
  dir_readable?(path) ? path : nil
end
dir_readable?(path) click to toggle source

Returns whether a directory is readable or not. In order to be readable, the directory must a) exist b) be a directory c) be readable by the current user

@param [Pathname] path @return [true|false]

# File lib/racket/utils/file_system.rb, line 96
def self.dir_readable?(path)
  path.exist? && path.directory? && path.readable?
end
extract_dir_and_glob(path) click to toggle source

Extracts the correct directory and glob for a given base path/path combination.

@param [Pathname] path @return [Array]

# File lib/racket/utils/file_system.rb, line 114
def self.extract_dir_and_glob(path)
  basename = path.basename
  [
    path.dirname,
    path.extname.empty? ? Pathname.new("#{basename}.*") : basename
  ]
end
file_readable?(path) click to toggle source

Returns whether a file is readable or not. In order to be readable, the file must a) exist b) be a file c) be readable by the current user

@param [Pathname|String] path @return [true|false] @todo Remove temporary workaround for handling string, we want to use Pathname everywhere

possible.
# File lib/racket/utils/file_system.rb, line 131
def self.file_readable?(path)
  # path = Pathname.new(path) unless path.is_a?(Pathname)
  path.exist? && path.file? && path.readable?
end
first_matching_path(base_path, glob) click to toggle source

Returns the first matching path under base_path matching glob. If no matching path can be found, nil is returned.

@param [Pathname] base_path @param [Pathname] glob @return [Pathname|nil]

# File lib/racket/utils/file_system.rb, line 142
def self.first_matching_path(base_path, glob)
  paths = matching_paths(base_path, glob)
  paths.empty? ? nil : paths.first
end
fs_path(base_pathname, url_path) click to toggle source

Given a base pathname and a url path string, returns a pathname.

@param [Pathname] base_pathname @param [String] url_path @return [Pathname]

# File lib/racket/utils/file_system.rb, line 152
def self.fs_path(base_pathname, url_path)
  parts = url_path.split('/').reject(&:empty?)
  parts.each { |part| base_pathname = base_pathname.join(part) }
  base_pathname
end
matching_paths(base_path, glob) click to toggle source

Returns all paths under base_path that matches glob.

@param [Pathname] base_path @param [Pathname] glob @return [Array]

# File lib/racket/utils/file_system.rb, line 191
def self.matching_paths(base_path, glob)
  return [] unless dir_readable?(base_path)
  Dir.chdir(base_path) { Pathname.glob(glob) }.map { |path| base_path.join(path) }
end
resolve_path(path) click to toggle source

Locates a file in the filesystem matching an URL path. If there exists a matching file, the path to it is returned. If there is no matching file, nil is returned. @param [Pathname] path @return [Pathname|nil]

# File lib/racket/utils/file_system.rb, line 162
def self.resolve_path(path)
  first_matching_path(*extract_dir_and_glob(path))
end
resolve_path_with_default(path, default) click to toggle source

Locates a file in the filesystem matching an URL path. If there exists a matching file, the path to it is returned. If there is no matching file and default is a String or a Symbol, another lookup will be performed using default. If default is a Proc or nil, default will be used as is instead.

@param [Pathname] path @param [String|Symbol|Proc|nil] default @return [String|Proc|nil]

# File lib/racket/utils/file_system.rb, line 174
def self.resolve_path_with_default(path, default)
  # Return template if it can be found in the file system
  template = resolve_path(path)
  return template if template
  # No template found for path. Try the default template instead.
  # If default template is a string or a symbol, look it up in the file system
  return resolve_path(fs_path(path.dirname, default)) if
    default.is_a?(String) || default.is_a?(Symbol)
  # If default template is a proc or nil, just return it
  default
end

Public Instance Methods

build_path(*args) click to toggle source

Builds and returns a path in the file system from the provided arguments. The first element in the argument list can be either absolute or relative, all other arguments must be relative, otherwise they will be removed from the final path.

@param [Array] args @return [Pathname]

# File lib/racket/utils/file_system.rb, line 202
def build_path(*args)
  PathBuilder.to_pathname(@root_dir, *args)
end
safe_require(resource) click to toggle source

Safely requires a file. This method will catch load errors and return true (if the file was loaded) or false (if the file was not loaded).

@param [String] resource @return [true|false]

# File lib/racket/utils/file_system.rb, line 211
def safe_require(resource)
  run_block(LoadError) { require resource }
end