Skip to content

Commit 7c22744

Browse files
committed
ci: fixes version check script
1 parent 14c97fc commit 7c22744

File tree

1 file changed

+51
-14
lines changed

1 file changed

+51
-14
lines changed

scripts/ValidateProjectVersionUpdated.ps1

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,71 @@
1010
NuGet. If the version has not been updated, the script will fail and indicate
1111
that the project version neeeds to be updated.
1212
#>
13-
Install-Module SemVerPS -Scope CurrentUser -Force
1413
$packageName = "Microsoft.OpenApi.OData"
15-
$csprojPath = Join-Path $PSScriptRoot "..\src\Microsoft.OpenApi.OData.Reader\Microsoft.OpenAPI.OData.Reader.csproj"
14+
$csprojPath = Join-Path $PSScriptRoot "..\Directory.Build.props"
1615

1716
[XML]$csprojFile = Get-Content $csprojPath
1817
$versionNode = Select-Xml $csprojFile -XPath "//Project/PropertyGroup/Version" | Select-Object -ExpandProperty Node
1918
$projectVersion = $versionNode.InnerText
2019

20+
# If <Version> is missing, try <VersionPrefix> + <VersionSuffix>
21+
if ($null -eq $projectVersion)
22+
{
23+
$versionPrefixNode = Select-Xml $csprojFile -XPath "//Project/PropertyGroup/VersionPrefix" | Select-Object -ExpandProperty Node
24+
$versionSuffixNode = Select-Xml $csprojFile -XPath "//Project/PropertyGroup/VersionSuffix" | Select-Object -ExpandProperty Node
25+
$projectVersion = $versionPrefixNode.InnerText + $versionSuffixNode.InnerText
26+
}
27+
28+
# Ensure a valid version exists
29+
if (-not $projectVersion -or $projectVersion -eq "") {
30+
Write-Error "No valid version found in .csproj file. Please define <Version> or <VersionPrefix> for $packageName."
31+
Exit 1
32+
}
33+
2134
# Cast the project version string to System.Version
22-
$currentProjectVersion = ConvertTo-SemVer -Version $projectVersion
35+
$currentProjectVersion = [System.Management.Automation.SemanticVersion]$projectVersion
36+
$currentMajorVersion = $currentProjectVersion.Major
2337

2438
# API is case-sensitive
2539
$packageName = $packageName.ToLower()
2640
$url = "https://api.nuget.org/v3/registration5-gz-semver2/$packageName/index.json"
2741

2842
# Call the NuGet API for the package and get the current published version.
29-
$nugetIndex = Invoke-RestMethod -Uri $url -Method Get
30-
$publishedVersionString = $nugetIndex.items[0].upper
43+
Try {
44+
$nugetIndex = Invoke-RestMethod -Uri $url -Method Get -ErrorAction Stop
45+
} Catch {
46+
if ($_.Exception.Response.StatusCode -eq 404) {
47+
Write-Host "No package exists. You will probably be publishing $packageName for the first time."
48+
Exit 0
49+
}
50+
Write-Error "Error fetching package details: $_"
51+
Exit 1
52+
}
3153

32-
# Cast the published version string to System.Version
33-
$currentPublishedVersion = ConvertTo-SemVer -Version $publishedVersionString
54+
# Extract and sort all published versions (handling null/empty cases)
55+
$publishedVersions = $nugetIndex.items | ForEach-Object { [System.Management.Automation.SemanticVersion]$_.upper } | Sort-Object -Descending
3456

35-
# Validate that the version number has been updated.
36-
if ($currentProjectVersion -le $currentPublishedVersion) {
37-
Write-Error "The project version in versioning.props file ($projectVersion) `
38-
has not been bumped up. The current published version is $publishedVersionString. `
39-
Please increment the current project version."
57+
if (-not $publishedVersions -or $publishedVersions.Count -eq 0) {
58+
Write-Host "No previous versions found on NuGet. Proceeding with publish." -ForegroundColor Green
59+
Exit 0
4060
}
41-
else {
42-
Write-Host "Validated that the version has been updated from $publishedVersionString to $currentProjectVersion" -ForegroundColor Green
61+
62+
# Find the highest published version within the same major version
63+
$highestPublishedVersionInMajor = ($publishedVersions | Where-Object { $_.Major -eq $currentMajorVersion })
64+
65+
# Handle empty or null major versions list
66+
if (-not $highestPublishedVersionInMajor -or $highestPublishedVersionInMajor.Count -eq 0) {
67+
Write-Host "No previous versions found for major version $currentMajorVersion. Proceeding with publish." -ForegroundColor Green
68+
Exit 0
4369
}
70+
71+
# Get the latest version for the current major version
72+
$latestMajorVersion = $highestPublishedVersionInMajor[0]
73+
74+
# Validate that the version number has increased
75+
if ($currentProjectVersion -le $latestMajorVersion) {
76+
Write-Error "The version in .csproj ($currentProjectVersion) must be greater than the highest published version in the same major ($latestMajorVersion)."
77+
Exit 1
78+
} else {
79+
Write-Host "Validated version update: $latestMajorVersion -> $currentProjectVersion" -ForegroundColor Green
80+
}

0 commit comments

Comments
 (0)