10
10
NuGet. If the version has not been updated, the script will fail and indicate
11
11
that the project version neeeds to be updated.
12
12
#>
13
- Install-Module SemVerPS - Scope CurrentUser - Force
14
13
$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 "
16
15
17
16
[XML ]$csprojFile = Get-Content $csprojPath
18
17
$versionNode = Select-Xml $csprojFile - XPath " //Project/PropertyGroup/Version" | Select-Object - ExpandProperty Node
19
18
$projectVersion = $versionNode.InnerText
20
19
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
+
21
34
# 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
23
37
24
38
# API is case-sensitive
25
39
$packageName = $packageName.ToLower ()
26
40
$url = " https://api.nuget.org/v3/registration5-gz-semver2/$packageName /index.json"
27
41
28
42
# 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
+ }
31
53
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
34
56
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
40
60
}
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
43
69
}
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