Class: ClaudeMemory::Commands::Initializers::HooksConfigurator
- Inherits:
-
Object
- Object
- ClaudeMemory::Commands::Initializers::HooksConfigurator
- Defined in:
- lib/claude_memory/commands/initializers/hooks_configurator.rb
Overview
Configures Claude Code hooks for ClaudeMemory
Instance Method Summary collapse
- #configure_global_hooks(replace: false) ⇒ Object
- #configure_project_hooks(replace: false) ⇒ Object
- #has_claude_memory_hooks?(settings_path) ⇒ Boolean
-
#initialize(stdout) ⇒ HooksConfigurator
constructor
A new instance of HooksConfigurator.
- #remove_hooks_from_file(settings_path) ⇒ Object
Constructor Details
#initialize(stdout) ⇒ HooksConfigurator
Returns a new instance of HooksConfigurator.
11 12 13 |
# File 'lib/claude_memory/commands/initializers/hooks_configurator.rb', line 11 def initialize(stdout) @stdout = stdout end |
Instance Method Details
#configure_global_hooks(replace: false) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/claude_memory/commands/initializers/hooks_configurator.rb', line 33 def configure_global_hooks(replace: false) settings_path = File.join(Dir.home, ".claude", "settings.json") FileUtils.mkdir_p(File.dirname(settings_path)) db_path = ClaudeMemory.global_db_path ingest_cmd = "claude-memory hook ingest --db #{db_path}" sweep_cmd = "claude-memory hook sweep --db #{db_path}" hooks_config = build_hooks_config(ingest_cmd, sweep_cmd) existing = load_json_file(settings_path) existing["hooks"] ||= {} merge_hooks!(existing["hooks"], hooks_config["hooks"], replace: replace) File.write(settings_path, JSON.pretty_generate(existing)) @stdout.puts "✓ Configured hooks in #{settings_path}" end |
#configure_project_hooks(replace: false) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/claude_memory/commands/initializers/hooks_configurator.rb', line 15 def configure_project_hooks(replace: false) settings_path = ".claude/settings.json" FileUtils.mkdir_p(File.dirname(settings_path)) db_path = ClaudeMemory.project_db_path ingest_cmd = "claude-memory hook ingest --db #{db_path}" sweep_cmd = "claude-memory hook sweep --db #{db_path}" hooks_config = build_hooks_config(ingest_cmd, sweep_cmd) existing = load_json_file(settings_path) existing["hooks"] ||= {} merge_hooks!(existing["hooks"], hooks_config["hooks"], replace: replace) File.write(settings_path, JSON.pretty_generate(existing)) @stdout.puts "✓ Configured hooks in #{settings_path}" end |
#has_claude_memory_hooks?(settings_path) ⇒ Boolean
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/claude_memory/commands/initializers/hooks_configurator.rb', line 51 def has_claude_memory_hooks?(settings_path) return false unless File.exist?(settings_path) begin config = JSON.parse(File.read(settings_path)) return false unless config["hooks"] config["hooks"].values.flatten.any? do |hook_array| next false unless hook_array.is_a?(Hash) && hook_array["hooks"].is_a?(Array) hook_array["hooks"].any? { |h| h["command"]&.include?("claude-memory") } end rescue JSON::ParserError false end end |
#remove_hooks_from_file(settings_path) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/claude_memory/commands/initializers/hooks_configurator.rb', line 67 def remove_hooks_from_file(settings_path) return unless File.exist?(settings_path) begin config = JSON.parse(File.read(settings_path)) rescue JSON::ParserError return end return unless config["hooks"] config["hooks"].each do |event, hook_arrays| next unless hook_arrays.is_a?(Array) # Filter out hook arrays that contain claude-memory commands hook_arrays.reject! do |hook_array| next false unless hook_array["hooks"].is_a?(Array) hook_array["hooks"].any? { |h| h["command"]&.include?("claude-memory") } end # Remove empty event keys config["hooks"].delete(event) if hook_arrays.empty? end # Remove hooks key entirely if empty config.delete("hooks") if config["hooks"].empty? File.write(settings_path, JSON.pretty_generate(config)) end |