Class: RailsAiContext::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_context/watcher.rb

Overview

File system watcher that regenerates context files when key files change. Requires the ‘listen` gem (optional dependency).

Constant Summary collapse

DEBOUNCE_SECONDS =
2
WATCH_PATTERNS =
%w[
  app/models
  app/controllers
  app/jobs
  app/mailers
  app/javascript/controllers
  config
  db
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil) ⇒ Watcher

Returns a new instance of Watcher.



20
21
22
23
# File 'lib/rails_ai_context/watcher.rb', line 20

def initialize(app = nil)
  @app = app || Rails.application
  @last_fingerprint = Fingerprinter.compute(@app)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



18
19
20
# File 'lib/rails_ai_context/watcher.rb', line 18

def app
  @app
end

Instance Method Details

#startObject



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
# File 'lib/rails_ai_context/watcher.rb', line 25

def start
  require "listen"

  root = app.root.to_s
  dirs = WATCH_PATTERNS.map { |p| File.join(root, p) }.select { |d| Dir.exist?(d) }

  if dirs.empty?
    $stderr.puts "[rails-ai-context] No watchable directories found"
    return
  end

  # One-time v5.0.0 legacy UI-pattern files warning (warn_only: no prompt in watch mode)
  LegacyCleanup.prompt_legacy_files(
    RailsAiContext.configuration.ai_tools,
    root: root,
    warn_only: true
  )

  $stderr.puts "[rails-ai-context] Watching for changes..."
  $stderr.puts "[rails-ai-context] Directories: #{dirs.map { |d| d.sub("#{root}/", '') }.join(', ')}"

  listener = Listen.to(*dirs) do |modified, added, removed|
    next if (modified + added + removed).empty?
    handle_change
  end

  listener.start

  # Keep the process alive
  loop do
    sleep 1
  rescue Interrupt
    $stderr.puts "\n[rails-ai-context] Stopping watcher..."
    listener.stop
    break
  end
rescue LoadError
  $stderr.puts "Error: The `listen` gem is required for watch mode."
  $stderr.puts "Add to your Gemfile:  gem 'listen', group: :development"
  exit 1
end