-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
372 lines (324 loc) · 15.3 KB
/
Copy pathinstall.ps1
File metadata and controls
372 lines (324 loc) · 15.3 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#Requires -Version 5.1
<#
.SYNOPSIS
Build and install CodePatrol skills for Windows.
.DESCRIPTION
PowerShell-версия install.sh — сборка и установка скиллов из шаблонов.
.EXAMPLE
.\install.ps1 build
.\install.ps1 claude
.\install.ps1 codex
.\install.ps1 cursor
.\install.ps1 opencode
#>
param(
[Parameter(Position = 0)]
[ValidateSet('build', 'validate', 'claude', 'codex', 'cursor', 'opencode', 'omp')]
[string]$Command
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$RepoUrl = 'https://github.com/unger1984/codepatrol.git'
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$ClonedDir = $null
# Удалённая установка: если templates/_shared нет рядом со скриптом — клонируем репо
if (-not (Test-Path (Join-Path $ScriptDir 'templates' '_shared'))) {
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
Write-Error 'Error: git is required for remote installation'
exit 1
}
$ClonedDir = Join-Path ([System.IO.Path]::GetTempPath()) "codepatrol-$(Get-Random)"
Write-Host 'Cloning codepatrol...'
git clone --depth 1 $RepoUrl $ClonedDir 2>$null
$ScriptDir = $ClonedDir
}
$TemplatesDir = Join-Path $ScriptDir 'templates'
$SkillsDir = Join-Path $ScriptDir 'skills'
$PlatformsDir = Join-Path $ScriptDir 'platforms'
$LegacySkills = @(
'cpatrol', 'cpresume', 'cpexecute', 'cpplanreview', 'cpplanfix',
'cpreview', 'cpfix', 'cpdocs', 'cprules', 'cp-idea', 'cp-plan',
'cp-execute', 'cp-plan-review', 'cp-plan-fix', 'cp-resume',
'cp-docs', 'cp-rules', 'code-review', 'code-review-fix'
)
function Show-Usage {
Write-Host "Usage: .\install.ps1 <command>"
Write-Host ""
Write-Host "Commands:"
Write-Host " build Generate skills/ from templates using Claude platform values"
Write-Host " validate Generate all supported platforms to temp output and verify generated markdown"
Write-Host " claude Generate and install skills to ~/.claude/skills/"
Write-Host " codex Generate and install skills to ~/.codex/skills/"
Write-Host " cursor Generate and install skills to ~/.cursor/skills/"
Write-Host " opencode Generate and install skills to ~/.config/opencode/skills/"
Write-Host " omp Generate and install skills to ~/.omp/agent/skills/"
Write-Host ""
exit 1
}
function Remove-InstalledSkills {
param([string]$TargetDir)
if (-not (Test-Path $TargetDir)) {
New-Item -ItemType Directory -Path $TargetDir -Force | Out-Null
}
# Удаляем текущие скиллы (по именам из templates)
foreach ($skillDir in Get-ChildItem -Path $TemplatesDir -Directory) {
if ($skillDir.Name.StartsWith('_')) { continue }
$target = Join-Path $TargetDir $skillDir.Name
if (Test-Path $target) {
Remove-Item -Recurse -Force $target
}
}
# Удаляем legacy-скиллы
foreach ($name in $LegacySkills) {
$target = Join-Path $TargetDir $name
if (Test-Path $target) {
Remove-Item -Recurse -Force $target
}
}
}
function Resolve-PlatformIncludes {
param(
[string]$File,
[string]$BaseDir,
[string]$Platform
)
$content = Get-Content -Path $File -Raw -Encoding UTF8
while ($content -match '\{\{@platform-include:([^}]+)\}\}') {
$includeName = $Matches[1]
$fullPath = Join-Path $BaseDir "_shared\${includeName}-${Platform}.md"
if (-not (Test-Path $fullPath)) {
Write-Error "Error: platform include file not found: $fullPath"
exit 1
}
$includeContent = Get-Content -Path $fullPath -Raw -Encoding UTF8
# Убираем trailing newline из включаемого файла, чтобы не дублировать
$includeContent = $includeContent.TrimEnd("`r", "`n")
$placeholder = "{{@platform-include:${includeName}}}"
# Заменяем всю строку с placeholder на содержимое файла
$content = $content -replace [regex]::Escape($placeholder), $includeContent
}
Set-Content -Path $File -Value $content -NoNewline -Encoding UTF8
}
function Resolve-Includes {
param(
[string]$File,
[string]$BaseDir
)
$content = Get-Content -Path $File -Raw -Encoding UTF8
while ($content -match '\{\{@include:([^}]+)\}\}') {
$includePath = $Matches[1]
$fullPath = Join-Path $BaseDir $includePath
if (-not (Test-Path $fullPath)) {
Write-Error "Error: include file not found: $fullPath"
exit 1
}
$includeContent = Get-Content -Path $fullPath -Raw -Encoding UTF8
$includeContent = $includeContent.TrimEnd("`r", "`n")
$placeholder = "{{@include:${includePath}}}"
$content = $content -replace [regex]::Escape($placeholder), $includeContent
}
Set-Content -Path $File -Value $content -NoNewline -Encoding UTF8
}
function Invoke-Substitute {
param(
[string]$Template,
[string]$EnvFile,
[string]$Output,
[string]$Platform
)
Copy-Item -Path $Template -Destination $Output -Force
Resolve-PlatformIncludes -File $Output -BaseDir $TemplatesDir -Platform $Platform
Resolve-Includes -File $Output -BaseDir $TemplatesDir
# Подставляем переменные из env-файла
$content = Get-Content -Path $Output -Raw -Encoding UTF8
foreach ($line in Get-Content -Path $EnvFile -Encoding UTF8) {
$line = $line.Trim()
if ([string]::IsNullOrEmpty($line) -or $line.StartsWith('#')) { continue }
$eqIndex = $line.IndexOf('=')
if ($eqIndex -lt 0) { continue }
$key = $line.Substring(0, $eqIndex)
$value = $line.Substring($eqIndex + 1)
$placeholder = "{{${key}}}"
if ([string]::IsNullOrEmpty($value)) {
# Пустое значение — удаляем строки с placeholder
$content = ($content -split "`n" | Where-Object { $_ -notmatch [regex]::Escape($placeholder) }) -join "`n"
}
else {
$content = $content.Replace($placeholder, $value)
}
}
Set-Content -Path $Output -Value $content -NoNewline -Encoding UTF8
}
function Invoke-Generate {
param(
[string]$Platform,
[string]$OutputDir
)
$envFile = Join-Path $PlatformsDir "${Platform}.env"
if (-not (Test-Path $envFile)) {
Write-Error "Error: platform file not found: $envFile"
exit 1
}
Write-Host "Generating skills for $Platform..."
if (-not (Test-Path $OutputDir)) {
New-Item -ItemType Directory -Path $OutputDir -Force | Out-Null
}
# Обрабатываем каждую директорию шаблонов (пропускаем _shared и прочие _*)
foreach ($skillDir in Get-ChildItem -Path $TemplatesDir -Directory) {
if ($skillDir.Name.StartsWith('_')) { continue }
$outSkillDir = Join-Path $OutputDir $skillDir.Name
if (-not (Test-Path $outSkillDir)) {
New-Item -ItemType Directory -Path $outSkillDir -Force | Out-Null
}
foreach ($templateFile in Get-ChildItem -Path $skillDir.FullName -Filter '*.md') {
Invoke-Substitute `
-Template $templateFile.FullName `
-EnvFile $envFile `
-Output (Join-Path $outSkillDir $templateFile.Name) `
-Platform $Platform
}
Write-Host " $($skillDir.Name): done"
}
Write-Host "Generated to: $OutputDir"
}
function Test-GeneratedMarkdown {
param(
[string]$Platform,
[string]$OutputDir
)
foreach ($file in Get-ChildItem -Path $OutputDir -Recurse -File -Filter '*.md') {
$content = Get-Content -Path $file.FullName -Raw -Encoding UTF8
if ($content -match '\{\{@include:|\{\{@platform-include:|\{\{[A-Z][A-Z0-9_]*\}\}') {
throw "Error: unresolved template markers in $Platform output: $($file.FullName)"
}
}
$requiredMarkers = @(
@{ Path = 'cp-review/SKILL.md'; Marker = 'requires_deep_compliance'; Error = 'missing requires_deep_compliance marker' },
@{ Path = 'cp-review/SKILL.md'; Marker = 'prepared_context'; Error = 'missing prepared_context marker' },
@{ Path = 'cp-review/SKILL.md'; Marker = 'architecture_risk'; Error = 'missing architecture_risk marker' },
@{ Path = 'cp-review/SKILL.md'; Marker = 'compare every extracted requirement locally'; Error = 'missing local comparison marker' },
@{ Path = 'cp-review/SKILL.md'; Marker = 'does not cancel independent quality review'; Error = 'missing full-review compliance marker' },
@{ Path = 'cp-review/SKILL.md'; Marker = 'explicit verdict for every dimension'; Error = 'missing grouped verdict coverage' },
@{ Path = 'cp-review/SKILL.md'; Marker = 'independent security review'; Error = 'missing independent security routing' },
@{ Path = 'cp-fix/SKILL.md'; Marker = 'Fix Decision Brief'; Error = 'missing Fix Decision Brief' },
@{ Path = 'cp-fix/SKILL.md'; Marker = 'Manual Per Item Gate'; Error = 'missing Manual Per Item Gate' },
@{ Path = 'cp-fix/SKILL.md'; Marker = 'auto safe fixes'; Error = 'missing safe-auto policy' },
@{ Path = 'cp-fix/SKILL.md'; Marker = 'Fix Intake Gate'; Error = 'missing fix intake gate' },
@{ Path = 'cp-fix/SKILL.md'; Marker = 'generic request to start fixing is not a selected policy'; Error = 'missing explicit-policy gate' },
@{ Path = 'cp-review/SKILL.md'; Marker = 'Tool unavailability never authorizes'; Error = 'missing review question fallback' },
@{ Path = 'cp-review/SKILL.md'; Marker = 'applicable_handoffs'; Error = 'missing handoff review context' },
@{ Path = 'cp-fix/SKILL.md'; Marker = 'Numbered options are examples, not a required response format'; Error = 'missing flexible fix-policy input' },
@{ Path = 'using-codepatrol/SKILL.md'; Marker = 'Implementation Handoff'; Error = 'missing implementation handoff contract' },
@{ Path = 'using-codepatrol/SKILL.md'; Marker = 'prepared planning context'; Error = 'missing prepared planning context' },
@{ Path = 'using-codepatrol/SKILL.md'; Marker = 'Planning check: classify the request before loading `brainstorming`.'; Error = 'missing direct-request planning gate' },
@{ Path = 'using-codepatrol/SKILL.md'; Marker = 'Discover optional project inputs with `Glob` before reading.'; Error = 'missing optional-input discovery rule' },
@{ Path = 'using-codepatrol/SKILL.md'; Marker = 'Creative ideation is not engineering planning.'; Error = 'missing creative-work routing boundary' },
@{ Path = 'cp-docs/SKILL.md'; Marker = 'Temporary working notes, task'; Error = 'missing non-documentation scope boundary' },
@{ Path = 'cp-review/SKILL.md'; Marker = 'Discover optional project-rule files, `.ai/docs/README.md`, and `.ai/tasks/` artifacts with `Glob` before'; Error = 'missing optional-input discovery rule' },
@{ Path = 'cp-fix/SKILL.md'; Marker = 'Discover project-rule files with `Glob` before reading.'; Error = 'missing rule discovery guard' }
)
foreach ($check in $requiredMarkers) {
$file = Join-Path $OutputDir $check.Path
$content = Get-Content -Path $file -Raw -Encoding UTF8
if (-not $content.Contains($check.Marker)) {
throw "Error: $($check.Error) in $Platform $($check.Path)"
}
}
}
function Invoke-Validate {
$tmpDir = Join-Path ([System.IO.Path]::GetTempPath()) "codepatrol-validate-$(Get-Random)"
New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null
try {
foreach ($platform in @('claude', 'codex', 'cursor', 'omp', 'opencode')) {
$outputDir = Join-Path $tmpDir $platform
Invoke-Generate -Platform $platform -OutputDir $outputDir
Test-GeneratedMarkdown -Platform $platform -OutputDir $outputDir
}
Write-Host 'Validation passed.'
}
finally {
if (Test-Path $tmpDir) {
Remove-Item -Recurse -Force $tmpDir
}
}
}
# --- Main ---
if (-not $Command) {
Show-Usage
}
switch ($Command) {
'build' {
if (Test-Path $SkillsDir) {
Remove-Item -Recurse -Force $SkillsDir
}
Invoke-Generate -Platform 'claude' -OutputDir $SkillsDir
}
'validate' {
Invoke-Validate
}
'claude' {
$localDir = Join-Path $HOME '.claude\skills'
if (Test-Path $SkillsDir) {
Remove-Item -Recurse -Force $SkillsDir
}
Invoke-Generate -Platform 'claude' -OutputDir $SkillsDir
Remove-InstalledSkills -TargetDir $localDir
foreach ($skillDir in Get-ChildItem -Path $SkillsDir -Directory) {
$target = Join-Path $localDir $skillDir.Name
Copy-Item -Recurse -Force -Path $skillDir.FullName -Destination $target
Write-Host "Installed: $target"
}
}
'codex' {
$localDir = Join-Path $HOME '.agents\skills'
$legacyDir = Join-Path $HOME '.codex\skills'
$tmpDir = Join-Path ([System.IO.Path]::GetTempPath()) "codepatrol-$(Get-Random)"
New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null
Invoke-Generate -Platform 'codex' -OutputDir $tmpDir
Remove-InstalledSkills -TargetDir $legacyDir
Remove-InstalledSkills -TargetDir $localDir
foreach ($skillDir in Get-ChildItem -Path $tmpDir -Directory) {
$target = Join-Path $localDir $skillDir.Name
Copy-Item -Recurse -Force -Path $skillDir.FullName -Destination $target
Write-Host "Installed: $target"
}
Remove-Item -Recurse -Force $tmpDir
}
'cursor' {
$localDir = Join-Path $HOME '.cursor\skills'
$tmpDir = Join-Path ([System.IO.Path]::GetTempPath()) "codepatrol-$(Get-Random)"
New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null
Invoke-Generate -Platform 'cursor' -OutputDir $tmpDir
Remove-InstalledSkills -TargetDir $localDir
foreach ($skillDir in Get-ChildItem -Path $tmpDir -Directory) {
$target = Join-Path $localDir $skillDir.Name
Copy-Item -Recurse -Force -Path $skillDir.FullName -Destination $target
Write-Host "Installed: $target"
}
Remove-Item -Recurse -Force $tmpDir
}
'opencode' {
$localDir = Join-Path $HOME '.config\opencode\skills'
$tmpDir = Join-Path ([System.IO.Path]::GetTempPath()) "codepatrol-$(Get-Random)"
New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null
Invoke-Generate -Platform 'opencode' -OutputDir $tmpDir
Remove-InstalledSkills -TargetDir $localDir
foreach ($skillDir in Get-ChildItem -Path $tmpDir -Directory) {
$target = Join-Path $localDir $skillDir.Name
Copy-Item -Recurse -Force -Path $skillDir.FullName -Destination $target
Write-Host "Installed: $target"
}
Remove-Item -Recurse -Force $tmpDir
}
'omp' {
if (-not (Get-Command omp -ErrorAction SilentlyContinue)) {
throw 'omp is required for OMP installation'
}
& omp install $ScriptDir
}
}
# Очистка клонированного репо
if ($ClonedDir -and (Test-Path $ClonedDir)) {
Remove-Item -Recurse -Force $ClonedDir
}
Write-Host "Done."