Skip to content

Commit ef82e50

Browse files
Add Get-FunctionName and -Alias (#11)
## Description This pull request includes changes to the `README.md` documentation, the addition of 2 new functions, and the removal of some obsolete code. ### Documentation Updates * [`README.md`](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L3-R3): Updated module description, installation instructions, and usage examples. Removed outdated examples and documentation sections. [[1]](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L3-R3) [[2]](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L15-R29) [[3]](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L47-L51) [[4]](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L67-R56) ### New Functions * [`src/functions/public/Get-FunctionAlias.ps1`](diffhunk://#diff-b3648733339fdf4ed730fba43cac9f8527f9927483116108a6e1d8056392a494R1-R51): Added a new function `Get-FunctionAlias` to retrieve function aliases from a PowerShell script file. * [`src/functions/public/Get-FunctionName.ps1`](diffhunk://#diff-6036d4a4965d6d1611bc4c77a3f97de19cbcc9dda18de43a9be06a22277a17dbR1-R37): Added a new function `Get-FunctionName` to extract function names from a specified PowerShell script. ### Code Removal * [`src/finally.ps1`](diffhunk://#diff-9b129ae41500f974ec52cf82cff6b579519ff2ce25e7f93dae0269434317ce76L1-L3): Removed verbose logging statements. * [`src/header.ps1`](diffhunk://#diff-bf5fadbc74186e4ef45ea67f13aa39ff1aa054d079df0853f7c2c7180b486f71L1-L3): Removed suppression message attribute and parameter block. * [`src/manifest.psd1`](diffhunk://#diff-89820c99acb3b881e314c7e358eea621a8687cb4e0c687f541b9dcc9f30cc5dcL1-L5): Removed the module manifest file. ### Tests * [`tests/Module.Tests.ps1`](diffhunk://#diff-a94f0e90abaaff044e3cb0327ce3a63a10cd61551269a414e4dc40ad2af8a951L2-R13): Updated tests to include new functions `Get-FunctionName` and `Get-FunctionAlias`. * [`tests/src/Test-Function.ps1`](diffhunk://#diff-acc15d75d49a37326a9b7d551a44356f13821f4ab425a228f1b8255c95132859L1-R11): Renamed from `src/functions/public/Test-PSModuleTemplate.ps1` and updated to reflect new function name. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent 23eb2c9 commit ef82e50

8 files changed

Lines changed: 115 additions & 39 deletions

File tree

README.md

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AST
22

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

55
## Prerequisites
66

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

1414
```powershell
15-
Install-PSResource -Name YourModuleName
16-
Import-Module -Name YourModuleName
15+
Install-PSResource -Name AST
16+
Import-Module -Name AST
1717
```
1818

1919
## Usage
2020

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

23-
### Example 1: Greet an entity
23+
### Example 1: Get the function name from a script
2424

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

2727
```powershell
28-
Greet-Entity -Name 'World'
29-
Hello, World!
30-
```
31-
32-
### Example 2
33-
34-
Provide examples for typical commands that a user would like to do with the module.
35-
36-
```powershell
37-
Import-Module -Name PSModuleTemplate
28+
Get-FunctionName -Path 'Test-Me.ps1'
29+
Test-Me
3830
```
3931

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

47-
## Documentation
48-
49-
Link to further documentation if available, or describe where in the repository users can find more detailed documentation about
50-
the module's functions and features.
51-
5239
## Contributing
5340

5441
Coder or not, you can contribute to the project! We welcome all contributions.
@@ -64,6 +51,6 @@ Please see the issues tab on this project and submit a new issue that matches yo
6451
If you do code, we'd love to have your contributions. Please read the [Contribution guidelines](CONTRIBUTING.md) for more information.
6552
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.
6653

67-
## Acknowledgements
54+
## Tools
6855

69-
Here is a list of people and projects that helped this project in some way.
56+
- [lzybkr/ShowPSAst](https://github.com/lzybkr/ShowPSAst)

src/finally.ps1

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function Get-FunctionAlias {
2+
<#
3+
.SYNOPSIS
4+
Retrieves function aliases from a PowerShell script file.
5+
6+
.DESCRIPTION
7+
Parses a specified PowerShell script file to identify function definitions and extract their associated aliases.
8+
Returns a custom object containing function names and their corresponding aliases.
9+
10+
.EXAMPLE
11+
Get-FunctionAlias -Path "C:\Scripts\MyScript.ps1"
12+
13+
Retrieves all function aliases defined in the specified script file.
14+
15+
.EXAMPLE
16+
Get-FunctionAlias -Name "Get-Data" -Path "C:\Scripts\MyScript.ps1"
17+
18+
Retrieves the alias information for the function named "Get-Data" from the specified script file.
19+
#>
20+
[CmdletBinding()]
21+
param (
22+
# The name of the function to search for. Defaults to all functions ('*').
23+
[Parameter()]
24+
[string] $Name = '*',
25+
26+
# The path to the PowerShell script file to be parsed.
27+
[Parameter(Mandatory)]
28+
[string] $Path
29+
)
30+
31+
# Parse the script file into an AST
32+
$ast = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$null, [ref]$null)
33+
34+
# Extract function definitions
35+
$functions = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true)
36+
37+
# Process each function and extract aliases
38+
$functions | ForEach-Object {
39+
$funcName = $_.Name
40+
$attributes = $_.FindAll({ $args[0] -is [System.Management.Automation.Language.AttributeAst] }, $true)
41+
$aliasAttr = $attributes | Where-Object { $_ -is [System.Management.Automation.Language.AttributeAst] -and $_.TypeName.Name -eq 'Alias' }
42+
43+
if ($aliasAttr) {
44+
$aliases = $aliasAttr.PositionalArguments | ForEach-Object { $_.ToString().Trim('"', "'") }
45+
[PSCustomObject]@{
46+
Name = $funcName
47+
Alias = $aliases
48+
}
49+
}
50+
} | Where-Object { $_.Name -like $Name }
51+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function Get-FunctionName {
2+
<#
3+
.SYNOPSIS
4+
Extracts function names from a specified PowerShell script.
5+
6+
.DESCRIPTION
7+
Parses the given PowerShell script file and retrieves all function names
8+
defined within it. This function utilizes the PowerShell Abstract Syntax Tree (AST)
9+
to analyze the script and extract function definitions.
10+
11+
.EXAMPLE
12+
Get-FunctionName -Path "C:\Scripts\MyScript.ps1"
13+
14+
Retrieves all function names defined in the specified script file.
15+
16+
.NOTES
17+
Uses PowerShell's AST to analyze script structure.
18+
#>
19+
20+
[CmdletBinding()]
21+
param (
22+
# The path to the script file to parse
23+
[Parameter(Mandatory)]
24+
[string] $Path
25+
)
26+
27+
# Parse the script file into an AST
28+
$ast = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$null, [ref]$null)
29+
30+
# Extract function definitions
31+
$functions = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true)
32+
33+
# Process each function and extract the name
34+
$functions | ForEach-Object {
35+
$_.Name
36+
}
37+
}

src/header.ps1

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/manifest.psd1

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/Module.Tests.ps1

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
Describe 'Module' {
2-
Context "Function: 'Test-PSModuleTemplate'" {
3-
It 'Should be able to call the function' {
4-
Test-PSModuleTemplate -Name 'World' | Should -Be 'Hello, World!'
5-
Write-Verbose (Test-PSModuleTemplate -Name 'World' | Out-String) -Verbose
2+
Context "Function: 'Get-FunctionName'" {
3+
It 'Get-FunctionName gets the function name' {
4+
$path = Join-Path $PSScriptRoot 'src\Test-Function.ps1'
5+
$functionName = Get-FunctionName -Path $path
6+
$functionName | Should -Be 'Test-Function'
7+
}
8+
}
9+
Context "Function: 'Get-FunctionAlias'" {
10+
It 'Get-FunctionAlias gets the function alias' {
11+
$path = Join-Path $PSScriptRoot 'src\Test-Function.ps1'
12+
$functionAlias = Get-FunctionAlias -Path $path
13+
$functionAlias.Alias | Should -Contain 'Test'
14+
$functionAlias.Alias | Should -Contain 'TestFunc'
15+
$functionAlias.Alias | Should -Contain 'Test-Func'
616
}
717
}
818
}

src/functions/public/Test-PSModuleTemplate.ps1 renamed to tests/src/Test-Function.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
function Test-PSModuleTemplate {
1+
function Test-Function {
22
<#
33
.SYNOPSIS
44
Performs tests on a module.
55
66
.EXAMPLE
7-
Test-PSModuleTemplate -Name 'World'
7+
Test-Function -Name 'World'
88
99
"Hello, World!"
1010
#>
11+
[Alias('Test', 'TestFunc')]
12+
[Alias('Test-Func')]
1113
[CmdletBinding()]
1214
param (
1315
# Name of the person to greet.

0 commit comments

Comments
 (0)