Module: FileHelper
- Included in:
- VpsCli::Copy
- Defined in:
- lib/vps_cli/helpers/file_helper.rb
Overview
Used for copying files, making directories, copying directories etc
Class Method Summary collapse
-
.backup_file_not_found?(file, verbose = false) ⇒ Boolean
Checks that a backup file does not exist.
-
.copy_dir(from, to, interactive = false) ⇒ Object
base method to copy a dir and ask for permission prior to copying.
-
.copy_dirs(opts = {}) ⇒ Object
Copies directories instead of files, called by copy_all.
-
.copy_file(from, to, interactive = false) ⇒ Object
base method to copy a file and ask for permission prior to copying.
-
.copy_files(opts = {}) ⇒ Object
Copies files, called by copy_all.
-
.create_backup?(opts = {}) ⇒ Boolean
Helper method for determining whether or not to create a backup file.
-
.file_found?(file, verbose = false) ⇒ Boolean
Default way of checking if the dotfile already exists.
-
.mkdirs(*dirs) ⇒ Object
Helper method for making multiple directories.
-
.overwrite?(file, interactive = false) ⇒ Boolean
asks permission to copy a file.
- .retrieve_file(directory, name) ⇒ Object
Class Method Details
.backup_file_not_found?(file, verbose = false) ⇒ Boolean
Checks that a backup file does not exist
63 64 65 66 67 68 |
# File 'lib/vps_cli/helpers/file_helper.rb', line 63 def self.backup_file_not_found?(file, verbose = false) return true unless File.exist?(file) puts "#{file} exists already. No backup created." if verbose false end |
.copy_dir(from, to, interactive = false) ⇒ Object
base method to copy a dir and ask for permission prior to copying
118 119 120 121 122 |
# File 'lib/vps_cli/helpers/file_helper.rb', line 118 def self.copy_dir(from, to, interactive = false) mkdirs(to) to_path = File.join(to, File.basename(from)) Rake.cp_r(from, to) if overwrite?(to_path, interactive) end |
.copy_dirs(opts = {}) ⇒ Object
Copies directories instead of files, called by copy_all
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/vps_cli/helpers/file_helper.rb', line 44 def self.copy_dirs(opts = {}) mkdirs(opts[:local_file]) if create_backup?(opts) copy_dir(opts[:local_file], opts[:backup_file], opts[:interactive]) end Dir.each_child(opts[:config_file]) do |dir| dir = File.join(opts[:config_file], dir) # copies to local dir copy_dir(dir, opts[:local_file], opts[:interactive]) end end |
.copy_file(from, to, interactive = false) ⇒ Object
base method to copy a file and ask for permission prior to copying
108 109 110 |
# File 'lib/vps_cli/helpers/file_helper.rb', line 108 def self.copy_file(from, to, interactive = false) Rake.cp(from, to) if overwrite?(to, interactive) end |
.copy_files(opts = {}) ⇒ Object
Copies files, called by copy_all
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/vps_cli/helpers/file_helper.rb', line 23 def self.copy_files(opts = {}) # if there is an original dot file & no backup file in the backupdir # Copy the dot file to the backup dir if create_backup?(opts) copy_file(opts[:local_file], opts[:backup_file], opts[:interactive]) end # Copies from vps_cli/dotfiles to the location of the dot_file copy_file(opts[:config_file], opts[:local_file], opts[:interactive]) end |
.create_backup?(opts = {}) ⇒ Boolean
Helper method for determining whether or not to create a backup file
77 78 79 80 81 82 83 84 |
# File 'lib/vps_cli/helpers/file_helper.rb', line 77 def self.create_backup?(opts = {}) return false unless file_found?(opts[:local_file], opts[:verbose]) unless backup_file_not_found?(opts[:backup_file], opts[:verbose]) return false end true end |
.file_found?(file, verbose = false) ⇒ Boolean
Default way of checking if the dotfile already exists
90 91 92 93 94 95 |
# File 'lib/vps_cli/helpers/file_helper.rb', line 90 def self.file_found?(file, verbose = false) return true if File.exist?(file) puts "#{file} does not exist. No backup created." if verbose false end |
.mkdirs(*dirs) ⇒ Object
Helper method for making multiple directories
9 10 11 |
# File 'lib/vps_cli/helpers/file_helper.rb', line 9 def self.mkdirs(*dirs) dirs.flatten.each { |dir| Rake.mkdir_p(dir) unless Dir.exist?(dir) } end |
.overwrite?(file, interactive = false) ⇒ Boolean
asks permission to copy a file
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/vps_cli/helpers/file_helper.rb', line 130 def self.overwrite?(file, interactive = false) return true if interactive == false string = "Attempting to overwrite file #{file}" if File.exist?(file) string ||= "Attempting to create file #{file}" # +string is equivalent to string.dup string = +string + ' Is this okay? (Y/N)' loop do puts string input = $stdin.gets.chomp.downcase.to_sym return true if input == :y return false if input == :n end end |
.retrieve_file(directory, name) ⇒ Object
97 98 99 |
# File 'lib/vps_cli/helpers/file_helper.rb', line 97 def self.retrieve_file(directory, name) Dir.children(directory).select { |file| name == file } end |