2024-12-19 09:00:53 +08:00
|
|
|
Param(
|
2025-04-23 18:25:47 -07:00
|
|
|
# Using the default value of 1.7 for winAppSdkVersionNumber and useExperimentalVersion as false
|
2024-12-19 09:00:53 +08:00
|
|
|
[Parameter(Mandatory=$False,Position=1)]
|
2025-04-23 18:25:47 -07:00
|
|
|
[string]$winAppSdkVersionNumber = "1.7",
|
2024-12-19 09:00:53 +08:00
|
|
|
|
|
|
|
# When the pipeline calls the PS1 file, the passed parameters are converted to string type
|
|
|
|
[Parameter(Mandatory=$False,Position=2)]
|
2025-04-23 18:25:47 -07:00
|
|
|
[boolean]$useExperimentalVersion = $False,
|
|
|
|
|
|
|
|
# Root folder Path for processing
|
|
|
|
[Parameter(Mandatory=$False,Position=3)]
|
|
|
|
[string]$rootPath = $(Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)),
|
|
|
|
|
|
|
|
# Root folder Path for processing
|
|
|
|
[Parameter(Mandatory=$False,Position=4)]
|
|
|
|
[string]$sourceLink = "https://microsoft.pkgs.visualstudio.com/ProjectReunion/_packaging/Project.Reunion.nuget.internal/nuget/v3/index.json"
|
2024-12-19 09:00:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
function Update-NugetConfig {
|
|
|
|
param (
|
2025-04-23 18:25:47 -07:00
|
|
|
[string]$filePath = [System.IO.Path]::Combine($rootPath, "nuget.config")
|
2024-12-19 09:00:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
Write-Host "Updating nuget.config file"
|
|
|
|
[xml]$xml = Get-Content -Path $filePath
|
|
|
|
|
|
|
|
# Add localpackages source into nuget.config
|
|
|
|
$packageSourcesNode = $xml.configuration.packageSources
|
|
|
|
$addNode = $xml.CreateElement("add")
|
|
|
|
$addNode.SetAttribute("key", "localpackages")
|
|
|
|
$addNode.SetAttribute("value", "localpackages")
|
|
|
|
$packageSourcesNode.AppendChild($addNode) | Out-Null
|
|
|
|
|
|
|
|
# Remove <packageSourceMapping> tag and its content
|
|
|
|
$packageSourceMappingNode = $xml.configuration.packageSourceMapping
|
|
|
|
if ($packageSourceMappingNode) {
|
|
|
|
$xml.configuration.RemoveChild($packageSourceMappingNode) | Out-Null
|
|
|
|
}
|
|
|
|
|
|
|
|
# print nuget.config after modification
|
|
|
|
$xml.OuterXml
|
|
|
|
# Save the modified nuget.config file
|
|
|
|
$xml.Save($filePath)
|
|
|
|
}
|
|
|
|
|
2025-04-23 18:25:47 -07:00
|
|
|
function Read-FileWithEncoding {
|
|
|
|
param (
|
|
|
|
[string]$Path
|
|
|
|
)
|
|
|
|
|
|
|
|
$reader = New-Object System.IO.StreamReader($Path, $true) # auto-detect encoding
|
|
|
|
$content = $reader.ReadToEnd()
|
|
|
|
$encoding = $reader.CurrentEncoding
|
|
|
|
$reader.Close()
|
|
|
|
|
|
|
|
return [PSCustomObject]@{
|
|
|
|
Content = $content
|
|
|
|
Encoding = $encoding
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function Write-FileWithEncoding {
|
|
|
|
param (
|
|
|
|
[string]$Path,
|
|
|
|
[string]$Content,
|
|
|
|
[System.Text.Encoding]$Encoding
|
|
|
|
)
|
|
|
|
|
|
|
|
$writer = New-Object System.IO.StreamWriter($Path, $false, $Encoding)
|
|
|
|
$writer.Write($Content)
|
|
|
|
$writer.Close()
|
|
|
|
}
|
2024-12-19 09:00:53 +08:00
|
|
|
|
|
|
|
# Execute nuget list and capture the output
|
|
|
|
if ($useExperimentalVersion) {
|
|
|
|
# The nuget list for experimental versions will cost more time
|
|
|
|
# So, we will not use -AllVersions to wast time
|
|
|
|
# But it can only get the latest experimental version
|
|
|
|
Write-Host "Fetching WindowsAppSDK with experimental versions"
|
|
|
|
$nugetOutput = nuget list Microsoft.WindowsAppSDK `
|
|
|
|
-Source $sourceLink `
|
|
|
|
-Prerelease
|
|
|
|
# Filter versions based on the specified version prefix
|
|
|
|
$escapedVersionNumber = [regex]::Escape($winAppSdkVersionNumber)
|
|
|
|
$filteredVersions = $nugetOutput | Where-Object { $_ -match "Microsoft.WindowsAppSDK $escapedVersionNumber\." }
|
|
|
|
$latestVersions = $filteredVersions
|
|
|
|
} else {
|
|
|
|
Write-Host "Fetching stable WindowsAppSDK versions for $winAppSdkVersionNumber"
|
|
|
|
$nugetOutput = nuget list Microsoft.WindowsAppSDK `
|
|
|
|
-Source $sourceLink `
|
|
|
|
-AllVersions
|
|
|
|
# Filter versions based on the specified version prefix
|
|
|
|
$escapedVersionNumber = [regex]::Escape($winAppSdkVersionNumber)
|
|
|
|
$filteredVersions = $nugetOutput | Where-Object { $_ -match "Microsoft.WindowsAppSDK $escapedVersionNumber\." }
|
|
|
|
$latestVersions = $filteredVersions | Sort-Object { [version]($_ -split ' ')[1] } -Descending | Select-Object -First 1
|
|
|
|
}
|
|
|
|
|
|
|
|
Write-Host "Latest versions found: $latestVersions"
|
|
|
|
# Extract the latest version number from the output
|
|
|
|
$latestVersion = $latestVersions -split "`n" | `
|
|
|
|
Select-String -Pattern 'Microsoft.WindowsAppSDK\s*([0-9]+\.[0-9]+\.[0-9]+-*[a-zA-Z0-9]*)' | `
|
|
|
|
ForEach-Object { $_.Matches[0].Groups[1].Value } | `
|
|
|
|
Sort-Object -Descending | `
|
|
|
|
Select-Object -First 1
|
|
|
|
|
|
|
|
if ($latestVersion) {
|
|
|
|
$WinAppSDKVersion = $latestVersion
|
|
|
|
Write-Host "Extracted version: $WinAppSDKVersion"
|
|
|
|
Write-Host "##vso[task.setvariable variable=WinAppSDKVersion]$WinAppSDKVersion"
|
|
|
|
} else {
|
|
|
|
Write-Host "Failed to extract version number from nuget list output"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# Update packages.config files
|
2025-04-23 18:25:47 -07:00
|
|
|
Get-ChildItem -Path $rootPath -Recurse packages.config | ForEach-Object {
|
|
|
|
$file = Read-FileWithEncoding -Path $_.FullName
|
|
|
|
$content = $file.Content
|
2024-12-19 09:00:53 +08:00
|
|
|
if ($content -match 'package id="Microsoft.WindowsAppSDK"') {
|
|
|
|
$newVersionString = 'package id="Microsoft.WindowsAppSDK" version="' + $WinAppSDKVersion + '"'
|
|
|
|
$oldVersionString = 'package id="Microsoft.WindowsAppSDK" version="[-.0-9a-zA-Z]*"'
|
|
|
|
$content = $content -replace $oldVersionString, $newVersionString
|
2025-04-23 18:25:47 -07:00
|
|
|
Write-FileWithEncoding -Path $_.FullName -Content $content -Encoding $file.encoding
|
2024-12-19 09:00:53 +08:00
|
|
|
Write-Host "Modified " $_.FullName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Update Directory.Packages.props file
|
2025-05-22 20:19:24 -07:00
|
|
|
Get-ChildItem -Path $rootPath -Recurse "Directory.Packages.props" | ForEach-Object {
|
|
|
|
$file = Read-FileWithEncoding -Path $_.FullName
|
2025-04-23 18:25:47 -07:00
|
|
|
$content = $file.Content
|
2024-12-19 09:00:53 +08:00
|
|
|
if ($content -match '<PackageVersion Include="Microsoft.WindowsAppSDK"') {
|
|
|
|
$newVersionString = '<PackageVersion Include="Microsoft.WindowsAppSDK" Version="' + $WinAppSDKVersion + '" />'
|
|
|
|
$oldVersionString = '<PackageVersion Include="Microsoft.WindowsAppSDK" Version="[-.0-9a-zA-Z]*" />'
|
|
|
|
$content = $content -replace $oldVersionString, $newVersionString
|
2025-05-22 20:19:24 -07:00
|
|
|
Write-FileWithEncoding -Path $_.FullName -Content $content -Encoding $file.encoding
|
|
|
|
Write-Host "Modified " $_.FullName
|
2024-12-19 09:00:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Update .vcxproj files
|
2025-04-23 18:25:47 -07:00
|
|
|
Get-ChildItem -Path $rootPath -Recurse *.vcxproj | ForEach-Object {
|
|
|
|
$file = Read-FileWithEncoding -Path $_.FullName
|
|
|
|
$content = $file.Content
|
2024-12-19 09:00:53 +08:00
|
|
|
if ($content -match '\\Microsoft.WindowsAppSDK.') {
|
2025-05-22 20:19:24 -07:00
|
|
|
$newVersionString = '\Microsoft.WindowsAppSDK.' + $WinAppSDKVersion
|
|
|
|
$oldVersionString = '\\Microsoft.WindowsAppSDK.(?=[-.0-9a-zA-Z]*\d)[-.0-9a-zA-Z]*' #positive lookahead for at least a digit
|
2024-12-19 09:00:53 +08:00
|
|
|
$content = $content -replace $oldVersionString, $newVersionString
|
2025-04-23 18:25:47 -07:00
|
|
|
Write-FileWithEncoding -Path $_.FullName -Content $content -Encoding $file.encoding
|
2024-12-19 09:00:53 +08:00
|
|
|
Write-Host "Modified " $_.FullName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Update .csproj files
|
2025-04-23 18:25:47 -07:00
|
|
|
Get-ChildItem -Path $rootPath -Recurse *.csproj | ForEach-Object {
|
|
|
|
$file = Read-FileWithEncoding -Path $_.FullName
|
|
|
|
$content = $file.Content
|
2024-12-19 09:00:53 +08:00
|
|
|
if ($content -match 'PackageReference Include="Microsoft.WindowsAppSDK"') {
|
|
|
|
$newVersionString = 'PackageReference Include="Microsoft.WindowsAppSDK" Version="'+ $WinAppSDKVersion + '"'
|
|
|
|
$oldVersionString = 'PackageReference Include="Microsoft.WindowsAppSDK" Version="[-.0-9a-zA-Z]*"'
|
|
|
|
$content = $content -replace $oldVersionString, $newVersionString
|
2025-04-23 18:25:47 -07:00
|
|
|
Write-FileWithEncoding -Path $_.FullName -Content $content -Encoding $file.encoding
|
2024-12-19 09:00:53 +08:00
|
|
|
Write-Host "Modified " $_.FullName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Update-NugetConfig
|