Class: RailsMcpCodeSearch::Indexer

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_mcp_code_search/indexer.rb

Constant Summary collapse

INCLUDE_PATTERNS =
%w[**/*.rb **/*.erb **/*.js **/*.ts **/*.yml **/*.yaml **/*.md].freeze
EXCLUDE_PATTERNS =
%w[vendor/ node_modules/ tmp/ log/ .git/].freeze
BATCH_SIZE =
50
NotAGitRepo =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(embedding_adapter:, project_path: Dir.pwd, logger: nil) ⇒ Indexer

Returns a new instance of Indexer.



14
15
16
17
18
19
# File 'lib/rails_mcp_code_search/indexer.rb', line 14

def initialize(embedding_adapter:, project_path: Dir.pwd, logger: nil)
  @embedding_adapter = embedding_adapter
  @project_path = File.realpath(project_path)
  @logger = logger
  @errors = []
end

Instance Attribute Details

#errors ⇒ Object (readonly)

Returns the value of attribute errors.



12
13
14
# File 'lib/rails_mcp_code_search/indexer.rb', line 12

def errors
  @errors
end

Instance Method Details

#changed_files ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rails_mcp_code_search/indexer.rb', line 36

def changed_files
  @_changed_files_cache ||= {}
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  if @_changed_files_cache[:at] && (now - @_changed_files_cache[:at]) < 3
    return @_changed_files_cache[:files]
  end

  stdout, _, status = Open3.capture3("git", "diff", "--name-only", "HEAD", chdir: @project_path)
  files = status.success? ? stdout.lines.map(&:strip).select { valid_path?(_1) } : []

  @_changed_files_cache = { files:, at: now }
  files
end

#discover_files ⇒ Object



51
52
53
54
55
56
57
# File 'lib/rails_mcp_code_search/indexer.rb', line 51

def discover_files
  tracked = git_ls_files
  untracked = git_ls_files("--others", "--exclude-standard")
  all_files = (tracked + untracked).uniq

  all_files.select { include_file?(_1) && valid_path?(_1) }
end

#index_all ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/rails_mcp_code_search/indexer.rb', line 21

def index_all
  @errors = []
  files = discover_files
  return if files.empty?

  process_files(files)
  
end

#index_files(file_paths) ⇒ Object



30
31
32
33
34
# File 'lib/rails_mcp_code_search/indexer.rb', line 30

def index_files(file_paths)
  @errors = []
  safe_paths = file_paths.select { valid_path?(_1) }
  process_files(safe_paths)
end