Skip to content

Commit 8de4104

Browse files
committed
fix(database): lazy load versions.ps1 for semantic version comparison
1 parent efc3b7f commit 8de4104

2 files changed

Lines changed: 86 additions & 75 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- **checkver:** Remove redundant always-true condition in GitHub checkver logic ([#6571](https://github.com/ScoopInstaller/Scoop/issues/6571))
3636
- **core:** Give `dark` higher priority when use `Extract-DarkArchive` ([#6637](https://github.com/ScoopInstaller/Scoop/issues/6637))
3737
- **checkver:** Harden github checkver ([#6641](https://github.com/ScoopInstaller/Scoop/issues/6641))
38+
- **scoop-search:** Select latest search result semantically ([#6643](https://github.com/ScoopInstaller/Scoop/issues/6643))
3839

3940
### Code Refactoring
4041

lib/database.ps1

Lines changed: 85 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,5 @@
11
# Description: Functions for interacting with the Scoop database cache
22

3-
if (-not (Get-Command Compare-Version -ErrorAction Ignore)) {
4-
. "$PSScriptRoot\versions.ps1"
5-
}
6-
7-
<#
8-
.SYNOPSIS
9-
Get the latest row from a set of Scoop database rows.
10-
.DESCRIPTION
11-
Compares the `version` property of each row semantically and returns the
12-
latest row. Returns `$null` when no rows are provided.
13-
#>
14-
function Get-LatestScoopDBRow {
15-
param(
16-
[Parameter(Mandatory)]
17-
[AllowEmptyCollection()]
18-
[object[]]
19-
$Rows
20-
)
21-
22-
if (-not $Rows -or $Rows.Count -eq 0) {
23-
return $null
24-
}
25-
26-
$latest = $Rows[0]
27-
for ($i = 1; $i -lt $Rows.Count; $i++) {
28-
$row = $Rows[$i]
29-
if ((Compare-Version -ReferenceVersion $latest.version -DifferenceVersion $row.version) -gt 0) {
30-
$latest = $row
31-
}
32-
}
33-
34-
return $latest
35-
}
36-
37-
<#
38-
.SYNOPSIS
39-
Return the semantically latest row or rows from a Scoop database result set.
40-
.DESCRIPTION
41-
Clones the schema of `Table` and imports the latest row per group when
42-
`GroupBy` is supplied, or the single latest row across the whole table when
43-
it is omitted. Returns an empty cloned table when the source table has no rows.
44-
.PARAMETER Table
45-
The source `System.Data.DataTable` returned from a Scoop database query.
46-
.PARAMETER GroupBy
47-
Optional column names used to group rows before semantic latest-row selection.
48-
.OUTPUTS
49-
System.Data.DataTable
50-
A cloned table containing the latest matching row for each requested scope.
51-
#>
52-
function Select-LatestScoopDBRow {
53-
param(
54-
[Parameter(Mandatory)]
55-
[System.Data.DataTable]
56-
$Table,
57-
[string[]]
58-
$GroupBy
59-
)
60-
61-
$latestRows = $Table.Clone()
62-
$rows = @($Table.Rows)
63-
if ($rows.Count -eq 0) {
64-
return $latestRows
65-
}
66-
67-
if ($GroupBy -and $GroupBy.Count -gt 0) {
68-
foreach ($group in ($rows | Group-Object -Property $GroupBy)) {
69-
$latestRows.ImportRow((Get-LatestScoopDBRow -Rows @($group.Group)))
70-
}
71-
} else {
72-
$latestRows.ImportRow((Get-LatestScoopDBRow -Rows $rows))
73-
}
74-
75-
return $latestRows
76-
}
77-
783
<#
794
.SYNOPSIS
805
Get SQLite .NET driver
@@ -438,6 +363,91 @@ function Get-ScoopDBItem {
438363
}
439364
}
440365

366+
<#
367+
.SYNOPSIS
368+
Get the latest row from a set of Scoop database rows.
369+
.DESCRIPTION
370+
Compares the `version` property of each row semantically and returns the
371+
latest row. Returns `$null` when no rows are provided.
372+
.PARAMETER Rows
373+
System.Object[]
374+
The rows to evaluate for the latest version. Each row must have a `version` property.
375+
.INPUTS
376+
System.Object[]
377+
.OUTPUTS
378+
System.Object
379+
The latest row based on semantic versioning, or `$null` if no rows are provided.
380+
#>
381+
function Get-LatestScoopDBRow {
382+
param(
383+
[Parameter(Mandatory)]
384+
[AllowEmptyCollection()]
385+
[object[]]
386+
$Rows
387+
)
388+
389+
if (-not $Rows -or $Rows.Count -eq 0) {
390+
return $null
391+
}
392+
393+
if (-not (Get-Command Compare-Version -ErrorAction Ignore)) {
394+
. "$PSScriptRoot\versions.ps1"
395+
}
396+
397+
$latest = $Rows[0]
398+
for ($i = 1; $i -lt $Rows.Count; $i++) {
399+
$row = $Rows[$i]
400+
if ((Compare-Version -ReferenceVersion $latest.version -DifferenceVersion $row.version) -gt 0) {
401+
$latest = $row
402+
}
403+
}
404+
405+
return $latest
406+
}
407+
408+
<#
409+
.SYNOPSIS
410+
Return the semantically latest row or rows from a Scoop database result set.
411+
.DESCRIPTION
412+
Clones the schema of `Table` and imports the latest row per group when
413+
`GroupBy` is supplied, or the single latest row across the whole table when
414+
it is omitted. Returns an empty cloned table when the source table has no rows.
415+
.PARAMETER Table
416+
System.Data.DataTable
417+
The source table returned from a Scoop database query.
418+
.PARAMETER GroupBy
419+
System.String[]
420+
Optional column names used to group rows before semantic latest-row selection.
421+
.OUTPUTS
422+
System.Data.DataTable
423+
A cloned table containing the latest matching row for each requested scope.
424+
#>
425+
function Select-LatestScoopDBRow {
426+
param(
427+
[Parameter(Mandatory)]
428+
[System.Data.DataTable]
429+
$Table,
430+
[string[]]
431+
$GroupBy
432+
)
433+
434+
$latestRows = $Table.Clone()
435+
$rows = @($Table.Rows)
436+
if ($rows.Count -eq 0) {
437+
return $latestRows
438+
}
439+
440+
if ($GroupBy -and $GroupBy.Count -gt 0) {
441+
foreach ($group in ($rows | Group-Object -Property $GroupBy)) {
442+
$latestRows.ImportRow((Get-LatestScoopDBRow -Rows @($group.Group)))
443+
}
444+
} else {
445+
$latestRows.ImportRow((Get-LatestScoopDBRow -Rows $rows))
446+
}
447+
448+
return $latestRows
449+
}
450+
441451
<#
442452
.SYNOPSIS
443453
Remove Scoop database item(s).

0 commit comments

Comments
 (0)