Module: Roast::Tools::ReadFile

Extended by:
ReadFile
Included in:
ReadFile
Defined in:
lib/roast/tools/read_file.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/roast/tools/read_file.rb', line 11

def included(base)
  base.class_eval do
    function(
      :read_file,
      "Read the contents of a file. (If the path is a directory, list the contents.) " \
        "NOTE: Do not use for .rbi files, they are not useful.",
      path: { type: "string", description: "The path to the file to read" },
    ) do |params|
      Roast::Tools::ReadFile.call(params[:path]).tap do |result|
        if ENV["DEBUG"]
          result_lines = result.lines
          if result_lines.size > 20
            Roast::Helpers::Logger.debug(result_lines.first(20).join + "\n...")
          else
            Roast::Helpers::Logger.debug(result)
          end
        end
      end
    end
  end
end

Instance Method Details

#call(path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/roast/tools/read_file.rb', line 34

def call(path)
  Roast::Helpers::Logger.info("📖 Reading file: #{path}\n")
  if File.directory?(path)
    %x(ls -la #{path})
  else
    File.read(File.join(Dir.pwd, path))
  end
rescue StandardError => e
  "Error reading file: #{e.message}".tap do |error_message|
    Roast::Helpers::Logger.error(error_message + "\n")
    Roast::Helpers::Logger.debug(e.backtrace.join("\n") + "\n") if ENV["DEBUG"]
  end
end