Module: EcoRake::Shell::Files

Includes:
Rake::DSL
Included in:
Gpg
Defined in:
lib/eco-rake/shell/files.rb

Constant Summary collapse

DAY_SECONDS =
(60 * 60 * 24)

Instance Method Summary collapse

Instance Method Details

#csv_files(folder = ".", regexp: nil, older_than: nil) ⇒ Object



53
54
55
# File 'lib/eco-rake/shell/files.rb', line 53

def csv_files(folder = ".", regexp: nil, older_than: nil)
  folder_files(folder, "*.csv", regexp: regexp, older_than: older_than)
end

#delete_file(*files, message: 'Deleting files:') ⇒ Object

TODO: check delete status



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/eco-rake/shell/files.rb', line 19

def delete_file(*files, message: 'Deleting files:')
  files = files.select {|file| File.exist?(file)}
  return if files.empty?

  puts message if message

  files.each do |file|
    File.delete(file)
    puts "#{file}" if message
  end
end

#folder_files(folder = ".", pattern = "*", regexp: nil, older_than: nil) ⇒ Array<String>

It identifies files in a folder by using different criteria.

Returns:

  • (Array<String>)

    files that match the criteria sorted by name ascendant (i.e. abc)



43
44
45
46
47
48
49
50
# File 'lib/eco-rake/shell/files.rb', line 43

def folder_files(folder = ".", pattern = "*", regexp: nil, older_than: nil)
  target = File.join(File.expand_path(folder), pattern)
  Dir[target].tap do |dir_files|
    dir_files.select! {|f| File.file?(f)}
    dir_files.select! {|f| file_older_than?(f, days: older_than)} if older_than
    dir_files.select! {|f| File.basename(f).match(regexp)}        if regexp.is_a?(Regexp)
  end.sort
end

#gpg_files(folder = ".", regexp: nil, older_than: nil) ⇒ Object



58
59
60
61
62
# File 'lib/eco-rake/shell/files.rb', line 58

def gpg_files(folder = ".", regexp: nil, older_than: nil)
  gpg = folder_files(folder, "*.gpg", regexp: regexp, older_than: older_than)
  pgp = folder_files(folder, "*.pgp", regexp: regexp, older_than: older_than)
  gpg.concat(pgp)
end

#gpg_to_csv_filename(gpg_file) ⇒ String

Preserves the folder and the base name of gpg_file name and changes its extension to csv.

Returns:

  • (String)


67
68
69
70
71
72
73
74
# File 'lib/eco-rake/shell/files.rb', line 67

def gpg_to_csv_filename(gpg_file)
  return nil unless gpg_file

  ext    = gpg_file.split('.')[1..-1].join('.')
  base   = File.basename(gpg_file, ".#{ext}")
  folder = File.dirname(gpg_file)
  File.join(folder, "#{base}.csv")
end

#move_file(*files, folder:, message: 'Moving files:') ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/eco-rake/shell/files.rb', line 31

def move_file(*files, folder:, message: 'Moving files:')
  puts message if message

  files.each do |file|
    new_name = File.join(folder, File.basename(file))
    File.name(file, new_name)
    puts "#{File.basename(file)}" if message
  end
end

#upsert_local_dir(path) ⇒ Object

Note:

if any parent directory does not exist, it creates it as well.

It creates directory path constructively.



9
10
11
12
13
14
15
16
# File 'lib/eco-rake/shell/files.rb', line 9

def upsert_local_dir(path)
  return if path.to_s.strip.empty?
  return if File.directory?(path)

  require 'fileutils'
  puts "Creating directory '#{path}'"
  FileUtils.mkdir_p(path)
end