Created
September 7, 2023 16:48
-
-
Save ephos/d7d515d450c18eb9cf2919d5e006f52d to your computer and use it in GitHub Desktop.
Uses AST to split out functions from a mono-psm1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$psm1 = Get-Item -Path "./*.psm1" | |
# Parse the content to produce the AST | |
$ast = [System.Management.Automation.Language.Parser]::ParseFile($psm1, [ref]$null, [ref]$null) | |
# Find all function definitions | |
$functions = $ast.FindAll({ | |
param($node) | |
$node -is [System.Management.Automation.Language.FunctionDefinitionAst] | |
}, $true) | |
foreach ($function in $functions) { | |
# Extract function name | |
$functionName = $function.Name | |
# Find the comment-based help (if any) associated with the function | |
$helpComment = $function.GetHelpContent() | |
# Combine the help comment (if any) and the function's extent (its full text) to get the full function code | |
$functionCode = if ($helpComment) { | |
$helpComment.Extent.Text + "`n" + $function.Extent.Text | |
} else { | |
$function.Extent.Text | |
} | |
# Write the function code to its own file | |
New-Item -Path "./splits/$functionName.ps1" -ItemType File -Force | |
Set-Content -Path "./splits/$functionName.ps1" -Value $functionCode -Force | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment