playbook/scripts/install_codex_skills.ps1

76 lines
2.2 KiB
PowerShell

# Install Codex skills from this Playbook snapshot into CODEX_HOME.
# - Source: <snapshot>\codex\skills\<skill-name>\
# - Dest: $env:CODEX_HOME\skills\<skill-name>\ (default CODEX_HOME=$HOME\.codex)
#
# Usage:
# powershell -File scripts/install_codex_skills.ps1
# powershell -File scripts/install_codex_skills.ps1 style-cleanup code-review-workflow
# powershell -File scripts/install_codex_skills.ps1 -Local
#
# Notes:
# - Codex loads skills at startup; restart `codex` after installation.
# - Existing destination skill dirs are backed up with a timestamp suffix.
[CmdletBinding()]
param(
[switch]$Local,
[Parameter(Mandatory = $false, ValueFromRemainingArguments = $true)]
[string[]]$Skills
)
$ErrorActionPreference = "Stop"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$Src = (Resolve-Path (Join-Path $ScriptDir "..")).Path
$SkillsSrcRoot = Join-Path $Src "codex/skills"
if (-not (Test-Path $SkillsSrcRoot)) {
throw "Skills source dir not found: $SkillsSrcRoot"
}
$CodexHome = $env:CODEX_HOME
if ($Local) {
$localHome = Join-Path (Get-Location) ".codex"
if (-not $CodexHome) { $CodexHome = $localHome }
}
if (-not $CodexHome) {
$homeDir = $HOME
if (-not $homeDir) { $homeDir = $env:USERPROFILE }
$CodexHome = (Join-Path $homeDir ".codex")
}
$SkillsDstRoot = Join-Path $CodexHome "skills"
New-Item -ItemType Directory -Path $SkillsDstRoot -Force | Out-Null
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
function Install-One([string]$Name) {
$srcDir = Join-Path $SkillsSrcRoot $Name
$dstDir = Join-Path $SkillsDstRoot $Name
if (-not (Test-Path $srcDir)) {
throw "Skill not found: $Name ($srcDir)"
}
if (Test-Path $dstDir) {
$bak = Join-Path $SkillsDstRoot "$Name.bak.$timestamp"
Move-Item $dstDir $bak
Write-Host "Backed up existing skill: $Name -> $(Split-Path -Leaf $bak)"
}
Copy-Item $srcDir $dstDir -Recurse -Force
Write-Host "Installed: $Name"
}
if ($Skills -and $Skills.Count -gt 0) {
foreach ($name in $Skills) {
if (-not $name) { continue }
Install-One $name
}
} else {
foreach ($dir in (Get-ChildItem -Path $SkillsSrcRoot -Directory)) {
if ($dir.Name.StartsWith(".")) { continue }
Install-One $dir.Name
}
}
Write-Host "Done. Skills installed to: $SkillsDstRoot"