Public/Progress/Get-WriteBetterProgressSteps.ps1

# Module: Orbit.Tools
# Function: Helper
# Author: David Eberhardt
# Updated: 29-OCT-2021
# Status: Live




function Get-WriteBetterProgressSteps {
  <#
  .SYNOPSIS
    Max number of Steps Write-BetterProgress is used to calculate PercentComplete
  .DESCRIPTION
    Determines the number of times 'Write-BetterProgress' is called in the provided Code
  .PARAMETER Code
    Required. Code to read.
    Planned: If not provided, will use the code of the calling function
  .PARAMETER MaxId
    Optional. Number of cascaded levels to use.
    If not provided, will assume single level (ID 0)
  .EXAMPLE
    Get-WriteBetterProgressSteps -Code $Code -MaxId 1
 
    Runs through the provided Code object (or, if not provided, the code of the calling function)
    Returns integer value for each level discovered
  .INPUTS
    System.String, System.Integer
  .OUTPUTS
    System.Object
  .NOTES
    Helper function to create a dynamic count how often Write-BetterProgress is used in a command with a given ID.
    Used by the Function itself to determine how many steps there are to be calculated against.
  .COMPONENT
    SupportingFunction
  .FUNCTIONALITY
    Uses Write-Progress without its inherent clunkyness, improving on consistency
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/Orbit.Tools/Get-WriteBetterProgressSteps.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/Orbit.Tools/Write-BetterProgress.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/about/about_Supporting_Functions.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/
  .LINK
    Get-WriteBetterProgressSteps
    #>


  [CmdletBinding()]
  [OutputType([System.Int32])]
  param(
    [Parameter(Mandatory)]
    $Code,

    [Parameter()]
    [int]$MaxId = 0
  ) #param

  begin {
    #Show-OrbitFunctionStatus -Level Live
    #Write-Verbose -Message "[BEGIN ] $($MyInvocation.MyCommand)"

    # Setting Preference Variables according to Upstream settings
    if (-not $PSBoundParameters['Verbose']) { $VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference') }
    if (-not $PSBoundParameters['Confirm']) { $ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference') }
    if (-not $PSBoundParameters['WhatIf']) { $WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference') }
    $DebugPreference = if (-not $PSBoundParameters['Debug']) { $PSCmdlet.SessionState.PSVariable.GetValue('DebugPreference') } else { 'Continue' }
    $InformationPreference = if ( $PSBoundParameters['InformationAction']) { $PSCmdlet.SessionState.PSVariable.GetValue('InformationAction') } else { 'Continue' }

    #Initialising Counters
    $ScriptAst = [System.Management.Automation.Language.Parser]::ParseInput($Code, [ref] $null, [ref]$null)

    #Retrieving Call Stack
    $Stack = Get-PSCallStack
    $FunctionCalling = $Stack[1].FunctionName
    #$FunctionCalling = $MyInvocation.InvocationName

  } #begin

  process {
    #Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand)"

    0..$MaxId | ForEach-Object {
      Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand) - Processing available Steps for ID:$_"
      $Steps = ($ScriptAst.Extent.Text -Split "Write-BetterProgress -Id $_ " | Measure-Object | Select-Object -ExpandProperty Count) - 1 # Deducting two to be precise
      if ($PSBoundParameters['Debug']) { "Function: '$FunctionCalling': Steps for Level $_`: $Steps" | Write-Debug }
      Write-Output $Steps
    }
  } #process

  end {
    #Write-Verbose -Message "[END ] $($MyInvocation.MyCommand)"
  } #end
} # Get-WriteBetterProgressSteps