AST.psm1

[CmdletBinding()]
param()
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($PSCommandPath)
$script:PSModuleInfo = Test-ModuleManifest -Path "$PSScriptRoot\$baseName.psd1"
$script:PSModuleInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Debug $_ }
$scriptName = $script:PSModuleInfo.Name
Write-Debug "[$scriptName] - Importing module"
#region [functions] - [public]
Write-Debug "[$scriptName] - [functions] - [public] - Processing folder"
#region [functions] - [public] - [Get-FunctionAlias]
Write-Debug "[$scriptName] - [functions] - [public] - [Get-FunctionAlias] - Importing"
function Get-FunctionAlias {
    <#
        .SYNOPSIS
        Retrieves function aliases from a PowerShell script file.

        .DESCRIPTION
        Parses a specified PowerShell script file to identify function definitions and extract their associated aliases.
        Returns a custom object containing function names and their corresponding aliases.

        .EXAMPLE
        Get-FunctionAlias -Path "C:\Scripts\MyScript.ps1"

        Retrieves all function aliases defined in the specified script file.

        .EXAMPLE
        Get-FunctionAlias -Name "Get-Data" -Path "C:\Scripts\MyScript.ps1"

        Retrieves the alias information for the function named "Get-Data" from the specified script file.
    #>

    [CmdletBinding()]
    param (
        # The name of the function to search for. Defaults to all functions ('*').
        [Parameter()]
        [string] $Name = '*',

        # The path to the PowerShell script file to be parsed.
        [Parameter(Mandatory)]
        [string] $Path
    )

    # Parse the script file into an AST
    $ast = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$null, [ref]$null)

    # Extract function definitions
    $functions = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true)

    # Process each function and extract aliases
    $functions | ForEach-Object {
        $funcName = $_.Name
        $attributes = $_.FindAll({ $args[0] -is [System.Management.Automation.Language.AttributeAst] }, $true)
        $aliasAttr = $attributes | Where-Object { $_ -is [System.Management.Automation.Language.AttributeAst] -and $_.TypeName.Name -eq 'Alias' }

        if ($aliasAttr) {
            $aliases = $aliasAttr.PositionalArguments | ForEach-Object { $_.ToString().Trim('"', "'") }
            [PSCustomObject]@{
                Name  = $funcName
                Alias = $aliases
            }
        }
    } | Where-Object { $_.Name -like $Name }
}
Write-Debug "[$scriptName] - [functions] - [public] - [Get-FunctionAlias] - Done"
#endregion [functions] - [public] - [Get-FunctionAlias]
#region [functions] - [public] - [Get-FunctionName]
Write-Debug "[$scriptName] - [functions] - [public] - [Get-FunctionName] - Importing"
function Get-FunctionName {
    <#
        .SYNOPSIS
        Extracts function names from a specified PowerShell script.

        .DESCRIPTION
        Parses the given PowerShell script file and retrieves all function names
        defined within it. This function utilizes the PowerShell Abstract Syntax Tree (AST)
        to analyze the script and extract function definitions.

        .EXAMPLE
        Get-FunctionName -Path "C:\Scripts\MyScript.ps1"

        Retrieves all function names defined in the specified script file.

        .NOTES
        Uses PowerShell's AST to analyze script structure.
    #>


    [CmdletBinding()]
    param (
        # The path to the script file to parse
        [Parameter(Mandatory)]
        [string] $Path
    )

    # Parse the script file into an AST
    $ast = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$null, [ref]$null)

    # Extract function definitions
    $functions = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true)

    # Process each function and extract the name
    $functions | ForEach-Object {
        $_.Name
    }
}
Write-Debug "[$scriptName] - [functions] - [public] - [Get-FunctionName] - Done"
#endregion [functions] - [public] - [Get-FunctionName]
Write-Debug "[$scriptName] - [functions] - [public] - Done"
#endregion [functions] - [public]

#region Member exporter
$exports = @{
    Alias    = '*'
    Cmdlet   = ''
    Function = @(
        'Get-FunctionAlias'
        'Get-FunctionName'
    )
}
Export-ModuleMember @exports
#endregion Member exporter