Skip to content

Commit 30d81cc

Browse files
Justin GroteJustin Grote
authored andcommitted
Fix comparisons and sort out Profile order
1 parent be1435c commit 30d81cc

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

ModuleFast.ps1

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,17 @@ function Install-ModuleFast {
4949
New-Item -ItemType Directory -Path $Destination -Force
5050
}
5151
}
52+
5253
# Should error if not present
53-
$Destination = Resolve-Path $Destination
54+
[string]$Destination = Resolve-Path $Destination
5455

55-
if ($Destination -ne $defaultRepoPath) {
56+
if ($defaultRepoPath -ne $Destination) {
5657
if (-not $NoProfileUpdate) {
5758
Write-Warning 'Parameter -Destination is set to a custom path. We assume you know what you are doing, so it will not automatically be added to your Profile but will be added to PSModulePath. Set -NoProfileUpdate to suppress this message in the future.'
5859
}
5960
$NoProfileUpdate = $true
6061
}
6162
if (-not $NoPSModulePathUpdate) {
62-
$pathUpdateMessage = "Update PSModulePath $($NoProfileUpdate ? '' : 'and CurrentUserAllHosts profile ')to include $Destination"
63-
if (-not $PSCmdlet.ShouldProcess($pathUpdateMessage, '', '')) { return }
64-
6563
Add-DestinationToPSModulePath -Destination $Destination -NoProfileUpdate:$NoProfileUpdate
6664
}
6765

@@ -1052,7 +1050,7 @@ function Get-ModuleInfoAsync {
10521050
$SCRIPT:__registrationIndex = $HttpClient.GetStringAsync($Endpoint, $CancellationToken).GetAwaiter().GetResult()
10531051
}
10541052

1055-
$SCRIPT:__registrationIndex
1053+
$registrationBase = $SCRIPT:__registrationIndex
10561054
| ConvertFrom-Json
10571055
| Select-Object -ExpandProperty Resources
10581056
| Where-Object {
@@ -1061,7 +1059,7 @@ function Get-ModuleInfoAsync {
10611059
| Sort-Object -Property '@type' -Descending
10621060
| Select-Object -ExpandProperty '@id' -First 1
10631061

1064-
$uri = "$registrationBase/$ModuleId/$Path"
1062+
$uri = "$registrationBase/$($ModuleId.ToLower())/$Path"
10651063
}
10661064

10671065
#TODO: System.Text.JSON serialize this with fancy generic methods in 7.3?
@@ -1074,33 +1072,37 @@ function Get-ModuleInfoAsync {
10741072
.SYNOPSIS
10751073
Adds an existing PowerShell Modules path to the current session as well as the profile
10761074
#>
1077-
function Add-DestinationToPSModulePath ([string]$Destination, [switch]$NoProfileUpdate) {
1075+
function Add-DestinationToPSModulePath {
1076+
[CmdletBinding(SupportsShouldProcess)]
1077+
param(
1078+
[string]$Destination,
1079+
[switch]$NoProfileUpdate
1080+
)
10781081
$ErrorActionPreference = 'Stop'
10791082
$Destination = Resolve-Path $Destination #Will error if it doesn't exist
10801083

10811084
# Check if the destination is in the PSModulePath
10821085
[string[]]$modulePaths = $env:PSModulePath -split [Path]::PathSeparator
10831086

10841087
if ($Destination -notin $modulePaths) {
1085-
Write-Warning "$Destination is not in current PSModulePath list. Adding to both the current session and Current User All Hosts profile."
1088+
$pathUpdateMessage = "Update PSModulePath $($NoProfileUpdate ? '' : 'and CurrentUserAllHosts profile ')to include $Destination"
1089+
if (-not $PSCmdlet.ShouldProcess($pathUpdateMessage, '', '')) { return }
10861090
$modulePaths += $Destination
10871091
$env:PSModulePath = $modulePaths -join [Path]::PathSeparator
1088-
} else {
1089-
Write-Warning 'The module repository is not in your PSModulePath. Please add it to use the modules.'
10901092
}
10911093

10921094
if (-not $NoProfileUpdate) {
10931095
$myProfile = $profile.CurrentUserAllHosts
10941096
if (-not (Test-Path $myProfile)) {
10951097
Write-Verbose 'User All Hosts profile not found, creating one.'
1096-
New-Item -ItemType File -Path $Destination -Force
1098+
New-Item -ItemType File -Path $myProfile -Force | Out-Null
10971099
}
1098-
$ProfileLine = "`$env:PSModulePath += [System.IO.Path]::PathSeparator + $Destination #Added by ModuleFast. If you dont want this, add -NoProfileUpdate to your command."
1100+
$ProfileLine = "`$env:PSModulePath += [System.IO.Path]::PathSeparator + $Destination #Added by ModuleFast. DO NOT EDIT THIS LINE. If you dont want this, add -NoProfileUpdate to your command."
10991101
if ((Get-Content -Raw $myProfile) -notmatch [Regex]::Escape($ProfileLine)) {
11001102
Write-Verbose "Adding $Destination to profile $myProfile"
11011103
Add-Content -Path $myProfile -Value $ProfileLine
11021104
} else {
1103-
Write-Verbose "$Destination PSModulePath addition detected in profile, skipping..."
1105+
Write-Verbose "PSModulePath $Destination already in profile, skipping..."
11041106
}
11051107
}
11061108
}
@@ -1237,6 +1239,11 @@ function ConvertTo-AuthenticationHeaderValue ([PSCredential]$Credential) {
12371239
return [Net.Http.Headers.AuthenticationHeaderValue]::New('Basic', $basicCredential)
12381240
}
12391241

1242+
#Get the hash of a string
1243+
function Get-StringHash ([string]$String, [string]$Algorithm = 'SHA256') {
1244+
(Get-FileHash -InputStream ([MemoryStream]::new([Encoding]::UTF8.GetBytes($String))) -Algorithm $algorithm).Hash
1245+
}
1246+
12401247
#endregion Helpers
12411248

12421249
# Export-ModuleMember Get-ModuleFast

0 commit comments

Comments
 (0)