Includes/PwSh.Fw.Write.psm1

# $Script:NS = "PwShFwOS"
$Script:PwShFwOSEnv = 'PROD' # 'DBG', 'DEV'
$Script:EnterFunctionTitle = ">> "
$Script:LeaveFunctionTitle = "<< "

<#
.SYNOPSIS
Get current PwSh.Fw.OS display environment
 
.DESCRIPTION
Return current PwSh.Fw.OS display environment
 
.EXAMPLE
Get-PwShFwOSEnv
 
.NOTES
General notes
#>

function Get-PwShFwOSEnv {
    [CmdletBinding()]
    [OutputType([String])]
    Param (
        # [Parameter(Mandatory = $true, ValueFromPipeLine = $true)][string]$string
    )
    Begin {
        # Write-PwShFwOSEnterFunction
    }

    Process {
        return $Script:PwShFwOSEnv
    }

    End {
        # Write-PwShFwOSLeaveFunction
    }
}

<#
.SYNOPSIS
Set current PwSh.Fw.OS display environment
 
.DESCRIPTION
PwSh.Fw.OS display environment can be DBG, DEV, PROD
In PROD mode, PwSh.Fw.OS will not display anything.
In DEV mode, PwSh.Fw.OS will display all messages.
 
.EXAMPLE
Set-PwShFwOSEnv -Value PROD
 
.NOTES
General notes
#>

function Set-PwShFwOSEnv {
    [CmdletBinding()]
    [OutputType([void])]
    Param (
        # Environment to set to PwSh.Fw.OS
        [ValidateSet('DBG', 'DEV', 'PROD')]
        [Parameter(Mandatory = $true, ValueFromPipeLine = $true)][string]$Value
    )
    Begin {
        # Write-PwShFwOSEnterFunction
    }

    Process {
        $Script:PwShFwOSEnv = $Value
    }

    End {
        # Write-PwShFwOSLeaveFunction
    }
}

function Write-PwShFwOSMessage {
    [CmdletBinding()]
    [OutputType([void])]
    Param (
        [AllowEmptyString()][AllowNull()]
        [Parameter(Mandatory = $false, ValueFromPipeLine = $true)][string]$Message
    )
    Begin {
    }

    Process {
        Write-Message @PSBoundParameters
    }

    End {
    }
}

function Write-PwShFwOSDebug {
    [CmdletBinding()]
    [OutputType([void])]
    Param (
        [Parameter(Mandatory = $false, ValueFromPipeLine = $true)][string]$Message
    )
    Begin {
    }

    Process {
        if ($Script:PwShFwOSEnv -eq 'DBG' -or $Script:PwShFwOSEnv -eq 'DEV') { Write-Debug @PSBoundParameters }
    }

    End {
    }
}

function Write-PwShFwOSDevel {
    [CmdletBinding()]
    [OutputType([void])]
    Param (
        [Parameter(Mandatory = $false, ValueFromPipeLine = $true)][string]$Message
    )
    Begin {
    }

    Process {
        if ($Script:PwShFwOSEnv -eq 'DEV') { Write-Devel @PSBoundParameters }
    }

    End {
    }
}

function Write-PwShFwOSEnterFunction {
    if ($Script:PwShFwOSEnv -eq 'DEV') {
        $callStack = Get-PSCallStack
        if ($callStack.Count -gt 1) {
            $message = "$($callStack[1].InvocationInfo.MyCommand.Module)"
            if (-not [string]::IsNullOrEmpty($message)) { $message += "\" }
            $message += $($callStack[1].Command)
            # $callStack[1] | ConvertTo-Json | Set-Content /tmp/callstack.txt
            # $callStack[1].InvocationInfo | ConvertTo-Json | ForEach-Object { Write-Devel $_ }
        }
        $message = $Script:EnterFunctionTitle + $message + "()"
        Write-Devel -Message $message
        Write-Indent
    }
}

function Write-PwShFwOSLeaveFunction() {
    # [CmdletBinding()]param(
    # [Parameter(Mandatory = $false, ValueFromPipeLine = $true)][string]$Message
    # )
    if ($Script:PwShFwOSEnv -eq 'DEV') {
        $callStack = Get-PSCallStack
        if ($callStack.Count -gt 1) {
            $message = "$($callStack[1].InvocationInfo.MyCommand.Module)"
            if (-not [string]::IsNullOrEmpty($message)) { $message += "\" }
            $message += $($callStack[1].Command)
            # $callStack[1] | fl *
        }
        $message = $Script:LeaveFunctionTitle + $message + "()"
        Write-Outdent
        Write-Devel -Message $message
    }
}