Compare commits

...

2 Commits

Author SHA1 Message Date
c62e9815ac feat: improve parse script 2025-04-23 22:21:46 +02:00
a3471ab94b feat: add script to test parsing 2025-04-23 22:21:04 +02:00
3 changed files with 203 additions and 105 deletions

View File

@ -1,110 +1,160 @@
[cmdletbinding()] param (
Param( [Parameter(Mandatory)]
[Parameter( [ValidateScript({
Mandatory= $true Test-Path $_ -PathType Leaf
)] })]
[ValidateScript({Test-Path $_ -PathType Leaf})] [string]$Path
[string]$Path
) )
begin process {
{ Add-Type -AssemblyName System.Management.Automation
$system_types = @('Parameter','ValidatePattern','ValidateScript','ValidateSet') $script_path_raw = Get-Item $Path
}
process
{
$script_path_raw = Get-Item $Path
$script_name = $script_path_raw.Name $script_name = $script_path_raw.Name
$script_path = $script_path_raw | Select-Object -ExpandProperty FullName $script_path = $script_path_raw | Select-Object -ExpandProperty FullName
$tokens = $null
$errors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile($script_path, [ref]$tokens, [ref]$errors)
$parse_errors = @() if ($errors.Count -gt 0) {
throw "Parse errors found: $($errors | ForEach-Object { $_.Message })"
$AST = [System.Management.Automation.Language.Parser]::ParseFile($script_path, [ref]$null, [ref]$parse_errors) }
if($null -eq $AST.ParamBlock) $paramBlock = $ast.ParamBlock
{ if (-not $paramBlock) {
throw "No 'Param(...)' block found: $($Path)" throw "No param() block found at the script level."
} }
$parameters = @() $cmdletBindingAttr = $paramBlock.Attributes | Where-Object { $_.TypeName.Name -eq 'CmdletBinding' }
foreach($param in $AST.ParamBlock.Parameters) $defaultParamSet = $cmdletBindingAttr.NamedArguments |
{ Where-Object { $_.ArgumentName -eq 'DefaultParameterSetName' } |
$param_name = $param.Name.Extent.Text Select-Object -ExpandProperty Argument |
$param_type = $null Select-Object -ExpandProperty Value
$param_mandatory = $false
$param_parameters = @() $parameters = @()
$param_validations = @()
foreach ($param in $paramBlock.Parameters) {
foreach($attr in $param.Attributes) $paramName = $param.Name.VariablePath.UserPath
{
if($system_types -contains $attr.TypeName.Name) # Fix the type name
{ $staticType = $param.StaticType
if ($staticType.IsGenericType -and $staticType.Name -eq 'Nullable`1') {
switch -Wildcard ($attr.TypeName.Name) $innerType = $staticType.GenericTypeArguments[0].Name
{ $paramType = "Nullable[$innerType]"
"Parameter" } else {
{ $paramType = $staticType.Name
foreach($named_argument in $attr.NamedArguments) }
{
if($named_argument.ArgumentName -eq 'Mandatory') $aliases = @()
{ $validations = @()
$param_mandatory = $named_argument.Argument.Extent.Text -eq '$true' $parameterSets = @()
} else $description = $null
{ $helpMessage = $null
$param_parameters += [pscustomobject]@{ foreach ($attr in $param.Attributes) {
name = $named_argument.ArgumentName switch ($attr.TypeName.Name) {
value = $named_argument.Argument.Extent.Text 'Parameter' {
}
} $mandatory = $false
} $parametersetname = 'Default'
} $position = $null
"Validate*" $frompipeline = $null
{ $frompipeline_by_property = $null
foreach($positional_argument in $attr.PositionalArguments) $helpmessage = $null
{
if($positional_argument.ScriptBlock) foreach ($arg in $attr.NamedArguments) {
{ switch ($arg.ArgumentName) {
$value = $positional_argument.ScriptBlock.Extent.Text 'Mandatory' {
} else $mandatory = $arg.Argument.Extent.Text -eq '$true'
{ }
$value = $positional_argument.Value 'Position' {
} $position = $arg.Argument.Value
$param_validations += }
[pscustomobject]@{ 'ParameterSetName' {
name = $attr.TypeName.Name $parametersetname = $arg.Argument.Value
value = $value }
} 'ValueFromPipeline' {
} $frompipeline = $arg.Argument.Extent.Text -eq '$true'
} }
} 'ValueFromPipelineByPropertyName' {
} else $frompipeline_by_property = $arg.Argument.Extent.Text -eq '$true'
{ }
if($attr.Extent.Text -like '`[*`]') 'HelpMessage' {
{ $helpMessage = $arg.Argument.Value
if($param_type -ne $null) }
{ }
Write-Warning "Multiple Types found for Parameter: $($attr.TypeName.Name) ($($Path))" }
continue
} $paramInfo = @{
$param_type = $attr.TypeName.Name name = $parametersetname
} mandatory = $mandatory
} }
}
$parameters += [pscustomobject]@{ if($null -ne $position) {
name = $param_name $paramInfo.position = $position
type = $param_type }
mandatory = $param_mandatory
arguments = $param_parameters if($null -ne $frompipeline) {
validations = $param_validations $paramInfo.valueFromPipeline = $frompipeline
} }
} if($null -ne $frompipeline_by_property) {
return [pscustomobject]@{ $paramInfo.valueFromPipelineByPropertyName = $frompipeline_by_property
name = $script_name }
path = $script_path
parameters = $parameters $parameterSets += [pscustomobject]$paramInfo
} break
}
'Alias' {
$aliases += $attr.PositionalArguments.Value
break
}
{ $_ -like 'Validate*' } {
$val = @{
type = $attr.TypeName.Name
}
if ($attr.TypeName.Name -eq 'ValidateRange') {
$val.min = $attr.PositionalArguments[0].Value
$val.max = $attr.PositionalArguments[1].Value
} elseif ($attr.TypeName.Name -eq 'ValidateSet') {
$val.values = $attr.PositionalArguments.Value
} elseif ($attr.TypeName.Name -eq 'ValidateScript') {
$val.script = $attr.PositionalArguments.ScriptBlock.Extent.Text
}
$validations += [pscustomobject]$val
break
}
}
}
if ($parameterSets.Count -eq 0) {
$parameterSets += [pscustomobject]@{
name = "Default"
mandatory = $false
}
}
$parameters += [pscustomobject]@{
name = $paramName
type = $paramType
aliases = $aliases
description = $description
helpMessage = $helpMessage
validations = $validations
parameterSets = $parameterSets
}
}
$result = [pscustomobject]@{
name = $script_name
path = $script_path
parameters = $parameters
cmdletBinding = [pscustomobject]@{
defaultParameterSetName = $defaultParamSet ?? "Default"
}
}
return $result
} }

View File

@ -0,0 +1,48 @@
[CmdletBinding(DefaultParameterSetName = 'Set1')]
param (
# Mandatory string with alias and validation, positional
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'Set1')]
[Parameter(Position = 2, Mandatory = $false, ParameterSetName = 'Set2')]
[Alias('n')]
[ValidateNotNullOrEmpty()]
[ValidateScript({
$_ -notlike 'undefined'
})]
[string]$Name,
# Optional int with default value, validated range
[Parameter(ParameterSetName = 'Set1')]
[ValidateRange(1, 100)]
[int]$Count = 10,
# Switch parameter
[Parameter(ParameterSetName = 'Set1')]
[switch]$VerboseMode,
# Accepts pipeline input by value
[Parameter(ValueFromPipeline = $true, ParameterSetName = 'Set2')]
[string]$InputObject,
# Accepts pipeline input by property name
[Parameter(ValueFromPipelineByPropertyName = $true, ParameterSetName = 'Set2')]
[string]$Property,
# Parameter that takes an array
[Parameter(ParameterSetName = 'Set1')]
[string[]]$Tags,
# Optional parameter with script block default
[Parameter(ParameterSetName = 'Set1')]
[datetime]$StartTime = (Get-Date),
# Mandatory parameter in a different parameter set
[Parameter(Mandatory = $true, ParameterSetName = 'Set3')]
[ValidateSet('Low', 'Medium', 'High')]
[string]$Priority,
# Nullable type
[Parameter(ParameterSetName = 'Set1')]
[Nullable[bool]]$IsEnabled
)
process {}

View File

@ -32,7 +32,7 @@ mod test {
#[test] #[test]
fn test_parse_powershell_file() { fn test_parse_powershell_file() {
let got = parse_powershell_file("powershell/get-metadata.ps1"); let got = parse_powershell_file("powershell/test-script.ps1");
assert!(got.is_ok()); assert!(got.is_ok());
} }
} }