Skip to content

🐛 Make CurrentUser work properly and ignore profile setup #65

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
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions ModuleFast.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ $SCRIPT:DefaultSource = 'https://pwsh.gallery/index.json'

#region Public

enum InstallScope {
CurrentUser
}

function Install-ModuleFast {
<#
.SYNOPSIS
Expand Down Expand Up @@ -140,7 +144,7 @@ function Install-ModuleFast {
Installs a specific version of ImportExcel using

.EXAMPLE
Install-ModuleFast 'ImportExcel' -Destination 'CurrentUser' -NoProfileUpdate -NoPSModulePathUpdate
Install-ModuleFast 'ImportExcel' -Destination 'CurrentUser'

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.

Expand Down Expand Up @@ -232,24 +236,36 @@ function Install-ModuleFast {
#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
[Switch]$Plan,
#This will output the resulting modules that were installed.
[Switch]$PassThru
[Switch]$PassThru,
#Setting this to "CurrentUser" is the same as specifying the destination as 'Current'. This is a usability convenience.
[InstallScope]$Scope
)
begin {
trap {$PSCmdlet.ThrowTerminatingError($PSItem)}

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

# Get the current PSModulePath
$PSModulePaths = $env:PSModulePath.Split([Path]::PathSeparator, [StringSplitOptions]::RemoveEmptyEntries)

#Clear the ModuleFastCache if -Update is specified to ensure fresh lookups of remote module availability
if ($Update) {
Clear-ModuleFastCache
}

if ($Scope -eq [InstallScope]::CurrentUser) {
$Destination = 'CurrentUser'
}
if (-not $Destination) {
$Destination = $defaultRepoPath
} elseif ($IsWindows -and $Destination -eq 'CurrentUser') {
$windowsDefaultDocumentsPath = Join-Path ([Environment]::GetFolderPath('MyDocuments')) 'PowerShell/Modules'
$Destination = $windowsDefaultDocumentsPath
# if CurrentUser and is on Windows, we do not need to update the PSModulePath or the user profile.
# this allows for a similar experience to Install-Module and Install-PSResource
$NoPSModulePathUpdate = $true
$NoProfileUpdate = $true
}

# Autocreate the default as a convenience, otherwise require the path to be present to avoid mistakes
Expand Down
21 changes: 21 additions & 0 deletions ModuleFast.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,27 @@ Describe 'Install-ModuleFast' -Tag 'E2E' {
Install-ModuleFast @imfParams 'PreReleaseTest' -DestinationOnly -PassThru | Should -BeNullOrEmpty
}

#TODO: Possibly mock this so we don't touch the testing system documents directory
It 'Destination CurrentUser installs to $HOME\Documents\PowerShell\Modules' {
try {
Remove-Item $HOME\Documents\PowerShell\Modules\PrereleaseTest -Recurse -Force -ErrorAction SilentlyContinue
Install-ModuleFast @imfParams 'PrereleaseTest' -Destination CurrentUser
Resolve-Path $HOME\Documents\PowerShell\Modules\PrereleaseTest -EA Stop
} finally {
Remove-Item $HOME\Documents\PowerShell\Modules\PrereleaseTest -Recurse -Force -ErrorAction SilentlyContinue
}
}

It 'Scope CurrentUser installs to $HOME\Documents\PowerShell\Modules' {
try {
Remove-Item $HOME\Documents\PowerShell\Modules\PrereleaseTest -Recurse -Force -ErrorAction SilentlyContinue
Install-ModuleFast @imfParams 'PrereleaseTest' -Scope CurrentUser
Resolve-Path $HOME\Documents\PowerShell\Modules\PrereleaseTest -EA Stop
} finally {
Remove-Item $HOME\Documents\PowerShell\Modules\PrereleaseTest -Recurse -Force -ErrorAction SilentlyContinue
}
}

It '-DestinationOnly works on modules with dependencies' {
Install-ModuleFast @imfParams 'Az.Compute' -DestinationOnly -PassThru | Should -HaveCount 2
}
Expand Down