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
- .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
293 294 295 |
# File 'lib/atk/file_system.rb', line 293 def self.absolute_path(*args) File.absolute_path(*args) end |
.absolute_path?(path) ⇒ Boolean
Pathname aliases
206 207 208 |
# File 'lib/atk/file_system.rb', line 206 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
299 300 301 |
# File 'lib/atk/file_system.rb', line 299 def self.basename(*args) File.basename(*args) end |
.cd(*args, verbose: false) ⇒ Object
257 258 259 260 261 262 |
# File 'lib/atk/file_system.rb', line 257 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
263 264 265 |
# File 'lib/atk/file_system.rb', line 263 def self.chdir(*args) FS.cd(*args) end |
.copy(from: nil, to: nil, new_name: "", force: true, preserve: false, dereference_root: false) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/atk/file_system.rb', line 144 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
296 297 298 |
# File 'lib/atk/file_system.rb', line 296 def self.dirname(*args) File.dirname(*args) end |
.download(the_url, to: nil) ⇒ Object
400 401 402 403 |
# File 'lib/atk/file_system.rb', line 400 def self.download(the_url, to:nil) require 'open-uri' FileSystem.write(open(URI.encode(the_url)).read, to: to) end |
.empty?(*args) ⇒ Boolean
325 326 327 |
# File 'lib/atk/file_system.rb', line 325 def self.empty?(*args) File.empty?(*args) end |
.executable?(*args) ⇒ Boolean
330 331 332 |
# File 'lib/atk/file_system.rb', line 330 def self.executable?(*args) File.executable?(*args) end |
.exists?(*args) ⇒ Boolean
314 315 316 |
# File 'lib/atk/file_system.rb', line 314 def self.exists?(*args) File.exist?(*args) end |
.expand_path(*args) ⇒ Object
390 391 392 |
# File 'lib/atk/file_system.rb', line 390 def self.(*args) File.(*args) end |
.extname(*args) ⇒ Object
302 303 304 |
# File 'lib/atk/file_system.rb', line 302 def self.extname(*args) File.extname(*args) end |
.file?(*args) ⇒ Boolean
320 321 322 |
# File 'lib/atk/file_system.rb', line 320 def self.file?(*args) File.file?(*args) end |
.folder?(*args) ⇒ Boolean
305 306 307 |
# File 'lib/atk/file_system.rb', line 305 def self.folder?(*args) File.directory?(*args) end |
.glob(path) ⇒ Object
242 243 244 |
# File 'lib/atk/file_system.rb', line 242 def self.glob(path) Dir.glob(path, File::FNM_DOTMATCH) - %w[. ..] end |
.home ⇒ Object
dir aliases
239 240 241 |
# File 'lib/atk/file_system.rb', line 239 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
245 246 247 |
# File 'lib/atk/file_system.rb', line 245 def self.list_files(path=".") Dir.children(path).map{|each| path/each }.select {|each| FileSystem.file?(each)} end |
.list_folders(path = ".") ⇒ Object
248 249 250 |
# File 'lib/atk/file_system.rb', line 248 def self.list_folders(path=".") Dir.children(path).map{|each| path/each }.select {|each| FileSystem.directory?(each)} end |
.ls(path = ".") ⇒ Object
251 252 253 |
# File 'lib/atk/file_system.rb', line 251 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 |
.mkfifo(*args) ⇒ Object
393 394 395 |
# File 'lib/atk/file_system.rb', line 393 def self.mkfifo(*args) File.mkfifo(*args) end |
.move(from: nil, to: nil, new_name: "", force: true, noop: nil, verbose: nil, secure: nil) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/atk/file_system.rb', line 156 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
405 406 407 408 409 410 411 412 |
# File 'lib/atk/file_system.rb', line 405 def self.online? require 'open-uri' begin true if open("http://www.google.com/") rescue false end end |
.owned?(*args) ⇒ Boolean
340 341 342 |
# File 'lib/atk/file_system.rb', line 340 def self.owned?(*args) File.owned?(*args) end |
.path_pieces(path) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/atk/file_system.rb', line 220 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
345 346 347 |
# File 'lib/atk/file_system.rb', line 345 def self.pipe?(*args) File.pipe?(*args) end |
.pwd ⇒ Object
254 255 256 |
# File 'lib/atk/file_system.rb', line 254 def self.pwd 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
350 351 352 |
# File 'lib/atk/file_system.rb', line 350 def self.readable?(*args) File.readable?(*args) end |
.relative_path?(path) ⇒ Boolean
213 214 215 |
# File 'lib/atk/file_system.rb', line 213 def self.relative_path?(path) Pathname.new(path).relative? end |
.rename(path, new_name: nil, force: true) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/atk/file_system.rb', line 168 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 = FileSystem.dirname(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
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 " \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
355 356 357 358 359 360 361 362 |
# File 'lib/atk/file_system.rb', line 355 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
365 366 367 |
# File 'lib/atk/file_system.rb', line 365 def self.socket?(*args) File.socket?(*args) end |
.stat(*args) ⇒ Object
396 397 398 |
# File 'lib/atk/file_system.rb', line 396 def self.stat(*args) File.stat(*args) end |
.symlink?(*args) ⇒ Boolean
335 336 337 |
# File 'lib/atk/file_system.rb', line 335 def self.symlink?(*args) File.symlink?(*args) end |
.time_access(*args) ⇒ Object
File aliases
268 269 270 |
# File 'lib/atk/file_system.rb', line 268 def self.time_access(*args) File.atime(*args) end |
.time_created(*args) ⇒ Object
271 272 273 |
# File 'lib/atk/file_system.rb', line 271 def self.time_created(*args) File.birthtime(*args) end |
.time_modified(*args) ⇒ Object
274 275 |
# File 'lib/atk/file_system.rb', line 274 def self.time_modified(*args) end |
.touch(path) ⇒ Object
189 190 191 192 193 194 |
# File 'lib/atk/file_system.rb', line 189 def self.touch(path) FileSystem.makedirs(File.dirname(path)) if not FileSystem.file?(path) return IO.write(path, "") end end |
.touch_dir(path) ⇒ Object
198 199 200 201 202 |
# File 'lib/atk/file_system.rb', line 198 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
370 371 372 |
# File 'lib/atk/file_system.rb', line 370 def self.world_readable?(*args) File.world_readable?(*args) end |
.world_writable?(*args) ⇒ Boolean
375 376 377 |
# File 'lib/atk/file_system.rb', line 375 def self.world_writable?(*args) File.world_writable?(*args) end |
.writable?(*args) ⇒ Boolean
380 381 382 |
# File 'lib/atk/file_system.rb', line 380 def self.writable?(*args) File.writable?(*args) end |
.writable_real?(*args) ⇒ Boolean
385 386 387 |
# File 'lib/atk/file_system.rb', line 385 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 |