From 729fdf1032399da350b659e55d8da65ee77534a5 Mon Sep 17 00:00:00 2001 From: DarkSideMilk Date: Sat, 9 Nov 2024 09:37:46 -0700 Subject: [PATCH] Add error handling if a module path doesn't exist for choco package install template --- .../PSGetModule/tools/chocolateyInstall.ps1 | 50 +++++++++++++++---- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/chocoTemplate/PSGetModule/tools/chocolateyInstall.ps1 b/chocoTemplate/PSGetModule/tools/chocolateyInstall.ps1 index be7fecd..7d7ed2d 100644 --- a/chocoTemplate/PSGetModule/tools/chocolateyInstall.ps1 +++ b/chocoTemplate/PSGetModule/tools/chocolateyInstall.ps1 @@ -42,21 +42,52 @@ if ($pp.RemoveAll) { # module may already be installed outside of Chocolatey, if so get it out of the current session Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue +#get the destination paths to install too + $destinationRootPath = New-Object -TypeName 'System.Collections.generic.list[System.Object]'; +$ps7Pth = (Join-Path -Path $env:ProgramFiles -ChildPath "PowerShell\Modules\$moduleName") +$ps5Pth = (Join-Path -Path $env:ProgramFiles -ChildPath "WindowsPowerShell\Modules\$moduleName") + if($pp.PS7Only) { - $destinationRootPath.add((Join-Path -Path $env:ProgramFiles -ChildPath "PowerShell\Modules\$moduleName")) + if (Test-Path ($ps7Pth | Split-Path)) { #check if parent path exists + $destinationRootPath.add($ps7Pth) + } else { + Write-Error "Powershell core module path doesn't exist but PS7Only was specified! install powershell-core package to install!" + Set-PowershellExitCode -exitcode 1 + exit 1; + } "Installing $modulename version $moduleVersion in pwsh 7+ system path only" | out-host; } elseif ($pp.PS5Only) { - $destinationRootPath.add((Join-Path -Path $env:ProgramFiles -ChildPath "WindowsPowerShell\Modules\$moduleName")) + if (Test-Path ($ps5Pth | Split-Path)) { #check if parent path exists + $destinationRootPath.add($ps5Pth) + } else { + Write-Error "Powershell 5.1 module path doesn't exist and ps5only was specified, install powershell package to install" + Set-PowershellExitCode -exitcode 1 + exit 1; + } "Installing $modulename version $moduleVersion in windows powershell 5.1 system path only" | out-host; } else { - #ps 5.1 installed system mods - $destinationRootPath.add((Join-Path -Path $env:ProgramFiles -ChildPath "WindowsPowerShell\Modules\$moduleName")) - #ps 7 installed system mods - $destinationRootPath.add((Join-Path -Path $env:ProgramFiles -ChildPath "PowerShell\Modules\$moduleName")) + #add ps5 path + if (Test-Path ($ps5Pth | Split-Path)) { #make sure parent path exists before adding it as a destination + $destinationRootPath.add($ps5Pth) + } else { + Write-Warning "Powershell 5.1 module path doesn't exist, install powershell package for best experience" + } + #add ps7 path if exists + if (Test-Path ($ps7Pth | Split-Path)) { #make sure parent path exists before adding it as a destination + $destinationRootPath.add($ps7Pth) + } else { + Write-Warning "Powershell core path doesn't exist, install powershell-core package for best experience" + } "Installing $modulename version $moduleVersion in both pwsh 7+ and windows powershell 5.1 system paths" | out-host; } - + +if ($destinationRootPath.count -eq 0) { + Write-Error "Neither powershell module path exists! Install powershell and or powershell-core and try again" + Set-PowershellExitCode -exitcode 1 + exit 1 +} + ForEach ($dest in $destinationRootPath) { $destPath = "$dest\$moduleVersion" Write-Verbose "Installing '$modulename' of version '$moduleVersion' to '$destPath'." @@ -89,7 +120,7 @@ ForEach ($dest in $destinationRootPath) { if (($pp.NoRemove)) { "'NoRemove' was specified, not removing any other installed versions of $modulename" | out-host; } else { - Write-Verbose "Finding and uninstalling/removing any other versions of $modulename" + Write-Verbose "Finding and uninstalling/removing any other versions (older or newer) of $modulename" "Finding and removing any other versions of $modulename so only the latest is available at system level" | out-host; ForEach ($destPath in $destinationRootPath) { @@ -119,4 +150,5 @@ $($destinationRootPath | ForEach-Object {"`t- $($_)\$moduleVersion`n"}) - Use 'Get-Help Function-name' to get inline help, append '-online' to open the web version - Use 'Get-Help about_fogapi' for an overview of the module ----------------------------------------------------------------------------- -"@ \ No newline at end of file +"@ +Set-PowershellExitCode -exitcode 0 \ No newline at end of file