-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.ps1
More file actions
221 lines (208 loc) · 10.7 KB
/
Copy pathinstall.ps1
File metadata and controls
221 lines (208 loc) · 10.7 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
[CmdletBinding()]
param(
[string]$CodexHome = '',
[string]$BinDir = '',
[string]$CodexBin = '',
[switch]$Update,
[switch]$Uninstall,
[switch]$ShellHook,
[string]$ProfilePath = ''
)
$ErrorActionPreference = 'Stop'
$Source = $PSScriptRoot
if (-not $CodexHome) { $CodexHome = if ($env:CODEX_HOME) { $env:CODEX_HOME } else { Join-Path $HOME '.codex' } }
if (-not $BinDir) { $BinDir = Join-Path $HOME '.local\bin' }
$InstallDir = Join-Path $CodexHome 'coralline-codex'
$ThemeDir = Join-Path $CodexHome 'themes'
$Config = Join-Path $CodexHome 'coralline-codex.windows.json'
$ShellState = Join-Path $CodexHome 'coralline-codex-powershell.json'
$BackupRoot = Join-Path $CodexHome 'coralline-codex-backups'
$Stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$BackupDir = Join-Path $BackupRoot $Stamp
$Wrapper = Join-Path $InstallDir 'bin\coralline-codex.ps1'
$CommandShim = Join-Path $BinDir 'coralline-codex.cmd'
$StartMarker = '# >>> coralline-codex managed PowerShell integration >>>'
$EndMarker = '# <<< coralline-codex managed PowerShell integration <<<'
$PreviousVersion = if (Test-Path -LiteralPath (Join-Path $InstallDir 'VERSION')) { (Get-Content -LiteralPath (Join-Path $InstallDir 'VERSION') -Raw).Trim() } else { '' }
$MinimumCodexVersion = [version]'0.145.0'
$DefaultNativeFields = @('model-with-reasoning', 'run-state', 'context-remaining', 'five-hour-limit', 'weekly-limit', 'used-tokens', 'git-branch', 'branch-changes', 'fast-mode', 'task-progress')
function Backup-File([string]$Path, [string]$Bucket) {
if (-not (Test-Path -LiteralPath $Path)) { return $null }
New-Item -ItemType Directory -Force -Path $Bucket | Out-Null
$target = Join-Path $Bucket ((Split-Path -Leaf $Path) + ".bak.$Stamp")
Copy-Item -LiteralPath $Path -Destination $target
return $target
}
function Remove-ManagedHook([string]$Path) {
if (-not (Test-Path -LiteralPath $Path)) { return $false }
$text = Get-Content -LiteralPath $Path -Raw
$pattern = '(?ms)^' + [regex]::Escape($StartMarker) + '.*?^' + [regex]::Escape($EndMarker) + '\r?\n?'
$clean = [regex]::Replace($text, $pattern, '').TrimEnd() + [Environment]::NewLine
if ($clean -eq $text) { return $false }
Backup-File $Path (Join-Path $BackupRoot 'powershell') | Out-Null
Set-Content -LiteralPath $Path -Value $clean -Encoding UTF8
return $true
}
function Remove-ShellIntegration {
if (-not (Test-Path -LiteralPath $ShellState)) { return }
$state = Get-Content -LiteralPath $ShellState -Raw | ConvertFrom-Json
if ($state.profile) {
if (Remove-ManagedHook ([string]$state.profile)) {
Write-Output "Managed PowerShell hook removed from $($state.profile)"
}
}
Remove-Item -LiteralPath $ShellState -Force
}
function Get-CorallineThemeNames([string]$PalettePath) {
$names = @()
Get-Content -LiteralPath $PalettePath | ForEach-Object {
if ($_ -and -not $_.StartsWith('#')) { $names += ($_ -split "`t")[0] }
}
return $names
}
function Test-ManagedShim([string]$Path, [string]$ExpectedWrapper) {
if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { return $false }
return (Get-Content -LiteralPath $Path -Raw).Contains($ExpectedWrapper)
}
if ($Uninstall) {
if (-not (Test-Path -LiteralPath $InstallDir)) {
Write-Output "Coralline Codex is not installed in $CodexHome"
exit 0
}
New-Item -ItemType Directory -Force -Path $BackupDir | Out-Null
Remove-ShellIntegration
if (Test-Path -LiteralPath $Config) { Move-Item -LiteralPath $Config -Destination (Join-Path $BackupDir (Split-Path -Leaf $Config)) }
if (Test-Path -LiteralPath $ThemeDir) {
New-Item -ItemType Directory -Force -Path (Join-Path $BackupDir 'themes') | Out-Null
Get-CorallineThemeNames (Join-Path $InstallDir 'themes\palettes.tsv') | ForEach-Object {
$theme = Join-Path $ThemeDir "coralline-$_.tmTheme"
if (Test-Path -LiteralPath $theme) {
Move-Item -LiteralPath $theme -Destination (Join-Path $BackupDir 'themes')
}
}
}
if (Test-ManagedShim $CommandShim $Wrapper) { Remove-Item -LiteralPath $CommandShim -Force }
Move-Item -LiteralPath $InstallDir -Destination (Join-Path $BackupDir 'install')
Write-Output "Uninstalled Coralline Codex. Recoverable backup: $BackupDir"
Write-Output 'Codex config.toml was not changed.'
exit 0
}
if (-not $CodexBin) {
$command = Get-Command codex -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1
if (-not $command) { throw 'The Codex CLI is required.' }
$CodexBin = $command.Source
}
$CodexBin = (Resolve-Path -LiteralPath $CodexBin).Path
$codexVersionOutput = (& $CodexBin --version | Out-String).Trim()
if ($LASTEXITCODE -ne 0) { throw "Failed to run Codex: $CodexBin" }
if ($codexVersionOutput -notmatch '(\d+\.\d+\.\d+)') { throw "Could not parse Codex version: $codexVersionOutput" }
$codexVersion = [version]$Matches[1]
if ($codexVersion -lt $MinimumCodexVersion) {
throw "Codex $MinimumCodexVersion or newer is required (found $codexVersion)."
}
$python = Get-Command python -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1
if (-not $python) { $python = Get-Command python3 -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1 }
if (-not $python) { throw 'Python 3.8+ is required.' }
New-Item -ItemType Directory -Force -Path $CodexHome, $BinDir, $ThemeDir | Out-Null
$ExistingInstall = Test-Path -LiteralPath $InstallDir -PathType Container
if (-not $ExistingInstall -and (Test-Path -LiteralPath $CommandShim)) {
throw "Refusing to overwrite unrelated command path: $CommandShim"
}
if (-not $ExistingInstall) {
Get-CorallineThemeNames (Join-Path $Source 'themes\palettes.tsv') | ForEach-Object {
$theme = Join-Path $ThemeDir "coralline-$_.tmTheme"
if (Test-Path -LiteralPath $theme) { throw "Refusing to overwrite unrelated theme: $theme" }
}
}
$Stage = Join-Path $CodexHome ('.coralline-codex-stage.' + [guid]::NewGuid().ToString('N'))
try {
New-Item -ItemType Directory -Force -Path $Stage | Out-Null
foreach ($file in @('VERSION', 'CHANGELOG.md', 'CONTRIBUTING.md', 'LICENSE', 'NOTICE.md', 'README.md', 'README.zh-TW.md', 'SECURITY.md', 'install.sh', 'install.ps1', 'configure.sh', 'configure.ps1')) {
if (Test-Path -LiteralPath (Join-Path $Source $file)) { Copy-Item -LiteralPath (Join-Path $Source $file) -Destination $Stage }
}
foreach ($directory in @('assets', 'bin', 'lib', 'themes', 'tools', 'test', 'docs')) {
Copy-Item -LiteralPath (Join-Path $Source $directory) -Destination $Stage -Recurse
}
& $python.Source (Join-Path $Stage 'tools\generate_themes.py') --palettes (Join-Path $Stage 'themes\palettes.tsv') --output (Join-Path $Stage 'themes\generated')
if ($LASTEXITCODE -ne 0) { throw 'Theme generation failed.' }
if (Test-Path -LiteralPath $InstallDir) {
New-Item -ItemType Directory -Force -Path $BackupDir | Out-Null
Move-Item -LiteralPath $InstallDir -Destination (Join-Path $BackupDir 'install')
}
Move-Item -LiteralPath $Stage -Destination $InstallDir
$Stage = ''
} finally {
if ($Stage -and (Test-Path -LiteralPath $Stage)) { Remove-Item -LiteralPath $Stage -Recurse -Force }
}
Get-ChildItem -LiteralPath (Join-Path $InstallDir 'themes\generated') -Filter '*.tmTheme' | ForEach-Object {
$target = Join-Path $ThemeDir $_.Name
if (Test-Path -LiteralPath $target) {
Backup-File $target (Join-Path $BackupDir 'themes') | Out-Null
}
Copy-Item -LiteralPath $_.FullName -Destination $target -Force
}
$shimText = "@echo off`r`npowershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$Wrapper`" %*`r`n"
Set-Content -LiteralPath $CommandShim -Value $shimText -Encoding ASCII
if (Test-Path -LiteralPath $Config) {
$settings = Get-Content -LiteralPath $Config -Raw | ConvertFrom-Json
Backup-File $Config (Join-Path $BackupRoot 'windows-config') | Out-Null
$settings.codexBin = $CodexBin
if ($settings.PSObject.Properties.Name -notcontains 'nativeFields') {
$settings | Add-Member -NotePropertyName nativeFields -NotePropertyValue $DefaultNativeFields
}
} else {
$settings = [pscustomobject]@{
version = 1; theme = 'claude-coral'; nativeStatus = $true; codexBin = $CodexBin
nativeFields = $DefaultNativeFields
}
}
$settings | ConvertTo-Json | Set-Content -LiteralPath $Config -Encoding UTF8
if (-not $ShellHook -and (Test-Path -LiteralPath $ShellState)) {
$existingShellState = Get-Content -LiteralPath $ShellState -Raw | ConvertFrom-Json
if ($existingShellState.profile) {
$ShellHook = $true
if (-not $ProfilePath) { $ProfilePath = [string]$existingShellState.profile }
}
}
if ($ShellHook) {
if (-not $ProfilePath) { $ProfilePath = $PROFILE.CurrentUserAllHosts }
$profileDirectory = Split-Path -Parent $ProfilePath
New-Item -ItemType Directory -Force -Path $profileDirectory | Out-Null
$old = if (Test-Path -LiteralPath $ProfilePath) { Get-Content -LiteralPath $ProfilePath -Raw } else { '' }
$pattern = '(?ms)^' + [regex]::Escape($StartMarker) + '.*?^' + [regex]::Escape($EndMarker) + '\r?\n?'
$clean = [regex]::Replace($old, $pattern, '').TrimEnd()
$escapedWrapper = $Wrapper.Replace("'", "''")
$block = @"
$StartMarker
function global:codex {
& '$escapedWrapper' run @args
}
function global:coralline-codex {
& '$escapedWrapper' @args
}
$EndMarker
"@
$new = if ($clean) { $clean + [Environment]::NewLine + [Environment]::NewLine + $block.Trim() + [Environment]::NewLine } else { $block.Trim() + [Environment]::NewLine }
if ($new -ne $old) {
Backup-File $ProfilePath (Join-Path $BackupRoot 'powershell') | Out-Null
Set-Content -LiteralPath $ProfilePath -Value $new -Encoding UTF8
}
@{ version = 1; profile = $ProfilePath } | ConvertTo-Json | Set-Content -LiteralPath $ShellState -Encoding UTF8
Write-Output "Managed PowerShell hook installed in $ProfilePath"
}
$CurrentVersion = (Get-Content -LiteralPath (Join-Path $InstallDir 'VERSION') -Raw).Trim()
Write-Output "Coralline Codex $CurrentVersion installed."
Write-Output " command: $CommandShim"
Write-Output " runtime: $InstallDir"
Write-Output " config: $Config"
Write-Output ' Codex config.toml: unchanged'
if ($PreviousVersion -and $PreviousVersion -ne $CurrentVersion) {
Write-Output ''
Write-Output "Updated $PreviousVersion -> $CurrentVersion. New in this release:"
$show = $false
foreach ($line in Get-Content -LiteralPath (Join-Path $InstallDir 'CHANGELOG.md')) {
if ($line.StartsWith("## $CurrentVersion ")) { $show = $true; continue }
if ($show -and $line.StartsWith('## ')) { break }
if ($show -and $line.StartsWith('- ')) { Write-Output " $line" }
}
}