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)
Defined Under Namespace
Classes: ProfileHelper
Class Method Summary collapse
-
.absolute_path(*args) ⇒ Object
inherit from File.
-
.absolute_path?(path) ⇒ Boolean
Pathname aliases.
- .append(data, to: nil) ⇒ Object
- .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
- .merge(from, into: nil, force: true) ⇒ 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(path) ⇒ 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
342 343 344 |
# File 'lib/atk/file_system.rb', line 342 def self.absolute_path(*args) File.absolute_path(*args) end |
.absolute_path?(path) ⇒ Boolean
Pathname aliases
255 256 257 |
# File 'lib/atk/file_system.rb', line 255 def self.absolute_path?(path) Pathname.new(path).absolute? end |
.append(data, to: nil) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/atk/file_system.rb', line 50 def self.append(data, to:nil) FileSystem.makedirs(File.dirname(to)) return open(to, 'a') do |file| file << data end end |
.basename(*args) ⇒ Object
348 349 350 |
# File 'lib/atk/file_system.rb', line 348 def self.basename(*args) File.basename(*args) end |
.cd(*args, verbose: false) ⇒ Object
306 307 308 309 310 311 |
# File 'lib/atk/file_system.rb', line 306 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
312 313 314 |
# File 'lib/atk/file_system.rb', line 312 def self.chdir(*args) FS.cd(*args) end |
.copy(from: nil, to: nil, new_name: "", force: true, preserve: false, dereference_root: false) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/atk/file_system.rb', line 190 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
112 113 114 115 116 117 118 |
# File 'lib/atk/file_system.rb', line 112 def self.delete(path) if File.file?(path) File.delete(path) elsif File.directory?(path) FileUtils.rm_rf(path) end end |
.dirname(*args) ⇒ Object
345 346 347 |
# File 'lib/atk/file_system.rb', line 345 def self.dirname(*args) File.dirname(*args) end |
.download(the_url, to: nil) ⇒ Object
449 450 451 452 |
# File 'lib/atk/file_system.rb', line 449 def self.download(the_url, to:nil) require 'open-uri' FileSystem.write(open(URI.encode(the_url)).read, to: to) end |
.empty?(*args) ⇒ Boolean
374 375 376 |
# File 'lib/atk/file_system.rb', line 374 def self.empty?(*args) File.empty?(*args) end |
.executable?(*args) ⇒ Boolean
379 380 381 |
# File 'lib/atk/file_system.rb', line 379 def self.executable?(*args) File.executable?(*args) end |
.exists?(*args) ⇒ Boolean
363 364 365 |
# File 'lib/atk/file_system.rb', line 363 def self.exists?(*args) File.exist?(*args) end |
.expand_path(*args) ⇒ Object
439 440 441 |
# File 'lib/atk/file_system.rb', line 439 def self.(*args) File.(*args) end |
.extname(*args) ⇒ Object
351 352 353 |
# File 'lib/atk/file_system.rb', line 351 def self.extname(*args) File.extname(*args) end |
.file?(*args) ⇒ Boolean
369 370 371 |
# File 'lib/atk/file_system.rb', line 369 def self.file?(*args) File.file?(*args) end |
.folder?(*args) ⇒ Boolean
354 355 356 |
# File 'lib/atk/file_system.rb', line 354 def self.folder?(*args) File.directory?(*args) end |
.glob(path) ⇒ Object
291 292 293 |
# File 'lib/atk/file_system.rb', line 291 def self.glob(path) Dir.glob(path, File::FNM_DOTMATCH) - %w[. ..] end |
.home ⇒ Object
dir aliases
288 289 290 |
# File 'lib/atk/file_system.rb', line 288 def self.home HOME end |
.in_dir(path_to_somewhere) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/atk/file_system.rb', line 132 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
294 295 296 |
# File 'lib/atk/file_system.rb', line 294 def self.list_files(path=".") Dir.children(path).map{|each| path/each }.select {|each| FileSystem.file?(each)} end |
.list_folders(path = ".") ⇒ Object
297 298 299 |
# File 'lib/atk/file_system.rb', line 297 def self.list_folders(path=".") Dir.children(path).map{|each| path/each }.select {|each| FileSystem.directory?(each)} end |
.ls(path = ".") ⇒ Object
300 301 302 |
# File 'lib/atk/file_system.rb', line 300 def self.ls(path=".") Dir.children(path) end |
.makedirs(path) ⇒ Object
128 129 130 |
# File 'lib/atk/file_system.rb', line 128 def self.makedirs(path) FileUtils.makedirs(path) end |
.merge(from, into: nil, force: true) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/atk/file_system.rb', line 144 def self.merge(from, into: nil, force: true) to = into if !FS.exist?(from) raise <<~HEREDOC When calling FileSystem.merge(#{from.inspect}, into: #{into.inspect}) The path: #{from.inspect} Doesn't exist HEREDOC end # recursive case (folder) if FS.is_folder(from) # if theres a target file in the way if FS.exist?(to) && ( !FS.is_folder(to) ) if force # remove it FS.delete(to) else # continue with the process return end end # create a folder if needed if !FS.exist?(to) FS.touch_dir(to) end # become recursive for all contents for each in FS.ls(from) FS.merge(from/each, into: to/each, force: force) end # base case (file) else if FS.exist?(to) if force FS.delete(to) else # do nothing return end end FS.copy(from: from, to: FS.dirname(to), new_name: nil) end end |
.mkfifo(*args) ⇒ Object
442 443 444 |
# File 'lib/atk/file_system.rb', line 442 def self.mkfifo(*args) File.mkfifo(*args) end |
.move(from: nil, to: nil, new_name: "", force: true, noop: nil, verbose: nil, secure: nil) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/atk/file_system.rb', line 202 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
454 455 456 457 458 459 460 461 |
# File 'lib/atk/file_system.rb', line 454 def self.online? require 'open-uri' begin true if open("http://www.google.com/") rescue false end end |
.owned?(*args) ⇒ Boolean
389 390 391 |
# File 'lib/atk/file_system.rb', line 389 def self.owned?(*args) File.owned?(*args) end |
.path_pieces(path) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/atk/file_system.rb', line 269 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
394 395 396 |
# File 'lib/atk/file_system.rb', line 394 def self.pipe?(*args) File.pipe?(*args) end |
.pwd ⇒ Object
303 304 305 |
# File 'lib/atk/file_system.rb', line 303 def self.pwd FS.join(Dir.pwd, "") end |
.read(filepath) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/atk/file_system.rb', line 104 def self.read(filepath) begin return IO.read(filepath) rescue Errno::ENOENT => exception return nil end end |
.readable?(*args) ⇒ Boolean
399 400 401 |
# File 'lib/atk/file_system.rb', line 399 def self.readable?(*args) File.readable?(*args) end |
.relative_path?(path) ⇒ Boolean
262 263 264 |
# File 'lib/atk/file_system.rb', line 262 def self.relative_path?(path) Pathname.new(path).relative? end |
.rename(path, new_name: nil, force: true) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/atk/file_system.rb', line 214 def self.rename(path, new_name:nil, force: true) if File.dirname(new_name) != "." raise <<-HEREDOC.remove_indent When using FileSystem.rename(path, new_name) The new_name needs to be a filename, not a file path e.g. "foo.txt" not "a_folder/foo.txt" If you want to move the file, use FileSystem.move(from:nil, to:nil, new_name:"") HEREDOC end to = FileSystem.dirname(path)/new_name # if they are different if FS.absolute_path(to) != FS.absolute_path(path) # make sure the path is clear if force FileSystem.delete(to) end # perform the rename return File.rename(path, to) end end |
.save(value, to: nil, as: nil) ⇒ Object
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 96 97 98 99 100 101 102 |
# File 'lib/atk/file_system.rb', line 57 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 <<-HEREDOC.remove_indent The FileSystem.save(value, to: #{to.inspect}, as: #{as.inspect}) had a problem. The as: #{as}, gets converted into value.to_#{as} Normally that returns a string that can be saved to a file However, the value.to_#{as} did not return a string. Value is of the #{value.class} class. Add a `to_#{as}` method to that class that returns a string to get FileSystem.save() working HEREDOC end FS.write(string_value, to:to) else raise <<-HEREDOC.remove_indent The FileSystem.save(value, to: #{to.inspect}, as: #{as.inspect}) had a problem. The as: #{as}, gets converted into value.to_#{as} Normally that returns a string that can be saved to a file However, the value.to_#{as} is not a method for value Value is of the #{value.class} class. Add a `to_#{as}` method to that class that returns a string to get FileSystem.save() working HEREDOC end end end |
.size?(*args) ⇒ Boolean
404 405 406 407 408 409 410 411 |
# File 'lib/atk/file_system.rb', line 404 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
414 415 416 |
# File 'lib/atk/file_system.rb', line 414 def self.socket?(*args) File.socket?(*args) end |
.stat(*args) ⇒ Object
445 446 447 |
# File 'lib/atk/file_system.rb', line 445 def self.stat(*args) File.stat(*args) end |
.symlink?(*args) ⇒ Boolean
384 385 386 |
# File 'lib/atk/file_system.rb', line 384 def self.symlink?(*args) File.symlink?(*args) end |
.time_access(*args) ⇒ Object
File aliases
317 318 319 |
# File 'lib/atk/file_system.rb', line 317 def self.time_access(*args) File.atime(*args) end |
.time_created(*args) ⇒ Object
320 321 322 |
# File 'lib/atk/file_system.rb', line 320 def self.time_created(*args) File.birthtime(*args) end |
.time_modified(*args) ⇒ Object
323 324 |
# File 'lib/atk/file_system.rb', line 323 def self.time_modified(*args) end |
.touch(path) ⇒ Object
238 239 240 241 242 243 |
# File 'lib/atk/file_system.rb', line 238 def self.touch(path) FileSystem.makedirs(File.dirname(path)) if not FileSystem.file?(path) return IO.write(path, "") end end |
.touch_dir(path) ⇒ Object
247 248 249 250 251 |
# File 'lib/atk/file_system.rb', line 247 def self.touch_dir(path) if not FileSystem.directory?(path) FileUtils.makedirs(path) end end |
.username ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/atk/file_system.rb', line 120 def self.username if OS.is?(:windows) return File.basename(ENV["userprofile"]) else return Etc.getlogin end end |
.world_readable?(*args) ⇒ Boolean
419 420 421 |
# File 'lib/atk/file_system.rb', line 419 def self.world_readable?(*args) File.world_readable?(*args) end |
.world_writable?(*args) ⇒ Boolean
424 425 426 |
# File 'lib/atk/file_system.rb', line 424 def self.world_writable?(*args) File.world_writable?(*args) end |
.writable?(*args) ⇒ Boolean
429 430 431 |
# File 'lib/atk/file_system.rb', line 429 def self.writable?(*args) File.writable?(*args) end |
.writable_real?(*args) ⇒ Boolean
434 435 436 |
# File 'lib/atk/file_system.rb', line 434 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 |