Skip to content

Commit 5c4bb61

Browse files
CopilotJoshLove-msftCopilot
authored
Consume SdkReleaseType in automation script to update package versions (#53780)
* Initial plan * Add SdkReleaseType support to update package version suffix Co-authored-by: JoshLove-msft <[email protected]> * Fix XML node update and add unit tests Co-authored-by: JoshLove-msft <[email protected]> Co-authored-by: JoshLove-msft <[email protected]> * Update eng/scripts/Invoke-GenerateAndBuildV2.ps1 Co-authored-by: Copilot <[email protected]> * Update eng/scripts/Invoke-GenerateAndBuildV2.ps1 Co-authored-by: Copilot <[email protected]> * Add link to Pester documentation in test file Co-authored-by: JoshLove-msft <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: JoshLove-msft <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 14fb46b commit 5c4bb61

File tree

2 files changed

+241
-2
lines changed

2 files changed

+241
-2
lines changed

eng/scripts/Invoke-GenerateAndBuildV2.ps1

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,77 @@ $relatedTypeSpecProjectFolder = $inputJson.relatedTypeSpecProjectFolder
4545
$apiVersion = $inputJson.apiVersion
4646
$sdkReleaseType = $inputJson.sdkReleaseType
4747

48-
if ($sdkReleaseType) {
49-
Write-Warning "sdkReleaseType is not supported by .NET and user will need to update the package version manually"
48+
function Update-PackageVersionSuffix {
49+
param(
50+
[string]$csprojPath,
51+
[string]$sdkReleaseType
52+
)
53+
54+
if (-not $sdkReleaseType) {
55+
Write-Host "No sdkReleaseType provided, skipping version suffix update"
56+
return
57+
}
58+
59+
if (-not (Test-Path $csprojPath)) {
60+
Write-Warning "csproj file not found at $csprojPath"
61+
return
62+
}
63+
64+
Write-Host "Updating package version suffix based on sdkReleaseType: $sdkReleaseType"
65+
66+
# Load the csproj file as XML
67+
[xml]$csproj = Get-Content $csprojPath
68+
69+
# Find the PropertyGroup that contains the Version element
70+
$versionElement = $null
71+
foreach ($propertyGroup in $csproj.Project.PropertyGroup) {
72+
if ($propertyGroup.Version) {
73+
$versionElement = $propertyGroup.SelectSingleNode("Version")
74+
break
75+
}
76+
}
77+
78+
if (-not $versionElement) {
79+
Write-Warning "No Version element found in $csprojPath"
80+
return
81+
}
82+
83+
$currentVersion = $versionElement.InnerText
84+
Write-Host "Current version: $currentVersion"
85+
86+
$newVersion = $currentVersion
87+
$versionChanged = $false
88+
89+
if ($sdkReleaseType -eq "beta") {
90+
# If release type is beta, ensure version has beta suffix
91+
if ($currentVersion -notmatch '-beta\.\d+$') {
92+
# Version doesn't have beta suffix, add it
93+
$newVersion = "$currentVersion-beta.1"
94+
Write-Host "Adding beta suffix. New version: $newVersion"
95+
$versionChanged = $true
96+
} else {
97+
Write-Host "Version already has beta suffix, no change needed"
98+
}
99+
} elseif ($sdkReleaseType -eq "stable") {
100+
# If release type is stable, remove beta suffix if present
101+
if ($currentVersion -match '^(.+)-beta\.\d+$') {
102+
$newVersion = $matches[1]
103+
Write-Host "Removing beta suffix. New version: $newVersion"
104+
$versionChanged = $true
105+
} else {
106+
Write-Host "Version is already stable (no beta suffix), no change needed"
107+
}
108+
} else {
109+
Write-Warning "Unknown sdkReleaseType: $sdkReleaseType. Expected 'beta' or 'stable'"
110+
return
111+
}
112+
113+
# Update and save if version changed
114+
if ($versionChanged) {
115+
$versionElement.InnerText = $newVersion
116+
$csproj.Save($csprojPath)
117+
Write-Host "Successfully updated version to $newVersion in $csprojPath"
118+
}
50119
}
51120

52121
$autorestConfigYaml = ""
@@ -176,6 +245,12 @@ if ($relatedTypeSpecProjectFolder) {
176245
})
177246
$exitCode = $LASTEXITCODE
178247
} else {
248+
# Update package version suffix based on sdkReleaseType
249+
if ($sdkReleaseType) {
250+
$csprojPath = Join-Path $sdkProjectFolder "src" "$packageName.csproj"
251+
Update-PackageVersionSuffix -csprojPath $csprojPath -sdkReleaseType $sdkReleaseType
252+
}
253+
179254
$relativeSdkPath = Resolve-Path $sdkProjectFolder -Relative
180255
GeneratePackage `
181256
-projectFolder $sdkProjectFolder `
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#Requires -Version 7.0
2+
<#
3+
.How-To-Run
4+
This test file uses Pester, a testing framework for PowerShell.
5+
For more information about Pester, see: https://pester.dev/docs/quick-start
6+
7+
First, ensure you have `pester` installed:
8+
9+
`Install-Module Pester -Force`
10+
11+
Then invoke tests with:
12+
13+
`Invoke-Pester ./Update-PackageVersionSuffix.tests.ps1`
14+
15+
#>
16+
17+
. (Join-Path $PSScriptRoot ".." ".." "common" "scripts" "Helpers" PSModule-Helpers.ps1)
18+
Install-ModuleIfNotInstalled "Pester" "5.3.3" | Import-Module
19+
20+
# Source the function from the main script
21+
$scriptContent = Get-Content (Join-Path $PSScriptRoot "../Invoke-GenerateAndBuildV2.ps1") -Raw
22+
$functionPattern = '(?s)function Update-PackageVersionSuffix \{.*?\n\}\n'
23+
if ($scriptContent -match $functionPattern) {
24+
$functionCode = $Matches[0]
25+
Invoke-Expression $functionCode
26+
} else {
27+
throw "Could not extract Update-PackageVersionSuffix function from Invoke-GenerateAndBuildV2.ps1"
28+
}
29+
30+
Describe "Update-PackageVersionSuffix" {
31+
BeforeAll {
32+
$testDir = Join-Path ([System.IO.Path]::GetTempPath()) "PackageVersionSuffixTests"
33+
if (Test-Path $testDir) {
34+
Remove-Item $testDir -Recurse -Force
35+
}
36+
New-Item -Path $testDir -ItemType Directory | Out-Null
37+
}
38+
39+
AfterAll {
40+
if (Test-Path $testDir) {
41+
Remove-Item $testDir -Recurse -Force
42+
}
43+
}
44+
45+
Context "When sdkReleaseType is 'beta'" {
46+
It "Should add beta suffix to stable version" {
47+
$testCsproj = Join-Path $testDir "test-add-beta.csproj"
48+
$csprojContent = @"
49+
<Project Sdk="Microsoft.NET.Sdk">
50+
<PropertyGroup>
51+
<Version>1.0.0</Version>
52+
</PropertyGroup>
53+
</Project>
54+
"@
55+
Set-Content -Path $testCsproj -Value $csprojContent
56+
57+
Update-PackageVersionSuffix -csprojPath $testCsproj -sdkReleaseType "beta"
58+
59+
[xml]$updatedCsproj = Get-Content $testCsproj
60+
$version = $updatedCsproj.Project.PropertyGroup.Version
61+
$version | Should -Be "1.0.0-beta.1"
62+
}
63+
64+
It "Should not modify version that already has beta suffix" {
65+
$testCsproj = Join-Path $testDir "test-keep-beta.csproj"
66+
$csprojContent = @"
67+
<Project Sdk="Microsoft.NET.Sdk">
68+
<PropertyGroup>
69+
<Version>1.2.0-beta.3</Version>
70+
</PropertyGroup>
71+
</Project>
72+
"@
73+
Set-Content -Path $testCsproj -Value $csprojContent
74+
75+
Update-PackageVersionSuffix -csprojPath $testCsproj -sdkReleaseType "beta"
76+
77+
[xml]$updatedCsproj = Get-Content $testCsproj
78+
$version = $updatedCsproj.Project.PropertyGroup.Version
79+
$version | Should -Be "1.2.0-beta.3"
80+
}
81+
}
82+
83+
Context "When sdkReleaseType is 'stable'" {
84+
It "Should remove beta suffix from version" {
85+
$testCsproj = Join-Path $testDir "test-remove-beta.csproj"
86+
$csprojContent = @"
87+
<Project Sdk="Microsoft.NET.Sdk">
88+
<PropertyGroup>
89+
<Version>2.0.0-beta.5</Version>
90+
</PropertyGroup>
91+
</Project>
92+
"@
93+
Set-Content -Path $testCsproj -Value $csprojContent
94+
95+
Update-PackageVersionSuffix -csprojPath $testCsproj -sdkReleaseType "stable"
96+
97+
[xml]$updatedCsproj = Get-Content $testCsproj
98+
$version = $updatedCsproj.Project.PropertyGroup.Version
99+
$version | Should -Be "2.0.0"
100+
}
101+
102+
It "Should not modify stable version" {
103+
$testCsproj = Join-Path $testDir "test-keep-stable.csproj"
104+
$csprojContent = @"
105+
<Project Sdk="Microsoft.NET.Sdk">
106+
<PropertyGroup>
107+
<Version>3.1.0</Version>
108+
</PropertyGroup>
109+
</Project>
110+
"@
111+
Set-Content -Path $testCsproj -Value $csprojContent
112+
113+
Update-PackageVersionSuffix -csprojPath $testCsproj -sdkReleaseType "stable"
114+
115+
[xml]$updatedCsproj = Get-Content $testCsproj
116+
$version = $updatedCsproj.Project.PropertyGroup.Version
117+
$version | Should -Be "3.1.0"
118+
}
119+
}
120+
121+
Context "Edge cases" {
122+
It "Should handle missing sdkReleaseType" {
123+
$testCsproj = Join-Path $testDir "test-no-type.csproj"
124+
$csprojContent = @"
125+
<Project Sdk="Microsoft.NET.Sdk">
126+
<PropertyGroup>
127+
<Version>1.0.0</Version>
128+
</PropertyGroup>
129+
</Project>
130+
"@
131+
Set-Content -Path $testCsproj -Value $csprojContent
132+
133+
Update-PackageVersionSuffix -csprojPath $testCsproj -sdkReleaseType ""
134+
135+
[xml]$updatedCsproj = Get-Content $testCsproj
136+
$version = $updatedCsproj.Project.PropertyGroup.Version
137+
$version | Should -Be "1.0.0"
138+
}
139+
140+
It "Should handle unknown sdkReleaseType" {
141+
$testCsproj = Join-Path $testDir "test-unknown-type.csproj"
142+
$csprojContent = @"
143+
<Project Sdk="Microsoft.NET.Sdk">
144+
<PropertyGroup>
145+
<Version>1.0.0</Version>
146+
</PropertyGroup>
147+
</Project>
148+
"@
149+
Set-Content -Path $testCsproj -Value $csprojContent
150+
151+
Update-PackageVersionSuffix -csprojPath $testCsproj -sdkReleaseType "unknown"
152+
153+
[xml]$updatedCsproj = Get-Content $testCsproj
154+
$version = $updatedCsproj.Project.PropertyGroup.Version
155+
$version | Should -Be "1.0.0"
156+
}
157+
158+
It "Should handle missing csproj file" {
159+
$testCsproj = Join-Path $testDir "nonexistent.csproj"
160+
161+
{ Update-PackageVersionSuffix -csprojPath $testCsproj -sdkReleaseType "beta" } | Should -Not -Throw
162+
}
163+
}
164+
}

0 commit comments

Comments
 (0)