Module: RJSV::Core::Files

Defined in:
lib/rjsv/core/files.rb

Overview

The file module ensures safe handling of files. It always checks if a file really exists on the input path or if a certain folder exists on the output path. It can also change the absolute path or find all necessary files for further manipulation.

Class Method Summary collapse

Class Method Details

.change_path_to_output(path, options_cli) ⇒ Object

The method is special in that it examines arguments from the CLI and modifies the definition path for the output path.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rjsv/core/files.rb', line 63

def change_path_to_output(path, options_cli)
  find_output_path = lambda do
    index_s_path = options_cli[:source].split(RJSV::Constants::PATH_SPLIT)
                  .index {|e| path.index(e) != nil }
    s_path  = options_cli[:source].split(RJSV::Constants::PATH_SPLIT)[index_s_path]
    mo_path = options_cli[:output].split(RJSV::Constants::PATH_SPLIT)[index_s_path]

    unless mo_path
      mo_path = options_cli[:output].split(RJSV::Constants::PATH_SPLIT)[0]
      Core::Event.print('warning', "Output path not found for '#{s_path}'.")
    end

    return path.sub(s_path, mo_path)
  end

  find_output_path.call()
    .sub(/\.#{RJSV::Constants::SUFFIX_RB}$/, '')
    .sub(File.join(Dir.pwd(), ''), '')
end

.copy(path_input, path_output) ⇒ Object

Copies all files from the input path to the output path. This is a method that copies all files even those that are invisible to the UNIX system (dot file).



102
103
104
105
106
107
108
109
110
# File 'lib/rjsv/core/files.rb', line 102

def copy(path_input, path_output)
  files = Dir.glob("#{path_input}/**/*", File::FNM_DOTMATCH)
          .select { |f| File.file?(f) }
  files.each do |path_file|
    path_cp = path_file.sub(path_input, path_output)
    FileUtils.mkdir_p File.dirname(path_cp)
    FileUtils.cp(path_file, path_cp)
  end
end

.find_all(paths) ⇒ Object

Finds all files with the extension ‘.*.rb’ from the defined paths.



87
88
89
90
91
92
93
94
# File 'lib/rjsv/core/files.rb', line 87

def find_all(paths)
  path_all = []
  paths.split(RJSV::Constants::PATH_SPLIT).each do |path|
    path_all << File.join(path, '**', '*') + ".*.#{RJSV::Constants::SUFFIX_RB}"
  end

  Dir.glob(path_all)
end

.open(path) ⇒ Object

Opens the file securely and returns the content from the file. If the file does not exist, it returns nil.



21
22
23
24
25
26
27
28
29
# File 'lib/rjsv/core/files.rb', line 21

def open(path)
  if File.exist? path
    File.open path do |f|
      return f.read
    end
  end

  return nil
end

.remove(path) ⇒ Object

Safely removes the file from the path. If the file is the last one in the folder, the folder is also deleted with the file.



52
53
54
55
56
# File 'lib/rjsv/core/files.rb', line 52

def remove(path)
  File.delete(path) if File.exist?(path)
  path_dir = File.dirname(path)
  Dir.delete(path_dir) if Dir.empty?(path_dir)
end

.write_with_dir(content, path, mode = 'w+') ⇒ Object

Stores the file with the assigned container by safely discovering its folder and, if necessary, creating it when it does not exist in the path. It can also be assigned a file write mode.



37
38
39
40
41
42
43
44
45
# File 'lib/rjsv/core/files.rb', line 37

def write_with_dir(content, path, mode = 'w+')
  unless Dir.exist? File.dirname(path)
    FileUtils.mkdir_p File.dirname(path)
  end

  File.open path, mode do |f|
    f.write(content)
  end
end