Module: Racket::Utils::FileSystem
- Defined in:
- lib/racket/utils/file_system.rb
Overview
Utility functions for filesystem.
Defined Under Namespace
Classes: PathBuilder, SizedPath
Class Method Summary collapse
-
.build_path(*args) ⇒ Pathname
Builds and returns a path in the file system from the provided arguments.
-
.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_pathmatchingglob. -
.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_paththat matchesglob. -
.paths_by_longest_path(base_dir, glob) ⇒ Object
Returns a list of relative file paths, sorted by path (longest first).
-
.safe_require(resource) ⇒ true|false
Safely requires a file.
Class 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.
95 96 97 |
# File 'lib/racket/utils/file_system.rb', line 95 def self.build_path(*args) PathBuilder.to_pathname(*args) 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
106 107 108 |
# File 'lib/racket/utils/file_system.rb', line 106 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
142 143 144 145 |
# File 'lib/racket/utils/file_system.rb', line 142 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.
163 164 165 166 |
# File 'lib/racket/utils/file_system.rb', line 163 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.
127 128 129 130 131 |
# File 'lib/racket/utils/file_system.rb', line 127 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.
152 153 154 155 |
# File 'lib/racket/utils/file_system.rb', line 152 def self.matching_paths(base_path, glob) return [] unless Utils.dir_readable?(base_path) Dir.chdir(base_path) { Pathname.glob(glob) }.map { |path| base_path.join(path) } end |
.paths_by_longest_path(base_dir, glob) ⇒ Object
Returns a list of relative file paths, sorted by path (longest first).
return [Array]
173 174 175 176 |
# File 'lib/racket/utils/file_system.rb', line 173 def self.paths_by_longest_path(base_dir, glob) paths = matching_paths(base_dir, glob).map { |path| SizedPath.new(path) }.sort paths.map(&:path) 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).
183 184 185 |
# File 'lib/racket/utils/file_system.rb', line 183 def self.safe_require(resource) Utils.run_block(LoadError) { require resource } end |