Module: Roast::Tools::Grep

Extended by:
Grep
Included in:
Grep
Defined in:
lib/roast/tools/grep.rb

Constant Summary collapse

MAX_RESULT_LINES =
100

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Add this method to be included in other classes



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/roast/tools/grep.rb', line 12

def included(base)
  base.class_eval do
    function(
      :grep,
      'Search for a string in the project using `grep -rni "#{@search_string}" .` in the project root',
      string: { type: "string", description: "The string to search for" },
    ) do |params|
      Roast::Tools::Grep.call(params[:string]).tap do |result|
        Roast::Helpers::Logger.debug(result) if ENV["DEBUG"]
      end
    end
  end
end

Instance Method Details

#call(string) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/roast/tools/grep.rb', line 27

def call(string)
  Roast::Helpers::Logger.info("🔍 Grepping for string: #{string}\n")

  # Use Open3 to safely pass the string as an argument, avoiding shell injection
  cmd = ["rg", "-C", "4", "--trim", "--color=never", "--heading", "-F", "--", string, "."]
  stdout, _stderr, _status = Open3.capture3(*cmd)

  # Limit output to MAX_RESULT_LINES
  lines = stdout.lines
  if lines.size > MAX_RESULT_LINES
    lines.first(MAX_RESULT_LINES).join + "\n... (truncated to #{MAX_RESULT_LINES} lines)"
  else
    stdout
  end
rescue StandardError => e
  "Error grepping for string: #{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