Skip to content
Open
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
47 changes: 47 additions & 0 deletions src/functions/private/Read-AstDirectory.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
function Read-AstDirectory {
<#
.SYNOPSIS
Reads all PowerShell script files in a directory and processes them with Read-AstScriptFile.

.DESCRIPTION
This function retrieves all `.ps1` files in the specified directory and passes each file's path
to the `Read-AstScriptFile` function. It supports recursion to include subdirectories.

.EXAMPLE
Read-AstDirectory -DirPath "C:\Scripts" -RecurseDir $true

Output:
```powershell
Processing: C:\Scripts\Script1.ps1
Processing: C:\Scripts\Subfolder\Script2.ps1
```

Reads all `.ps1` files from `C:\Scripts` and its subdirectories and processes them.

.OUTPUTS
PSCustomObject

.NOTES
An object containing the AST, tokens, and errors from parsing the script.

.LINK
https://psmodule.io/Ast/Functions/Read-AstDirectory
#>
[OutputType([pscustomobject])]
[CmdletBinding()]
param(
# Specifies the directory path to search for `.ps1` script files.
[Parameter(Mandatory)]
[string] $DirPath,

# Indicates whether to search subdirectories recursively.
[Parameter()]
[bool] $RecurseDir
)

$files = Get-ChildItem -Path $DirPath -Filter '*.ps1' -File -Recurse:$RecurseDir

foreach ($file in $files) {
Read-AstScriptFile -FilePath $file.FullName
}
}
49 changes: 49 additions & 0 deletions src/functions/private/Read-AstScriptContent.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function Read-AstScriptContent {
<#
.SYNOPSIS
Parses a PowerShell script and returns its abstract syntax tree (AST), tokens, and errors.

.DESCRIPTION
This function takes a PowerShell script as a string and parses it using the PowerShell language parser.
It returns an object containing the AST representation, tokens, and any syntax errors.

.EXAMPLE
$script = "Get-Process"
Read-AstScriptContent -ScriptContent $script

Output:
```powershell
Ast : [System.Management.Automation.Language.ScriptBlockAst]
Tokens : {Get-Process, EndOfInput}
Errors : {}
```

Parses the provided script and returns the abstract syntax tree, tokens, and errors.

.OUTPUTS
PSCustomObject

.NOTES
An object containing the AST, tokens, and errors from parsing the script.

.LINK
https://psmodule.io/Ast/Functions/Read-AstScriptContent/
#>
[OutputType([pscustomobject])]
[CmdletBinding()]
param(
# The PowerShell script content to parse.
[Parameter(Mandatory)]
[string] $ScriptContent
)

$tokens = $null
$errors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseInput($ScriptContent, [ref]$tokens, [ref]$errors)

[pscustomobject]@{
Ast = $ast
Tokens = $tokens
Errors = $errors
}
}
51 changes: 51 additions & 0 deletions src/functions/private/Read-AstScriptFile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function Read-AstScriptFile {
<#
.SYNOPSIS
Parses a PowerShell script file and returns its AST, tokens, and errors.

.DESCRIPTION
Reads a PowerShell script file and processes it using the PowerShell parser.
Returns a custom object containing the file path, abstract syntax tree (AST),
tokens, and any errors encountered during parsing.

.EXAMPLE
Read-AstScriptFile -FilePath "C:\Scripts\example.ps1"

Output:
```powershell
Path : C:\Scripts\example.ps1
Ast : [System.Management.Automation.Language.ScriptBlockAst]
Tokens : {Token1, Token2, Token3}
Errors : {}
```

Parses the script file "example.ps1" and returns its AST, tokens, and any parsing errors.

.OUTPUTS
PSCustomObject

.NOTES
Contains the file path, AST, tokens, and errors.

.LINK
https://psmodule.io/Ast/Functions/Read-AstScriptFile/
#>

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

$tokens = $null
$errors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile($FilePath, [ref]$tokens, [ref]$errors)

[pscustomobject]@{
Path = $FilePath
Ast = $ast
Tokens = $tokens
Errors = $errors
}
}
102 changes: 82 additions & 20 deletions src/functions/public/Core/Get-AstScript.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@

Parses the provided PowerShell script string and returns its Ast, tokens, and any parsing errors.

.EXAMPLE
Get-AstScript -Path "C:\\Scripts" -Recurse

Parses all PowerShell script files in the "C:\\Scripts" directory and its subdirectories.

.EXAMPLE
Get-AstScript -Path @("C:\\Scripts\\example.ps1", "C:\\Scripts\\example2.ps1")

Parses multiple PowerShell script files and returns their Asts.

.EXAMPLE
Get-AstScript -Script @("Write-Host 'Hello'", "Write-Host 'World'")

Parses multiple PowerShell script strings and returns their Asts.

.EXAMPLE
"Write-Host 'Hello'", "Write-Host 'World'" | Get-AstScript

Parses multiple PowerShell script strings from the pipeline and returns their Asts.

.EXAMPLE
Get-ChildItem -Path "C:\\Scripts" -Filter "*.ps1" | Get-AstScript

Parses all PowerShell script files returned by Get-ChildItem.

.OUTPUTS
PSCustomObject

Expand All @@ -45,46 +70,83 @@
https://psmodule.io/Ast/Functions/Core/Get-AstScript/
#>
[outputType([System.Management.Automation.Language.ScriptBlockAst])]
[CmdletBinding()]
[CmdletBinding(DefaultParameterSetName = 'PipelineInput')]
param (
# The path to the PowerShell script file to be parsed.
# Validate using Test-Path
# The path(s) to PowerShell script file(s) or folder(s) to be parsed.
[Parameter(
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
ParameterSetName = 'Path'
)]
[ValidateScript({ Test-Path -Path $_ })]
[string] $Path,
[ValidateScript({
foreach ($p in $_) {
if (-not (Test-Path -Path $p)) { return $false }
}
return $true
})]
[string[]] $Path,

# The PowerShell script to be parsed.
# Process directories recursively
[Parameter(ParameterSetName = 'Path')]
[Parameter(ParameterSetName = 'PipelineInput')]
[switch] $Recurse,

# The PowerShell script(s) to be parsed.
[Parameter(
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
ParameterSetName = 'Script'
)]
[string] $Script
[string[]] $Script,

# Input from pipeline that will be automatically detected as path or script
[Parameter(
Mandatory,
ValueFromPipeline,
ParameterSetName = 'PipelineInput'
)]
[string[]] $InputObject
)

begin {}

process {
$tokens = $null
$errors = $null
switch ($PSCmdlet.ParameterSetName) {
'Path' {
$ast = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$tokens, [ref]$errors)
foreach ($p in $Path) {
# Check if the path is a directory
if (Test-Path -Path $p -PathType Container) {
Read-AstDirectory -DirPath $p -RecurseDir $Recurse
} else {
# Path is a file
Read-AstScriptFile -FilePath $p
}
}
}
'Script' {
$ast = [System.Management.Automation.Language.Parser]::ParseInput($Script, [ref]$tokens, [ref]$errors)
foreach ($scriptContent in $Script) {
Read-AstScriptContent -ScriptContent $scriptContent
}
}
'PipelineInput' {
# Default parameter set for handling pipeline input
if ($null -ne $InputObject) {
foreach ($item in $InputObject) {
# Check if input is a file path or directory
if (Test-Path -Path $item -ErrorAction SilentlyContinue) {
if (Test-Path -Path $item -PathType Container) {
Read-AstDirectory -DirPath $item -RecurseDir $Recurse
} else {
Read-AstScriptFile -FilePath $item
}
} elseif ($PSBoundParameters.ContainsKey('InputObject') -and
$InputObject.PSObject.Properties.Name -contains 'FullName' -and
(Test-Path -Path $InputObject.FullName -ErrorAction SilentlyContinue)) {
Read-AstScriptFile -FilePath $InputObject.FullName
} else {
Read-AstScriptContent -ScriptContent $item
}
}
}
}
}
[pscustomobject]@{
Ast = $ast
Tokens = $tokens
Errors = $errors
}
}

Expand Down
Loading