Skip to content

Commit e485750

Browse files
author
Justin Grote
committed
Add some Update gating and tests
1 parent 3f8398c commit e485750

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

ModuleFast.psm1

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,23 @@ function Install-ModuleFast {
7676

7777
if ($plan.Count -eq 0) {
7878
if ($WhatIfPreference) {
79-
Write-Host -fore DarkGreen ' No modules found to install or all modules are already installed.'
79+
Write-Host -fore DarkGreen "`u{2705} No modules found to install or all modules are already installed."
8080
}
81-
Write-Verbose '✅ All required modules installed! Exiting.'
81+
#TODO: Deduplicate this with the end into its own function
82+
Write-Verbose "`u{2705} All required modules installed! Exiting."
8283
return
8384
}
8485

8586
if (-not $PSCmdlet.ShouldProcess($Destination, "Install $($plan.Count) Modules")) {
86-
Write-Host -fore DarkGreen '🚀 ModuleFast Install Plan BEGIN'
87+
Write-Host -fore DarkGreen "`u{1F680} ModuleFast Install Plan BEGIN"
8788
#TODO: Separate planned installs and dependencies
8889
$plan
8990
| Select-Object Name, @{N = 'Version'; E = { [ModuleFastSpec]::VersionToString($_.Required) } }
9091
| Sort-Object Name
9192
| Format-Table -AutoSize
9293
| Out-String
9394
| Write-Host -ForegroundColor DarkGray
94-
Write-Host -fore DarkGreen '🚀 ModuleFast Install Plan END'
95+
Write-Host -fore DarkGreen "`u{1F680} ModuleFast Install Plan END"
9596
return
9697
}
9798

@@ -106,9 +107,11 @@ function Install-ModuleFast {
106107
CancellationToken = $cancelSource.Token
107108
ModuleCache = $ModuleCache
108109
HttpClient = $httpClient
110+
Update = $Update
109111
}
110112
Install-ModuleFastHelper @installHelperParams
111113
Write-Progress -Id 1 -Activity 'Install-ModuleFast' -Completed
114+
Write-Verbose "`u{2705} All required modules installed! Exiting."
112115
}
113116

114117
function New-ModuleFastClient {
@@ -509,7 +512,8 @@ function Install-ModuleFastHelper {
509512
[string]$Destination,
510513
[string]$ModuleCache,
511514
[CancellationToken]$CancellationToken,
512-
[HttpClient]$HttpClient
515+
[HttpClient]$HttpClient,
516+
[switch]$Update
513517
)
514518
$ErrorActionPreference = 'Stop'
515519

@@ -521,6 +525,21 @@ function Install-ModuleFastHelper {
521525
Module = $module
522526
DownloadPath = Join-Path $ModuleCache "$($module.Name).$($module.Version).nupkg"
523527
}
528+
529+
$installPath = Join-Path $Destination $context.Module.Name $context.Module.Version
530+
if (Test-Path $installPath) {
531+
#TODO: Check for a corrupted module
532+
#TODO: Prerelease checking
533+
if (-not $Update) {
534+
throw "$($context.Module)`: Module already exists at $installPath and -Update wasn't specified. This is a bug"
535+
} else {
536+
Write-Verbose "$($context.Module)`: Module already exists at $installPath but -Update was specified. This can happen because we did in fact have the latest version. Skipping."
537+
continue
538+
}
539+
}
540+
541+
$context.InstallPath = $installPath
542+
524543
Write-Verbose "$module`: Starting Download for $($module.DownloadLink)"
525544
if (-not $module.DownloadLink) {
526545
throw "$module`: No Download Link found. This is a bug"
@@ -541,16 +560,14 @@ function Install-ModuleFastHelper {
541560
$streamTasks.RemoveAt($thisTaskIndex)
542561

543562
#We are going to extract these straight out of memory, so we don't need to write the nupkg to disk
544-
545-
$installPath = Join-Path $Destination $context.Module.Name $context.Module.Version
546-
Write-Verbose "$($context.Module): Starting Extract Job to $installPath"
563+
Write-Verbose "$($context.Module): Starting Extract Job to $($context.installPath)"
547564
# This is a sync process and we want to do it in parallel, hence the threadjob
548565
$installJob = Start-ThreadJob -ThrottleLimit 8 {
549566
param(
550-
[ValidateNotNullOrEmpty()][string]$InstallPath = $USING:installPath,
551567
[ValidateNotNullOrEmpty()]$stream = $USING:stream,
552568
[ValidateNotNullOrEmpty()]$context = $USING:context
553569
)
570+
$installPath = $context.InstallPath
554571
#TODO: Add a ".incomplete" marker file to the folder and remove it when done. This will allow us to detect failed installations
555572
$zip = [IO.Compression.ZipArchive]::new($stream, 'Read')
556573
[IO.Compression.ZipFileExtensions]::ExtractToDirectory($zip, $installPath)

ModuleFast.tests.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,13 @@ Describe 'Install-ModuleFast' -Tag 'E2E' {
425425
$installedVersions | Should -Contain '2.7.4'
426426
}
427427
It 'Installs when Maximumversion is lower than currently installed' {
428-
$DebugPreference = 'continue'
429428
Install-ModuleFast @imfParams 'Az.Accounts'
430429
Install-ModuleFast @imfParams @{ ModuleName = 'Az.Accounts'; MaximumVersion = '2.7.3' }
431430
Get-Module Az.Accounts -ListAvailable | Select-Object -ExpandProperty Version | Should -Contain '2.7.3'
432431
}
432+
It 'Only installs once when Update is specified' {
433+
Install-ModuleFast @imfParams 'Az.Accounts' -Update
434+
#This will error if the file already exists
435+
Install-ModuleFast @imfParams 'Az.Accounts' -Update
436+
}
433437
}

0 commit comments

Comments
 (0)