PS.Severance.psm1

<#
.SYNOPSIS
PS.Severance is a PowerShell module that enables the creation of completely isolated PowerShell environments.
 
.DESCRIPTION
This module contains several functions that facilitate the management and configuration of multiple PowerShell environments.
It severes the management and configuration between different PowerShell environments, e.g. Console name and color, Environmental variables, command history, and module versions.
 
#>

$ErrorActionPreference = "Stop"

#Get public and private function definition files.
$Public  = @( Get-ChildItem -Path $PSScriptRoot\Public\*.ps1)
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private\*.ps1)

#Dot source the files
Foreach($import in ($Private+$Public)) #@($Public + $Private)
{
    Try {
        . $import.fullname #-ExecutionPolicy Bypass
    }
    Catch {
        Throw "Failed to import function $($import.fullname): $_"
    }
}
Try {
    $PSSLocation = ((Get-Location).Path -replace '\\',"/") -match ($env:PSSEnvRoot -replace '\\',"/")
    if ($PSSLocation) {
        $PssConsoleTitle = ((Get-Location).Path -split "\\") | Select-Object -Last 1
    }
    else {
        $PssConsoleTitle = ($PssConsoleTitle.ToString() -replace "$ENV:PSSConPrefix ")
    }
}
Catch {
    Throw "Failed to determine Environment using current location or console title: $_"
}

$CmdletAliasMatrix = @{
    "UPSSEV" = "Update-PSSEnvVariables"
    "GPSSEV" = "Get-PSSEnvVariables"
    "PSSCon"    = "New-PSSConsole"
    "NPSSEV" = "New-PSSEnvVariables"
    "PSSVsc"   = "New-PSSVSCodeWorkSpaceSession"
    "PSSWelcome" = "Invoke-PssWelcome"
}
Try {
    Foreach ($PublicFunction in $Public.Basename) {
        if ($CmdletAliasMatrix.ContainsValue($PublicFunction)) {
            $Alias = ($CmdletAliasMatrix.GetEnumerator() | Where-Object { $_.Value -eq $PublicFunction }).Key
            Set-Alias -Name $Alias -Value $PublicFunction
            Export-ModuleMember -Function $PublicFunction -Alias $Alias
        }
        else {
            Export-ModuleMember -Function $PublicFunction
        }
    }
}
Catch {
    Throw "Failed to set up module aliases and export functions: $_"
}

Try {
    if ($PssConsoleTitle -notmatch "PowerShell|pwsh") {
        Get-PSSEnvVariables -Environment $PssConsoleTitle -ErrorAction SilentlyContinue
        if ((Get-Location).Path -ne "$ENV:PSSEnvRoot\$PssConsoleTitle") { Set-Location "$ENV:PSSEnvRoot\$PssConsoleTitle" -ErrorAction SilentlyContinue }
        Set-PSReadLineOption -HistorySavePath .\CommandHistory.txt -ErrorAction SilentlyContinue

        if ($UseCustomModuleVersions -eq "True") {
            $CustomModulePath = "$ENV:PSSEnvRoot\$PssConsoleTitle\CustomModules"
            if (Test-Path $CustomModulePath) {
                $CustomModules = Get-ChildItem -Path $CustomModulePath -Directory
                Foreach ($CustomModule in $CustomModules) {
                    Remove-Module -Name $CustomModule.Name -ErrorAction SilentlyContinue
                    Import-Module $CustomModule.FullName -Scope Global -Force -ErrorAction Continue
                }
            }
        }
    }
    Elseif ($ENV:PssWelcomeAscii) {
        Invoke-PSSWelcome
    }
}
Catch {
    Throw "Failed to initialize environment $($PssConsoleTitle): $_"
}

if ($PSSFirstTimeRan) {Invoke-PSSGetStarted}