-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
225 lines (197 loc) · 7.53 KB
/
install.ps1
File metadata and controls
225 lines (197 loc) · 7.53 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
#Requires -Version 5.1
<#
.SYNOPSIS
Claude Code Notify — Windows Installer
.DESCRIPTION
Installs notification hooks into Claude Code settings.json.
Checks/installs BurntToast, generates default icon, registers 3 hooks.
.PARAMETER Uninstall
Remove notification hooks from settings.json
.EXAMPLE
pwsh -File install.ps1
pwsh -File install.ps1 -Uninstall
#>
[CmdletBinding()]
param(
[switch]$Uninstall
)
$ErrorActionPreference = 'Stop'
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$SettingsFile = Join-Path $env:USERPROFILE '.claude' 'settings.json'
$HooksDir = Join-Path $env:USERPROFILE '.claude' 'hooks'
$IconPath = Join-Path $env:USERPROFILE '.claude' 'notify-icon.png'
$NotifyPs1 = Join-Path $HooksDir 'notify.ps1'
# ============================================================
# Helpers
# ============================================================
function Write-Step($msg) { Write-Host " $msg" -ForegroundColor Cyan }
function Write-Ok($msg) { Write-Host " ✓ $msg" -ForegroundColor Green }
function Write-Warn($msg) { Write-Host " ⚠ $msg" -ForegroundColor Yellow }
function Write-Fail($msg) { Write-Host " ✗ $msg" -ForegroundColor Red }
function Test-Pwsh7 {
$v = $PSVersionTable.PSVersion
if ($v.Major -ge 7) { return $true }
Write-Warn "PowerShell 7+ recommended (current: $v). BurntToast requires PS7."
return $false
}
function Install-BurntToastIfNeeded {
if (Get-Module -ListAvailable -Name BurntToast) {
Write-Ok "BurntToast already installed"
return $true
}
Write-Step "Installing BurntToast module..."
try {
Install-Module -Name BurntToast -Force -Scope CurrentUser -ErrorAction Stop
Write-Ok "BurntToast installed"
return $true
}
catch {
Write-Fail "Failed to install BurntToast: $_"
Write-Warn "Run manually: Install-Module -Name BurntToast -Force -Scope CurrentUser"
return $false
}
}
function Get-HookCommand {
param([string]$Type)
return "pwsh -ExecutionPolicy Bypass -File `"$NotifyPs1`" -Type $Type"
}
function Read-Settings {
if (Test-Path $SettingsFile) {
return (Get-Content $SettingsFile -Raw -Encoding UTF8 | ConvertFrom-Json)
}
return [PSCustomObject]@{}
}
function Write-Settings($settings) {
$dir = Split-Path $SettingsFile
if (-not (Test-Path $dir)) { New-Item -Path $dir -ItemType Directory -Force | Out-Null }
$jsonContent = $settings | ConvertTo-Json -Depth 20
[System.IO.File]::WriteAllText($SettingsFile, $jsonContent)
}
# ============================================================
# Hook Registration
# ============================================================
function Add-NotifyHook {
param(
[string]$EventName,
[string]$Type,
[string]$Matcher = $null
)
$settings = Read-Settings
if ($null -eq $settings.hooks) {
$settings | Add-Member -NotePropertyName hooks -NotePropertyValue @{} -Force
}
if ($null -eq $settings.hooks.$EventName) {
$settings.hooks | Add-Member -NotePropertyName $EventName -NotePropertyValue @() -Force
}
$cmd = Get-HookCommand -Type $Type
# Check if already registered
$arr = @($settings.hooks.$EventName)
foreach ($entry in $arr) {
if ($null -eq $entry.hooks) { continue }
foreach ($h in @($entry.hooks)) {
if ($h.command -eq $cmd) {
Write-Ok "$EventName hook ($Type) already registered"
return
}
}
}
# Build new hook entry
$hookDef = [ordered]@{ type = 'command'; command = $cmd }
if ($Matcher) {
$entry = [ordered]@{
matcher = $Matcher
hooks = @($hookDef)
}
} else {
$entry = [ordered]@{
hooks = @($hookDef)
}
}
# Add to array (PSCustomObject -> array manipulation)
$arr += $entry
$settings.hooks.$EventName = $arr
Write-Settings $settings
Write-Ok "$EventName hook ($Type) registered"
}
function Remove-NotifyHook {
param([string]$EventName)
$settings = Read-Settings
if ($null -eq $settings.hooks -or $null -eq $settings.hooks.$EventName) { return }
$notifyCmd = Join-Path $HooksDir 'notify.ps1'
$filtered = @()
foreach ($entry in @($settings.hooks.$EventName)) {
if ($null -eq $entry.hooks) { continue }
$keep = $false
foreach ($h in @($entry.hooks)) {
if ($h.command -like "*notify.ps1*") { $keep = $true; break }
}
if (-not $keep) { $filtered += $entry }
}
$settings.hooks.$EventName = $filtered
Write-Settings $settings
Write-Ok "$EventName hooks removed"
}
# ============================================================
# Main
# ============================================================
if ($Uninstall) {
Write-Host "`n Uninstalling Claude Code Notify hooks...`n" -ForegroundColor Yellow
Remove-NotifyHook 'Stop'
Remove-NotifyHook 'Notification'
Remove-NotifyHook 'StopFailure'
Write-Ok "Done. Icon file preserved at: $IconPath"
Write-Host ""
return
}
Write-Host "`n Claude Code Notify — Windows Installer`n" -ForegroundColor White
# 1. Check PowerShell version
Test-Pwsh7 | Out-Null
# 2. Ensure hooks directory
if (-not (Test-Path $HooksDir)) {
New-Item -Path $HooksDir -ItemType Directory -Force | Out-Null
}
# 3. Check notify.ps1 exists
if (-not (Test-Path $NotifyPs1)) {
Write-Fail "notify.ps1 not found at: $NotifyPs1"
Write-Warn "Ensure notify.ps1 is in the hooks directory."
return
}
Write-Ok "notify.ps1 found"
# 4. Install BurntToast
$btOk = Install-BurntToastIfNeeded
# 5. Copy bundled icon
if (-not (Test-Path $IconPath)) {
$BundledIcon = Join-Path $PSScriptRoot 'notify-icon.png'
if (Test-Path $BundledIcon) {
Copy-Item -Path $BundledIcon -Destination $IconPath -Force
Write-Ok "Icon copied: $IconPath"
} else {
Write-Warn "Bundled icon not found, skipping icon setup"
}
} else {
Write-Ok "Icon exists: $IconPath"
}
# 6. Register hooks
Write-Step "Registering hooks..."
Add-NotifyHook -EventName 'Stop' -Type 'info'
Add-NotifyHook -EventName 'Notification' -Type 'warning' -Matcher 'permission_prompt'
Add-NotifyHook -EventName 'StopFailure' -Type 'error'
# 7. Summary
Write-Host ""
Write-Host " ┌──────────────────────────────────────────┐" -ForegroundColor White
Write-Host " │ Installation complete! │" -ForegroundColor Green
Write-Host " │ │" -ForegroundColor White
if ($btOk) {
Write-Host " │ BurntToast: OK │" -ForegroundColor Green
} else {
Write-Host " │ BurntToast: FAILED (install manually) │" -ForegroundColor Red
}
Write-Host " │ Icon: $IconPath" -ForegroundColor White
Write-Host " │ │" -ForegroundColor White
Write-Host " │ To customize avatar, replace: │" -ForegroundColor White
Write-Host " │ ~/.claude/notify-icon.png │" -ForegroundColor Cyan
Write-Host " │ │" -ForegroundColor White
Write-Host " │ To uninstall: │" -ForegroundColor White
Write-Host " │ pwsh -File install.ps1 -Uninstall │" -ForegroundColor Cyan
Write-Host " └──────────────────────────────────────────┘" -ForegroundColor White
Write-Host ""