Skip to content

Commit 3d17c2d

Browse files
trackdJustinGrote
andauthored
✨ CurrentUser Improvements (#65)
-Scope CurrentUser has been added to mimic the Install-Module behavior, and the automatic profile setup is excluded when done on Windows. --------- Co-authored-by: trackd <[email protected]> Co-authored-by: Justin Grote <[email protected]>
1 parent fe4d0f1 commit 3d17c2d

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

ModuleFast.psm1

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ $SCRIPT:DefaultSource = 'https://pwsh.gallery/index.json'
2828

2929
#region Public
3030

31+
enum InstallScope {
32+
CurrentUser
33+
}
34+
3135
function Install-ModuleFast {
3236
<#
3337
.SYNOPSIS
@@ -140,7 +144,7 @@ function Install-ModuleFast {
140144
Installs a specific version of ImportExcel using
141145
142146
.EXAMPLE
143-
Install-ModuleFast 'ImportExcel' -Destination 'CurrentUser' -NoProfileUpdate -NoPSModulePathUpdate
147+
Install-ModuleFast 'ImportExcel' -Destination 'CurrentUser'
144148
145149
Installs ImportExcel to the legacy PowerShell Modules folder in My Documents on Windows only, but does not update the PSModulePath or the user profile to include the new module path. This behavior is similar to Install-Module or Install-PSResource.
146150
@@ -232,24 +236,36 @@ function Install-ModuleFast {
232236
#Outputs the installation plan of modules not already available and needing to be installed to the pipeline as well as the console. This can be saved and provided to Install-ModuleFast at a later date. This is functionally the same as -WhatIf but without the additional WhatIf Output
233237
[Switch]$Plan,
234238
#This will output the resulting modules that were installed.
235-
[Switch]$PassThru
239+
[Switch]$PassThru,
240+
#Setting this to "CurrentUser" is the same as specifying the destination as 'Current'. This is a usability convenience.
241+
[InstallScope]$Scope
236242
)
237243
begin {
238244
trap {$PSCmdlet.ThrowTerminatingError($PSItem)}
239245

240246
# Setup the Destination repository
241247
$defaultRepoPath = $(Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) 'powershell/Modules')
242248

249+
# Get the current PSModulePath
250+
$PSModulePaths = $env:PSModulePath.Split([Path]::PathSeparator, [StringSplitOptions]::RemoveEmptyEntries)
251+
243252
#Clear the ModuleFastCache if -Update is specified to ensure fresh lookups of remote module availability
244253
if ($Update) {
245254
Clear-ModuleFastCache
246255
}
247256

257+
if ($Scope -eq [InstallScope]::CurrentUser) {
258+
$Destination = 'CurrentUser'
259+
}
248260
if (-not $Destination) {
249261
$Destination = $defaultRepoPath
250262
} elseif ($IsWindows -and $Destination -eq 'CurrentUser') {
251263
$windowsDefaultDocumentsPath = Join-Path ([Environment]::GetFolderPath('MyDocuments')) 'PowerShell/Modules'
252264
$Destination = $windowsDefaultDocumentsPath
265+
# if CurrentUser and is on Windows, we do not need to update the PSModulePath or the user profile.
266+
# this allows for a similar experience to Install-Module and Install-PSResource
267+
$NoPSModulePathUpdate = $true
268+
$NoProfileUpdate = $true
253269
}
254270

255271
# Autocreate the default as a convenience, otherwise require the path to be present to avoid mistakes

ModuleFast.tests.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,27 @@ Describe 'Install-ModuleFast' -Tag 'E2E' {
545545
Install-ModuleFast @imfParams 'PreReleaseTest' -DestinationOnly -PassThru | Should -BeNullOrEmpty
546546
}
547547

548+
#TODO: Possibly mock this so we don't touch the testing system documents directory
549+
It 'Destination CurrentUser installs to $HOME\Documents\PowerShell\Modules' {
550+
try {
551+
Remove-Item $HOME\Documents\PowerShell\Modules\PrereleaseTest -Recurse -Force -ErrorAction SilentlyContinue
552+
Install-ModuleFast @imfParams 'PrereleaseTest' -Destination CurrentUser
553+
Resolve-Path $HOME\Documents\PowerShell\Modules\PrereleaseTest -EA Stop
554+
} finally {
555+
Remove-Item $HOME\Documents\PowerShell\Modules\PrereleaseTest -Recurse -Force -ErrorAction SilentlyContinue
556+
}
557+
}
558+
559+
It 'Scope CurrentUser installs to $HOME\Documents\PowerShell\Modules' {
560+
try {
561+
Remove-Item $HOME\Documents\PowerShell\Modules\PrereleaseTest -Recurse -Force -ErrorAction SilentlyContinue
562+
Install-ModuleFast @imfParams 'PrereleaseTest' -Scope CurrentUser
563+
Resolve-Path $HOME\Documents\PowerShell\Modules\PrereleaseTest -EA Stop
564+
} finally {
565+
Remove-Item $HOME\Documents\PowerShell\Modules\PrereleaseTest -Recurse -Force -ErrorAction SilentlyContinue
566+
}
567+
}
568+
548569
It '-DestinationOnly works on modules with dependencies' {
549570
Install-ModuleFast @imfParams 'Az.Compute' -DestinationOnly -PassThru | Should -HaveCount 2
550571
}

0 commit comments

Comments
 (0)