|
| 1 | +#Requires -Version 5.1 |
| 2 | +<# |
| 3 | +.SYNOPSIS |
| 4 | + Updates the Windows WDK and SDK NuGet package version referenced by |
| 5 | + Directory.Build.props and packages.config. |
| 6 | +
|
| 7 | +.DESCRIPTION |
| 8 | + Rewrites the WDK/SDK NuGet version in both repo-root files so the whole |
| 9 | + sample tree can be moved to a new WDK/SDK build with a single command. |
| 10 | +
|
| 11 | + Only packages whose id starts with 'Microsoft.Windows.WDK' or |
| 12 | + 'Microsoft.Windows.SDK' are touched. Any other package reference (for |
| 13 | + example Microsoft.Windows.ImplementationLibrary) is left untouched. |
| 14 | +
|
| 15 | + The files' existing encoding (BOM / no BOM) and line endings are preserved. |
| 16 | +
|
| 17 | +.EXAMPLE |
| 18 | + .\UpdateNuGet --package 10.0.28000.2525 |
| 19 | +
|
| 20 | +.EXAMPLE |
| 21 | + .\UpdateNuGet --package 10.0.28000.2525-preview |
| 22 | +
|
| 23 | +.EXAMPLE |
| 24 | + .\UpdateNuGet --package 10.0.28000.2525 --whatif |
| 25 | +#> |
| 26 | + |
| 27 | +Set-StrictMode -Version Latest |
| 28 | +$ErrorActionPreference = 'Stop' |
| 29 | + |
| 30 | +function Show-Usage { |
| 31 | + @' |
| 32 | +Usage: .\UpdateNuGet --package <version> [--whatif] |
| 33 | +
|
| 34 | + --package, -p <version> New WDK/SDK NuGet version. Examples: |
| 35 | + 10.0.28000.2525 |
| 36 | + 10.0.28000.2525-preview |
| 37 | + --whatif Show what would change without writing any file. |
| 38 | + --help, -h Show this help. |
| 39 | +'@ | Write-Host |
| 40 | +} |
| 41 | + |
| 42 | +# --- parse arguments --------------------------------------------------------- |
| 43 | +$NewVersion = $null |
| 44 | +$WhatIf = $false |
| 45 | + |
| 46 | +for ($i = 0; $i -lt $args.Count; $i++) { |
| 47 | + $a = [string]$args[$i] |
| 48 | + if ($a -match '^(?:--package|-package|-p)=(.+)$') { |
| 49 | + $NewVersion = $Matches[1] |
| 50 | + } |
| 51 | + elseif ($a -match '^(?:--package|-package|-p)$') { |
| 52 | + $i++ |
| 53 | + if ($i -lt $args.Count) { $NewVersion = [string]$args[$i] } |
| 54 | + else { throw "Missing value after '$a'." } |
| 55 | + } |
| 56 | + elseif ($a -match '^(?:--whatif|-whatif|-WhatIf)$') { |
| 57 | + $WhatIf = $true |
| 58 | + } |
| 59 | + elseif ($a -match '^(?:--help|-help|-h|-\?|/\?)$') { |
| 60 | + Show-Usage |
| 61 | + return |
| 62 | + } |
| 63 | + elseif ($a -notmatch '^-' -and -not $NewVersion) { |
| 64 | + $NewVersion = $a |
| 65 | + } |
| 66 | + else { |
| 67 | + Show-Usage |
| 68 | + throw "Unknown or unexpected argument: '$a'." |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +if (-not $NewVersion) { |
| 73 | + Show-Usage |
| 74 | + throw "No version supplied. Pass --package <version>." |
| 75 | +} |
| 76 | + |
| 77 | +# Allow a leading 'v' (v10.0.28000.2525) for convenience. |
| 78 | +$NewVersion = $NewVersion.TrimStart('v', 'V') |
| 79 | + |
| 80 | +if ($NewVersion -notmatch '^\d+\.\d+\.\d+\.\d+(?:-[0-9A-Za-z.\-]+)?$') { |
| 81 | + throw "Version '$NewVersion' does not look like a NuGet version " + |
| 82 | + "(expected e.g. 10.0.28000.2525 or 10.0.28000.2525-preview)." |
| 83 | +} |
| 84 | + |
| 85 | +# --- files to update --------------------------------------------------------- |
| 86 | +$root = if ($PSScriptRoot) { $PSScriptRoot } else { (Get-Location).Path } |
| 87 | + |
| 88 | +$targets = @( |
| 89 | + [pscustomobject]@{ |
| 90 | + Path = Join-Path $root 'Directory.Build.props' |
| 91 | + # packages\Microsoft.Windows.WDK.x64.<version>\build\native\... |
| 92 | + Pattern = [regex]'(Microsoft\.Windows\.(?:WDK|SDK)(?:\.CPP)?(?:\.(?:x64|x86|arm64|arm))?\.)(\d[0-9A-Za-z.\-]*?)(\\build\\native\\)' |
| 93 | + }, |
| 94 | + [pscustomobject]@{ |
| 95 | + Path = Join-Path $root 'packages.config' |
| 96 | + # <package id="Microsoft.Windows.WDK.x64" version="<version>" ... /> |
| 97 | + Pattern = [regex]'(<package id="Microsoft\.Windows\.(?:WDK|SDK)[^"]*" version=")([^"]*)(")' |
| 98 | + } |
| 99 | +) |
| 100 | + |
| 101 | +$replacement = '${1}' + $NewVersion + '${3}' |
| 102 | +$anyChange = $false |
| 103 | + |
| 104 | +Write-Host "Updating WDK/SDK NuGet packages to $NewVersion" -ForegroundColor Cyan |
| 105 | +Write-Host "" |
| 106 | + |
| 107 | +foreach ($t in $targets) { |
| 108 | + $name = [System.IO.Path]::GetFileName($t.Path) |
| 109 | + |
| 110 | + if (-not (Test-Path -LiteralPath $t.Path)) { |
| 111 | + Write-Warning "Skipping (not found): $($t.Path)" |
| 112 | + continue |
| 113 | + } |
| 114 | + |
| 115 | + $bytes = [System.IO.File]::ReadAllBytes($t.Path) |
| 116 | + $hasBom = ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF) |
| 117 | + $text = [System.IO.File]::ReadAllText($t.Path) |
| 118 | + |
| 119 | + $found = $t.Pattern.Matches($text) |
| 120 | + if ($found.Count -eq 0) { |
| 121 | + Write-Warning "No WDK/SDK package references found in $name." |
| 122 | + continue |
| 123 | + } |
| 124 | + |
| 125 | + $oldVersions = $found | ForEach-Object { $_.Groups[2].Value } | Sort-Object -Unique |
| 126 | + $newText = $t.Pattern.Replace($text, $replacement) |
| 127 | + |
| 128 | + if ($newText -ceq $text) { |
| 129 | + Write-Host (" {0,-22} already at {1} ({2} reference(s))" -f $name, ($oldVersions -join ', '), $found.Count) |
| 130 | + continue |
| 131 | + } |
| 132 | + |
| 133 | + Write-Host (" {0,-22} {1} -> {2} ({3} reference(s))" -f $name, ($oldVersions -join ', '), $NewVersion, $found.Count) |
| 134 | + |
| 135 | + if (-not $WhatIf) { |
| 136 | + $enc = New-Object System.Text.UTF8Encoding($hasBom) |
| 137 | + [System.IO.File]::WriteAllText($t.Path, $newText, $enc) |
| 138 | + } |
| 139 | + $anyChange = $true |
| 140 | +} |
| 141 | + |
| 142 | +Write-Host "" |
| 143 | +if ($WhatIf) { |
| 144 | + Write-Host "WhatIf: no files were modified." -ForegroundColor Yellow |
| 145 | +} |
| 146 | +elseif ($anyChange) { |
| 147 | + Write-Host "Done. WDK/SDK packages set to $NewVersion." -ForegroundColor Green |
| 148 | + Write-Host "Next: restore packages (e.g. 'nuget restore' or 'msbuild -t:restore')." |
| 149 | +} |
| 150 | +else { |
| 151 | + Write-Host "No changes were necessary." -ForegroundColor Green |
| 152 | +} |
0 commit comments