Skip to content

Commit 4dfa1c3

Browse files
authored
🐛 -Update now verifies if the best remote match is locally installed
1 parent b40337d commit 4dfa1c3

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

ModuleFast.psm1

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,14 @@ function Get-ModuleFastPlan {
330330
#We dont need this to be ConcurrentList because we only manipulate it in the "main" runspace.
331331
[List[Task[String]]]$currentTasks = @()
332332

333+
333334
#This try finally is so that we can interrupt all http call tasks if Ctrl-C is pressed
334335
foreach ($moduleSpec in $ModulesToResolve) {
336+
#This is used to track the highest candidate if -Update was specified to force a remote lookup. If the candidate is still the most valid after remote lookup we can skip it without hitting disk to read the manifest again.
337+
[ModuleFastInfo]$bestLocalCandidate = $null
338+
335339
Write-Verbose "${moduleSpec}: Evaluating Module Specification"
336-
[ModuleFastInfo]$localMatch = Find-LocalModule $moduleSpec -Update:$Update
340+
[ModuleFastInfo]$localMatch = Find-LocalModule $moduleSpec -Update:$Update -BestCandidate:([ref]$bestLocalCandidate)
337341
if ($localMatch) {
338342
Write-Debug "${localMatch}: 🎯 FOUND satisfing version $($localMatch.ModuleVersion) at $($localMatch.Location). Skipping remote search."
339343
#TODO: Capture this somewhere that we can use it to report in the deploy plan
@@ -343,7 +347,6 @@ function Get-ModuleFastPlan {
343347
#If we get this far, we didn't find a manifest in this module path
344348
Write-Debug "${moduleSpec}: 🔍 No installed versions matched the spec. Will check remotely."
345349

346-
347350
$task = Get-ModuleInfoAsync @httpContext -Endpoint $Source -Name $moduleSpec.Name
348351
$taskSpecMap[$task] = $moduleSpec
349352
$currentTasks.Add($task)
@@ -509,6 +512,17 @@ function Get-ModuleFastPlan {
509512
$selectedEntry.PackageContent
510513
)
511514

515+
#If -Update was specified, we need to re-check that none of the selected modules are already installed.
516+
#TODO: Persist state of the local modules found to this point so we don't have to recheck.
517+
if ($Update -and $bestLocalCandidate -and $bestLocalCandidate.ModuleVersion -eq $selectedModule.ModuleVersion) {
518+
Write-Debug "${selectedModule}: ✅ -Update was specified and the best remote candidate matches what is locally installed, so we can skip this module."
519+
#TODO: Fix the flow so this isn't stated twice
520+
[void]$taskSpecMap.Remove($completedTask)
521+
[void]$currentTasks.Remove($completedTask)
522+
$tasksCompleteCount++
523+
continue
524+
}
525+
512526
#Check if we have already processed this item and move on if we have
513527
if (-not $modulesToInstall.Add($selectedModule)) {
514528
Write-Debug "$selectedModule already exists in the install plan. Skipping..."
@@ -518,6 +532,7 @@ function Get-ModuleFastPlan {
518532
$tasksCompleteCount++
519533
continue
520534
}
535+
521536
Write-Verbose "$selectedModule`: Added to install plan"
522537

523538
# HACK: Pwsh doesn't care about target framework as of today so we can skip that evaluation
@@ -606,6 +621,7 @@ function Get-ModuleFastPlan {
606621
throw
607622
}
608623
} while ($currentTasks.count -gt 0)
624+
609625
if ($modulesToInstall) { return $modulesToInstall }
610626
}
611627
CLEAN {
@@ -911,6 +927,11 @@ class ModuleFastSpec {
911927
$this.Initialize($ModuleSpec)
912928
}
913929

930+
ModuleFastSpec([ModuleFastInfo]$ModuleFastInfo) {
931+
$RequiredVersion = [VersionRange]::Parse("[$($ModuleFastInfo.ModuleVersion)]")
932+
$this.Initialize($ModuleFastInfo.Name, $requiredVersion, [guid]::Empty)
933+
}
934+
914935
ModuleFastSpec([string]$Name) {
915936
#Used as a reference handle for TryParse
916937
[ModuleSpecification]$moduleSpec = $null
@@ -1265,7 +1286,8 @@ function Find-LocalModule {
12651286
param(
12661287
[Parameter(Mandatory)][ModuleFastSpec]$ModuleSpec,
12671288
[string[]]$ModulePath = $($env:PSModulePath -split [Path]::PathSeparator),
1268-
[Switch]$Update
1289+
[Switch]$Update,
1290+
[ref]$BestCandidate
12691291
)
12701292
$ErrorActionPreference = 'Stop'
12711293

@@ -1388,7 +1410,12 @@ function Find-LocalModule {
13881410

13891411
if ($ModuleSpec.SatisfiedBy($candidateVersion)) {
13901412
if ($Update -and ($ModuleSpec.Max -ne $candidateVersion)) {
1391-
Write-Debug "${ModuleSpec}: Skipping $candidateVersion - The -Update was specified and the version does not exactly meet the upper bound of the spec, meaning there is a possible newer version remotely."
1413+
Write-Debug "${ModuleSpec}: Skipping $candidateVersion because -Update was specified and the version does not exactly meet the upper bound of the spec or no upper bound was specified at all, meaning there is a possible newer version remotely."
1414+
#We can use this ref later to find out if our best remote version matches what is installed without having to read the manifest again
1415+
if ($BestCandidate -and $manifestCandidate.ModuleVersion -gt $bestCandidate.Value.ModuleVersion) {
1416+
Write-Debug "${ModuleSpec}: New Best Candidate Version $($manifestCandidate.ModuleVersion)"
1417+
$BestCandidate.Value = $manifestCandidate
1418+
}
13921419
continue
13931420
}
13941421

0 commit comments

Comments
 (0)