Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions scripts/IncrementPreviewVersion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@

#>

[CmdletBinding()]
Param(
[parameter(Mandatory = $true)]
[string]$packageName,
[parameter(Mandatory = $false)]
[string]$packageName = 'microsoft.graph.core',

[parameter(Mandatory = $true)]
[string]$projectPath
[parameter(Mandatory = $false)]
[string]$projectPath = '.\src\Microsoft.Graph.Core\Microsoft.Graph.Core.csproj'
)

$xmlDoc = New-Object System.Xml.XmlDocument
Expand All @@ -31,9 +32,9 @@ $versionSuffixString = $xmlDoc.Project.PropertyGroup[0].VersionSuffix

# Don't do anything if a VersionSuffix has been set in the .csproj.
if ($versionSuffixString -ne '' -and $versionSuffixString -ne $null) {
Write-Host "The VersionSuffix has been set as $versionSuffixString in the csproj file. `
Skip the automatic setting or incrementing of the version suffix. Delete the value in `
VersionSuffix to enable auto-incrementing the preview version." -ForegroundColor Yellow
Write-Host "`tThe VersionSuffix has been set as $versionSuffixString in the csproj file. `
`tSkip the automatic setting or incrementing of the version suffix. Delete the value in `
`tVersionSuffix to enable auto-incrementing the preview version." -ForegroundColor Yellow

Exit 0
}
Expand All @@ -51,19 +52,31 @@ $url = "https://api.nuget.org/v3-flatcontainer/$packageName/index.json"
# Call the NuGet API for the package and get the highest SemVer 2.0.0 version for the package.
# Per rules https://semver.org/spec/v2.0.0.html#spec-item-11
$nugetIndex = Invoke-RestMethod -Uri $url -Method Get
$highestPublishedVersion = $nugetIndex.versions[$nugetIndex.versions.Length - 1]

# We do need to make sure it is listed. For example, we didn't properly suffix M.G.A so the
# highest reported version is incorrect.
# We need the versionPrefix from the csproj so that we target the
# the highest version. This enables us to support v1.x.x and vNext.x.x
$versionPrefix = $xmlDoc.Project.PropertyGroup[0].VersionPrefix

# We assume that the Version takes the form of 'x.y.z' with an optional '-preview.n' appended for preview releases.
Write-Host "The highest published version of $packageName is $highestPublishedVersion"

# Set or increment the VersionSuffix.
if ($highestPublishedVersion.Indexof('-preview') -eq -1) {
# Only consider versions that match the version prefix in the csproj.
$versionmatches = $nugetIndex.versions -match $versionPrefix
if ($versionmatches.Count -eq 0) {
# if the version hasn't been published, we need to add a preview versionSuffix.
# Add an initial preview versionSuffix
$versionSuffixString = 'preview.1' # Build applies the hyphen
}
else {
else {
# We have published preview versions for this version prefix.

# Only look at preview versions, also to referred to as versionSuffix
$versionmatches = $versionmatches -match 'preview'

# Assumption: the API returns the versions in order.
$highestPublishedVersion = $versionmatches[$versionmatches.Count - 1]

# We assume that the Version takes the form of 'x.y.z' with an optional '-preview.n' appended for preview releases.
Write-Host "The highest published version of $packageName is $highestPublishedVersion"

# if the version has been published with a versionSuffix, then increment the suffix.
# A preview has been previously released. Let's increment the VersionSuffix.
$currentPreviewVersion = [int]$highestPublishedVersion.Split('-')[1].Split('.')[1]

Expand All @@ -72,7 +85,7 @@ else {
$versionSuffixString = "preview.{0}" -f $incrementedPreviewVersion
}

Write-Host "The preview version is now $versionSuffixString" -ForegroundColor Green
Write-Host "The preview version is now $versionPrefix-$versionSuffixString" -ForegroundColor Green

$xmlDoc.Project.PropertyGroup[0].VersionSuffix = $versionSuffixString
$xmlDoc.Save($projectPath)