Skip to content
Open
Changes from 2 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
33 changes: 29 additions & 4 deletions src/init.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,40 @@ process {
}
}

# Resolve the exact installed version that satisfies the request (newest match), so the loaded
# module is deterministic instead of whatever PowerShell would auto-load from PSModulePath. When
# prerelease versions are not requested, never resolve to one; when a stable and a prerelease share
# a base version, the stable one wins.
$resolveParams = @{
Name = $Name
ErrorAction = 'SilentlyContinue'
}
if ($Version) {
$resolveParams['Version'] = $Version
}
$candidates = @(Get-InstalledPSResource @resolveParams)
if (-not $Prerelease) {
$candidates = @($candidates | Where-Object { -not $_.IsPrerelease })
}
Comment thread
MariusStorhaug marked this conversation as resolved.
$resolved = $candidates | Sort-Object -Property @(
@{ Expression = 'Version'; Descending = $true }
@{ Expression = 'IsPrerelease'; Descending = $false }
) | Select-Object -First 1
if (-not $resolved) {
throw "No installed '$Name' version satisfies the requested version '$Version'."
Comment thread
MariusStorhaug marked this conversation as resolved.
Outdated
}
Comment thread
MariusStorhaug marked this conversation as resolved.
Comment thread
MariusStorhaug marked this conversation as resolved.
Comment thread
MariusStorhaug marked this conversation as resolved.

$alreadyImported = Get-Module -Name $Name
if ($showInit) {
Write-Output 'Already imported:'
$alreadyImported | Format-List | Out-String
}
if (-not $alreadyImported) {
Write-Verbose "Importing module: $Name"
Import-Module -Name $Name
}
# Remove every already-loaded instance (all versions, including nested) so only the chosen version
# remains loaded, then import that exact version into the global session state so every subsequent
# command (info.ps1, the user script, clean.ps1) uses the selected version.
Get-Module -Name $Name -All | Remove-Module -Force -ErrorAction SilentlyContinue
Comment thread
MariusStorhaug marked this conversation as resolved.
Comment thread
MariusStorhaug marked this conversation as resolved.
Write-Verbose "Importing module: $Name $($resolved.Version)"
Import-Module -Name $Name -RequiredVersion $resolved.Version -Force -Global
Comment thread
MariusStorhaug marked this conversation as resolved.
Outdated

$providedToken = -not [string]::IsNullOrEmpty($env:PSMODULE_GITHUB_SCRIPT_INPUT_Token)
$providedClientID = -not [string]::IsNullOrEmpty($env:PSMODULE_GITHUB_SCRIPT_INPUT_ClientID)
Expand Down