-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
refactor(versions): Refactor 'versions.ps1' #3721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
niheaven
merged 23 commits into
ScoopInstaller:develop
from
niheaven:fix-version-compare
Nov 13, 2021
Merged
Changes from 12 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
09f44a4
fix(versions): Refactor `compare_versions`
niheaven 41bafef
fix(versions): Handle plus sign and improve test base
ddavness 6b6c4e3
fix(versions-test): Rearrange test cases
niheaven a803ef1
fix(versions): Refactor 'versions.ps1'
niheaven 18b6fea
Fix 'Get-InstalledVersion' error
niheaven a2d160d
Add functions' help
niheaven 1dec12c
Use '+' as post-release delimiter
niheaven 04bf0f9
Add JDK style version
niheaven d14bcb3
Using single quotes in test
niheaven 0741501
Improve functions' help
niheaven 1e8890c
Add pipeline support to functions
niheaven f8d3e3e
Use '.PARAMETER'
niheaven 4206179
Fix help texts
niheaven 0127ce4
Add deprecated warning for previous functions
niheaven fc7b27d
Add FIrefox style tests
niheaven d5b3ded
Misc changes from vode review
niheaven e8ec9b2
Change param name
niheaven 5e99ed2
Update lib/versions.ps1
niheaven a04f0cd
Merge branch 'develop' into fix-version-compare
niheaven 0efebc4
Merge branch 'develop' into fix-version-compare
niheaven 8537b0d
Fix 'nightly' version error
niheaven 113bb25
Fix pipeline input
niheaven c44f8ef
Add `force-update` config
niheaven File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,210 @@ | ||
| # versions | ||
| function latest_version($app, $bucket, $url) { | ||
| (manifest $app $bucket $url).version | ||
| function Get-LatestVersion { | ||
| <# | ||
| .SYNOPSIS | ||
| Get latest version of app | ||
| .DESCRIPTION | ||
| Get latest version of app from manifest | ||
| .PARAMETER App | ||
| App's name | ||
| .PARAMETER Bucket | ||
| Bucket which the app is belong to | ||
|
niheaven marked this conversation as resolved.
Outdated
|
||
| .PARAMETER URL | ||
| Remote app manifest's URI | ||
| #> | ||
| [OutputType([String])] | ||
| [CmdletBinding()] | ||
| param ( | ||
| [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] | ||
| [String] | ||
| $App, | ||
| [Parameter(Position = 1)] | ||
| [String] | ||
| $Bucket, | ||
| [Parameter(Position = 2)] | ||
| [String] | ||
| $URL | ||
| ) | ||
| return (manifest $App $Bucket $URL).version | ||
| } | ||
| function current_version($app, $global) { | ||
| @(versions $app $global)[-1] | ||
| } | ||
| function versions($app, $global) { | ||
| $appdir = appdir $app $global | ||
| if(!(test-path $appdir)) { return @() } | ||
|
|
||
| sort_versions (Get-ChildItem $appdir -dir -attr !reparsePoint | Where-Object { $null -ne $(Get-ChildItem $_.fullname) } | ForEach-Object { $_.name }) | ||
| function Select-CurrentVersion { | ||
|
niheaven marked this conversation as resolved.
|
||
| <# | ||
| .SYNOPSIS | ||
| Select current version of app | ||
| .DESCRIPTION | ||
| Select current version of installed app, from 'current\manifest.json' or modified time of version directory | ||
| .PARAMETER App | ||
| App's name | ||
| .PARAMETER Global | ||
| If global installed | ||
|
niheaven marked this conversation as resolved.
Outdated
|
||
| #> | ||
| [OutputType([String])] | ||
| [CmdletBinding()] | ||
| param ( | ||
| [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] | ||
| [String] | ||
| $App, | ||
| [Parameter(Position = 1)] | ||
| [Switch] | ||
| $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) { | ||
|
niheaven marked this conversation as resolved.
Outdated
|
||
| $currentVersion = $installedVersion[-1] | ||
| } else { | ||
| $currentVersion = $null | ||
| } | ||
| } | ||
| return $currentVersion | ||
| } | ||
|
|
||
| function version($ver) { | ||
| $ver -split '[\.-]' | ForEach-Object { | ||
| $num = $_ -as [int] | ||
| if($num) { $num } else { $_ } | ||
| function Get-InstalledVersion { | ||
|
niheaven marked this conversation as resolved.
|
||
| <# | ||
| .SYNOPSIS | ||
| Get installed version of app | ||
| .DESCRIPTION | ||
| Get all installed version of app, by checking version directories' 'install.json' | ||
|
niheaven marked this conversation as resolved.
|
||
| .PARAMETER App | ||
| App's name | ||
| .PARAMETER Global | ||
| If global installed | ||
|
niheaven marked this conversation as resolved.
Outdated
|
||
| #> | ||
| [OutputType([Object[]])] | ||
| [CmdletBinding()] | ||
| param ( | ||
| [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] | ||
| [String] | ||
| $App, | ||
| [Parameter(Position = 1)] | ||
| [Switch] | ||
| $Global | ||
| ) | ||
|
|
||
| $appPath = appdir $App $Global | ||
| if (!(Test-Path $appPath)) { | ||
|
niheaven marked this conversation as resolved.
Outdated
|
||
| 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_versions($a, $b) { | ||
| $ver_a = @(version $a) | ||
| $ver_b = @(version $b) | ||
|
|
||
| for($i=0;$i -lt $ver_a.length;$i++) { | ||
| if($i -gt $ver_b.length) { return 1; } | ||
| function Compare-Version { | ||
|
niheaven marked this conversation as resolved.
|
||
| <# | ||
| .SYNOPSIS | ||
| Compare versions | ||
| .DESCRIPTION | ||
|
niheaven marked this conversation as resolved.
Outdated
|
||
| Compare versions, mainly according to SemVer's rules | ||
|
niheaven marked this conversation as resolved.
|
||
| .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([Int])] | ||
| [CmdletBinding()] | ||
| param ( | ||
| [Parameter(Position = 0)] | ||
| [String] | ||
| $ReferenceVersion, | ||
| [Parameter(Position = 1, ValueFromPipeline = $true)] | ||
| [String] | ||
| $DifferenceVersion, | ||
| [String] | ||
| $Delimiter = '-' | ||
| ) | ||
|
|
||
| # Use '+' sign as post-release, see https://github.com/lukesampson/scoop/pull/3721#issuecomment-553718093 | ||
| $ReferenceVersion = $ReferenceVersion -replace '\+', '-' | ||
|
niheaven marked this conversation as resolved.
Outdated
|
||
| $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('.') } }) | ||
|
niheaven marked this conversation as resolved.
Outdated
|
||
| $splitDifferenceVersion = @($DifferenceVersion -split $Delimiter | ForEach-Object { if ($_ -match "^\d+$") { [Long]$_ } else { ($_ -replace '[a-zA-Z]+', '.$&.').Replace('..', '.').Trim('.') } }) | ||
|
|
||
| # don't try to compare int to string | ||
| if($ver_b[$i] -is [string] -and $ver_a[$i] -isnot [string]) { | ||
| $ver_a[$i] = "$($ver_a[$i])" | ||
| 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) { | ||
|
niheaven marked this conversation as resolved.
Outdated
|
||
| if ($splitDifferenceVersion[$i] -match "alpha|beta|rc|pre") { | ||
|
niheaven marked this conversation as resolved.
Outdated
|
||
| return -1 | ||
| } else { | ||
| return 1 | ||
| } | ||
| } | ||
| if ($i -ge $splitDifferenceVersion.Length) { | ||
| if ($splitReferenceVersion[$i] -match "alpha|beta|rc|pre") { | ||
|
niheaven marked this conversation as resolved.
Outdated
|
||
| return 1 | ||
| } else { | ||
| return -1 | ||
| } | ||
| } | ||
|
|
||
| if (($splitReferenceVersion[$i] -match "\.") -or ($splitDifferenceVersion[$i] -match "\.")) { | ||
|
niheaven marked this conversation as resolved.
Outdated
|
||
| $Result = Compare-Version $splitReferenceVersion[$i] $splitDifferenceVersion[$i] -Delimiter '\.' | ||
| if ($Result -ne 0) { | ||
| return $Result | ||
| } else { | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| if($ver_a[$i] -gt $ver_b[$i]) { return 1; } | ||
| if($ver_a[$i] -lt $ver_b[$i]) { return -1; } | ||
| 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 } | ||
| } | ||
| if($ver_b.length -gt $ver_a.length) { return -1 } | ||
|
|
||
| return 0 | ||
| } | ||
|
|
||
| # Deprecated | ||
|
niheaven marked this conversation as resolved.
|
||
| # 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 | ||
| $lesser = qsort ($rem | Where-Object { (& $fn $pivot $_) -lt 0 }) $fn | ||
|
|
||
| $greater = qsort ($rem | Where-Object { (& $fn $_ $pivot) -ge 0 }) $fn | ||
| $greater = qsort ($rem | Where-Object { (& $fn $pivot $_) -ge 0 }) $fn | ||
|
|
||
| return @() + $lesser + @($pivot) + $greater | ||
| } | ||
| function sort_versions($versions) { qsort $versions compare_versions } | ||
|
|
||
| # 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 | ||
| } | ||
|
niheaven marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.