|
| 1 | +param( |
| 2 | + [Parameter(Mandatory = $true)] |
| 3 | + [string]$ProductId, |
| 4 | + |
| 5 | + [Parameter(Mandatory = $true)] |
| 6 | + [string]$PackagePath, |
| 7 | + |
| 8 | + [string]$WhatsNewPath = "STORE_WHATS_NEW.txt", |
| 9 | + [string]$StoreListingPath = "docs/STORE_LISTING.md" |
| 10 | +) |
| 11 | + |
| 12 | +$ErrorActionPreference = "Stop" |
| 13 | + |
| 14 | +function Invoke-MsStoreCommand { |
| 15 | + param( |
| 16 | + [Parameter(Mandatory = $true)] |
| 17 | + [string]$Description, |
| 18 | + |
| 19 | + [Parameter(Mandatory = $true)] |
| 20 | + [string[]]$Arguments, |
| 21 | + |
| 22 | + [int]$Attempts = 1, |
| 23 | + [switch]$AllowFailure |
| 24 | + ) |
| 25 | + |
| 26 | + for ($attempt = 1; $attempt -le $Attempts; $attempt++) { |
| 27 | + Write-Host "::group::$Description (attempt $attempt/$Attempts)" |
| 28 | + & msstore @Arguments |
| 29 | + $exitCode = $LASTEXITCODE |
| 30 | + Write-Host "::endgroup::" |
| 31 | + |
| 32 | + if ($exitCode -eq 0) { |
| 33 | + return $true |
| 34 | + } |
| 35 | + |
| 36 | + if ($attempt -lt $Attempts) { |
| 37 | + $delay = [Math]::Min(60, 10 * $attempt) |
| 38 | + Write-Host "::warning::$Description failed with exit code $exitCode; retrying in $delay seconds." |
| 39 | + Start-Sleep -Seconds $delay |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if ($AllowFailure) { |
| 44 | + Write-Host "::warning::$Description failed after $Attempts attempt(s); continuing." |
| 45 | + return $false |
| 46 | + } |
| 47 | + |
| 48 | + throw "$Description failed after $Attempts attempt(s)." |
| 49 | +} |
| 50 | + |
| 51 | +function Remove-PendingStoreSubmission { |
| 52 | + Invoke-MsStoreCommand ` |
| 53 | + -Description "Delete pending Microsoft Store submission" ` |
| 54 | + -Arguments @("submission", "delete", $ProductId, "--no-confirm", "--verbose") ` |
| 55 | + -AllowFailure | Out-Null |
| 56 | +} |
| 57 | + |
| 58 | +function Stage-StorePackage { |
| 59 | + Invoke-MsStoreCommand ` |
| 60 | + -Description "Stage Microsoft Store package" ` |
| 61 | + -Arguments @("publish", $PackagePath, "-id", $ProductId, "--noCommit", "--verbose") ` |
| 62 | + -Attempts 2 | Out-Null |
| 63 | +} |
| 64 | + |
| 65 | +function Publish-StoreSubmission { |
| 66 | + Invoke-MsStoreCommand ` |
| 67 | + -Description "Publish Microsoft Store submission" ` |
| 68 | + -Arguments @("submission", "publish", $ProductId, "--verbose") ` |
| 69 | + -Attempts 2 | Out-Null |
| 70 | +} |
| 71 | + |
| 72 | +function Get-StoreListingBlock { |
| 73 | + param([Parameter(Mandatory = $true)][string]$HeadingPattern) |
| 74 | + |
| 75 | + if (-not (Test-Path $StoreListingPath)) { |
| 76 | + return "" |
| 77 | + } |
| 78 | + |
| 79 | + $lines = Get-Content $StoreListingPath -Encoding utf8 |
| 80 | + $inSection = $false |
| 81 | + $inFence = $false |
| 82 | + $block = New-Object System.Collections.Generic.List[string] |
| 83 | + |
| 84 | + foreach ($line in $lines) { |
| 85 | + if (-not $inSection) { |
| 86 | + if ($line -match $HeadingPattern) { |
| 87 | + $inSection = $true |
| 88 | + } |
| 89 | + continue |
| 90 | + } |
| 91 | + if ($line -match '^```') { |
| 92 | + if ($inFence) { |
| 93 | + break |
| 94 | + } |
| 95 | + $inFence = $true |
| 96 | + continue |
| 97 | + } |
| 98 | + if ($inFence) { |
| 99 | + $block.Add($line) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return (($block -join "`n").Trim()) |
| 104 | +} |
| 105 | + |
| 106 | +function Set-ListingText { |
| 107 | + param( |
| 108 | + $Node, |
| 109 | + [string]$ReleaseNotes, |
| 110 | + [string]$Description, |
| 111 | + [string]$ShortDescription |
| 112 | + ) |
| 113 | + |
| 114 | + if ($null -eq $Node) { |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + if ($Node -is [pscustomobject]) { |
| 119 | + foreach ($property in $Node.PSObject.Properties) { |
| 120 | + if ($property.Name -eq "releaseNotes") { |
| 121 | + $property.Value = $ReleaseNotes |
| 122 | + $script:ReleaseNotesUpdated++ |
| 123 | + } elseif ($Description -and $property.Name -eq "description") { |
| 124 | + $property.Value = $Description |
| 125 | + $script:DescriptionUpdated++ |
| 126 | + } elseif ($ShortDescription -and $property.Name -eq "shortDescription") { |
| 127 | + $property.Value = $ShortDescription |
| 128 | + $script:ShortDescriptionUpdated++ |
| 129 | + } else { |
| 130 | + Set-ListingText $property.Value $ReleaseNotes $Description $ShortDescription |
| 131 | + } |
| 132 | + } |
| 133 | + } elseif ($Node -is [System.Collections.IEnumerable] -and $Node -isnot [string]) { |
| 134 | + foreach ($item in $Node) { |
| 135 | + Set-ListingText $item $ReleaseNotes $Description $ShortDescription |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +function Update-StoreMetadata { |
| 141 | + if (-not (Test-Path $WhatsNewPath)) { |
| 142 | + Write-Host "::warning::$WhatsNewPath not found; Store What's New left unchanged." |
| 143 | + return $true |
| 144 | + } |
| 145 | + |
| 146 | + $notes = (Get-Content $WhatsNewPath -Raw).Trim() |
| 147 | + if (-not $notes) { |
| 148 | + Write-Host "::warning::Store What's New is empty; Store metadata left unchanged." |
| 149 | + return $true |
| 150 | + } |
| 151 | + |
| 152 | + $storeDescription = Get-StoreListingBlock '^## Description ' |
| 153 | + $storeShortDescription = Get-StoreListingBlock '^## Short description ' |
| 154 | + |
| 155 | + $raw = (msstore submission get $ProductId --verbose | Out-String) |
| 156 | + if ($LASTEXITCODE -ne 0) { |
| 157 | + throw "msstore submission get failed with exit code $LASTEXITCODE" |
| 158 | + } |
| 159 | + |
| 160 | + $open = $raw.IndexOf('{') |
| 161 | + $close = $raw.LastIndexOf('}') |
| 162 | + if ($open -lt 0 -or $close -lt $open) { |
| 163 | + throw "submission get returned no JSON object" |
| 164 | + } |
| 165 | + |
| 166 | + $json = $raw.Substring($open, $close - $open + 1) | ConvertFrom-Json |
| 167 | + $script:ReleaseNotesUpdated = 0 |
| 168 | + $script:DescriptionUpdated = 0 |
| 169 | + $script:ShortDescriptionUpdated = 0 |
| 170 | + Set-ListingText $json $notes $storeDescription $storeShortDescription |
| 171 | + |
| 172 | + if ($script:ReleaseNotesUpdated -le 0) { |
| 173 | + Write-Host "::warning::No 'releaseNotes' field found in submission metadata; What's New left unchanged." |
| 174 | + return $true |
| 175 | + } |
| 176 | + |
| 177 | + $metadata = $json | ConvertTo-Json -Depth 50 -Compress |
| 178 | + $ok = Invoke-MsStoreCommand ` |
| 179 | + -Description "Update Microsoft Store metadata" ` |
| 180 | + -Arguments @("submission", "updateMetadata", $ProductId, $metadata, "--verbose") ` |
| 181 | + -Attempts 2 ` |
| 182 | + -AllowFailure |
| 183 | + |
| 184 | + if ($ok) { |
| 185 | + Write-Host "Updated What's New in $($script:ReleaseNotesUpdated) listing(s)." |
| 186 | + Write-Host "Updated Store description in $($script:DescriptionUpdated) listing(s)." |
| 187 | + Write-Host "Updated Store short description in $($script:ShortDescriptionUpdated) listing(s)." |
| 188 | + } |
| 189 | + |
| 190 | + return $ok |
| 191 | +} |
| 192 | + |
| 193 | +try { |
| 194 | + Stage-StorePackage |
| 195 | +} catch { |
| 196 | + Write-Host "::warning::Initial package staging failed: $($_.Exception.Message)" |
| 197 | + Remove-PendingStoreSubmission |
| 198 | + Stage-StorePackage |
| 199 | +} |
| 200 | + |
| 201 | +$metadataUpdated = $false |
| 202 | +try { |
| 203 | + $metadataUpdated = Update-StoreMetadata |
| 204 | +} catch { |
| 205 | + Write-Host "::warning::Store metadata update failed: $($_.Exception.Message)" |
| 206 | +} |
| 207 | + |
| 208 | +if (-not $metadataUpdated) { |
| 209 | + Write-Host "::warning::Restaging package without metadata update to recover from a rejected draft." |
| 210 | + Remove-PendingStoreSubmission |
| 211 | + Stage-StorePackage |
| 212 | +} |
| 213 | + |
| 214 | +try { |
| 215 | + Publish-StoreSubmission |
| 216 | +} catch { |
| 217 | + Write-Host "::warning::Submission publish failed: $($_.Exception.Message)" |
| 218 | + Remove-PendingStoreSubmission |
| 219 | + Stage-StorePackage |
| 220 | + Publish-StoreSubmission |
| 221 | +} |
0 commit comments