forked from ScoopInstaller/Scoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversions.ps1
175 lines (159 loc) · 5.42 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
# versions
function Get-LatestVersion {
<#
.SYNOPSIS
Get latest version of app
.DESCRIPTION
Get latest version of app from manifest
#>
param (
[String]
# App's name
$App,
[String]
# Bucket which the app is belong to
$Bucket,
[String]
# Remote app manifest's URI
$URL
)
return (manifest $App $Bucket $URL).version
}
function Select-CurrentVersion {
<#
.SYNOPSIS
Select current version of app
.DESCRIPTION
Select current version of installed app, from 'current\manifest.json' or modified time of version directory
#>
param (
[String]
# App's name
$App,
[Switch]
# If global installed
$Global
)
$appPath = appdir $App $Global
if (Test-Path "$appPath\current") {
$currentVersion = (installed_manifest $App 'current' $Global).version
} else {
$installedVersion = Get-InstalledVersion -App $App -Global:$Global
if ($installedVersion) {
$currentVersion = $installedVersion[-1]
} else {
$currentVersion = $null
}
}
return $currentVersion
}
function Get-InstalledVersion {
<#
.SYNOPSIS
Get installed version of app
.DESCRIPTION
Get all installed version of app, by checking version directories' 'install.json'
#>
param (
[String]
# App's name
$App,
[Switch]
# If global installed
$Global
)
$appPath = appdir $App $Global
if (!(Test-Path $appPath)) {
return @()
} else {
return @((Get-ChildItem "$appPath\*\install.json" | Sort-Object -Property LastWriteTimeUtc).Directory.Name) -ne 'current'
}
# 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
.DESCRIPTION
Compare versions, mainly according to SemVer's rules
#>
[OutputType('System.Int32')]
[CmdletBinding()]
param (
[Parameter(Position = 0)]
[String]
# Specifies a version used as a reference for comparison.
$ReferenceVersion,
[Parameter(Position = 1)]
[String]
# Specifies the version that are compared to the reference version.
$DifferenceVersion,
[String]
# Specifies the delimiter of versions
$Delimiter = '-'
)
# Use '+' sign as post-release, see https://github.com/lukesampson/scoop/pull/3721#issuecomment-553718093
$ReferenceVersion = $ReferenceVersion -replace '\+', '-'
$DifferenceVersion = $DifferenceVersion -replace '\+', '-'
if ($DifferenceVersion -eq $ReferenceVersion) {
return 0
}
$splitReferenceVersion = @($ReferenceVersion -split $Delimiter | ForEach-Object { if ($_ -match "^\d+$") { [Long]$_ } else { ($_ -replace '[a-zA-Z]+', '.$&.').Replace('..', '.').Trim('.') } })
$splitDifferenceVersion = @($DifferenceVersion -split $Delimiter | ForEach-Object { if ($_ -match "^\d+$") { [Long]$_ } else { ($_ -replace '[a-zA-Z]+', '.$&.').Replace('..', '.').Trim('.') } })
if ($splitReferenceVersion[0] -eq 'nightly' -and $splitDifferenceVersion[0] -eq 'nightly') {
return 0
}
for ($i = 0; $i -lt [Math]::Max($splitReferenceVersion.Length, $splitDifferenceVersion.Length); $i++) {
if ($i -ge $splitReferenceVersion.Length) {
if ($splitDifferenceVersion[$i] -match "alpha|beta|rc|pre") {
return -1
} else {
return 1
}
}
if ($i -ge $splitDifferenceVersion.Length) {
if ($splitReferenceVersion[$i] -match "alpha|beta|rc|pre") {
return 1
} else {
return -1
}
}
if (($splitReferenceVersion[$i] -match "\.") -or ($splitDifferenceVersion[$i] -match "\.")) {
$Result = Compare-Version $splitReferenceVersion[$i] $splitDifferenceVersion[$i] -Delimiter '\.'
if ($Result -ne 0) {
return $Result
} else {
continue
}
}
if ($null -ne $splitReferenceVersion[$i] -and $null -ne $splitDifferenceVersion[$i]) {
# don't try to compare int to string
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])"
}
}
if ($splitDifferenceVersion[$i] -gt $splitReferenceVersion[$i]) { return 1 }
if ($splitDifferenceVersion[$i] -lt $splitReferenceVersion[$i]) { return -1 }
}
return 0
}
# Deprecated
# function qsort($ary, $fn) {
# 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
# }
#
# function sort_versions($versions) { qsort $versions Compare-Version }