feat: improve parse script

This commit is contained in:
itsscb 2025-04-23 22:21:46 +02:00
parent a3471ab94b
commit c62e9815ac

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')
}
process
{
$script_path_raw = Get-Item $Path $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)
{
throw "No 'Param(...)' block found: $($Path)"
} }
$paramBlock = $ast.ParamBlock
if (-not $paramBlock) {
throw "No param() block found at the script level."
}
$cmdletBindingAttr = $paramBlock.Attributes | Where-Object { $_.TypeName.Name -eq 'CmdletBinding' }
$defaultParamSet = $cmdletBindingAttr.NamedArguments |
Where-Object { $_.ArgumentName -eq 'DefaultParameterSetName' } |
Select-Object -ExpandProperty Argument |
Select-Object -ExpandProperty Value
$parameters = @() $parameters = @()
foreach($param in $AST.ParamBlock.Parameters) foreach ($param in $paramBlock.Parameters) {
{ $paramName = $param.Name.VariablePath.UserPath
$param_name = $param.Name.Extent.Text
$param_type = $null
$param_mandatory = $false
$param_parameters = @()
$param_validations = @()
foreach($attr in $param.Attributes) # Fix the type name
{ $staticType = $param.StaticType
if($system_types -contains $attr.TypeName.Name) if ($staticType.IsGenericType -and $staticType.Name -eq 'Nullable`1') {
{ $innerType = $staticType.GenericTypeArguments[0].Name
$paramType = "Nullable[$innerType]"
} else {
$paramType = $staticType.Name
}
switch -Wildcard ($attr.TypeName.Name) $aliases = @()
{ $validations = @()
"Parameter" $parameterSets = @()
{ $description = $null
foreach($named_argument in $attr.NamedArguments) $helpMessage = $null
{
if($named_argument.ArgumentName -eq 'Mandatory')
{
$param_mandatory = $named_argument.Argument.Extent.Text -eq '$true'
} else
{
$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
$frompipeline = $null
$frompipeline_by_property = $null
$helpmessage = $null
foreach ($arg in $attr.NamedArguments) {
switch ($arg.ArgumentName) {
'Mandatory' {
$mandatory = $arg.Argument.Extent.Text -eq '$true'
}
'Position' {
$position = $arg.Argument.Value
}
'ParameterSetName' {
$parametersetname = $arg.Argument.Value
}
'ValueFromPipeline' {
$frompipeline = $arg.Argument.Extent.Text -eq '$true'
}
'ValueFromPipelineByPropertyName' {
$frompipeline_by_property = $arg.Argument.Extent.Text -eq '$true'
}
'HelpMessage' {
$helpMessage = $arg.Argument.Value
} }
} }
} }
$paramInfo = @{
name = $parametersetname
mandatory = $mandatory
} }
"Validate*"
{ if($null -ne $position) {
foreach($positional_argument in $attr.PositionalArguments) $paramInfo.position = $position
{
if($positional_argument.ScriptBlock)
{
$value = $positional_argument.ScriptBlock.Extent.Text
} else
{
$value = $positional_argument.Value
} }
$param_validations +=
[pscustomobject]@{ if($null -ne $frompipeline) {
name = $attr.TypeName.Name $paramInfo.valueFromPipeline = $frompipeline
value = $value }
if($null -ne $frompipeline_by_property) {
$paramInfo.valueFromPipelineByPropertyName = $frompipeline_by_property
}
$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
} }
} }
} }
}
} else if ($parameterSets.Count -eq 0) {
{ $parameterSets += [pscustomobject]@{
if($attr.Extent.Text -like '`[*`]') name = "Default"
{ mandatory = $false
if($param_type -ne $null)
{
Write-Warning "Multiple Types found for Parameter: $($attr.TypeName.Name) ($($Path))"
continue
}
$param_type = $attr.TypeName.Name
}
} }
} }
$parameters += [pscustomobject]@{ $parameters += [pscustomobject]@{
name = $param_name name = $paramName
type = $param_type type = $paramType
mandatory = $param_mandatory aliases = $aliases
arguments = $param_parameters description = $description
validations = $param_validations helpMessage = $helpMessage
validations = $validations
parameterSets = $parameterSets
}
} }
} $result = [pscustomobject]@{
return [pscustomobject]@{
name = $script_name name = $script_name
path = $script_path path = $script_path
parameters = $parameters parameters = $parameters
cmdletBinding = [pscustomobject]@{
defaultParameterSetName = $defaultParamSet ?? "Default"
} }
} }
return $result
}