-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinstall-opencode.ps1
More file actions
173 lines (145 loc) · 5.65 KB
/
Copy pathinstall-opencode.ps1
File metadata and controls
173 lines (145 loc) · 5.65 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
$ErrorActionPreference = "Stop"
$scriptPath = $MyInvocation.MyCommand.Path
if ([string]::IsNullOrWhiteSpace($scriptPath)) {
$repo = (Get-Location).Path
} else {
$repo = Split-Path -Parent $scriptPath
}
$sourceOpenCode = Join-Path $repo ".opencode"
$tempDir = $null
$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"
$defaultLevel = $env:CAVEMAN_OPENCODE_DEFAULT_LEVEL
if ([string]::IsNullOrWhiteSpace($defaultLevel)) {
$defaultLevel = "full"
}
$allowedDefaultLevels = @("lite", "full", "ultra", "wenyan", "wenyan-lite", "wenyan-ultra")
if ($allowedDefaultLevels -notcontains $defaultLevel) {
throw "CAVEMAN_OPENCODE_DEFAULT_LEVEL must be one of: $($allowedDefaultLevels -join ', ')"
}
$skills = @(
"caveman",
"caveman-commit",
"caveman-review",
"caveman-help",
"caveman-compress"
)
function Ensure-Property($Object, [string]$Name, $Value) {
if ($Object.PSObject.Properties.Name -notcontains $Name) {
$Object | Add-Member -MemberType NoteProperty -Name $Name -Value $Value
}
}
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
}
}
function Expand-ZipArchive([string]$Path, [string]$DestinationPath) {
Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction Stop
[System.IO.Compression.ZipFile]::ExtractToDirectory($Path, $DestinationPath)
}
if (-not (Test-Path -LiteralPath (Join-Path $sourceOpenCode "AGENTS.md"))) {
$archiveUrl = $env:CAVEMAN_OPENCODE_ARCHIVE_URL
if ([string]::IsNullOrWhiteSpace($archiveUrl)) {
$archiveUrl = "https://github.com/anthonystepvoy/caveman-opencode/archive/refs/heads/main.zip"
}
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ("caveman-opencode-" + [System.Guid]::NewGuid().ToString("N"))
$zipPath = Join-Path $tempDir "source.zip"
New-Item -ItemType Directory -Force $tempDir | Out-Null
Write-Host "Downloading Caveman for OpenCode from $archiveUrl"
Invoke-WebRequest -UseBasicParsing -Uri $archiveUrl -OutFile $zipPath
Expand-ZipArchive -Path $zipPath -DestinationPath $tempDir
$sourceDir = Get-ChildItem -LiteralPath $tempDir -Directory | Select-Object -First 1
if (-not $sourceDir) {
throw "Downloaded archive did not contain a source directory."
}
$sourceOpenCode = Join-Path $sourceDir.FullName ".opencode"
if (-not (Test-Path -LiteralPath (Join-Path $sourceOpenCode "AGENTS.md"))) {
throw "Downloaded archive did not contain .opencode/AGENTS.md."
}
}
New-Item -ItemType Directory -Force $skillsDir | Out-Null
New-Item -ItemType Directory -Force $commandsDir | Out-Null
foreach ($skill in $skills) {
$source = Join-Path $sourceOpenCode "skills\$skill"
$target = Join-Path $skillsDir $skill
if (Test-Path -LiteralPath $target) {
Remove-Item -Recurse -Force -LiteralPath $target
}
Copy-Item -Recurse -Force -LiteralPath $source -Destination $target
}
Copy-Item -Force (Join-Path $sourceOpenCode "commands\*.md") -Destination $commandsDir
$agentsText = Get-Content -LiteralPath (Join-Path $sourceOpenCode "AGENTS.md") -Raw -Encoding UTF8
$agentsText = $agentsText -replace "Default mode: full\.", "Default mode: $defaultLevel."
$agentsText | Set-Content -LiteralPath $agentsFile -Encoding UTF8
if (Test-Path -LiteralPath $configFile) {
$content = Get-Content -LiteralPath $configFile -Raw -Encoding UTF8
$config = ConvertFrom-OpenCodeJson $content
} else {
$config = [pscustomobject]@{}
}
Ensure-Property $config "instructions" @()
$instructions = @($config.instructions)
if ($instructions -notcontains $agentsFile) {
$config.instructions = @($instructions + $agentsFile)
}
Ensure-Property $config "permission" ([pscustomobject]@{})
Ensure-Property $config.permission "skill" ([pscustomobject]@{})
foreach ($skill in $skills) {
if ($config.permission.skill.PSObject.Properties.Name -contains $skill) {
$config.permission.skill.$skill = "allow"
} else {
$config.permission.skill | Add-Member -MemberType NoteProperty -Name $skill -Value "allow"
}
}
$config | ConvertTo-Json -Depth 50 | Set-Content -LiteralPath $configFile -Encoding UTF8
if ($tempDir -and (Test-Path -LiteralPath $tempDir)) {
Remove-Item -Recurse -Force -LiteralPath $tempDir
}
Write-Host "Installed Caveman for OpenCode to $configDir"
Write-Host "Default Caveman intensity: $defaultLevel"
Write-Host "Restart OpenCode, then use /caveman, /caveman-help, /caveman-review, /caveman-commit, or /caveman-compress."