ps/Modules/Load-PesterModules.ps1

74 lines
2.8 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
[CmdletBinding()]
param([switch]$DontReload)
## If the parameter was actually specified and it's false, then we should actually reload
## The business logic of this method is that unless told to do otherwise, we expect to unload and reload modules
$globalSuppressReload = !!$global:SuppressReload
$shouldReload = !$DontReload.IsPresent -or ($DontReload.IsPresent -and !$DontReload) -or !$globalSuppressReload
if ($shouldReload) {
Get-Module WebAdministration | Remove-Module -Force
}
$orderedModules = @(
'Cole.PowerShell.Developer'
'Alkami.PowerShell.Common'
'Alkami.Ops.Common'
'Alkami.Ops.SecretServer'
'Alkami.PowerShell.AD'
'Alkami.PowerShell.Configuration'
'Alkami.PowerShell.Services'
'Alkami.PowerShell.ServerManagement'
'Alkami.PowerShell.Choco'
'Alkami.PowerShell.IIS'
'Alkami.PowerShell.Database'
'Alkami.PowerShell.ServiceFabric'
'Alkami.DevOps.Certificates'
'Alkami.DevOps.Common'
'Alkami.DevOps.SqlReports'
'Alkami.DevOps.Installation'
'Alkami.DevOps.Operations'
'Alkami.DevOps.Inventory'
'Alkami.DevOps.Validations'
'Alkami.DevOps.SystemEngineering'
'Alkami.DevOps.TeamCity'
)
$rootFolder = $PSScriptRoot
$isTeamCityProcess = !([String]::IsNullOrEmpty($ENV:TEAMCITY_JRE)) #Same as Test-IsTeamCityProcess
foreach ($targetModuleName in $orderedModules) {
$targetModulePath = (Join-Path (Join-Path $rootFolder $targetModuleName) "$targetModuleName.psd1")
$targetModuleInternalsPath = (Join-Path (Join-Path $rootFolder $targetModuleName) "$targetModuleName.psm1")
try {
$existingModule = (Get-Module $targetModuleName)
if($null -ne $existingModule) {
$isInExpectedPath = ($existingModule.Path -eq $targetModuleInternalsPath)
if ($isInExpectedPath -and ($isTeamCityProcess -or $DontReload)) {
Write-Verbose "Skipping module $targetModuleName as it has already been loaded from $targetModulePath and we should not reload (TeamCity or DontReload flag)"
continue
}
if (!$isInExpectedPath) {
Write-Warning "[$targetModuleName] loaded from [$($existingModule.Path)] and not from [$targetModulePath]"
}
if (!$isInExpectedPath -or $shouldReload) {
Write-Verbose "Removing $targetModuleName"
Remove-Module $targetModuleName -Force
}
}
if ($null -eq (Get-Module $targetModuleName)) {
#If we've made it this far, the $targetModulePath module needs to be loaded
Import-Module $targetModulePath -Force -Global
Write-Verbose "Loaded $targetModuleName from $targetModulePath"
}
} catch {
Write-Host "`$orderedModules ($targetModuleName) Exception: $_"
}
}