Module: FileSystem
- Defined in:
- lib/atk/info.rb,
lib/atk/file_system.rb
Overview
duplicated for the sake of efficieny (so that the parser doesn’t need to import all of FileSystem)
Class Method Summary collapse
-
.absolute_path(*args) ⇒ Object
inherit from File.
-
.absolute_path?(path) ⇒ Boolean
Pathname aliases.
- .basename(*args) ⇒ Object
- .cd(*args, verbose: false) ⇒ Object
- .chdir(*args) ⇒ Object
- .copy(from: nil, to: nil, new_name: "", force: true, preserve: false, dereference_root: false) ⇒ Object
- .delete(path) ⇒ Object
- .dirname(*args) ⇒ Object
- .download(the_url, to: nil) ⇒ Object
- .empty?(*args) ⇒ Boolean
- .executable?(*args) ⇒ Boolean
- .exists?(*args) ⇒ Boolean
- .expand_path(*args) ⇒ Object
- .extname(*args) ⇒ Object
- .file?(*args) ⇒ Boolean
- .folder?(*args) ⇒ Boolean
- .glob(path) ⇒ Object
-
.home ⇒ Object
dir aliases.
- .in_dir(path_to_somewhere) ⇒ Object
- .join(*args) ⇒ Object
- .list_files(path = ".") ⇒ Object
- .list_folders(path = ".") ⇒ Object
- .ls(path = ".") ⇒ Object
- .makedirs(path) ⇒ Object
- .mkfifo(*args) ⇒ Object
- .move(from: nil, to: nil, new_name: "", force: true, noop: nil, verbose: nil, secure: nil) ⇒ Object
- .online? ⇒ Boolean
- .owned?(*args) ⇒ Boolean
- .path_pieces(path) ⇒ Object
- .pipe?(*args) ⇒ Boolean
- .pwd ⇒ Object
- .read(filepath) ⇒ Object
- .readable?(*args) ⇒ Boolean
- .relative_path?(path) ⇒ Boolean
- .rename(path, new_name: nil, force: true) ⇒ Object
- .save(value, to: nil, as: nil) ⇒ Object
- .size?(*args) ⇒ Boolean
- .socket?(*args) ⇒ Boolean
- .stat(*args) ⇒ Object
- .symlink?(*args) ⇒ Boolean
-
.time_access(*args) ⇒ Object
File aliases.
- .time_created(*args) ⇒ Object
- .time_modified(*args) ⇒ Object
- .touch(*args) ⇒ Object
- .touch_dir(path) ⇒ Object
- .username ⇒ Object
- .world_readable?(*args) ⇒ Boolean
- .world_writable?(*args) ⇒ Boolean
- .writable?(*args) ⇒ Boolean
- .writable_real?(*args) ⇒ Boolean
-
.write(data, to: nil) ⇒ Object
change_owner set_permissions relative_path_between relative_path_to add a force: true option to most of the commands zip unzip.
Class Method Details
.absolute_path(*args) ⇒ Object
inherit from File
283 284 285 |
# File 'lib/atk/file_system.rb', line 283 def self.absolute_path(*args) File.absolute_path(*args) end |
.absolute_path?(path) ⇒ Boolean
Pathname aliases
196 197 198 |
# File 'lib/atk/file_system.rb', line 196 def self.absolute_path?(path) Pathname.new(path).absolute? end |
.basename(*args) ⇒ Object
289 290 291 |
# File 'lib/atk/file_system.rb', line 289 def self.basename(*args) File.basename(*args) end |
.cd(*args, verbose: false) ⇒ Object
247 248 249 250 251 252 |
# File 'lib/atk/file_system.rb', line 247 def self.cd(*args, verbose: false) if args.size == 0 args[0] = FS.home end FileUtils.cd(args[0], verbose: verbose) end |
.chdir(*args) ⇒ Object
253 254 255 |
# File 'lib/atk/file_system.rb', line 253 def self.chdir(*args) FS.cd(*args) end |
.copy(from: nil, to: nil, new_name: "", force: true, preserve: false, dereference_root: false) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/atk/file_system.rb', line 137 def self.copy(from:nil, to:nil, new_name:"", force: true, preserve: false, dereference_root: false) if new_name == "" raise "\n\nFileSystem.copy() needs a new_name: argument\nset new_name:nil if you wish the file/folder to keep the same name\ne.g. FileSystem.copy(from:'place/thing', to:'place', new_name:nil)" elsif new_name == nil new_name = File.basename(from) end # make sure the "to" path exists FileSystem.touch_dir(to) # perform the copy FileUtils.copy_entry(from, to/new_name, preserve, dereference_root, force) end |
.delete(path) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/atk/file_system.rb', line 105 def self.delete(path) if File.file?(path) File.delete(path) elsif File.directory?(path) FileUtils.rm_rf(path) end end |
.dirname(*args) ⇒ Object
286 287 288 |
# File 'lib/atk/file_system.rb', line 286 def self.dirname(*args) File.dirname(*args) end |
.download(the_url, to: nil) ⇒ Object
390 391 392 393 |
# File 'lib/atk/file_system.rb', line 390 def self.download(the_url, to:nil) require 'open-uri' FileSystem.write(open(URI.encode(the_url)).read, to: to) end |
.empty?(*args) ⇒ Boolean
315 316 317 |
# File 'lib/atk/file_system.rb', line 315 def self.empty?(*args) File.empty?(*args) end |
.executable?(*args) ⇒ Boolean
320 321 322 |
# File 'lib/atk/file_system.rb', line 320 def self.executable?(*args) File.executable?(*args) end |
.exists?(*args) ⇒ Boolean
304 305 306 |
# File 'lib/atk/file_system.rb', line 304 def self.exists?(*args) File.exist?(*args) end |
.expand_path(*args) ⇒ Object
380 381 382 |
# File 'lib/atk/file_system.rb', line 380 def self.(*args) File.(*args) end |
.extname(*args) ⇒ Object
292 293 294 |
# File 'lib/atk/file_system.rb', line 292 def self.extname(*args) File.extname(*args) end |
.file?(*args) ⇒ Boolean
310 311 312 |
# File 'lib/atk/file_system.rb', line 310 def self.file?(*args) File.file?(*args) end |
.folder?(*args) ⇒ Boolean
295 296 297 |
# File 'lib/atk/file_system.rb', line 295 def self.folder?(*args) File.directory?(*args) end |
.glob(path) ⇒ Object
232 233 234 |
# File 'lib/atk/file_system.rb', line 232 def self.glob(path) Dir.glob(path, File::FNM_DOTMATCH) - %w[. ..] end |
.home ⇒ Object
dir aliases
229 230 231 |
# File 'lib/atk/file_system.rb', line 229 def self.home HOME end |
.in_dir(path_to_somewhere) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/atk/file_system.rb', line 125 def self.in_dir(path_to_somewhere) # save the current working dir current_dir = Dir.pwd # switch dirs Dir.chdir(path_to_somewhere) # do the thing output = yield # switch back Dir.chdir(current_dir) return output end |
.join(*args) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/atk/info.rb', line 19 def self.join(*args) if OS.is?("windows") folders_without_leading_or_trailing_slashes = args.map do |each| # replace all forward slashes with backslashes backslashed_only = each.gsub(/\//,"\\") # remove leading/trailing backslashes backslashed_only.gsub(/(^\\|^\/|\\$|\/$)/,"") end # join all of them with backslashes folders_without_leading_or_trailing_slashes.join("\\") else File.join(*args) end end |
.list_files(path = ".") ⇒ Object
235 236 237 |
# File 'lib/atk/file_system.rb', line 235 def self.list_files(path=".") Dir.children(path).map{|each| path/each }.select {|each| FileSystem.file?(each)} end |
.list_folders(path = ".") ⇒ Object
238 239 240 |
# File 'lib/atk/file_system.rb', line 238 def self.list_folders(path=".") Dir.children(path).map{|each| path/each }.select {|each| FileSystem.directory?(each)} end |
.ls(path = ".") ⇒ Object
241 242 243 |
# File 'lib/atk/file_system.rb', line 241 def self.ls(path=".") Dir.children(path) end |
.makedirs(path) ⇒ Object
121 122 123 |
# File 'lib/atk/file_system.rb', line 121 def self.makedirs(path) FileUtils.makedirs(path) end |
.mkfifo(*args) ⇒ Object
383 384 385 |
# File 'lib/atk/file_system.rb', line 383 def self.mkfifo(*args) File.mkfifo(*args) end |
.move(from: nil, to: nil, new_name: "", force: true, noop: nil, verbose: nil, secure: nil) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/atk/file_system.rb', line 149 def self.move(from:nil, to:nil, new_name:"", force: true, noop: nil, verbose: nil, secure: nil) if new_name == "" raise "\n\nFileSystem.move() needs a new_name: argument\nset new_name:nil if you wish the file/folder to keep the same name\ne.g. FileSystem.move(from:'place/thing', to:'place', new_name:nil)" elsif new_name == nil new_name = File.basename(from) end # make sure the "to" path exists FileSystem.touch_dir(to) # perform the move FileUtils.move(from, to/new_name, force: force, noop: noop, verbose: verbose, secure: secure) end |
.online? ⇒ Boolean
395 396 397 398 399 400 401 402 |
# File 'lib/atk/file_system.rb', line 395 def self.online? require 'open-uri' begin true if open("http://www.google.com/") rescue false end end |
.owned?(*args) ⇒ Boolean
330 331 332 |
# File 'lib/atk/file_system.rb', line 330 def self.owned?(*args) File.owned?(*args) end |
.path_pieces(path) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/atk/file_system.rb', line 210 def self.path_pieces(path) # use this function like this: # *path, filename, extension = FS.path_pieces('/Users/jeffhykin/Desktop/place1/file1.pdf') pieces = Pathname(path).each_filename.to_a extname = File.extname(pieces[-1]) basebasename = pieces[-1][0...(pieces[-1].size - extname.size)] # add the root if the path is absolute if FileSystem.abs?(path) if not OS.is?("windows") pieces.unshift('/') else # FUTURE: eventually make this work for any drive, not just the current drive pieces.unshift('\\') end end return [ *pieces[0...-1], basebasename, extname ] end |
.pipe?(*args) ⇒ Boolean
335 336 337 |
# File 'lib/atk/file_system.rb', line 335 def self.pipe?(*args) File.pipe?(*args) end |
.pwd ⇒ Object
244 245 246 |
# File 'lib/atk/file_system.rb', line 244 def self.pwd Dir.pwd end |
.read(filepath) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/atk/file_system.rb', line 97 def self.read(filepath) begin return IO.read(filepath) rescue Errno::ENOENT => exception return nil end end |
.readable?(*args) ⇒ Boolean
340 341 342 |
# File 'lib/atk/file_system.rb', line 340 def self.readable?(*args) File.readable?(*args) end |
.relative_path?(path) ⇒ Boolean
203 204 205 |
# File 'lib/atk/file_system.rb', line 203 def self.relative_path?(path) Pathname.new(path).relative? end |
.rename(path, new_name: nil, force: true) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/atk/file_system.rb', line 161 def self.rename(path, new_name:nil, force: true) if File.dirname(new_name) != "." raise " \n \n When using FileSystem.rename(path, new_name)\n The new_name needs to be a filename, not a file path\n e.g. \"foo.txt\" not \"a_folder/foo.txt\"\n \n If you want to move the file, use FileSystem.move(from:nil, to:nil, new_name:\"\")\n HEREDOC\n end\n to = path/new_name\n # make sure the path is clear\n if force\n FileSystem.delete(to)\n end\n # perform the rename\n File.rename(path, to)\nend\n".remove_indent |
.save(value, to: nil, as: nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 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 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/atk/file_system.rb', line 50 def self.save(value, to:nil, as:nil) # assume string if as was not given if as == nil as = :s end # add a special exception for csv files case as when :csv require 'csv' FS.write(value.map(&:to_csv).join, to: to) else require 'json' require 'yaml' conversion_method_name = "to_#{as}" if value.respond_to? conversion_method_name # this is like calling `value.to_json`, `value.to_yaml`, or `value.to_csv` but programatically string_value = value.public_send(conversion_method_name) if not string_value.is_a?(String) raise " \n \n The FileSystem.save(value, to: \#{to.inspect}, as: \#{as.inspect}) had a problem.\n The as: \#{as}, gets converted into value.to_\#{as}\n Normally that returns a string that can be saved to a file\n However, the value.to_\#{as} did not return a string.\n Value is of the \#{value.class} class. Add a `to_\#{as}` \n method to that class that returns a string to get FileSystem.save() working\n HEREDOC\n end\n FS.write(string_value, to:to)\n else\n raise <<-HEREDOC.remove_indent\n \n \n The FileSystem.save(value, to: \#{to.inspect}, as: \#{as.inspect}) had a problem.\n \n The as: \#{as}, gets converted into value.to_\#{as}\n Normally that returns a string that can be saved to a file\n However, the value.to_\#{as} is not a method for value\n Value is of the \#{value.class} class. Add a `to_\#{as}` \n method to that class that returns a string to get FileSystem.save() working\n HEREDOC\n end\n end\nend\n".remove_indent |
.size?(*args) ⇒ Boolean
345 346 347 348 349 350 351 352 |
# File 'lib/atk/file_system.rb', line 345 def self.size?(*args) if File.directory?(args[0]) # recursively get the size of the folder return Dir.glob(File.join(args[0], '**', '*')).map{ |f| File.size(f) }.inject(:+) else File.size?(*args) end end |
.socket?(*args) ⇒ Boolean
355 356 357 |
# File 'lib/atk/file_system.rb', line 355 def self.socket?(*args) File.socket?(*args) end |
.stat(*args) ⇒ Object
386 387 388 |
# File 'lib/atk/file_system.rb', line 386 def self.stat(*args) File.stat(*args) end |
.symlink?(*args) ⇒ Boolean
325 326 327 |
# File 'lib/atk/file_system.rb', line 325 def self.symlink?(*args) File.symlink?(*args) end |
.time_access(*args) ⇒ Object
File aliases
258 259 260 |
# File 'lib/atk/file_system.rb', line 258 def self.time_access(*args) File.atime(*args) end |
.time_created(*args) ⇒ Object
261 262 263 |
# File 'lib/atk/file_system.rb', line 261 def self.time_created(*args) File.birthtime(*args) end |
.time_modified(*args) ⇒ Object
264 265 |
# File 'lib/atk/file_system.rb', line 264 def self.time_modified(*args) end |
.touch(*args) ⇒ Object
182 183 184 |
# File 'lib/atk/file_system.rb', line 182 def self.touch(*args) return FileUtils.touch(*args) end |
.touch_dir(path) ⇒ Object
188 189 190 191 192 |
# File 'lib/atk/file_system.rb', line 188 def self.touch_dir(path) if not FileSystem.directory?(path) FileUtils.makedirs(path) end end |
.username ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/atk/file_system.rb', line 113 def self.username if OS.is?(:windows) return File.basename(ENV["userprofile"]) else return Etc.getlogin end end |
.world_readable?(*args) ⇒ Boolean
360 361 362 |
# File 'lib/atk/file_system.rb', line 360 def self.world_readable?(*args) File.world_readable?(*args) end |
.world_writable?(*args) ⇒ Boolean
365 366 367 |
# File 'lib/atk/file_system.rb', line 365 def self.world_writable?(*args) File.world_writable?(*args) end |
.writable?(*args) ⇒ Boolean
370 371 372 |
# File 'lib/atk/file_system.rb', line 370 def self.writable?(*args) File.writable?(*args) end |
.writable_real?(*args) ⇒ Boolean
375 376 377 |
# File 'lib/atk/file_system.rb', line 375 def self.writable_real?(*args) File.writable_real?(*args) end |
.write(data, to: nil) ⇒ Object
change_owner set_permissions relative_path_between relative_path_to add a force: true option to most of the commands zip unzip
43 44 45 46 47 48 |
# File 'lib/atk/file_system.rb', line 43 def self.write(data, to:nil) # make sure the containing folder exists FileSystem.makedirs(File.dirname(to)) # actually download the file IO.write(to, data) end |