Skip to content

Commit 5fc16db

Browse files
committed
fixes JustinGrote#89, check powershell.config.json for PSModulePath
1 parent de426e2 commit 5fc16db

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

ModuleFast.psm1

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ function Install-ModuleFast {
254254
begin {
255255
trap {$PSCmdlet.ThrowTerminatingError($PSItem)}
256256

257+
$CustomPSModulePath = Get-CustomPSModulePath
257258
# Setup the Destination repository
258259
$defaultRepoPath = $(Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) 'powershell/Modules')
259260

@@ -269,7 +270,13 @@ function Install-ModuleFast {
269270
$Destination = 'CurrentUser'
270271
}
271272
if (-not $Destination) {
272-
$Destination = $defaultRepoPath
273+
$Destination =
274+
if ($CustomPSModulePath) {
275+
$CustomPSModulePath
276+
}
277+
else {
278+
$defaultRepoPath
279+
}
273280
} elseif ($IsWindows -and $Destination -eq 'CurrentUser') {
274281
$windowsDefaultDocumentsPath = Join-Path ([Environment]::GetFolderPath('MyDocuments')) 'PowerShell/Modules'
275282
$Destination = $windowsDefaultDocumentsPath
@@ -2159,6 +2166,38 @@ function Approve-Action {
21592166

21602167
return $ThisCmdlet.ShouldProcess($Target, $Action)
21612168
}
2169+
function Get-CustomPSModulePath {
2170+
<#
2171+
.DESCRIPTION
2172+
Gets the custom PSModulePath from the powershell.config.json file in the user's profile directory or $PSHOME.
2173+
user profile is preferred over global profile.
2174+
.PARAMETER Config
2175+
The path to the powershell.config.json file. If not specified, it will default to the user's profile directory.
2176+
this should only be used in testing scenarios.
2177+
#>
2178+
[cmdletbinding()]
2179+
param(
2180+
[String] $Config
2181+
)
2182+
if (-Not $Config) {
2183+
$Config = Join-Path (Split-Path $PROFILE.CurrentUserCurrentHost) 'powershell.config.json'
2184+
}
2185+
if (Test-Path -Path $Config -PathType Leaf) {
2186+
$json = Get-Content -LiteralPath $Config -Raw | ConvertFrom-Json
2187+
$LocalPSModulePath = ${json}?.PSModulePath
2188+
if (-Not [String]::IsNullOrEmpty($LocalPSModulePath)) {
2189+
return $LocalPSModulePath
2190+
}
2191+
}
2192+
$GlobalConfig = "$PSHOME\powershell.config.json"
2193+
if (Test-Path -Path $GlobalConfig -PathType Leaf) {
2194+
$json = Get-Content -LiteralPath $globalconfig -Raw | ConvertFrom-Json
2195+
$GlobalPSModulePath = ${json}?.PSModulePath
2196+
if (-Not [String]::IsNullOrEmpty($GlobalPSModulePath)) {
2197+
$GlobalPSModulePath
2198+
}
2199+
}
2200+
}
21622201

21632202
#endregion Helpers
21642203

ModuleFast.tests.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ InModuleScope 'ModuleFast' {
7272
}
7373
}
7474
}
75+
Describe 'Custom Module Path' {
76+
It 'Should respect powershell.config.json PSModulePath' {
77+
$jsonconfig = New-TemporaryFile
78+
$CustomPSModulePath = Join-Path (Split-Path $jsonconfig -Parent) -ChildPath 'CustomPSModulePath'
79+
[PSCustomObject]@{
80+
PSModulePath = $CustomPSModulePath
81+
} | ConvertTo-Json | Set-Content $jsonconfig
82+
Get-CustomPSModulePath $jsonconfig | Should -Be $CustomPSModulePath
83+
}
84+
}
7585

7686
Describe 'Import-ModuleManifest' {
7787
It 'Reads Dynamic Manifest' {
@@ -770,4 +780,3 @@ Describe 'Install-ModuleFast' -Tag 'E2E' {
770780
}
771781
}
772782
}
773-

0 commit comments

Comments
 (0)