lib/pdk/util/filesystem.rb in pdk-1.14.1 vs lib/pdk/util/filesystem.rb in pdk-1.15.0
- old
+ new
@@ -1,6 +1,7 @@
require 'pdk'
+autoload :FileUtils, 'fileutils'
module PDK
module Util
module Filesystem
def write_file(path, content)
@@ -15,12 +16,12 @@
File.open(path, 'wb') { |f| f.write(content) }
end
module_function :write_file
- def read_file(file, nil_on_error: false)
- File.read(file)
+ def read_file(file, nil_on_error: false, open_args: 'r')
+ File.read(file, open_args: Array(open_args))
rescue => e
raise e unless nil_on_error
nil
end
module_function :read_file
@@ -56,10 +57,15 @@
def fnmatch(*args)
File.fnmatch(*args)
end
module_function :fnmatch
+ def fnmatch?(*args)
+ File.fnmatch?(*args)
+ end
+ module_function :fnmatch?
+
def readable?(*args)
File.readable?(*args)
end
module_function :readable?
@@ -70,9 +76,63 @@
def rm(*args)
FileUtils.rm(*args)
end
module_function :rm
+
+ def rm_f(*args)
+ FileUtils.rm_f(*args)
+ end
+ module_function :rm_f
+
+ def rm_rf(*args)
+ FileUtils.rm_rf(*args)
+ end
+ module_function :rm_rf
+
+ def remove_entry_secure(*args)
+ FileUtils.remove_entry_secure(*args)
+ end
+ module_function :remove_entry_secure
+
+ def zero?(*args)
+ File.zero?(*args)
+ end
+ module_function :zero?
+
+ def stat(*args)
+ File.stat(*args)
+ end
+ module_function :stat
+
+ def symlink?(*args)
+ File.symlink?(*args)
+ end
+ module_function :symlink?
+
+ def cp(*args)
+ FileUtils.cp(*args)
+ end
+ module_function :cp
+
+ def mv(*args)
+ FileUtils.mv(*args)
+ rescue Errno::ENOENT
+ # PDK-1169 - FileUtils.mv raises Errno::ENOENT when moving files inside
+ # VMWare shared folders on Windows. So we need to catch this
+ # case, check if the file exists to see if the exception is
+ # legit and "move" the file with cp & rm.
+ src, dest, opts = args
+ raise unless File.exist?(src)
+
+ FileUtils.cp(src, dest, preserve: true)
+ if (opts ||= {})[:secure]
+ FileUtils.remove_entry_secure(src, opts[:force])
+ else
+ FileUtils.remove_entry(src, opts[:force])
+ end
+ end
+ module_function :mv
#:nocov:
end
end
end