module VV::FileMethods::ClassMethods

Public Instance Methods

cache_home(sub_directory=nil) click to toggle source
# File lib/vv/file_methods.rb, line 178
def cache_home sub_directory=nil
  path   = ENV['XDG_CACHE_HOME']
  path ||= File.join ENV['HOME'], ".cache"

  return path unless sub_directory

  File.join path, sub_directory
end
Also aliased as: xdg_cache_home
cache_home!(sub_directory) click to toggle source
# File lib/vv/file_methods.rb, line 188
def cache_home! sub_directory
  path = cache_home sub_directory
  File.make_dir_if_not_exists path
  path
end
Also aliased as: xdg_cache_home!
config_home(sub_directory=nil) click to toggle source
# File lib/vv/file_methods.rb, line 156
def config_home sub_directory=nil
  path   = ENV['XDG_CACHE_HOME']
  path ||= File.join ENV['HOME'], ".config"

  return path unless sub_directory

  File.join path, sub_directory
end
Also aliased as: xdg_config_home
config_home!(sub_directory) click to toggle source
# File lib/vv/file_methods.rb, line 195
def config_home! sub_directory
  path = config_home sub_directory
  File.make_dir_if_not_exists path
  path
end
Also aliased as: xdg_config_home!
copy_into(filepath, directory, allow_hidden: true, allow_absolute: true) click to toggle source
# File lib/vv/file_methods.rb, line 78
def copy_into filepath,
              directory,
              allow_hidden: true,
              allow_absolute: true

  message = "Filepath `#{filepath}` is unsafe."
  fail message unless filepath.safe_path?

  message = "Filepath `#{filepath}` is a directory."
  fail message if filepath.is_directory_path?

  message = "No such `#{directory}` directory."
  fail message unless directory.is_directory_path?

  FileUtils.cp filepath, directory
end
create_dir(directory)
Alias for: make_directory
create_dir_if_not_exists(directory)
create_directory(directory)
Alias for: make_directory
create_directory_if_not_exists(directory)
data_home(sub_directory=nil) click to toggle source

Where application specific files should be stored

# File lib/vv/file_methods.rb, line 167
def data_home sub_directory=nil
  path   = ENV['XDG_DATA_HOME']
  path ||= File.join ENV['HOME'], ".local", "share"

  return path unless sub_directory

  File.join path, sub_directory
end
Also aliased as: output_home, xdg_data_home
data_home!(sub_directory) click to toggle source
# File lib/vv/file_methods.rb, line 202
def data_home! sub_directory
  path = data_home sub_directory
  File.make_dir_if_not_exists path
  path
end
Also aliased as: xdg_data_home!
file() click to toggle source
# File lib/vv/file_methods.rb, line 66
def file
  caller_locations[0].path
end
file_directory() click to toggle source
# File lib/vv/file_methods.rb, line 62
def file_directory
  File.expand_path(File.dirname(caller_locations[0].path))
end
make_dir(directory)
Alias for: make_directory
make_dir_if_not_exists(directory)
make_directory(directory) click to toggle source
# File lib/vv/file_methods.rb, line 219
def make_directory directory
  FileUtils.mkdir(directory).first
end
Also aliased as: create_directory, create_dir, make_dir
make_directory_if_not_exists(directory) click to toggle source
# File lib/vv/file_methods.rb, line 209
def make_directory_if_not_exists directory
  FileUtils.mkdir_p(directory).first
end
move_dir_into(dir, into, allow_hidden: true, allow_absolute: true)
Alias for: move_directory_into
move_directory(*args, **kwargs) click to toggle source

TODO: Think about making a `directory` method on

string so from / to is super clear.
# File lib/vv/file_methods.rb, line 121
def move_directory *args, **kwargs
  message = \
  %w[ Moving directories is confusing. Call either
      `rename_directory` or `move_directory_into`
      depending on your needs. There are many aliases. ]
    .spaced

  fail NoMethodError, message
end
move_directory_into(dir, into, allow_hidden: true, allow_absolute: true) click to toggle source
# File lib/vv/file_methods.rb, line 131
def move_directory_into dir,
                        into,
                        allow_hidden: true,
                        allow_absolute: true

  safe   = dir.safe_dir_path?  allow_hidden: allow_hidden,
                               allow_absolute: allow_absolute

  safe &&= into.safe_dir_path? allow_hidden: allow_hidden,
                               allow_absolute: allow_absolute

  message = "Refusing to rename unsafe directory"
  fail message unless safe

  message = "Target #{into} is not a directory"
  fail message unless into.is_directory_path?

  message = "Source #{dir} is not a directory"
  fail message unless dir.is_directory_path?

  FileUtils.mv dir, into
end
Also aliased as: move_dir_into, mv_dir_into
mv_dir_into(dir, into, allow_hidden: true, allow_absolute: true)
Alias for: move_directory_into
output_home(sub_directory=nil)
Alias for: data_home
pwd() click to toggle source
# File lib/vv/file_methods.rb, line 70
def pwd
  File.expand_path(Dir.pwd)
end
remove(filepath, force: false) click to toggle source
# File lib/vv/file_methods.rb, line 235
def remove filepath, force: false
  message = \
  "Cowardly refusing to remove directory (see `remove_directory`)"
  fail message if filepath.is_directory_path?

  force ? FileUtils.rm_f(filepath) : FileUtils.rm(filepath)
end
Also aliased as: remove_file, rm
remove_dir(directory, quiet_if_gone: false)
Alias for: remove_directory
remove_directory(directory, quiet_if_gone: false) click to toggle source
# File lib/vv/file_methods.rb, line 226
def remove_directory directory, quiet_if_gone: false
  no_directory_exists = ! directory.is_directory_path?
  return if quiet_if_gone && no_directory_exists
  FileUtils.remove_dir directory
end
Also aliased as: rm_directory, remove_dir, rm_dir
remove_file(filepath, force: false)
Alias for: remove
rename_dir(from, to, allow_hidden: true, allow_absolute: true)
Alias for: rename_directory
rename_directory(from, to, allow_hidden: true, allow_absolute: true) click to toggle source
# File lib/vv/file_methods.rb, line 95
def rename_directory from,
                     to,
                     allow_hidden: true,
                     allow_absolute: true
  safe   = from.safe_dir_path? allow_hidden: allow_hidden,
                               allow_absolute: allow_absolute

  safe &&= to.safe_dir_path? allow_hidden: allow_hidden,
                             allow_absolute: allow_absolute

  message = "Refusing to rename unsafe directory"
  fail message unless safe

  message = "Source #{from} is not a directory"
  fail message unless from.is_directory_path?

  message = "Target directory name `#{to}` already exists"
  fail message if to.is_directory_path?

  return FileUtils.mv from, to

end
Also aliased as: rename_dir
repo_directory(missing_throws_exception: true) click to toggle source
# File lib/vv/file_methods.rb, line 36
def repo_directory missing_throws_exception: true
  _path = caller_locations(1)[0].path
  _file_directory = File.expand_path(File.dirname(_path))

  directory_fragments = File.vv_split _file_directory

  while directory_fragments.any?
    current_directory = File.join directory_fragments

    supported_source_control_directories = %w(.git .hg)

    supported_source_control_directories
      .each do | directory |
      full_path = File.join current_directory, directory
      path_exists = File.directory? full_path
      return current_directory if path_exists
    end

    directory_fragments.pop
  end

  return unless missing_throws_exception

  fail "No repository detected"
end
rm(filepath, force: false)
Alias for: remove
rm_dir(directory, quiet_if_gone: false)
Alias for: remove_directory
rm_directory(directory, quiet_if_gone: false)
Alias for: remove_directory
separator() click to toggle source
# File lib/vv/file_methods.rb, line 74
def separator
  File::SEPARATOR
end
vv_included?() click to toggle source
# File lib/vv/file_methods.rb, line 22
def vv_included?
  true
end
vv_readlines(*args, **kwargs) click to toggle source
# File lib/vv/file_methods.rb, line 26
def vv_readlines *args, **kwargs
  File.readlines(*args, **kwargs).map!(&:chomp)
end
vv_split(string) click to toggle source
# File lib/vv/file_methods.rb, line 30
def vv_split string
  response = string.split(File::SEPARATOR)
  response[0] = File::SEPARATOR if response.first.blank?
  response
end
xdg_cache_home(sub_directory=nil)
Alias for: cache_home
xdg_cache_home!(sub_directory)
Alias for: cache_home!
xdg_config_home(sub_directory=nil)
Alias for: config_home
xdg_config_home!(sub_directory)
Alias for: config_home!
xdg_data_home(sub_directory=nil)
Alias for: data_home
xdg_data_home!(sub_directory)
Alias for: data_home!