Skip to content

Commit 770f0db

Browse files
committed
Add modified version comparison support
1 parent 329a01c commit 770f0db

File tree

1 file changed

+18
-30
lines changed

1 file changed

+18
-30
lines changed

Public/Compare-Version.ps1

+18-30
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ function Compare-Version {
2222
File name: Compare-Version.ps1
2323
Author: Florian Carrier
2424
Creation date: 19/10/2019
25-
Last modified: 19/10/2019
25+
Last modified: 05/02/2020
26+
WARNING In case of modified formatting, Compare-Version only checks the semantic versionned part
2627
#>
2728
[CmdletBinding (
2829
SupportsShouldProcess = $true
@@ -80,13 +81,13 @@ function Compare-Version {
8081
"semantic" {
8182
# Prepare version numbers for comparison
8283
try {
83-
$VersionNumber = [System.Version]::Parse($Version)
84+
$VersionNumber = [System.Version]::Parse($Version)
8485
} catch [FormatException] {
8586
Write-Log -Type "ERROR" -Object "The version number ""$Version"" does not match $Format numbering"
8687
return $false
8788
}
8889
try {
89-
$ReferenceNumber = [System.Version]::Parse($Reference)
90+
$ReferenceNumber = [System.Version]::Parse($Reference)
9091
} catch [FormatException] {
9192
Write-Log -Type "ERROR" -Object "The version number ""$Reference"" does not match $Format numbering"
9293
return $false
@@ -101,36 +102,23 @@ function Compare-Version {
101102
}
102103
"modified" {
103104
if ($Operator -in ("eq", "ne")) {
104-
# Build comparison command
105-
$Command = """$Version"" -$Operator ""$Reference"""
106-
Write-Log -Type "DEBUG" -Object $Command
107-
# Execute comparison
108-
$Result = Invoke-Expression -Command $Command
109-
# Return comparison result
110-
return $Result
105+
# Compare strings as-is
106+
$VersionNumber = $Version
107+
$ReferenceNumber = $Reference
111108
} else {
112109
# Parse version numbers
113-
$VersionNumbers = $Version.Split(".")
114-
$ReferenceNumbers = $Reference.Split(".")
115-
# Check comparison operator
116-
if ($Operator -in ("gt", "ge")) {
117-
# TODO implement
118-
# for ($i = 0; $i -lt $Count; $i++) {
119-
# if ($i -lt ($Count - 1)) {
120-
# $Command = """$($VersionNumbers[$i])"" -ge ""$($ReferenceNumbers[$i])"""
121-
# } else {
122-
# $Command = """$($VersionNumbers[$i])"" -Operator ""$($ReferenceNumbers[$i])"""
123-
# }
124-
# Write-Log -Type "DEBUG" -Object $Command
125-
# $Result = Invoke-Expression -Command $Command
126-
# if ($Result -eq $false) {
127-
# return $false
128-
# }
129-
# }
130-
} elseif ($Operator -in ("lt", "le")) {
131-
# TODO implement
132-
}
110+
$SemanticVersion = Select-String -InputObject $Version -Pattern '(\d+.\d+\d+)(?=\D*)' | ForEach-Object { $_.Matches.Value }
111+
$VersionNumber = [System.Version]::Parse($SemanticVersion)
112+
$SemanticReference = Select-String -InputObject $Reference -Pattern '(\d+.\d+\d+)(?=\D*)' | ForEach-Object { $_.Matches.Value }
113+
$ReferenceNumber = [System.Version]::Parse($SemanticReference)
133114
}
115+
# Build comparison command
116+
$Command = """$VersionNumber"" -$Operator ""$ReferenceNumber"""
117+
Write-Log -Type "DEBUG" -Object $Command
118+
# Execute comparison
119+
$Result = Invoke-Expression -Command $Command
120+
# Return comparison result
121+
return $Result
134122
}
135123
default {
136124
Write-Log -Type "ERROR" -Object "The $Format versionning format is not yet supported"

0 commit comments

Comments
 (0)