Repository navigation
Expand file tree
/
Copy pathuninstall-opencode.ps1
More file actions
108 lines (93 loc) · 2.95 KB
/
Copy pathuninstall-opencode.ps1
File metadata and controls
108 lines (93 loc) · 2.95 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
$ErrorActionPreference = "Stop"
$configDir = Join-Path $env:USERPROFILE ".config\opencode"
$skillsDir = Join-Path $configDir "skills"
$commandsDir = Join-Path $configDir "commands"
$configFile = Join-Path $configDir "opencode.json"
$agentsFile = Join-Path $configDir "AGENTS.caveman.md"
$skills = @(
"caveman",
"caveman-commit",
"caveman-review",
"caveman-help",
"caveman-compress"
)
function Remove-JsonTrailingCommas([string]$Content) {
$result = [System.Text.StringBuilder]::new($Content.Length)
$inString = $false
$escaped = $false
for ($index = 0; $index -lt $Content.Length; $index++) {
$character = $Content[$index]
if ($inString) {
[void]$result.Append($character)
if ($escaped) {
$escaped = $false
} elseif ($character -eq '\') {
$escaped = $true
} elseif ($character -eq '"') {
$inString = $false
}
continue
}
if ($character -eq '"') {
$inString = $true
[void]$result.Append($character)
continue
}
if ($character -eq ',') {
$next = $index + 1
while ($next -lt $Content.Length -and [char]::IsWhiteSpace($Content[$next])) {
$next++
}
if ($next -lt $Content.Length -and ($Content[$next] -eq '}' -or $Content[$next] -eq ']')) {
continue
}
}
[void]$result.Append($character)
}
return $result.ToString()
}
function ConvertFrom-OpenCodeJson([string]$Content) {
try {
return $Content | ConvertFrom-Json
} catch {
$normalized = Remove-JsonTrailingCommas $Content
if ($normalized -eq $Content) {
throw
}
return $normalized | ConvertFrom-Json
}
}
foreach ($skill in $skills) {
$skillPath = Join-Path $skillsDir $skill
if (Test-Path -LiteralPath $skillPath) {
Remove-Item -Recurse -Force -LiteralPath $skillPath
}
}
foreach ($command in @("caveman", "caveman-commit", "caveman-review", "caveman-help", "caveman-compress")) {
$commandPath = Join-Path $commandsDir "$command.md"
if (Test-Path -LiteralPath $commandPath) {
Remove-Item -Force -LiteralPath $commandPath
}
}
if (Test-Path -LiteralPath $agentsFile) {
Remove-Item -Force -LiteralPath $agentsFile
}
if (Test-Path -LiteralPath $configFile) {
$content = Get-Content -LiteralPath $configFile -Raw -Encoding UTF8
$config = ConvertFrom-OpenCodeJson $content
if ($config.PSObject.Properties.Name -contains "instructions") {
$config.instructions = @(@($config.instructions) | Where-Object { $_ -ne $agentsFile })
}
if (
$config.PSObject.Properties.Name -contains "permission" -and
$config.permission.PSObject.Properties.Name -contains "skill"
) {
foreach ($skill in $skills) {
if ($config.permission.skill.PSObject.Properties.Name -contains $skill) {
$config.permission.skill.PSObject.Properties.Remove($skill)
}
}
}
$config | ConvertTo-Json -Depth 50 | Set-Content -LiteralPath $configFile -Encoding UTF8
}
Write-Host "Removed Caveman OpenCode files from $configDir"