forked from ScoopInstaller/Scoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversions.ps1
274 lines (253 loc) · 9.16 KB
/
versions.ps1
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# versions
function Get-LatestVersion {
<#
.SYNOPSIS
Get latest version of app from manifest
.PARAMETER AppName
App's name
.PARAMETER Bucket
Bucket which the app belongs to
.PARAMETER Uri
Remote app manifest's URI
#>
[OutputType([String])]
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[Alias('App')]
[String]
$AppName,
[Parameter(Position = 1)]
[String]
$Bucket,
[Parameter(Position = 2)]
[String]
$Uri
)
process {
return (manifest $AppName $Bucket $Uri).version
}
}
function Select-CurrentVersion {
<#
.SYNOPSIS
Select current version of installed app, from 'current\manifest.json' or modified time of version directory
.PARAMETER AppName
App's name
.PARAMETER Global
Globally installed application
#>
[OutputType([String])]
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[Alias('App')]
[String]
$AppName,
[Parameter(Position = 1)]
[Switch]
$Global
)
process {
$appPath = appdir $AppName $Global
if (Test-Path "$appPath\current" -PathType Container) {
$currentVersion = (installed_manifest $AppName 'current' $Global).version
if ($currentVersion -eq 'nightly') {
$currentVersion = (Get-Item "$appPath\current").Target | Split-Path -Leaf
}
} else {
$installedVersion = Get-InstalledVersion -AppName $AppName -Global:$Global
if ($installedVersion) {
$currentVersion = $installedVersion[-1]
} else {
$currentVersion = $null
}
}
return $currentVersion
}
}
function Get-InstalledVersion {
<#
.SYNOPSIS
Get all installed version of app, by checking version directories' 'install.json'
.PARAMETER AppName
App's name
.PARAMETER Global
Globally installed application
.NOTES
Versions are sorted from oldest to newest, i.e., latest installed version is the last one in the output array.
If no installed version found, empty array will be returned.
#>
[OutputType([Object[]])]
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[Alias('App')]
[String]
$AppName,
[Parameter(Position = 1)]
[Switch]
$Global
)
process {
$appPath = appdir $AppName $Global
if (Test-Path $appPath) {
$versions = @((Get-ChildItem "$appPath\*\install.json" | Sort-Object -Property LastWriteTimeUtc).Directory.Name)
return $versions | Where-Object { ($_ -ne 'current') -and ($_ -notlike '_*.old*') }
} else {
return @()
}
}
# Deprecated
# sort_versions (Get-ChildItem $appPath -dir -attr !reparsePoint | Where-Object { $null -ne $(Get-ChildItem $_.FullName) } | ForEach-Object { $_.Name })
}
function Compare-Version {
<#
.SYNOPSIS
Compare versions, mainly according to SemVer's rules
.PARAMETER ReferenceVersion
Specifies a version used as a reference for comparison
.PARAMETER DifferenceVersion
Specifies the version that are compared to the reference version
.PARAMETER Delimiter
Specifies the delimiter of versions
.OUTPUTS
System.Int32
'0' if DifferenceVersion is equal to ReferenceVersion,
'1' if DifferenceVersion is greater then ReferenceVersion,
'-1' if DifferenceVersion is less then ReferenceVersion
#>
[OutputType([Int32])]
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[AllowEmptyString()]
[String]
$ReferenceVersion,
[Parameter(Mandatory = $true, Position = 1, ValueFromPipeline = $true)]
[AllowEmptyString()]
[String]
$DifferenceVersion,
[String]
$Delimiter = '-'
)
process {
# Use '+' sign as post-release, see https://github.com/lukesampson/scoop/pull/3721#issuecomment-553718093
$ReferenceVersion, $DifferenceVersion = @($ReferenceVersion, $DifferenceVersion) -replace '\+', '-'
# Return 0 if versions are equal
if ($DifferenceVersion -eq $ReferenceVersion) {
return 0
}
# Preprocess versions (split, convert and separate)
$splitReferenceVersion = @(SplitVersion -Version $ReferenceVersion -Delimiter $Delimiter)
$splitDifferenceVersion = @(SplitVersion -Version $DifferenceVersion -Delimiter $Delimiter)
# Nightly versions are always equal
if ($splitReferenceVersion[0] -eq 'nightly' -and $splitDifferenceVersion[0] -eq 'nightly') {
return 0
}
for ($i = 0; $i -lt [Math]::Max($splitReferenceVersion.Length, $splitDifferenceVersion.Length); $i++) {
# '1.1-alpha' is less then '1.1'
if ($i -ge $splitReferenceVersion.Length) {
if ($splitDifferenceVersion[$i] -match 'alpha|beta|rc|pre') {
return -1
} else {
return 1
}
}
# '1.1' is greater then '1.1-beta'
if ($i -ge $splitDifferenceVersion.Length) {
if ($splitReferenceVersion[$i] -match 'alpha|beta|rc|pre') {
return 1
} else {
return -1
}
}
# If some parts of versions have '.', compare them with delimiter '.'
if (($splitReferenceVersion[$i] -match '\.') -or ($splitDifferenceVersion[$i] -match '\.')) {
$Result = Compare-Version -ReferenceVersion $splitReferenceVersion[$i] -DifferenceVersion $splitDifferenceVersion[$i] -Delimiter '.'
# If the parts are equal, continue to next part, otherwise return
if ($Result -ne 0) {
return $Result
} else {
continue
}
}
# Don't try to compare [Long] to [String]
if ($null -ne $splitReferenceVersion[$i] -and $null -ne $splitDifferenceVersion[$i]) {
if ($splitReferenceVersion[$i] -is [String] -and $splitDifferenceVersion[$i] -isnot [String]) {
$splitDifferenceVersion[$i] = "$($splitDifferenceVersion[$i])"
}
if ($splitDifferenceVersion[$i] -is [String] -and $splitReferenceVersion[$i] -isnot [String]) {
$splitReferenceVersion[$i] = "$($splitReferenceVersion[$i])"
}
}
# Compare [String] or [Long]
if ($splitDifferenceVersion[$i] -gt $splitReferenceVersion[$i]) {
return 1
}
if ($splitDifferenceVersion[$i] -lt $splitReferenceVersion[$i]) {
return -1
}
}
}
}
# Helper function
function SplitVersion {
<#
.SYNOPSIS
Split version by Delimiter, convert number string to number, and separate letters from numbers
.PARAMETER Version
Specifies a version
.PARAMETER Delimiter
Specifies the delimiter of version (Literal)
#>
[OutputType([Object[]])]
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[AllowEmptyString()]
[String]
$Version,
[String]
$Delimiter = '-'
)
process {
$Version = $Version -replace '[a-zA-Z]+', "$Delimiter$&$Delimiter"
return ($Version -split [Regex]::Escape($Delimiter) -ne '' | ForEach-Object { if ($_ -match '^\d+$') { [Long]$_ } else { $_ } })
}
}
# Deprecated
# Not used anymore in scoop core
function qsort($ary, $fn) {
warn '"qsort" is deprecated. Please avoid using it anymore.'
if ($null -eq $ary) { return @() }
if (!($ary -is [array])) { return @($ary) }
$pivot = $ary[0]
$rem = $ary[1..($ary.length - 1)]
$lesser = qsort ($rem | Where-Object { (& $fn $pivot $_) -lt 0 }) $fn
$greater = qsort ($rem | Where-Object { (& $fn $pivot $_) -ge 0 }) $fn
return @() + $lesser + @($pivot) + $greater
}
# Deprecated
# Not used anymore in scoop core
function sort_versions($versions) {
warn '"sort_versions" is deprecated. Please avoid using it anymore.'
qsort $versions Compare-Version
}
function compare_versions($a, $b) {
Show-DeprecatedWarning $MyInvocation 'Compare-Version'
# Please note the parameters' sequence
return Compare-Version -ReferenceVersion $b -DifferenceVersion $a
}
function latest_version($app, $bucket, $url) {
Show-DeprecatedWarning $MyInvocation 'Get-LatestVersion'
return Get-LatestVersion -AppName $app -Bucket $bucket -Uri $url
}
function current_version($app, $global) {
Show-DeprecatedWarning $MyInvocation 'Select-CurrentVersion'
return Select-CurrentVersion -AppName $app -Global:$global
}
function versions($app, $global) {
Show-DeprecatedWarning $MyInvocation 'Get-InstalledVersion'
return Get-InstalledVersion -AppName $app -Global:$global
}