Skip to content

Commit

Permalink
Merge pull request #15 from webmd-health-services/bugfix/get-psreposi…
Browse files Browse the repository at this point in the history
…tory-fails-if-called-first-from-inside-prism

Bugfix: Get-PSRepository fails if called first from inside prism
  • Loading branch information
splatteredbits authored Jul 13, 2022
2 parents 998e750 + a911638 commit fc70a39
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 31 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 0.5.1

## Fixed

* `prism install` fails if `Get-PSRepository` hasn't been run before it.
* Prism commands are very slow in Windows PowerShell on Windows 10, Server 2012R2, and Server 2019 due to some overzealous logging.


# 0.5.0

## Changed
Expand Down
2 changes: 2 additions & 0 deletions Prism/Functions/Install-PrivateModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ function Install-PrivateModule
$repoByLocation[$trimmedRepoUrl] = $_.Name
}

$privateModulePathWildcard = Join-Path -Path $config.PSModulesPath -ChildPath '*'
$locks = Get-Content -Path $Configuration.LockPath | ConvertFrom-Json
$locks | Add-Member -Name 'PSModules' -MemberType NoteProperty -Value @() -ErrorAction Ignore
foreach( $module in $locks.PSModules )
{
$installedModules =
Get-Module -Name $module.name -ListAvailable -ErrorAction Ignore |
Where-Object 'Path' -Like $privateModulePathWildcard |
Add-Member -Name 'SemVer' -MemberType ScriptProperty -PassThru -Value {
$prerelease = $this.PrivateData['PSData']['PreRelease']
if( $prerelease )
Expand Down
55 changes: 36 additions & 19 deletions Prism/Functions/Invoke-Prism.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ function Invoke-Prism

$pkgMgmtPrefs = Get-PackageManagementPreference

Write-Debug 'AVAILABLE MODULES'
Get-Module -ListAvailable | Format-Table -AutoSize | Out-String | Write-Debug
Import-Module -Name 'PackageManagement' `
-MinimumVersion '1.3.2' `
-MaximumVersion '1.4.7' `
Expand All @@ -65,12 +63,6 @@ function Invoke-Prism
-Global `
-ErrorAction Stop `
@pkgMgmtPrefs
Write-Debug 'IMPORTED MODULES'
Get-Module | Format-Table -AutoSize | Out-String | Write-Debug

Write-Debug 'AVAILABLE MODULES'
Write-Debug "PSModulePath $($env:PSModulePath)"
Get-Module -ListAvailable | Format-Table -AutoSize | Out-String | Write-Debug
}

process
Expand Down Expand Up @@ -138,23 +130,48 @@ function Invoke-Prism
$lockExtension = ''
}

$lockPath = Join-Path -Path ($prismJsonPath | Split-Path -Parent) -ChildPath "$($lockBaseName).lock$($lockExtension)"
# private members that users aren't allowed to customize.
$config |
Add-Member -Name 'Path' -MemberType NoteProperty -Value $prismJsonPath -PassThru -Force |
Add-Member -Name 'File' -MemberType NoteProperty -Value $prismJsonFile -PassThru -Force |
Add-Member -Name 'LockPath' -MemberType NoteProperty -Value $lockPath -PassThru -Force |
Out-Null

$ignore = @{ 'ErrorAction' = 'Ignore' }
# public configuration that users can customize.
# Add-Member doesn't return an object if the member already exists, so these can't be part of the pipeline.
# Add-Member doesn't return an object if the member already exists, so these can't be part of one pipeline.
$config | Add-Member -Name 'PSModules' -MemberType NoteProperty -Value @() @ignore
$config | Add-Member -Name 'PSModulesDirectoryName' -MemberType NoteProperty -Value 'PSModules' @ignore

$privateModulePath =
Join-Path -Path $prismJsonFile.DirectoryName -ChildPath $config.PSModulesDirectoryName
$lockPath =
Join-Path -Path ($prismJsonPath |
Split-Path -Parent) -ChildPath "$($lockBaseName).lock$($lockExtension)"
$addMemberArgs = @{
MemberType = 'NoteProperty';
PassThru = $true;
# Force so users can't customize these properties.
Force = $true;
}
# Members that users aren't allowed to customize/override.
$config |
Add-Member -Name 'Path' -Value $prismJsonPath @addMemberArgs |
Add-Member -Name 'File' -Value $prismJsonFile @addMemberArgs |
Add-Member -Name 'LockPath' -Value $lockPath @addMemberArgs |
Add-Member -Name 'PSModulesPath' -Value $privateModulePath @addMemberArgs |
Out-Null

# This makes it so we can use PowerShell's module cmdlets as much as possible.
$privateModulePath = Join-Path -Path $prismJsonFile.DirectoryName -ChildPath $config.PSModulesDirectoryName
$env:PSModulePath = $privateModulePath
$privateModulePath = & {
# Prism's private module path, PSModules.
$config.PSModulesPath | Write-Output

# PackageManagement needs to be able to find and load PowerShellGet so it can get repositoriees,
# package sources, etc, so it and PowerShellGet have to be in PSModulePath, unfortunately.
Get-Module -Name 'PackageManagement','PowerShellGet' |
Select-Object -ExpandProperty 'Path' | # **\PSModules\MODULE\VERSION\MODULE.psd1
Split-Path -Parent | # **\PSModules\MODULE\VERSION
Split-Path -Parent | # **\PSModules\MODULE
Split-Path -Parent | # **\PSModules
Select-Object -Unique |
Write-Output
}
$env:PSModulePath = $privateModulePath -join [IO.Path]::PathSeparator
Write-Debug -Message "env:PSModulePath $($env:PSModulePath)"

switch( $Command )
{
Expand Down
2 changes: 1 addition & 1 deletion Prism/Prism.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
RootModule = 'Prism.psm1'

# Version number of this module.
ModuleVersion = '0.5.0'
ModuleVersion = '0.5.1'

# ID used to uniquely identify this module
GUID = '5b244346-40c9-4a50-a098-8758c19f7f25'
Expand Down
53 changes: 47 additions & 6 deletions Tests/install.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ BeforeAll {
[Parameter(Mandatory)]
[String] $Contents,

[String] $In
[String] $In = $script:testRoot
)

$path = 'prism.json'
Expand All @@ -51,7 +51,7 @@ BeforeAll {
[Parameter(Mandatory)]
[String] $Contents,

[String] $In
[String] $In = $script:testRoot
)

$path = 'prism.lock.json'
Expand All @@ -70,7 +70,7 @@ BeforeAll {
[Parameter(Mandatory)]
[hashtable] $Module,

[String] $In,
[String] $In = $script:testRoot,

[String] $UsingDirName = 'PSModules'
)
Expand Down Expand Up @@ -127,9 +127,15 @@ BeforeAll {
[hashtable] $WithParameters = @{}
)

Invoke-Prism -Command 'install' @WithParameters |
Out-String |
Write-Verbose
Push-Location $script:testRoot
try
{
Invoke-Prism -Command 'install' @WithParameters | Out-String | Write-Verbose
}
finally
{
Pop-Location
}
}
}

Expand Down Expand Up @@ -351,4 +357,39 @@ Describe 'prism install' {
WhenInstalling
ThenInstalled @{ 'NoOp' = '1.0.0' }
}

It 'should find repositories when Get-PSRepository has never been called before' {
GivenPrismFile @'
{
"PSModules": [
{
"Name": "NoOp",
"Version": "1.*"
}
]
}
'@
GivenLockFile @'
{
"PSModules": [
{
"name": "NoOp",
"version": "1.0.0",
"repositorySourceLocation": "https://www.powershellgallery.com/api/v2/"
}
]
}
'@
$prismJsonPath = Join-Path -Path $script:testRoot -ChildPath 'prism.json' -Resolve -ErrorAction Stop
$importPath = Join-Path -Path $PSScriptRoot -ChildPath '..\Prism' -Resolve
Start-Job {
$importPath = $using:importPath
$prismJsonPath = $using:prismJsonPath

Import-Module -Name $importPath
prism install -Path $prismJsonPath
} | Receive-Job -Wait -AutoRemoveJob
ThenSucceeded
ThenInstalled @{ 'NoOp' = '1.0.0' }
}
}
10 changes: 5 additions & 5 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@ environment:
matrix:
- job_name: PowerShell 7.2 on Windows
job_group: pwsh
appveyor_build_worker_image: Visual Studio 2022
appveyor_build_worker_image: Visual Studio 2022 # Windows Server 2019

- job_name: PowerShell 7.1 on macOS
job_group: pwsh
appveyor_build_worker_image: macOS

- job_name: Windows PowerShell 5.1/.NET 4.6.2
job_group: ps
appveyor_build_worker_image: Visual Studio 2013
appveyor_build_worker_image: Visual Studio 2013 # Windows Server 2012R2

- job_name: Windows PowerShell 5.1/.NET 4.8
job_group: ps
appveyor_build_worker_image: Visual Studio 2019
appveyor_build_worker_image: Visual Studio 2019 # Windows Server 2019

- job_name: PowerShell 6.2 on Windows
job_group: pwsh
appveyor_build_worker_image: Visual Studio 2015
appveyor_build_worker_image: Visual Studio 2015 # Windows Server 2012R2

- job_name: PowerShell 7.2 on Ubuntu
job_group: pwsh
appveyor_build_worker_image: Ubuntu

- job_name: PowerShell 7.1 on Windows
job_group: pwsh
appveyor_build_worker_image: Visual Studio 2019
appveyor_build_worker_image: Visual Studio 2019 # Windows Server 2019


artifacts:
Expand Down

0 comments on commit fc70a39

Please sign in to comment.