|
| 1 | +param( |
| 2 | + [string]$Destination = (Join-Path $HOME ".agents\skills") |
| 3 | +) |
| 4 | + |
| 5 | +$ErrorActionPreference = "Stop" |
| 6 | +$RepoUrl = "https://github.com/hoavdc/CodexKit" |
| 7 | +$ApiUrl = "https://api.github.com/repos/hoavdc/CodexKit/releases/latest" |
| 8 | +$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 9 | +$RootDir = Split-Path -Parent $ScriptDir |
| 10 | +$CurrentVersion = "" |
| 11 | + |
| 12 | +# ── Detect current version ── |
| 13 | +$pkgPath = Join-Path $RootDir "package.json" |
| 14 | +if (Test-Path $pkgPath) { |
| 15 | + $pkg = Get-Content $pkgPath -Raw | ConvertFrom-Json |
| 16 | + $CurrentVersion = $pkg.version |
| 17 | +} |
| 18 | + |
| 19 | +Write-Host "" |
| 20 | +Write-Host "================================================" -ForegroundColor Cyan |
| 21 | +Write-Host " CodexKit Updater " -ForegroundColor Cyan |
| 22 | +Write-Host "================================================" -ForegroundColor Cyan |
| 23 | +Write-Host "" |
| 24 | +if ($CurrentVersion) { Write-Host "Current version: v$CurrentVersion" } |
| 25 | +Write-Host "Skills destination: $Destination" |
| 26 | +Write-Host "" |
| 27 | + |
| 28 | +# ── Detect update method ── |
| 29 | +$gitDir = Join-Path $RootDir ".git" |
| 30 | +if (Test-Path $gitDir) { |
| 31 | + Write-Host "[GIT] Git repository detected. Updating via git pull..." -ForegroundColor Green |
| 32 | + Write-Host "" |
| 33 | + |
| 34 | + Push-Location $RootDir |
| 35 | + |
| 36 | + # Check for uncommitted changes |
| 37 | + $status = git status --porcelain 2>$null |
| 38 | + $stashed = $false |
| 39 | + if ($status) { |
| 40 | + Write-Host "Warning: You have uncommitted changes. Stashing them..." -ForegroundColor Yellow |
| 41 | + git stash |
| 42 | + $stashed = $true |
| 43 | + } |
| 44 | + |
| 45 | + # Pull latest |
| 46 | + $before = git rev-parse HEAD |
| 47 | + try { |
| 48 | + git pull --rebase origin main 2>&1 |
| 49 | + } catch { |
| 50 | + git pull origin main 2>&1 |
| 51 | + } |
| 52 | + $after = git rev-parse HEAD |
| 53 | + |
| 54 | + if ($before -eq $after) { |
| 55 | + Write-Host "" |
| 56 | + Write-Host "Already up to date!" -ForegroundColor Green |
| 57 | + } else { |
| 58 | + Write-Host "" |
| 59 | + Write-Host "Updated successfully!" -ForegroundColor Green |
| 60 | + Write-Host "" |
| 61 | + Write-Host "Changes:" |
| 62 | + git log --oneline "$before..$after" 2>$null |
| 63 | + } |
| 64 | + |
| 65 | + # Restore stashed changes |
| 66 | + if ($stashed) { |
| 67 | + Write-Host "" |
| 68 | + Write-Host "Restoring your local changes..." |
| 69 | + try { git stash pop } catch { Write-Warning "Could not auto-restore stash. Run 'git stash pop' manually." } |
| 70 | + } |
| 71 | + |
| 72 | + # Re-read version |
| 73 | + if (Test-Path $pkgPath) { |
| 74 | + $pkg = Get-Content $pkgPath -Raw | ConvertFrom-Json |
| 75 | + $LatestVersion = $pkg.version |
| 76 | + Write-Host "" |
| 77 | + Write-Host "Version: v$CurrentVersion -> v$LatestVersion" -ForegroundColor Green |
| 78 | + } |
| 79 | + |
| 80 | + Pop-Location |
| 81 | + |
| 82 | + # Re-install skills |
| 83 | + Write-Host "" |
| 84 | + Write-Host "Re-installing skills..." -ForegroundColor Cyan |
| 85 | + & "$RootDir\scripts\install-skills.ps1" -Destination $Destination -Force |
| 86 | + |
| 87 | +} else { |
| 88 | + # ── Non-git: Download latest release ── |
| 89 | + Write-Host "[DOWNLOAD] No git repo found. Checking GitHub for latest release..." -ForegroundColor Yellow |
| 90 | + Write-Host "" |
| 91 | + |
| 92 | + try { |
| 93 | + $headers = @{ "User-Agent" = "CodexKit-Updater" } |
| 94 | + $release = Invoke-RestMethod -Uri $ApiUrl -Headers $headers -TimeoutSec 15 |
| 95 | + $LatestVersion = $release.tag_name -replace '^v', '' |
| 96 | + } catch { |
| 97 | + Write-Host "Could not fetch latest version from GitHub." -ForegroundColor Red |
| 98 | + Write-Host "Check your internet connection or visit: $RepoUrl/releases" |
| 99 | + exit 1 |
| 100 | + } |
| 101 | + |
| 102 | + Write-Host "Latest version: v$LatestVersion" |
| 103 | + |
| 104 | + if ($CurrentVersion -eq $LatestVersion) { |
| 105 | + Write-Host "You are already on the latest version!" -ForegroundColor Green |
| 106 | + Write-Host "" |
| 107 | + Write-Host "Re-installing skills anyway..." |
| 108 | + & "$RootDir\scripts\install-skills.ps1" -Destination $Destination -Force |
| 109 | + exit 0 |
| 110 | + } |
| 111 | + |
| 112 | + # Download latest starter pack |
| 113 | + $downloadUrl = "$RepoUrl/releases/download/v$LatestVersion/codexkit-starter-pack-v$LatestVersion.zip" |
| 114 | + $tmpDir = Join-Path $env:TEMP "codexkit-update-$(Get-Random)" |
| 115 | + $zipFile = Join-Path $tmpDir "codexkit.zip" |
| 116 | + $extractDir = Join-Path $tmpDir "extracted" |
| 117 | + |
| 118 | + New-Item -ItemType Directory -Force -Path $tmpDir | Out-Null |
| 119 | + |
| 120 | + Write-Host "" |
| 121 | + Write-Host "Downloading v$LatestVersion from GitHub..." -ForegroundColor Cyan |
| 122 | + Write-Host " $downloadUrl" |
| 123 | + |
| 124 | + try { |
| 125 | + Invoke-WebRequest -Uri $downloadUrl -OutFile $zipFile -TimeoutSec 120 |
| 126 | + } catch { |
| 127 | + Write-Host "Download failed. Please download manually from:" -ForegroundColor Red |
| 128 | + Write-Host " $RepoUrl/releases" |
| 129 | + Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue |
| 130 | + exit 1 |
| 131 | + } |
| 132 | + |
| 133 | + Write-Host "Extracting..." -ForegroundColor Cyan |
| 134 | + Expand-Archive -Path $zipFile -DestinationPath $extractDir -Force |
| 135 | + |
| 136 | + # Find the extracted skills directory |
| 137 | + $extractedSkills = Get-ChildItem -Path $extractDir -Recurse -Directory -Filter "skills" | Select-Object -First 1 |
| 138 | + if (-not $extractedSkills) { |
| 139 | + Write-Host "Could not find skills directory in the downloaded package." -ForegroundColor Red |
| 140 | + Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue |
| 141 | + exit 1 |
| 142 | + } |
| 143 | + |
| 144 | + # Copy new skills |
| 145 | + Write-Host "Installing new skills from v$LatestVersion..." -ForegroundColor Cyan |
| 146 | + New-Item -ItemType Directory -Force -Path $Destination | Out-Null |
| 147 | + $installedCount = 0 |
| 148 | + |
| 149 | + Get-ChildItem -Path $extractedSkills.FullName -Directory | ForEach-Object { |
| 150 | + $target = Join-Path $Destination $_.Name |
| 151 | + if (Test-Path $target) { Remove-Item -Recurse -Force $target } |
| 152 | + Copy-Item -Recurse -Force $_.FullName $target |
| 153 | + $installedCount++ |
| 154 | + } |
| 155 | + |
| 156 | + Write-Host "Updated $installedCount skills to v$LatestVersion" -ForegroundColor Green |
| 157 | + |
| 158 | + # Update local reference files |
| 159 | + $extractedRoot = Get-ChildItem -Path $extractDir -Directory | Select-Object -First 1 |
| 160 | + if ($extractedRoot) { |
| 161 | + foreach ($file in @("skill-finder.md", "HUONG-DAN-NHANH.md", "CHANGELOG.md")) { |
| 162 | + $src = Join-Path $extractedRoot.FullName $file |
| 163 | + if (Test-Path $src) { |
| 164 | + Copy-Item -Force $src (Join-Path $RootDir $file) -ErrorAction SilentlyContinue |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + # Cleanup |
| 170 | + Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue |
| 171 | +} |
| 172 | + |
| 173 | +Write-Host "" |
| 174 | +Write-Host "================================================" -ForegroundColor Green |
| 175 | +Write-Host " Update complete! " -ForegroundColor Green |
| 176 | +Write-Host "================================================" -ForegroundColor Green |
| 177 | +Write-Host "" |
| 178 | +Write-Host "Next steps:" |
| 179 | +Write-Host " 1. Restart Codex to pick up new/updated skills" |
| 180 | +Write-Host " 2. Type /skills to verify the update" |
| 181 | +Write-Host " 3. Open skill-finder.md to browse new skills" |
| 182 | +Write-Host "" |
0 commit comments