Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
230 lines (221 loc) · 11.6 KB
/
Copy pathinstall.ps1
File metadata and controls
230 lines (221 loc) · 11.6 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
#Requires -Version 5.1
# Jeffy installer: verifies prerequisites, installs the /jeffy skill (which
# ships the Stop hook loop engine), and registers the hook in ~/.claude/settings.json.
$ErrorActionPreference = "Stop"
Write-Host "Jeffy installer"
Write-Host ""
$ok = $true
# 1. Claude Code CLI (the one prerequisite this installer cannot install for you)
$claudeFound = [bool](Get-Command claude -ErrorAction SilentlyContinue)
if ($claudeFound) {
Write-Host "[OK] Claude Code CLI found"
} else {
Write-Host "[MISSING] Claude Code CLI. Install it first: https://claude.com/claude-code"
$ok = $false
}
# 2. jq (required by Jeffy's Stop hook)
if (Get-Command jq -ErrorAction SilentlyContinue) {
Write-Host "[OK] jq found"
} elseif (Get-Command winget -ErrorAction SilentlyContinue) {
try {
$answer = Read-Host "[MISSING] jq is required by Jeffy's Stop hook. Install with winget now? (y/n)"
} catch {
$answer = ""
Write-Host "[MISSING] jq, and the install prompt could not be answered (non-interactive run)."
}
if ($answer -eq "y") {
winget install --id jqlang.jq --accept-source-agreements --accept-package-agreements
if ($LASTEXITCODE -ne 0) {
Write-Host "winget install failed. Install manually: winget install jqlang.jq"
$ok = $false
} else {
Write-Host "[OK] jq installed (open a new terminal if it is not on PATH yet)"
}
} else {
Write-Host "Skipped. Install later with: winget install jqlang.jq"
$ok = $false
}
} else {
Write-Host "[MISSING] jq, and winget is not available to install it. Install jq manually: winget install jqlang.jq (or see https://jqlang.github.io/jq/download/)"
$ok = $false
}
# 3. Install the skills (/jeffy and /cancel-jeffy). The jeffy skill folder
# carries the Stop hook at hooks\stop-hook.sh, so the engine lands here too.
foreach ($name in @("jeffy", "cancel-jeffy")) {
$src = Join-Path $PSScriptRoot "skills\$name\SKILL.md"
if (-not (Test-Path $src)) {
throw "skills\$name\SKILL.md not found next to this script. Run install.ps1 from the cloned repo."
}
$dest = Join-Path $HOME ".claude\skills\$name"
New-Item -ItemType Directory -Path $dest -Force | Out-Null
$srcDir = Join-Path $PSScriptRoot "skills\$name"
Copy-Item -Path (Join-Path $srcDir "*") -Destination $dest -Recurse -Force
# A copy over the top never removes: references\enhance-plan-default.md
# outlived its removal in 1.11.0 on every installed host. Files the shipped
# tree no longer carries are removed, and nothing else.
# -Name yields paths relative to $dest, so no string arithmetic against
# the absolute form: a $HOME spelled as an 8.3 short path (RUNNER~1 on
# the CI runner) does not match the long path Get-ChildItem reports,
# and a Substring on that mismatch once removed every installed file.
foreach ($rel in @(Get-ChildItem -Path $dest -Recurse -File -Name)) {
if (-not (Test-Path -LiteralPath (Join-Path $srcDir $rel))) {
Remove-Item -LiteralPath (Join-Path $dest $rel) -Force
Write-Host "[OK] removed $(Join-Path $dest $rel) (not in this version)"
}
}
Write-Host "[OK] /$name skill installed to $dest"
}
# 4. Register the Stop hook (the loop engine) in ~/.claude/settings.json.
# Idempotent: a settings file that already names the hook is left alone, and
# an unparseable settings file is never overwritten. The command uses forward
# slashes because it runs under bash (Git Bash on Windows).
# Built with nested Join-Path so the separators are native on every platform:
# the .NET write below does not translate backslashes on Linux the way the
# PowerShell provider cmdlets do.
$settingsDir = Join-Path $HOME ".claude"
$settingsPath = Join-Path $settingsDir "settings.json"
$hookScript = (Join-Path $HOME ".claude\skills\jeffy\hooks\stop-hook.sh") -replace '\\', '/'
$hookCmd = "bash `"$hookScript`""
# A settings.json write never truncates the file it replaces. The JSON goes
# to a temporary file beside it, is parsed back before anything moves, and
# replaces the target with a single rename. install.sh has always done this
# with mktemp, jq empty and mv, and the shipped Python installer does it with
# os.replace; this is the same contract on Windows. The rename is the fix and
# not a flourish: Move-Item -Force replaces a read-only destination on NTFS
# where WriteAllText into that same file is denied, so a profile this
# installer used to refuse is now updated. (T7)
function Write-JeffySettings {
param([string] $Path, $Data)
$dir = Split-Path -Parent $Path
New-Item -ItemType Directory -Path $dir -Force | Out-Null
$tmp = Join-Path $dir ("settings.json.jeffy-" + [System.IO.Path]::GetRandomFileName())
try {
[System.IO.File]::WriteAllText($tmp, (($Data | ConvertTo-Json -Depth 32) + "`n"))
# A truncated or unparseable temporary file is never moved into place,
# which is the gate install.sh spells as jq empty.
$null = (Get-Content -LiteralPath $tmp -Raw | ConvertFrom-Json)
Move-Item -LiteralPath $tmp -Destination $Path -Force
} finally {
if (Test-Path -LiteralPath $tmp) { Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue }
}
}
$settings = $null
if ((Test-Path -LiteralPath $settingsPath) -and ((Get-Item -LiteralPath $settingsPath).Length -gt 0)) {
try {
$settingsText = Get-Content -LiteralPath $settingsPath -Raw -Encoding UTF8
if ([string]::IsNullOrWhiteSpace($settingsText) -or $settingsText.Trim() -ceq 'null') {
$settings = New-Object psobject
} else {
$settings = $settingsText | ConvertFrom-Json
if ($settings -isnot [System.Management.Automation.PSCustomObject]) { throw "not a JSON object" }
if ($settings.PSObject.Properties['hooks'] -and $null -eq $settings.hooks) { $settings.hooks = New-Object psobject }
}
} catch {
$why = if ($_.Exception.Message -eq 'not a JSON object') { 'is valid JSON but not an object' } else { 'is not valid JSON' }
Write-Host "[FAILED] $settingsPath $why; fix it, then re-run this installer to register the hook."
$ok = $false
}
} else {
$settings = New-Object psobject
}
if ($null -ne $settings) {
# A registration naming a hook that does not exist (a settings.json carried
# in from another home) is removed, so the checks below see this home's truth.
$pruned = $false
$writeFailed = $false
if ($settings.PSObject.Properties['hooks'] -and $settings.hooks.PSObject.Properties['Stop']) {
$keptEntries = @()
foreach ($entry in @($settings.hooks.Stop)) {
if ($null -eq $entry -or -not $entry.PSObject.Properties['hooks']) { $keptEntries += $entry; continue }
$keptHooks = @()
foreach ($h in @($entry.hooks)) {
$c = "$($h.command)"
if ($null -ne $h -and $c -like "*skills/jeffy/hooks/stop-hook.sh*") {
# A quoted path may hold a space, so the quoted forms are tried first.
$m = [regex]::Match($c, '"([^"]*skills/jeffy/hooks/stop-hook\.sh)"|''([^'']*skills/jeffy/hooks/stop-hook\.sh)''|([^"'' ]*skills/jeffy/hooks/stop-hook\.sh)')
$p = @($m.Groups[1].Value, $m.Groups[2].Value, $m.Groups[3].Value) | Where-Object { $_ } | Select-Object -First 1
$p = $p -replace '^~', $HOME -replace '\$\{?HOME\}?', $HOME
# install.sh run from Git Bash writes the hook as /c/...; Test-Path reads C:/...
if ($env:OS -eq 'Windows_NT') { $p = $p -replace '^/([A-Za-z])/', '$1:/' }
if (-not (Test-Path -LiteralPath $p -PathType Leaf)) {
Write-Host "[OK] removed a Stop hook registration naming a hook that does not exist: $c"
$pruned = $true
continue
}
}
$keptHooks += $h
}
if ($keptHooks.Count -gt 0) { $entry.hooks = $keptHooks; $keptEntries += $entry }
}
$settings.hooks.Stop = $keptEntries
}
if ($pruned) {
try {
Write-JeffySettings $settingsPath $settings
} catch {
Write-Host "[FAILED] could not update $settingsPath ($($_.Exception.Message)); remove the stale Stop hook entry by hand (see README) and re-run to verify."
$ok = $false
$writeFailed = $true
}
}
$already = $false
$upgrade = @()
if ($settings.PSObject.Properties['hooks'] -and $settings.hooks.PSObject.Properties['Stop']) {
foreach ($entry in @($settings.hooks.Stop)) {
if ($null -eq $entry) { continue }
foreach ($h in @($entry.hooks)) {
if ($null -ne $h -and "$($h.command)" -like "*skills/jeffy/hooks/stop-hook.sh*") {
$already = $true
if (-not $h.PSObject.Properties['timeout'] -or $h.timeout -eq 600) { $upgrade += $h }
}
}
}
}
if ($writeFailed) {
# The pruned file never reached disk, so the branches below would write
# the same unwritable path and print a second [FAILED] for one cause.
# The diagnostic above is the one the user acts on.
} elseif ($already -and $upgrade.Count -eq 0) {
Write-Host "[OK] Jeffy Stop hook already registered in $settingsPath"
} elseif ($already) {
# Pre-1.2 registration lacks the field; 1.2-1.14 carries 600s, which the
# verify bound can exceed (P1-58). Either shape moves to 1800s once.
try {
foreach ($h in $upgrade) { if ($h.PSObject.Properties['timeout']) { $h.timeout = 1800 } else { $h | Add-Member -MemberType NoteProperty -Name timeout -Value 1800 } }
Write-JeffySettings $settingsPath $settings
Write-Host "[OK] Jeffy Stop hook registration upgraded to an 1800s timeout in $settingsPath"
} catch {
Write-Host "[FAILED] could not upgrade the Stop hook timeout in $settingsPath ($($_.Exception.Message)); add `"timeout`": 1800 to the hook entry by hand and re-run to verify."
$ok = $false
}
} else {
try {
$hookObj = [pscustomobject]@{ type = "command"; command = $hookCmd; timeout = 1800 }
$entryObj = [pscustomobject]@{ hooks = @($hookObj) }
if (-not $settings.PSObject.Properties['hooks']) {
$settings | Add-Member -MemberType NoteProperty -Name hooks -Value (New-Object psobject)
}
if (-not $settings.hooks.PSObject.Properties['Stop']) {
$settings.hooks | Add-Member -MemberType NoteProperty -Name Stop -Value @()
}
$settings.hooks.Stop = @($settings.hooks.Stop) + $entryObj
# WriteAllText writes UTF-8 without a BOM; Set-Content -Encoding UTF8
# on Windows PowerShell 5.1 would prepend one, and a BOM breaks
# strict JSON parsers reading settings.json. The helper creates
# $settingsDir, so the New-Item that stood here is gone rather than
# duplicated.
Write-JeffySettings $settingsPath $settings
Write-Host "[OK] Jeffy Stop hook registered in $settingsPath (1800s timeout)"
} catch {
Write-Host "[FAILED] could not update $settingsPath ($($_.Exception.Message)); add the Stop hook entry by hand (see README) and re-run to verify."
$ok = $false
}
}
}
Write-Host ""
if ($ok) {
Write-Host "Done. Start a new Claude Code session in any project and run: /jeffy"
} else {
Write-Host "Skills installed, but fix the items above, then re-run this installer to verify."
exit 1
}