Module: VV::FileMethods::ClassMethods

Defined in:
lib/vv/file_methods.rb

Instance Method Summary collapse

Instance Method Details

#cache_home(sub_directory = nil) ⇒ Object Also known as: xdg_cache_home



178
179
180
181
182
183
184
185
# 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

#cache_home!(sub_directory) ⇒ Object Also known as: xdg_cache_home!



188
189
190
191
192
# 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

#config_home(sub_directory = nil) ⇒ Object Also known as: xdg_config_home



156
157
158
159
160
161
162
163
# 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

#config_home!(sub_directory) ⇒ Object Also known as: xdg_config_home!



195
196
197
198
199
# 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

#copy_into(filepath, directory, allow_hidden: true, allow_absolute: true) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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

#data_home(sub_directory = nil) ⇒ Object Also known as: output_home, xdg_data_home

Where application specific files should be stored



167
168
169
170
171
172
173
174
# 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

#data_home!(sub_directory) ⇒ Object Also known as: xdg_data_home!



202
203
204
205
206
# 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

#fileObject



66
67
68
# File 'lib/vv/file_methods.rb', line 66

def file
  caller_locations[0].path
end

#file_directoryObject



62
63
64
# File 'lib/vv/file_methods.rb', line 62

def file_directory
  File.expand_path(File.dirname(caller_locations[0].path))
end

#make_directory(directory) ⇒ Object Also known as: create_directory, create_dir, make_dir



219
220
221
# File 'lib/vv/file_methods.rb', line 219

def make_directory directory
  FileUtils.mkdir(directory).first
end

#make_directory_if_not_exists(directory) ⇒ Object Also known as: make_dir_if_not_exists, create_directory_if_not_exists, create_dir_if_not_exists



209
210
211
# File 'lib/vv/file_methods.rb', line 209

def make_directory_if_not_exists directory
  FileUtils.mkdir_p(directory).first
end

#move_directory(*args, **kwargs) ⇒ Object

TODO: Think about making a ‘directory` method on

string so from / to is super clear.


121
122
123
124
125
126
127
128
129
# 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) ⇒ Object Also known as: move_dir_into, mv_dir_into



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 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

#pwdObject



70
71
72
# File 'lib/vv/file_methods.rb', line 70

def pwd
  File.expand_path(Dir.pwd)
end

#remove(filepath, force: false) ⇒ Object Also known as: remove_file, rm



235
236
237
238
239
240
241
# 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

#remove_directory(directory, quiet_if_gone: false) ⇒ Object Also known as: rm_directory, remove_dir, rm_dir



226
227
228
229
230
# 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

#rename_directory(from, to, allow_hidden: true, allow_absolute: true) ⇒ Object Also known as: rename_dir



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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

#repo_directory(missing_throws_exception: true) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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

#separatorObject



74
75
76
# File 'lib/vv/file_methods.rb', line 74

def separator
  File::SEPARATOR
end

#vv_included?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/vv/file_methods.rb', line 22

def vv_included?
  true
end

#vv_readlines(*args, **kwargs) ⇒ Object



26
27
28
# File 'lib/vv/file_methods.rb', line 26

def vv_readlines *args, **kwargs
  File.readlines(*args, **kwargs).map!(&:chomp)
end

#vv_split(string) ⇒ Object



30
31
32
33
34
# 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