-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpublish.ps1
More file actions
137 lines (124 loc) · 6.67 KB
/
publish.ps1
File metadata and controls
137 lines (124 loc) · 6.67 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
# publish.ps1
# Manual fallback for publishing to the VS Code Marketplace.
#
# PRIMARY METHOD: Use the GitHub Actions Release workflow (workflow_dispatch),
# which handles building, testing, releasing, and publishing in one step.
#
# Use this script only when you need to manually publish a pre-built VSIX
# (e.g., re-publishing after a marketplace upload failure).
#
# Usage:
# .\publish.ps1 -VsixPath ".\copilot-token-tracker-0.0.18.vsix"
param(
[string]$VsixPath # Path to the .vsix downloaded from the GitHub release
)
$ErrorActionPreference = "Stop"
# ── Read version from package.json ───────────────────────────────────────────
$packageJson = Get-Content -Path "package.json" -Raw | ConvertFrom-Json
$version = $packageJson.version
$expectedTag = "v$version"
$owner = "rajbos"
$repo = "github-copilot-token-usage"
$publisherName = "RobBos"
# ── Resolve / prompt for VSIX path ───────────────────────────────────────────
if ([string]::IsNullOrWhiteSpace($VsixPath)) {
# Default: look for a matching VSIX in the current directory
$defaultVsix = ".\copilot-token-tracker-$version.vsix"
if (Test-Path $defaultVsix) {
$VsixPath = $defaultVsix
Write-Host "Found VSIX: $VsixPath" -ForegroundColor Green
} else {
$VsixPath = Read-Host "Enter path to the .vsix downloaded from the GitHub release"
}
}
if (-not (Test-Path $VsixPath)) {
Write-Host "`n❌ VSIX not found: $VsixPath" -ForegroundColor Red
Write-Host " Download it from: https://github.com/$owner/$repo/releases/tag/$expectedTag" -ForegroundColor Yellow
exit 1
}
Write-Host "Using VSIX: $VsixPath" -ForegroundColor Cyan
# ── Step 1: Validate GitHub release exists ────────────────────────────────────
Write-Host "`nValidating GitHub release $expectedTag..." -ForegroundColor Cyan
$apiUrl = "https://api.github.com/repos/$owner/$repo/releases/tags/$expectedTag"
try {
$release = Invoke-RestMethod -Uri $apiUrl -Method Get -Headers @{
"Accept" = "application/vnd.github+json"
"User-Agent" = "PowerShell-Script"
}
Write-Host "✅ GitHub release found: $($release.html_url)" -ForegroundColor Green
} catch {
if ($_.Exception.Response.StatusCode -eq 404) {
Write-Host "❌ GitHub release $expectedTag not found." -ForegroundColor Red
Write-Host " Create it at: https://github.com/$owner/$repo/releases/new" -ForegroundColor Yellow
} else {
Write-Host "❌ Failed to reach GitHub API: $($_.Exception.Message)" -ForegroundColor Red
}
exit 1
}
# ── Step 2: Sync CHANGELOG.md from the release notes ─────────────────────────
Write-Host "`nSyncing CHANGELOG.md from GitHub releases..." -ForegroundColor Cyan
npm run sync-changelog
if ($LASTEXITCODE -ne 0) {
Write-Host "⚠️ CHANGELOG sync failed. Continuing anyway." -ForegroundColor Yellow
}
# ── Step 3: Marketplace login ─────────────────────────────────────────────────
Write-Host "`nChecking marketplace login status..." -ForegroundColor Cyan
$vsceListOutput = npx vsce ls-publishers 2>&1
$isLoggedIn = $LASTEXITCODE -eq 0
if (-not $isLoggedIn) {
Write-Host "You are not logged in to the VS Code Marketplace." -ForegroundColor Yellow
Write-Host ""
Write-Host "To publish extensions you need a Personal Access Token from Azure DevOps:" -ForegroundColor Yellow
Write-Host " 1. Go to https://dev.azure.com" -ForegroundColor Gray
Write-Host " 2. User Settings → Personal Access Tokens" -ForegroundColor Gray
Write-Host " 3. New token with 'Marketplace (Publish)' scope, all organisations" -ForegroundColor Gray
Write-Host ""
$publisher = Read-Host "Enter your publisher name (default: $publisherName)"
if ([string]::IsNullOrWhiteSpace($publisher)) { $publisher = $publisherName }
Write-Host "`nLogging in as '$publisher'..." -ForegroundColor Cyan
npx vsce login $publisher
if ($LASTEXITCODE -ne 0) {
Write-Host "`n❌ Login failed." -ForegroundColor Red
exit 1
}
Write-Host "Login successful!" -ForegroundColor Green
} else {
Write-Host "Already logged in to the marketplace." -ForegroundColor Green
}
# ── Step 3b: Validate PAT ─────────────────────────────────────────────────────
Write-Host "`nValidating Personal Access Token (PAT)..." -ForegroundColor Cyan
$pat = $env:VSCE_PAT
if ([string]::IsNullOrWhiteSpace($pat)) {
Write-Host "VSCE_PAT environment variable is not set." -ForegroundColor Yellow
Write-Host ""
Write-Host "You need a Personal Access Token from Azure DevOps:" -ForegroundColor Yellow
Write-Host " 1. Go to https://dev.azure.com" -ForegroundColor Gray
Write-Host " 2. User Settings → Personal Access Tokens" -ForegroundColor Gray
Write-Host " 3. New token with 'Marketplace (Publish)' scope, all organisations" -ForegroundColor Gray
Write-Host ""
$pat = Read-Host "Enter your PAT (input is hidden)" -AsSecureString | ForEach-Object {
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto(
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($_))
}
if ([string]::IsNullOrWhiteSpace($pat)) {
Write-Host "`n❌ No PAT provided. Cannot publish without a valid token." -ForegroundColor Red
exit 1
}
} else {
Write-Host "✅ VSCE_PAT environment variable found." -ForegroundColor Green
}
# ── Step 4: Publish the downloaded VSIX ──────────────────────────────────────
Write-Host "`nPublishing $VsixPath to the VS Code Marketplace..." -ForegroundColor Cyan
npx vsce publish --packagePath $VsixPath --pat $pat
if ($LASTEXITCODE -eq 0) {
Write-Host "`n✅ Extension published successfully!" -ForegroundColor Green
Write-Host "It may take a few minutes to appear in the marketplace." -ForegroundColor Gray
Write-Host ""
Write-Host "📝 Commit and push the updated CHANGELOG.md:" -ForegroundColor Cyan
Write-Host " git add CHANGELOG.md" -ForegroundColor Gray
Write-Host " git commit -m 'chore: sync changelog for $expectedTag'" -ForegroundColor Gray
Write-Host " git push" -ForegroundColor Gray
} else {
Write-Host "`n❌ Publishing failed. Check the error messages above." -ForegroundColor Red
exit 1
}