Skip to content

Instantly share code, notes, and snippets.

@LindnerBrewery
Last active January 8, 2024 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LindnerBrewery/5a4a7acdde660640cc86c6bf8c14ef0b to your computer and use it in GitHub Desktop.
Save LindnerBrewery/5a4a7acdde660640cc86c6bf8c14ef0b to your computer and use it in GitHub Desktop.
ConvertTo-SemVersion
function ConvertTo-SemVersion {
<#
.SYNOPSIS
Converts a Version to to semantic versioning v2
.DESCRIPTION
Converts a Version to to semantic versioning v2. The function accepts a version as a string and will try to convert it.
It will return a string representing at least major.minor.patch. If revision is 0 it will be dropped, unless is has a prerelease or buildmetadata.
1 -> 1.0.0
1.1 -> 1.1.0
1.1.1.0 -> 1.1.1
1.1.1.1 -> 1.1.1.1
1.01 -> 1.1.0
1-Alpha -> 1.0.0-Alpha
1.1.0.0-RC -> 1.1.0-RC
1.1.0.0-RC+2019 -> 1.1.0-RC+2019
.EXAMPLE
PS C:\> ConvertTo-SemVersion -Version 1.0
1.0.0
.INPUTS
[String]
.OUTPUTS
[String]
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
Position = 0,
HelpMessage = "Specifies the version to convert.")]
[String]$Version
)
begin {}
process {
$normVersion = $null
$regexPattern = '^(?<major>\d*)?(?:\.(?<minor>\d*))?(?:\.(?<patch>\d*))?(?:\.(?<build>\d*))?(?:-(?<prerelease>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?(?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$'
$result = [Regex]::Match($Version, $regexPattern)
# throw error if version not valid
if (! $result.Success) {
Throw "$Version is not a valid semantic version"
}
# convert the major minor and patch
Write-Verbose "Converting version"
$normVersion = [version]::new($result.groups["major"].Value, $result.groups["minor"].Value, $result.groups["patch"].Value)
$rev = $result.groups[4].Value
if ($rev -gt 0) {
Write-Verbose "Revision ($rev) is not 0 and will be preserved"
$normVersion = [System.Version]::Parse("$($normVersion).$($result.groups["build"].Value)")
}
elseif ($rev -eq 0) {
Write-Verbose "Revision is 0 and will be removed from the converted version"
}
$versionString = $normVersion.ToString()
# if version is a prerelease or has build metadata then return converted version with metadata
if ($result.Groups["prerelease"].Success -or $result.Groups["buildmetadata"].Success) {
Write-Verbose "Version has prerelease or buildmetadata and will be returned as is"
if ($result.groups["prerelease"].Value) {
Write-Verbose "Version has prerelease tag"
$versionString += "-" + $result.groups["prerelease"].Value
}
if ($result.groups["buildmetadata"].Value) {
Write-Verbose "Version has buildmetadata tag"
$versionString += "+" + $result.groups["buildmetadata"].Value
}
}
return $versionString
}
end {}
}
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Scope='Function')]
Param()
BeforeAll {
. $PSCommandPath.Replace('.Tests.ps1','.ps1')
}
Describe "ConvertTo-SemVersion Tests" {
Context "When providing a valid version" {
It "Should normalize version 1 to 1.0.0" {
$version = "1"
$expected = "1.0.0"
$result = ConvertTo-SemVersion -Version $version
$result | Should -Be $expected
}
It "Should normalize version 1.1 to 1.1.0" {
$version = "1.1"
$expected = "1.1.0"
$result = ConvertTo-SemVersion -Version $version
$result | Should -Be $expected
}
It "Should normalize version 1.1.1.0 to 1.1.1" {
$version = "1.1.1.0"
$expected = "1.1.1"
$result = ConvertTo-SemVersion -Version $version
$result | Should -Be $expected
}
It "Should preserve version 1.1.1.1" {
$version = "1.1.1.1"
$expected = "1.1.1.1"
$result = ConvertTo-SemVersion -Version $version
$result | Should -Be $expected
}
It "Should normalize version 1.01 to 1.1.0" {
$version = "1.01"
$expected = "1.1.0"
$result = ConvertTo-SemVersion -Version $version
$result | Should -Be $expected
}
It "Should normalize version 1-Alpha to 1.0.0-Alpha" {
$version = "1-Alpha"
$expected = "1.0.0-Alpha"
$result = ConvertTo-SemVersion -Version $version
$result | Should -Be $expected
}
It "Should normalize version 1-Alpha-1 to 1.0.0-Alpha-1" {
$version = "1-Alpha-1"
$expected = "1.0.0-Alpha-1"
$result = ConvertTo-SemVersion -Version $version
$result | Should -Be $expected
}
It "Should normalize version 1.1.0.0-RC to 1.1.0-RC" {
$version = "1.1.0.0-RC"
$expected = "1.1.0-RC"
$result = ConvertTo-SemVersion -Version $version
$result | Should -Be $expected
}
It "Should normalize version 1.1.0.0-RC+2019 to 1.1.0-RC+2019" {
$version = "1.1.0.0-RC+2019"
$expected = "1.1.0-RC+2019"
$result = ConvertTo-SemVersion -Version $version
$result | Should -Be $expected
}
}
Context "When providing an invalid version" {
It "Should throw an error for an invalid version" {
$version = "invalid"
{ ConvertTo-SemVersion -Version $version } | Should -Throw
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment