Module: Racket::Utils::FileSystem
- Included in:
- ToolBelt
- Defined in:
- lib/racket/utils/file_system.rb
Overview
Utility functions for filesystem.
Defined Under Namespace
Classes: PathBuilder, SizedPath
Class Method Summary collapse
-
.dir_or_nil(path) ⇒ Pathname|nil
Return a Pathname for a directory if the directory is readable, otherwise returns nil.
-
.dir_readable?(path) ⇒ true|false
Returns whether a directory is readable or not.
-
.extract_dir_and_glob(path) ⇒ Array
Extracts the correct directory and glob for a given base path/path combination.
-
.file_readable?(path) ⇒ true|false
Returns whether a file is readable or not.
-
.first_matching_path(base_path, glob) ⇒ Pathname|nil
Returns the first matching path under
base_path
matchingglob
. -
.fs_path(base_pathname, url_path) ⇒ Pathname
Given a base pathname and a url path string, returns a pathname.
-
.matching_paths(base_path, glob) ⇒ Array
Returns all paths under
base_path
that matchesglob
. -
.resolve_path(path) ⇒ Pathname|nil
Locates a file in the filesystem matching an URL path.
-
.resolve_path_with_default(path, default) ⇒ String|Proc|nil
Locates a file in the filesystem matching an URL path.
Instance Method Summary collapse
-
#build_path(*args) ⇒ Pathname
Builds and returns a path in the file system from the provided arguments.
-
#safe_require(resource) ⇒ true|false
Safely requires a file.
Class Method Details
.dir_or_nil(path) ⇒ Pathname|nil
Return a Pathname for a directory if the directory is readable, otherwise returns nil.
104 105 106 107 108 |
# 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) ⇒ true|false
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
96 97 98 |
# 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) ⇒ Array
Extracts the correct directory and glob for a given base path/path combination.
114 115 116 117 118 119 120 |
# 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) ⇒ true|false
Remove temporary workaround for handling string, we want to use Pathname everywhere possible.
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
131 132 133 134 |
# 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) ⇒ Pathname|nil
Returns the first matching path under base_path
matching glob
. If no matching path can be found, nil
is returned.
142 143 144 145 |
# 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) ⇒ Pathname
Given a base pathname and a url path string, returns a pathname.
152 153 154 155 156 |
# 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) ⇒ Array
Returns all paths under base_path
that matches glob
.
191 192 193 194 |
# 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) ⇒ Pathname|nil
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.
162 163 164 |
# 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) ⇒ String|Proc|nil
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.
174 175 176 177 178 179 180 181 182 183 184 |
# 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 |
Instance Method Details
#build_path(*args) ⇒ Pathname
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.
202 203 204 |
# File 'lib/racket/utils/file_system.rb', line 202 def build_path(*args) PathBuilder.to_pathname(@root_dir, *args) end |
#safe_require(resource) ⇒ true|false
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).
211 212 213 |
# File 'lib/racket/utils/file_system.rb', line 211 def safe_require(resource) run_block(LoadError) { require resource } end |