Module: Ace::Search::Atoms::RipgrepExecutor
- Defined in:
- lib/ace/search/atoms/ripgrep_executor.rb
Overview
RipgrepExecutor provides a safe wrapper around ripgrep (rg) command This is an atom - pure function for executing ripgrep commands
Class Method Summary collapse
-
.available? ⇒ Boolean
Check if ripgrep is available.
-
.build_command(pattern, options = {}) ⇒ String
Build ripgrep command with proper escaping.
-
.execute(pattern, options = {}) ⇒ Hash
Execute ripgrep with given options and pattern.
-
.version ⇒ String?
Get ripgrep version.
Class Method Details
.available? ⇒ Boolean
Check if ripgrep is available
72 73 74 75 76 77 |
# File 'lib/ace/search/atoms/ripgrep_executor.rb', line 72 def available? stdout, _stderr, status = Open3.capture3("which rg") status.success? && !stdout.strip.empty? rescue false end |
.build_command(pattern, options = {}) ⇒ String
Build ripgrep command with proper escaping
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/ace/search/atoms/ripgrep_executor.rb', line 83 def build_command(pattern, = {}) args = ["rg"] # Basic options args << "--color=never" unless [:color] args << "--json" if [:json_output] args << "--line-number" unless [:no_line_numbers] args << "--no-heading" unless [:heading] args << "--with-filename" unless [:no_filename] args << "--count" if [:count] args << "--files-with-matches" if [:files_with_matches] args << "--invert-match" if [:invert_match] args << "--hidden" if [:hidden] # Context options if [:context] args << "--context=#{[:context]}" elsif [:before_context] || [:after_context] args << "--before-context=#{[:before_context]}" if [:before_context] args << "--after-context=#{[:after_context]}" if [:after_context] end # File type filtering if [:file_type] Array([:file_type]).each { |t| args << "--type=#{t}" } end # Glob patterns if [:glob] Array([:glob]).each { |g| args << "--glob=#{Shellwords.escape(g)}" } end # Case sensitivity args << "--ignore-case" if [:ignore_case] args << "--case-sensitive" if [:case_sensitive] # Max results args << "--max-count=#{[:max_count]}" if [:max_count] # Multiline mode args << "--multiline" if [:multiline] args << "--multiline-dotall" if [:multiline_dotall] # Whole word matching args << "--word-regexp" if [:word_regexp] # Search paths # When search_path is set, we use chdir in execute(), so search current dir paths = if [:search_path] ["."] # Will chdir to search_path, then search current dir elsif [:paths] [:paths] else ["."] end # Build the complete command command_parts = args + [Shellwords.escape(pattern)] + paths.map { |p| Shellwords.escape(p) } command_parts.join(" ") end |
.execute(pattern, options = {}) ⇒ Hash
Execute ripgrep with given options and pattern
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 61 62 63 64 65 66 67 68 |
# File 'lib/ace/search/atoms/ripgrep_executor.rb', line 20 def execute(pattern, = {}) return {success: false, error: "Pattern cannot be nil or empty"} if pattern.nil? || pattern.empty? command = build_command(pattern, ) # Read timeout from options (runtime override), then config, then fallback to 120 seconds default_timeout = Ace::Search.config["timeout"] || 120 timeout_seconds = .fetch(:timeout, default_timeout) # Debug output DebugLogger.section("RipgrepExecutor") do DebugLogger.log("options[:search_path] = #{[:search_path].inspect}") DebugLogger.log("Current Dir.pwd = #{Dir.pwd}") DebugLogger.log("Command: #{command}") search_dir_debug = [:search_path] || "." DebugLogger.log("Will chdir to: #{search_dir_debug}") DebugLogger.log("Absolute chdir: #{File.(search_dir_debug)}") end begin # Change to search directory if specified, otherwise use current dir # This ensures .gitignore, excludes, and includes are processed correctly search_dir = [:search_path] || "." stdout, stderr, status = Timeout.timeout(timeout_seconds) do Open3.capture3(command, chdir: search_dir) end { success: status.success?, stdout: stdout, stderr: stderr, exit_code: status.exitstatus, command: command } rescue Timeout::Error { success: false, error: "Command timed out after #{timeout_seconds} seconds", exit_code: -1 } rescue => e { success: false, error: e., exit_code: -1 } end end |
.version ⇒ String?
Get ripgrep version
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/ace/search/atoms/ripgrep_executor.rb', line 146 def version return nil unless available? stdout, _stderr, status = Open3.capture3("rg --version") if status.success? version_match = stdout.match(/ripgrep ([\d.]+)/) version_match ? version_match[1] : nil end rescue nil end |