|
| 1 | +"""Install/uninstall commands — register claude-diary hook in Claude Code settings.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +HOOK_COMMAND = "PYTHONIOENCODING=utf-8 python -m claude_diary.hook" |
| 9 | + |
| 10 | +HOOK_ENTRY = { |
| 11 | + "type": "command", |
| 12 | + "command": HOOK_COMMAND, |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +def _get_claude_settings_path(): |
| 17 | + """Return path to ~/.claude/settings.json.""" |
| 18 | + home = os.path.expanduser("~") |
| 19 | + return os.path.join(home, ".claude", "settings.json") |
| 20 | + |
| 21 | + |
| 22 | +def _load_claude_settings(path): |
| 23 | + """Load existing settings or return empty dict.""" |
| 24 | + if os.path.exists(path): |
| 25 | + try: |
| 26 | + with open(path, "r", encoding="utf-8") as f: |
| 27 | + return json.load(f) |
| 28 | + except (json.JSONDecodeError, IOError): |
| 29 | + pass |
| 30 | + return {} |
| 31 | + |
| 32 | + |
| 33 | +def _save_claude_settings(path, settings): |
| 34 | + """Save settings to file, creating directory if needed.""" |
| 35 | + os.makedirs(os.path.dirname(path), exist_ok=True) |
| 36 | + with open(path, "w", encoding="utf-8") as f: |
| 37 | + json.dump(settings, f, indent=2, ensure_ascii=False) |
| 38 | + f.write("\n") |
| 39 | + |
| 40 | + |
| 41 | +def _is_diary_hook(hook): |
| 42 | + """Check if a hook entry is a claude-diary hook.""" |
| 43 | + command = hook.get("command", "") |
| 44 | + return "claude_diary.hook" in command |
| 45 | + |
| 46 | + |
| 47 | +def _find_existing_hook(settings): |
| 48 | + """Check if claude-diary hook is already registered. |
| 49 | + Returns True if found. |
| 50 | + """ |
| 51 | + hooks = settings.get("hooks", {}) |
| 52 | + stop_hooks = hooks.get("Stop", []) |
| 53 | + for group in stop_hooks: |
| 54 | + for hook in group.get("hooks", []): |
| 55 | + if _is_diary_hook(hook): |
| 56 | + return True |
| 57 | + return False |
| 58 | + |
| 59 | + |
| 60 | +def cmd_install(args): |
| 61 | + """Register claude-diary Stop hook in ~/.claude/settings.json.""" |
| 62 | + settings_path = _get_claude_settings_path() |
| 63 | + settings = _load_claude_settings(settings_path) |
| 64 | + |
| 65 | + if _find_existing_hook(settings): |
| 66 | + print("claude-diary hook is already installed.") |
| 67 | + print(" Settings: %s" % settings_path) |
| 68 | + return |
| 69 | + |
| 70 | + # Ensure hooks.Stop structure exists |
| 71 | + if "hooks" not in settings: |
| 72 | + settings["hooks"] = {} |
| 73 | + if "Stop" not in settings["hooks"]: |
| 74 | + settings["hooks"]["Stop"] = [] |
| 75 | + |
| 76 | + # Add hook |
| 77 | + settings["hooks"]["Stop"].append({ |
| 78 | + "hooks": [HOOK_ENTRY], |
| 79 | + }) |
| 80 | + |
| 81 | + _save_claude_settings(settings_path, settings) |
| 82 | + |
| 83 | + print("claude-diary hook installed successfully!") |
| 84 | + print() |
| 85 | + print(" Hook: %s" % HOOK_COMMAND) |
| 86 | + print(" Settings: %s" % settings_path) |
| 87 | + print() |
| 88 | + print("Every Claude Code session will now auto-generate a diary entry on exit.") |
| 89 | + |
| 90 | + |
| 91 | +def cmd_uninstall(args): |
| 92 | + """Remove claude-diary Stop hook from ~/.claude/settings.json.""" |
| 93 | + settings_path = _get_claude_settings_path() |
| 94 | + settings = _load_claude_settings(settings_path) |
| 95 | + |
| 96 | + if not _find_existing_hook(settings): |
| 97 | + print("claude-diary hook is not installed.") |
| 98 | + return |
| 99 | + |
| 100 | + # Remove diary hooks |
| 101 | + stop_hooks = settings.get("hooks", {}).get("Stop", []) |
| 102 | + new_stop = [] |
| 103 | + for group in stop_hooks: |
| 104 | + remaining = [h for h in group.get("hooks", []) if not _is_diary_hook(h)] |
| 105 | + if remaining: |
| 106 | + new_stop.append({"hooks": remaining}) |
| 107 | + |
| 108 | + settings["hooks"]["Stop"] = new_stop |
| 109 | + |
| 110 | + # Clean up empty structures |
| 111 | + if not settings["hooks"]["Stop"]: |
| 112 | + del settings["hooks"]["Stop"] |
| 113 | + if not settings["hooks"]: |
| 114 | + del settings["hooks"] |
| 115 | + |
| 116 | + _save_claude_settings(settings_path, settings) |
| 117 | + |
| 118 | + print("claude-diary hook uninstalled.") |
| 119 | + print(" Settings: %s" % settings_path) |
0 commit comments