Tuesday, November 17, 2026

Script to install pstack-claude from michael-denyer [opencode]

 param([switch]$Force)   # -Force = reinstall even if already up to date


$owner = "michael-denyer"

$repo = "pstack-claude"

$zipUrl = "https://github.com/$owner/$repo/archive/refs/heads/main.zip"

$apiUrl = "https://api.github.com/repos/$owner/$repo/commits/main"

$outputPath = "$PWD\$repo.zip"

$extractPath = "$PWD\$repo-main"

$dest = "$HOME\.agents\skills"

$versionFile = "$HOME\.agents\.pstack-version"


New-Item -ItemType Directory -Force -Path $dest | Out-Null


# Remove a junction safely (does NOT touch the target's contents) or a normal folder

function Remove-SkillEntry($path) {

    $item = Get-Item $path -Force

    if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) {

        cmd /c rmdir "`"$path`"" | Out-Null

    } else {

        Remove-Item $path -Recurse -Force

    }

}


# 0) COMPARE: latest commit on GitHub vs. what is installed

$remoteSha = $null

try {

    $remoteSha = (Invoke-RestMethod -Uri $apiUrl -Headers @{ Accept = "application/vnd.github.sha"; "User-Agent" = "pstack-installer" }).ToString().Trim()

} catch {

    Write-Host "Could not reach GitHub API ($($_.Exception.Message)). Continuing with install." -ForegroundColor Yellow

}

$localSha = if (Test-Path $versionFile) { (Get-Content $versionFile -Raw).Trim() } else { $null }


if ($remoteSha) {

    Write-Host "Installed: $(if ($localSha) { $localSha.Substring(0,7) } else { 'none' })   GitHub: $($remoteSha.Substring(0,7))" -ForegroundColor Cyan

}


# Links still healthy? (junctions pointing into the downloaded folder)

$links = @(Get-ChildItem $dest -Directory -Force -ErrorAction SilentlyContinue | Where-Object {

    ($_.Attributes -band [IO.FileAttributes]::ReparsePoint) -and ($_.Target -like "$extractPath*") -and (Test-Path $_.FullName)

})


if (-not $Force -and $remoteSha -and ($remoteSha -eq $localSha) -and ($links.Count -gt 0)) {

    Write-Host "Already up to date ($($links.Count) skills). Nothing to do. Use -Force to reinstall." -ForegroundColor Green

    return

}


# 1) CLEAN: remove old pstack skill links (junctions that point into the old download)

Write-Host "Cleaning old pstack skills..." -ForegroundColor Yellow

Get-ChildItem $dest -Directory -Force | Where-Object {

    ($_.Attributes -band [IO.FileAttributes]::ReparsePoint) -and ($_.Target -like "$extractPath*")

} | ForEach-Object { Remove-SkillEntry $_.FullName }


if (Test-Path $extractPath) { Remove-Item $extractPath -Recurse -Force }


# 2) DOWNLOAD + EXTRACT

Write-Host "Downloading $repo from GitHub..." -ForegroundColor Cyan

Invoke-WebRequest -Uri $zipUrl -OutFile $outputPath


Write-Host "Extracting files..." -ForegroundColor Cyan

Expand-Archive -Path $outputPath -DestinationPath $PWD -Force

Remove-Item $outputPath


# 3) LOAD NEW: link each skill (any same-named leftover is removed first)

Get-ChildItem "$extractPath\plugins\pstack\skills" -Directory | ForEach-Object {

    $t = Join-Path $dest $_.Name

    if (Test-Path $t) { Remove-SkillEntry $t }

    New-Item -ItemType Junction -Path $t -Value $_.FullName | Out-Null

    Write-Host "  loaded: $($_.Name)" -ForegroundColor Green

}


# 4) Remember installed version

if ($remoteSha) { Set-Content -Path $versionFile -Value $remoteSha -NoNewline }


Write-Host "Done. Skills installed to: $dest" -ForegroundColor Green