|
| 1 | +param() |
| 2 | + |
| 3 | +. $PSScriptRoot\common\tools.ps1 |
| 4 | + |
| 5 | +try { |
| 6 | + $vsInfo = LocateVisualStudio |
| 7 | +} |
| 8 | +catch { |
| 9 | + Write-Host "LocateVisualStudio failed: $_" |
| 10 | + return |
| 11 | +} |
| 12 | + |
| 13 | +if ($null -eq $vsInfo) { |
| 14 | + Write-Host "No Visual Studio instance detected; preview SDKs remain enabled by default." |
| 15 | + return |
| 16 | +} |
| 17 | + |
| 18 | +$vsId = $vsInfo.instanceId |
| 19 | +$vsMajorVersion = $vsInfo.installationVersion.Split('.')[0] |
| 20 | +$instanceDir = Join-Path $env:USERPROFILE "AppData\Local\Microsoft\VisualStudio\$vsMajorVersion.0_$vsId" |
| 21 | + |
| 22 | +Create-Directory $instanceDir |
| 23 | + |
| 24 | +$sdkFile = Join-Path $instanceDir 'sdk.txt' |
| 25 | + |
| 26 | +$desiredLine = 'UsePreviews=True' |
| 27 | +$existingLines = @() |
| 28 | + |
| 29 | +if (Test-Path $sdkFile) { |
| 30 | + $existingLines = @(Get-Content -Path $sdkFile -Encoding ASCII) |
| 31 | +} |
| 32 | + |
| 33 | +# Determine how to place the UsePreviews flag based on existing content. |
| 34 | +$replacementIndex = -1 |
| 35 | +for ($i = 0; $i -lt $existingLines.Count; $i++) { |
| 36 | + if ($existingLines[$i] -match '^UsePreviews=.*$') { |
| 37 | + $replacementIndex = $i |
| 38 | + break |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +# Replace the existing line to enforce it as True |
| 43 | +if ($replacementIndex -ge 0) { |
| 44 | + $updatedLines = $existingLines |
| 45 | + $updatedLines[$replacementIndex] = $desiredLine |
| 46 | +} |
| 47 | +elseif ($existingLines.Count -gt 0) { |
| 48 | + # Write to the top of the file but keep the remaining portion (assumption: order does not matter to VS) |
| 49 | + $updatedLines = @($desiredLine) + $existingLines |
| 50 | +} |
| 51 | +else { |
| 52 | + # Write a whole new file |
| 53 | + $updatedLines = @($desiredLine) |
| 54 | +} |
| 55 | + |
| 56 | +Set-Content -Path $sdkFile -Value $updatedLines -Encoding ASCII |
| 57 | + |
| 58 | +Write-Host "Updated $sdkFile" |
| 59 | +Get-Content -Path $sdkFile | ForEach-Object { Write-Host " $_" } |
0 commit comments