Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AST

A PowerShell module for using the Abstract Syntax Tree (AST) to analyze any PowerShell code.
A PowerShell module for using the Abstract Syntax Tree (AST) on any PowerShell code.

## Prerequisites

Expand All @@ -12,29 +12,21 @@ This uses the following external resources:
To install the module from the PowerShell Gallery, you can use the following command:

```powershell
Install-PSResource -Name YourModuleName
Import-Module -Name YourModuleName
Install-PSResource -Name AST
Import-Module -Name AST
```

## Usage

Here is a list of example that are typical use cases for the module.

### Example 1: Greet an entity
### Example 1: Get the function name from a script

Provide examples for typical commands that a user would like to do with the module.
This example shows how to get the function name from a script.

```powershell
Greet-Entity -Name 'World'
Hello, World!
```

### Example 2

Provide examples for typical commands that a user would like to do with the module.

```powershell
Import-Module -Name PSModuleTemplate
Get-FunctionName -Path 'Test-Me.ps1'
Test-Me
```

### Find more examples
Expand All @@ -44,11 +36,6 @@ To find more examples of how to use the module, please refer to the [examples](e
Alternatively, you can use the Get-Command -Module 'This module' to find more commands that are available in the module.
To find examples of each of the commands you can use Get-Help -Examples 'CommandName'.

## Documentation

Link to further documentation if available, or describe where in the repository users can find more detailed documentation about
the module's functions and features.

## Contributing

Coder or not, you can contribute to the project! We welcome all contributions.
Expand All @@ -64,6 +51,6 @@ Please see the issues tab on this project and submit a new issue that matches yo
If you do code, we'd love to have your contributions. Please read the [Contribution guidelines](CONTRIBUTING.md) for more information.
You can either help by picking up an existing issue or submit a new one if you have an idea for a new feature or improvement.

## Acknowledgements
## Tools

Here is a list of people and projects that helped this project in some way.
- [lzybkr/ShowPSAst](https://github.com/lzybkr/ShowPSAst)
3 changes: 0 additions & 3 deletions src/finally.ps1

This file was deleted.

51 changes: 51 additions & 0 deletions src/functions/public/Get-FunctionAlias.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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 }
}
37 changes: 37 additions & 0 deletions src/functions/public/Get-FunctionName.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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
}
}
3 changes: 0 additions & 3 deletions src/header.ps1

This file was deleted.

5 changes: 0 additions & 5 deletions src/manifest.psd1

This file was deleted.

18 changes: 14 additions & 4 deletions tests/Module.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
Describe 'Module' {
Context "Function: 'Test-PSModuleTemplate'" {
It 'Should be able to call the function' {
Test-PSModuleTemplate -Name 'World' | Should -Be 'Hello, World!'
Write-Verbose (Test-PSModuleTemplate -Name 'World' | Out-String) -Verbose
Context "Function: 'Get-FunctionName'" {
It 'Get-FunctionName gets the function name' {
$path = Join-Path $PSScriptRoot 'src\Test-Function.ps1'
$functionName = Get-FunctionName -Path $path
$functionName | Should -Be 'Test-Function'
}
}
Context "Function: 'Get-FunctionAlias'" {
It 'Get-FunctionAlias gets the function alias' {
$path = Join-Path $PSScriptRoot 'src\Test-Function.ps1'
$functionAlias = Get-FunctionAlias -Path $path
$functionAlias.Alias | Should -Contain 'Test'
$functionAlias.Alias | Should -Contain 'TestFunc'
$functionAlias.Alias | Should -Contain 'Test-Func'
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
function Test-PSModuleTemplate {
function Test-Function {
<#
.SYNOPSIS
Performs tests on a module.

.EXAMPLE
Test-PSModuleTemplate -Name 'World'
Test-Function -Name 'World'

"Hello, World!"
#>
[Alias('Test', 'TestFunc')]
[Alias('Test-Func')]
[CmdletBinding()]
param (
# Name of the person to greet.
Expand Down