microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
scripts/release/Update-VersionFiles.ps1
183lines · modecode
| 1 | #!/usr/bin/env pwsh |
| 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | #Requires -Version 7.0 |
| 5 | |
| 6 | <# |
| 7 | .SYNOPSIS |
| 8 | Updates version strings across all version-tracked files in the repository. |
| 9 | |
| 10 | .DESCRIPTION |
| 11 | Central version bump script called by both release-prerelease-pr.yml and |
| 12 | release-stable.yml workflows. Updates: |
| 13 | |
| 14 | - package.json |
| 15 | - extension/templates/package.template.json |
| 16 | - .github/plugin/marketplace.json (metadata.version and plugins[*].version) |
| 17 | - plugins/*/.github/plugin/plugin.json (glob) |
| 18 | - .release-please-manifest.json |
| 19 | |
| 20 | After updating the files, runs 'npm run plugin:generate' to regenerate |
| 21 | plugin outputs so plugin-validation passes. |
| 22 | |
| 23 | .PARAMETER Version |
| 24 | The version string to write (e.g. '3.3.0'). |
| 25 | |
| 26 | .PARAMETER RepoRoot |
| 27 | Optional. Repository root directory. Defaults to the git working tree root. |
| 28 | |
| 29 | .PARAMETER SkipPluginGenerate |
| 30 | Optional. Skip running 'npm run plugin:generate' after updating files. |
| 31 | |
| 32 | .EXAMPLE |
| 33 | ./Update-VersionFiles.ps1 -Version '3.3.0' |
| 34 | |
| 35 | .EXAMPLE |
| 36 | ./Update-VersionFiles.ps1 -Version '3.3.0' -RepoRoot '/path/to/repo' |
| 37 | |
| 38 | .NOTES |
| 39 | Called by CI workflows. Requires Node.js and npm dependencies installed |
| 40 | when SkipPluginGenerate is not set. |
| 41 | #> |
| 42 | |
| 43 | [CmdletBinding()] |
| 44 | param( |
| 45 | [Parameter(Mandatory = $true)] |
| 46 | [ValidatePattern('^\d+\.\d+\.\d+')] |
| 47 | [string]$Version, |
| 48 | |
| 49 | [Parameter(Mandatory = $false)] |
| 50 | [string]$RepoRoot = "", |
| 51 | |
| 52 | [Parameter(Mandatory = $false)] |
| 53 | [switch]$SkipPluginGenerate |
| 54 | ) |
| 55 | |
| 56 | $ErrorActionPreference = 'Stop' |
| 57 | |
| 58 | #region Helpers |
| 59 | |
| 60 | function Resolve-RepoRoot { |
| 61 | <# |
| 62 | .SYNOPSIS |
| 63 | Resolves the repository root directory. |
| 64 | #> |
| 65 | param([string]$Supplied) |
| 66 | |
| 67 | if ($Supplied) { |
| 68 | return (Resolve-Path $Supplied).Path |
| 69 | } |
| 70 | |
| 71 | # Walk up from script location to find the repo root |
| 72 | $candidate = (Resolve-Path (Join-Path $PSScriptRoot "../..")).Path |
| 73 | if (Test-Path (Join-Path $candidate ".git")) { |
| 74 | return $candidate |
| 75 | } |
| 76 | |
| 77 | throw "Unable to determine repository root. Pass -RepoRoot explicitly." |
| 78 | } |
| 79 | |
| 80 | function Update-JsonVersion { |
| 81 | <# |
| 82 | .SYNOPSIS |
| 83 | Updates a version field in a JSON file using a script block. |
| 84 | #> |
| 85 | param( |
| 86 | [string]$FilePath, |
| 87 | [string]$Description, |
| 88 | [scriptblock]$Transform |
| 89 | ) |
| 90 | |
| 91 | if (-not (Test-Path $FilePath)) { |
| 92 | Write-Host " ⏭️ Skipping $Description — file not found: $FilePath" -ForegroundColor Yellow |
| 93 | return |
| 94 | } |
| 95 | |
| 96 | $json = Get-Content -Raw $FilePath | ConvertFrom-Json -Depth 20 |
| 97 | $json = & $Transform $json |
| 98 | $json | ConvertTo-Json -Depth 20 | Set-Content -Path $FilePath -Encoding UTF8 -NoNewline |
| 99 | Write-Host " ✅ Updated $Description" -ForegroundColor Green |
| 100 | } |
| 101 | |
| 102 | #endregion Helpers |
| 103 | |
| 104 | #region Main |
| 105 | |
| 106 | if ($MyInvocation.InvocationName -ne '.') { |
| 107 | try { |
| 108 | $root = Resolve-RepoRoot -Supplied $RepoRoot |
| 109 | Write-Host "🔄 Updating version files to $Version" -ForegroundColor Cyan |
| 110 | Write-Host " 📂 Repo root: $root" -ForegroundColor Gray |
| 111 | |
| 112 | # 1. package.json |
| 113 | Update-JsonVersion ` |
| 114 | -FilePath (Join-Path $root "package.json") ` |
| 115 | -Description "package.json" ` |
| 116 | -Transform { param($j) $j.version = $Version; $j } |
| 117 | |
| 118 | # 2. extension/templates/package.template.json |
| 119 | Update-JsonVersion ` |
| 120 | -FilePath (Join-Path $root "extension/templates/package.template.json") ` |
| 121 | -Description "extension/templates/package.template.json" ` |
| 122 | -Transform { param($j) $j.version = $Version; $j } |
| 123 | |
| 124 | # 3. .github/plugin/marketplace.json |
| 125 | Update-JsonVersion ` |
| 126 | -FilePath (Join-Path $root ".github/plugin/marketplace.json") ` |
| 127 | -Description ".github/plugin/marketplace.json" ` |
| 128 | -Transform { |
| 129 | param($j) |
| 130 | $j.metadata.version = $Version |
| 131 | foreach ($plugin in $j.plugins) { |
| 132 | $plugin.version = $Version |
| 133 | } |
| 134 | $j |
| 135 | } |
| 136 | |
| 137 | # 4. plugins/*/.github/plugin/plugin.json (glob) |
| 138 | $pluginJsonFiles = Get-ChildItem -Path (Join-Path $root "plugins") ` |
| 139 | -Filter "plugin.json" -Recurse -Force ` |
| 140 | | Where-Object { $_.FullName -match 'plugins[/\\][^/\\]+[/\\]\.github[/\\]plugin[/\\]plugin\.json$' } |
| 141 | |
| 142 | foreach ($pluginFile in $pluginJsonFiles) { |
| 143 | $relativePath = $pluginFile.FullName.Replace($root, '').TrimStart('/\') |
| 144 | Update-JsonVersion ` |
| 145 | -FilePath $pluginFile.FullName ` |
| 146 | -Description $relativePath ` |
| 147 | -Transform { param($j) $j.version = $Version; $j } |
| 148 | } |
| 149 | |
| 150 | # 5. .release-please-manifest.json |
| 151 | Update-JsonVersion ` |
| 152 | -FilePath (Join-Path $root ".release-please-manifest.json") ` |
| 153 | -Description ".release-please-manifest.json" ` |
| 154 | -Transform { param($j) $j.'.' = $Version; $j } |
| 155 | |
| 156 | # 6. Regenerate plugin outputs |
| 157 | if (-not $SkipPluginGenerate) { |
| 158 | Write-Host " 🔧 Running npm run plugin:generate ..." -ForegroundColor Cyan |
| 159 | Push-Location $root |
| 160 | try { |
| 161 | npm run plugin:generate |
| 162 | if ($LASTEXITCODE -ne 0) { |
| 163 | throw "npm run plugin:generate failed with exit code $LASTEXITCODE" |
| 164 | } |
| 165 | Write-Host " ✅ Plugin generation complete" -ForegroundColor Green |
| 166 | } |
| 167 | finally { |
| 168 | Pop-Location |
| 169 | } |
| 170 | } |
| 171 | else { |
| 172 | Write-Host " ⏭️ Skipping plugin:generate (SkipPluginGenerate set)" -ForegroundColor Yellow |
| 173 | } |
| 174 | |
| 175 | Write-Host "✅ All version files updated to $Version" -ForegroundColor Green |
| 176 | } |
| 177 | catch { |
| 178 | Write-Host "❌ Version update failed: $_" -ForegroundColor Red |
| 179 | throw |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | #endregion Main |
| 184 | |