Repository navigation
Expand file tree
/
Copy pathInstall-CodexAuto.ps1
More file actions
188 lines (160 loc) · 6.26 KB
/
Copy pathInstall-CodexAuto.ps1
File metadata and controls
188 lines (160 loc) · 6.26 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
#Requires -Version 5.1
<#
.SYNOPSIS
Install or uninstall the codex-auto PowerShell command for the current user.
.EXAMPLE
.\Install-CodexAuto.ps1
.EXAMPLE
.\Install-CodexAuto.ps1 -Uninstall
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[switch]$Uninstall,
[switch]$Force
)
Set-StrictMode -Version 2.0
$ErrorActionPreference = 'Stop'
$markerStart = '# >>> codex-auto managed block >>>'
$markerEnd = '# <<< codex-auto managed block <<<'
$installRoot = Join-Path $env:LOCALAPPDATA 'STANKAL\codex-auto'
$installedScript = Join-Path $installRoot 'Codex-Auto.ps1'
$sourceScript = Join-Path $PSScriptRoot 'Codex-Auto.ps1'
$timestamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$utf8Bom = New-Object System.Text.UTF8Encoding($true)
function Get-CodexAutoProfilePaths {
$paths = New-Object System.Collections.ArrayList
[void]$paths.Add((Join-Path $HOME 'Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'))
if ($null -ne (Get-Command pwsh -ErrorAction SilentlyContinue)) {
[void]$paths.Add((Join-Path $HOME 'Documents\PowerShell\Microsoft.PowerShell_profile.ps1'))
}
return @($paths | Select-Object -Unique)
}
function Remove-CodexAutoManagedBlock {
param([string]$Content)
$escapedStart = [regex]::Escape($markerStart)
$escapedEnd = [regex]::Escape($markerEnd)
$pattern = "(?ms)^\s*$escapedStart\s*\r?\n.*?^\s*$escapedEnd\s*(?:\r?\n)?"
return [regex]::Replace($Content, $pattern, '').TrimEnd()
}
function Backup-CodexAutoProfile {
param([string]$ProfilePath)
if (-not (Test-Path -LiteralPath $ProfilePath -PathType Leaf)) {
return
}
$backupPath = "$ProfilePath.codex-auto.bak-$timestamp"
$suffix = 1
while (Test-Path -LiteralPath $backupPath) {
$backupPath = "$ProfilePath.codex-auto.bak-$timestamp-$suffix"
$suffix++
}
Copy-Item -LiteralPath $ProfilePath -Destination $backupPath
Write-Host "Backup created: $backupPath" -ForegroundColor DarkGray
}
function Write-CodexAutoProfile {
param(
[string]$ProfilePath,
[string]$Content
)
$parent = Split-Path -Parent $ProfilePath
if (-not (Test-Path -LiteralPath $parent)) {
[void](New-Item -ItemType Directory -Path $parent -Force)
}
[System.IO.File]::WriteAllText($ProfilePath, $Content, $utf8Bom)
}
$profiles = @(Get-CodexAutoProfilePaths)
if ($Uninstall) {
foreach ($profilePath in $profiles) {
if (-not (Test-Path -LiteralPath $profilePath -PathType Leaf)) {
continue
}
$content = [System.IO.File]::ReadAllText($profilePath)
$updated = Remove-CodexAutoManagedBlock -Content $content
if ($updated -ceq $content.TrimEnd()) {
continue
}
if ($PSCmdlet.ShouldProcess($profilePath, 'Remove the codex-auto managed block')) {
Backup-CodexAutoProfile -ProfilePath $profilePath
$finalContent = if ([string]::IsNullOrWhiteSpace($updated)) {
''
} else {
$updated + [Environment]::NewLine
}
Write-CodexAutoProfile -ProfilePath $profilePath -Content $finalContent
Write-Host "Profile updated: $profilePath" -ForegroundColor Green
}
}
if (Test-Path -LiteralPath $installedScript -PathType Leaf) {
if ($PSCmdlet.ShouldProcess($installedScript, 'Remove the installed codex-auto script')) {
Remove-Item -LiteralPath $installedScript
}
}
if ((Test-Path -LiteralPath $installRoot -PathType Container) -and
(@(Get-ChildItem -LiteralPath $installRoot -Force).Count -eq 0)) {
if ($PSCmdlet.ShouldProcess($installRoot, 'Remove the empty codex-auto installation directory')) {
Remove-Item -LiteralPath $installRoot
}
}
if ($WhatIfPreference) {
Write-Host 'Uninstall preview completed; no files were changed.' -ForegroundColor Cyan
} else {
Write-Host 'codex-auto was uninstalled. Restart PowerShell to refresh the command.' -ForegroundColor Green
}
return
}
if (-not (Test-Path -LiteralPath $sourceScript -PathType Leaf)) {
throw "Source script was not found: $sourceScript"
}
$profileStates = @()
foreach ($profilePath in $profiles) {
$content = if (Test-Path -LiteralPath $profilePath -PathType Leaf) {
[System.IO.File]::ReadAllText($profilePath)
} else {
''
}
$clean = Remove-CodexAutoManagedBlock -Content $content
$hasUnmanagedCommand = $clean -match '(?im)^\s*function\s+(?:global:)?codex-auto\b'
if ($hasUnmanagedCommand -and (-not $Force)) {
throw "An unmanaged codex-auto function already exists in $profilePath. Remove it first or rerun with -Force."
}
$profileStates += [pscustomobject]@{
Path = $profilePath
Content = $content
Clean = $clean
}
}
if ($PSCmdlet.ShouldProcess($installedScript, 'Install the codex-auto script')) {
if (-not (Test-Path -LiteralPath $installRoot)) {
[void](New-Item -ItemType Directory -Path $installRoot -Force)
}
Copy-Item -LiteralPath $sourceScript -Destination $installedScript -Force
Write-Host "Script installed: $installedScript" -ForegroundColor Green
}
$escapedScript = $installedScript.Replace("'", "''")
$managedBlock = @(
$markerStart
'function global:codex-auto {'
" & '$escapedScript' @args"
'}'
$markerEnd
) -join [Environment]::NewLine
foreach ($state in $profileStates) {
$baseContent = $state.Clean.TrimEnd()
$newContent = if ([string]::IsNullOrWhiteSpace($baseContent)) {
$managedBlock + [Environment]::NewLine
} else {
$baseContent + [Environment]::NewLine + [Environment]::NewLine +
$managedBlock + [Environment]::NewLine
}
if ($PSCmdlet.ShouldProcess($state.Path, 'Add the codex-auto managed block')) {
if ($state.Content -cne $newContent) {
Backup-CodexAutoProfile -ProfilePath $state.Path
Write-CodexAutoProfile -ProfilePath $state.Path -Content $newContent
}
Write-Host "Profile configured: $($state.Path)" -ForegroundColor Green
}
}
if ($WhatIfPreference) {
Write-Host 'Installation preview completed; no files were changed.' -ForegroundColor Cyan
} else {
Write-Host 'Installation complete. Restart PowerShell, then run: codex-auto' -ForegroundColor Cyan
}