-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathTest-VersionString.psm1
More file actions
177 lines (150 loc) · 5.64 KB
/
Copy pathTest-VersionString.psm1
File metadata and controls
177 lines (150 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Copyright (c) Microsoft Corporation. All rights reserved.
<#
.SYNOPSIS
Checks that a given Semantic Version is equal to, greater than, or less than an other version string
.NOTES
This function makes assumptions about the handling of "latest" versions.
Latest versions are defined as null, empty, or "Latest".
If a <Latest> string is provided as Version and EqualTo is used,
EqualTo must be a valid <Latest> value as well.
If a <Latest string is provided as Version and a Comparirson is used (GreaterThan or LessThan),
we make assumptions about what latest and comparisons mean.
We assume that <Latest> is always greater than any value (always return true),
unless the value provided for GreaterThan is a <Latest> value, in which they are equal and will return false.
We assume that <Latest> cannot be less than any value, including <Latest>, so it will always return false.
#>
Function Test-VersionString
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true, ParameterSetName="EqualTo")]
[Parameter(Mandatory=$true, ParameterSetName="Comparisons")]
[AllowNull()]
[AllowEmptyString()]
[String] $Version,
[Parameter(ParameterSetName="EqualTo")]
[AllowNull()]
[AllowEmptyString()]
[String] $EqualTo,
[Parameter(ParameterSetName="Comparisons")]
[AllowNull()]
[AllowEmptyString()]
[String] $GreaterThan,
[Parameter(ParameterSetName="Comparisons")]
[AllowNull()]
[AllowEmptyString()]
[String] $LessThan
)
$valid = $true
# If it's a latest version string
if (Test-LatestVersionString -Version $Version)
{
switch ($PSCmdlet.ParameterSetName)
{
'EqualTo'
{
# EqualTo must be a latest version string too
$valid = (Test-LatestVersionString -Version $EqualTo)
break
}
'Comparisons'
{
# Time to make some assumptions
# If GreaterThan is a latest version string
if (Test-LatestVersionString -Version $GreaterThan)
{
# Latest cannot be greater than Latest. They are equal to eachother.
$valid = $false
}
# else: Assume the "Latest" version is greater than any version
if ($LessThan)
{
# Assume the "Latest" version can't be less than any version
$valid = $false
}
break
}
}
Write-Output $valid
return
}
$parsedVersion, $parsedVersionPreRelease = Get-VersionFromString -Version $Version
$parsedEqualTo, $parsedEqualToPreRelease = Get-VersionFromString -Version $EqualTo
$parsedGreaterThan, $parsedGreaterThanPreRelease = Get-VersionFromString -Version $GreaterThan
$parsedLessThan, $parsedLessThanPreRelease = Get-VersionFromString -Version $LessThan
if ($EqualTo)
{
# We're checking the exact version
$valid = $parsedVersion -eq $parsedEqualTo -and $parsedVersionPreRelease -eq $parsedEqualToPreRelease
}
else
{
if ($GreaterThan)
{
# $parsedVersion is greater than the $parsedGreaterThan version
# Or $parsedVersion is equal to the $parsedGreaterThan version, but $Version is not a prerelease and the $GreaterThan version is a prerelease
$valid = ($parsedVersion -gt $parsedGreaterThan) -or (-not $parsedVersionPreRelease -and $parsedGreaterThanPreRelease -and ($parsedVersion -eq $parsedGreaterThan))
}
if ($valid -and $LessThan)
{
# $parsedVersion is less than the $parsedLessThan version
# Or $parsedVersion is equal to the $parsedLessThan version, but $Version is a prerelease and the $LessThan version is not a prerelease
$valid = ($parsedVersion -lt $parsedLessThan) -or ($parsedVersionPreRelease -and -not $parsedLessThanPreRelease -and ($parsedVersion -eq $parsedLessThan))
}
}
Write-Output $valid
}
Function Get-VersionFromString
{
Param
(
[String] $Version
)
if (Test-LatestVersionString -Version $Version)
{
Write-Output $null
Write-Output $null
return
}
$parsedVersion = $Version
if ($Version -match '\-')
{
$prereleaseParts = $Version -split '-'
if ($preReleaseParts.Length -ne 2)
{
throw [System.FormatException] 'Unrecognized pre-release version format detected.'
}
$parsedVersion = $preReleaseParts[0]
$preRelease = $preReleaseParts[1]
}
$versionObject = [System.Version] $parsedVersion
Write-Output $versionObject
Write-Output $preRelease
}
<#
.SYNOPSIS
Tests to see if a string represents a "Latest" version and returns true or false.
.DESCRIPTION
The DevToolsEngine assumes the following values as the "Latest" version:
* Null
* Empty
* White space
* "Latest" (case-invariant)
* "LatestPreRelease" (case-invariant)
#>
Function Test-LatestVersionString
{
Param
(
[Parameter(Position=0)]
[AllowNull()]
[String] $Version
)
Write-Output ([String]::IsNullOrWhiteSpace($Version) -or $Version -ieq 'Latest' -or $Version -ieq 'LatestPreRelease')
}
Export-ModuleMember -Function @(
'Test-VersionString',
'Get-VersionFromString',
'Test-LatestVersionString'
)