-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
242 lines (205 loc) · 6.97 KB
/
install.sh
File metadata and controls
242 lines (205 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env bash
# install.sh — Claude Code Notify installer for macOS / Linux
# Usage: bash install.sh [--uninstall]
set -euo pipefail
ICON_DIR="$HOME/.claude"
ICON_PATH="$ICON_DIR/notify-icon.png"
SETTINGS_FILE="$ICON_DIR/settings.json"
HOOKS_DIR="$ICON_DIR/hooks"
NOTIFY_SH="$HOOKS_DIR/notify.sh"
UNINSTALL=false
[ "${1:-}" = "--uninstall" ] && UNINSTALL=true
# ============================================================
# Helpers
# ============================================================
step() { printf " \033[36m%s\033[0m\n" "$1"; }
ok() { printf " \033[32m✓ %s\033[0m\n" "$1"; }
warn() { printf " \033[33m⚠ %s\033[0m\n" "$1"; }
fail() { printf " \033[31m✗ %s\033[0m\n" "$1"; }
# ============================================================
# Platform Detection
# ============================================================
detect_platform() {
case "$(uname -s)" in
Darwin) echo "macos" ;;
Linux) echo "linux" ;;
*) echo "unknown" ;;
esac
}
check_notification_tool() {
local platform="$1"
case "$platform" in
macos)
if command -v terminal-notifier &>/dev/null; then
ok "terminal-notifier found (supports custom icon)"
elif command -v osascript &>/dev/null; then
ok "osascript found (basic notifications)"
warn "Install terminal-notifier for icon support: brew install terminal-notifier"
else
fail "No notification tool found"
return 1
fi
;;
linux)
if command -v notify-send &>/dev/null; then
ok "notify-send found"
else
warn "notify-send not found. Install: sudo apt install libnotify-bin"
return 1
fi
;;
*)
warn "Unknown platform. Notifications may not work."
;;
esac
}
# ============================================================
# JSON Manipulation (settings.json)
# ============================================================
json_set_hook() {
# Adds or updates a hook entry in settings.json
# Usage: json_set_hook EVENT TYPE [MATCHER]
local event="$1" type="$2" matcher="${3:-}"
local hook_cmd="bash \"$NOTIFY_SH\" $type"
local py
py=$(command -v python3 || command -v python || true)
if [ -z "$py" ]; then
fail "python3 required for settings.json manipulation"
return 1
fi
"$py" - "$SETTINGS_FILE" "$event" "$type" "$matcher" "$hook_cmd" << 'PYEOF'
import json, sys, os
settings_file = sys.argv[1]
event_name = sys.argv[2]
hook_type = sys.argv[3]
matcher = sys.argv[4] if len(sys.argv) > 4 and sys.argv[4] else None
hook_cmd = sys.argv[5] if len(sys.argv) > 5 else None
# Read existing settings
if os.path.exists(settings_file):
with open(settings_file, 'r', encoding='utf-8') as f:
settings = json.load(f)
else:
settings = {}
# Ensure hooks structure
if 'hooks' not in settings:
settings['hooks'] = {}
if event_name not in settings['hooks']:
settings['hooks'][event_name] = []
# Check if already registered
hook_list = settings['hooks'][event_name]
for entry in hook_list:
hooks = entry.get('hooks', [])
for h in hooks:
if h.get('command') == hook_cmd:
print(f" already registered: {event_name}/{hook_type}")
sys.exit(0)
# Build new entry
new_hook = {"type": "command", "command": hook_cmd, "timeout": 5}
if matcher:
new_entry = {"matcher": matcher, "hooks": [new_hook]}
else:
new_entry = {"hooks": [new_hook]}
settings['hooks'][event_name].append(new_entry)
# Write back
os.makedirs(os.path.dirname(settings_file), exist_ok=True)
with open(settings_file, 'w', encoding='utf-8') as f:
json.dump(settings, f, indent=2, ensure_ascii=False)
f.write('\n')
print(f" registered: {event_name}/{hook_type}")
PYEOF
}
json_remove_hooks() {
local event="$1"
local py
py=$(command -v python3 || command -v python || true)
[ -z "$py" ] && return
"$py" - "$SETTINGS_FILE" "$event" << 'PYEOF'
import json, sys, os
settings_file = sys.argv[1]
event_name = sys.argv[2]
if not os.path.exists(settings_file):
sys.exit(0)
with open(settings_file, 'r', encoding='utf-8') as f:
settings = json.load(f)
if 'hooks' not in settings or event_name not in settings['hooks']:
sys.exit(0)
# Filter out notify.sh entries
original = settings['hooks'][event_name]
filtered = []
for entry in original:
hooks = entry.get('hooks', [])
keep = True
for h in hooks:
if 'notify.sh' in h.get('command', ''):
keep = False
break
if keep:
filtered.append(entry)
settings['hooks'][event_name] = filtered
with open(settings_file, 'w', encoding='utf-8') as f:
json.dump(settings, f, indent=2, ensure_ascii=False)
f.write('\n')
PYEOF
ok "$event hooks removed"
}
# ============================================================
# Icon Setup
# ============================================================
ensure_icon() {
[ -f "$ICON_PATH" ] && { ok "Icon exists: $ICON_PATH"; return; }
local bundled_icon
bundled_icon="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/notify-icon.png"
if [ -f "$bundled_icon" ]; then
mkdir -p "$ICON_DIR"
cp "$bundled_icon" "$ICON_PATH"
ok "Icon copied: $ICON_PATH"
else
warn "Bundled icon not found, skipping icon setup"
fi
}
# ============================================================
# Main
# ============================================================
if $UNINSTALL; then
printf "\n Uninstalling Claude Code Notify hooks...\n\n"
json_remove_hooks 'Stop'
json_remove_hooks 'Notification'
json_remove_hooks 'StopFailure'
ok "Done. Icon preserved at: $ICON_PATH"
printf "\n"
exit 0
fi
platform=$(detect_platform)
printf "\n Claude Code Notify — %s Installer\n\n" "$platform"
# 1. Check notification tools
check_notification_tool "$platform" || true
# 2. Ensure hooks dir
mkdir -p "$HOOKS_DIR"
# 3. Check notify.sh
if [ ! -f "$NOTIFY_SH" ]; then
fail "notify.sh not found at: $NOTIFY_SH"
exit 1
fi
chmod +x "$NOTIFY_SH"
ok "notify.sh found (+x)"
# 4. Generate icon
ensure_icon
# 5. Register hooks
step "Registering hooks..."
json_set_hook 'Stop' 'info'
json_set_hook 'Notification' 'warning' 'permission_prompt'
json_set_hook 'StopFailure' 'error'
# 6. Summary
printf "\n"
printf " ┌──────────────────────────────────────────┐\n"
printf " │ Installation complete! │\n"
printf " │ │\n"
printf " │ Icon: $ICON_PATH\n"
printf " │ │\n"
printf " │ To customize avatar, replace: │\n"
printf " │ ~/.claude/notify-icon.png │\n"
printf " │ │\n"
printf " │ To uninstall: │\n"
printf " │ bash install.sh --uninstall │\n"
printf " └──────────────────────────────────────────┘\n"
printf "\n"