[CmdletBinding()]
|
param(
|
[Parameter()]
|
[string]$CodexHome
|
)
|
|
Set-StrictMode -Version Latest
|
$ErrorActionPreference = 'Stop'
|
|
if ([string]::IsNullOrWhiteSpace($CodexHome)) {
|
$CodexHome = if ([string]::IsNullOrWhiteSpace($env:CODEX_HOME)) {
|
Join-Path ([Environment]::GetFolderPath('UserProfile')) '.codex'
|
}
|
else {
|
$env:CODEX_HOME
|
}
|
}
|
|
$suiteRoot = [IO.Path]::GetFullPath($PSScriptRoot)
|
$sourceSkillsRoot = Join-Path $suiteRoot 'skills'
|
$sourceSharedRoot = Join-Path $suiteRoot 'shared'
|
$sourceBundle = Join-Path $suiteRoot 'bundle.yaml'
|
$targetCodexRoot = [IO.Path]::GetFullPath($CodexHome)
|
$targetSkillsRoot = [IO.Path]::GetFullPath((Join-Path $targetCodexRoot 'skills'))
|
$skillNames = @(
|
'ai-agent-system-bootstrap'
|
'ai-agent-system-inspect'
|
'ai-agent-system-design'
|
'ai-agent-system-render'
|
'ai-agent-system-validate'
|
)
|
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
|
|
foreach ($requiredPath in @($sourceSkillsRoot, $sourceSharedRoot, $sourceBundle)) {
|
if (-not (Test-Path -LiteralPath $requiredPath)) {
|
throw "Required suite path is missing: $requiredPath"
|
}
|
}
|
|
New-Item -ItemType Directory -Path $targetSkillsRoot -Force | Out-Null
|
$resolvedSkillsRoot = (Get-Item -LiteralPath $targetSkillsRoot).FullName.TrimEnd('\', '/')
|
|
foreach ($skillName in $skillNames) {
|
$sourceSkill = Join-Path $sourceSkillsRoot $skillName
|
$sourceSkillFile = Join-Path $sourceSkill 'SKILL.md'
|
if (-not (Test-Path -LiteralPath $sourceSkillFile -PathType Leaf)) {
|
throw "Skill source is incomplete: $sourceSkillFile"
|
}
|
|
$targetSkill = [IO.Path]::GetFullPath((Join-Path $resolvedSkillsRoot $skillName))
|
if ([IO.Path]::GetDirectoryName($targetSkill).TrimEnd('\', '/') -ne $resolvedSkillsRoot) {
|
throw "Refusing to install outside the global Skill directory: $targetSkill"
|
}
|
|
if (Test-Path -LiteralPath $targetSkill) {
|
$existing = Get-Item -LiteralPath $targetSkill -Force
|
if (-not $existing.PSIsContainer) {
|
throw "Skill target exists and is not a directory: $targetSkill"
|
}
|
if (($existing.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) {
|
throw "Skill target is a symlink or junction; remove it manually first: $targetSkill"
|
}
|
Remove-Item -LiteralPath $targetSkill -Recurse -Force
|
}
|
|
New-Item -ItemType Directory -Path $targetSkill | Out-Null
|
Copy-Item -LiteralPath (Join-Path $sourceSkill 'SKILL.md') -Destination $targetSkill
|
if (Test-Path -LiteralPath (Join-Path $sourceSkill 'agents')) {
|
Copy-Item -LiteralPath (Join-Path $sourceSkill 'agents') -Destination $targetSkill -Recurse
|
}
|
Copy-Item -LiteralPath $sourceSharedRoot -Destination $targetSkill -Recurse
|
Copy-Item -LiteralPath $sourceBundle -Destination (Join-Path $targetSkill 'bundle.yaml')
|
|
$installedSkillFile = Join-Path $targetSkill 'SKILL.md'
|
$skillText = [IO.File]::ReadAllText($installedSkillFile)
|
$skillText = $skillText.Replace('../../shared/', 'shared/').Replace('../../bundle.yaml', 'bundle.yaml')
|
[IO.File]::WriteAllText($installedSkillFile, $skillText, $utf8NoBom)
|
|
if ($skillText -notmatch "(?m)^name:\s*$([regex]::Escape($skillName))\s*$") {
|
throw "Installed Skill frontmatter name does not match its directory: $installedSkillFile"
|
}
|
|
$localLinks = [regex]::Matches($skillText, '\]\((?!https?://|#)([^)]+)\)')
|
foreach ($match in $localLinks) {
|
$relativePath = $match.Groups[1].Value.Replace('/', [IO.Path]::DirectorySeparatorChar)
|
$linkedPath = [IO.Path]::GetFullPath((Join-Path $targetSkill $relativePath))
|
if (-not (Test-Path -LiteralPath $linkedPath)) {
|
throw "Installed Skill contains a broken local reference: $($match.Groups[1].Value)"
|
}
|
}
|
|
Write-Host "Installed $skillName -> $targetSkill"
|
}
|
|
Write-Host "Installed $($skillNames.Count) Skills into $resolvedSkillsRoot"
|
Write-Host 'The updated Skills are available to new Codex turns.'
|