Compare commits
No commits in common. "c62e9815ac93e14104ac2527cbe28fd79bba5201" and "3fb7f0926e53d9562be2fcfbe8b8bb9a8208b5ad" have entirely different histories.
c62e9815ac
...
3fb7f0926e
@ -1,160 +1,110 @@
|
|||||||
param (
|
[cmdletbinding()]
|
||||||
[Parameter(Mandatory)]
|
Param(
|
||||||
[ValidateScript({
|
[Parameter(
|
||||||
Test-Path $_ -PathType Leaf
|
Mandatory= $true
|
||||||
})]
|
)]
|
||||||
|
[ValidateScript({Test-Path $_ -PathType Leaf})]
|
||||||
[string]$Path
|
[string]$Path
|
||||||
)
|
)
|
||||||
|
|
||||||
process {
|
begin
|
||||||
Add-Type -AssemblyName System.Management.Automation
|
{
|
||||||
$script_path_raw = Get-Item $Path
|
$system_types = @('Parameter','ValidatePattern','ValidateScript','ValidateSet')
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
|
||||||
|
|
||||||
if ($errors.Count -gt 0) {
|
$parse_errors = @()
|
||||||
throw "Parse errors found: $($errors | ForEach-Object { $_.Message })"
|
|
||||||
}
|
|
||||||
|
|
||||||
$paramBlock = $ast.ParamBlock
|
$AST = [System.Management.Automation.Language.Parser]::ParseFile($script_path, [ref]$null, [ref]$parse_errors)
|
||||||
if (-not $paramBlock) {
|
|
||||||
throw "No param() block found at the script level."
|
|
||||||
}
|
|
||||||
|
|
||||||
$cmdletBindingAttr = $paramBlock.Attributes | Where-Object { $_.TypeName.Name -eq 'CmdletBinding' }
|
if($null -eq $AST.ParamBlock)
|
||||||
|
{
|
||||||
$defaultParamSet = $cmdletBindingAttr.NamedArguments |
|
throw "No 'Param(...)' block found: $($Path)"
|
||||||
Where-Object { $_.ArgumentName -eq 'DefaultParameterSetName' } |
|
|
||||||
Select-Object -ExpandProperty Argument |
|
|
||||||
Select-Object -ExpandProperty Value
|
|
||||||
|
|
||||||
$parameters = @()
|
|
||||||
|
|
||||||
foreach ($param in $paramBlock.Parameters) {
|
|
||||||
$paramName = $param.Name.VariablePath.UserPath
|
|
||||||
|
|
||||||
# Fix the type name
|
|
||||||
$staticType = $param.StaticType
|
|
||||||
if ($staticType.IsGenericType -and $staticType.Name -eq 'Nullable`1') {
|
|
||||||
$innerType = $staticType.GenericTypeArguments[0].Name
|
|
||||||
$paramType = "Nullable[$innerType]"
|
|
||||||
} else {
|
|
||||||
$paramType = $staticType.Name
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$aliases = @()
|
$parameters = @()
|
||||||
$validations = @()
|
|
||||||
$parameterSets = @()
|
|
||||||
$description = $null
|
|
||||||
$helpMessage = $null
|
|
||||||
|
|
||||||
foreach ($attr in $param.Attributes) {
|
foreach($param in $AST.ParamBlock.Parameters)
|
||||||
switch ($attr.TypeName.Name) {
|
{
|
||||||
'Parameter' {
|
$param_name = $param.Name.Extent.Text
|
||||||
|
$param_type = $null
|
||||||
|
$param_mandatory = $false
|
||||||
|
$param_parameters = @()
|
||||||
|
$param_validations = @()
|
||||||
|
|
||||||
$mandatory = $false
|
foreach($attr in $param.Attributes)
|
||||||
$parametersetname = 'Default'
|
{
|
||||||
$position = $null
|
if($system_types -contains $attr.TypeName.Name)
|
||||||
$frompipeline = $null
|
{
|
||||||
$frompipeline_by_property = $null
|
|
||||||
$helpmessage = $null
|
|
||||||
|
|
||||||
foreach ($arg in $attr.NamedArguments) {
|
switch -Wildcard ($attr.TypeName.Name)
|
||||||
switch ($arg.ArgumentName) {
|
{
|
||||||
'Mandatory' {
|
"Parameter"
|
||||||
$mandatory = $arg.Argument.Extent.Text -eq '$true'
|
{
|
||||||
}
|
foreach($named_argument in $attr.NamedArguments)
|
||||||
'Position' {
|
{
|
||||||
$position = $arg.Argument.Value
|
if($named_argument.ArgumentName -eq 'Mandatory')
|
||||||
}
|
{
|
||||||
'ParameterSetName' {
|
$param_mandatory = $named_argument.Argument.Extent.Text -eq '$true'
|
||||||
$parametersetname = $arg.Argument.Value
|
} else
|
||||||
}
|
{
|
||||||
'ValueFromPipeline' {
|
|
||||||
$frompipeline = $arg.Argument.Extent.Text -eq '$true'
|
|
||||||
}
|
|
||||||
'ValueFromPipelineByPropertyName' {
|
|
||||||
$frompipeline_by_property = $arg.Argument.Extent.Text -eq '$true'
|
|
||||||
}
|
|
||||||
'HelpMessage' {
|
|
||||||
$helpMessage = $arg.Argument.Value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$paramInfo = @{
|
$param_parameters += [pscustomobject]@{
|
||||||
name = $parametersetname
|
name = $named_argument.ArgumentName
|
||||||
mandatory = $mandatory
|
value = $named_argument.Argument.Extent.Text
|
||||||
}
|
|
||||||
|
|
||||||
if($null -ne $position) {
|
|
||||||
$paramInfo.position = $position
|
|
||||||
}
|
|
||||||
|
|
||||||
if($null -ne $frompipeline) {
|
|
||||||
$paramInfo.valueFromPipeline = $frompipeline
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if ($parameterSets.Count -eq 0) {
|
"Validate*"
|
||||||
$parameterSets += [pscustomobject]@{
|
{
|
||||||
name = "Default"
|
foreach($positional_argument in $attr.PositionalArguments)
|
||||||
mandatory = $false
|
{
|
||||||
|
if($positional_argument.ScriptBlock)
|
||||||
|
{
|
||||||
|
$value = $positional_argument.ScriptBlock.Extent.Text
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
$value = $positional_argument.Value
|
||||||
|
}
|
||||||
|
$param_validations +=
|
||||||
|
[pscustomobject]@{
|
||||||
|
name = $attr.TypeName.Name
|
||||||
|
value = $value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
if($attr.Extent.Text -like '`[*`]')
|
||||||
|
{
|
||||||
|
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 = $paramName
|
name = $param_name
|
||||||
type = $paramType
|
type = $param_type
|
||||||
aliases = $aliases
|
mandatory = $param_mandatory
|
||||||
description = $description
|
arguments = $param_parameters
|
||||||
helpMessage = $helpMessage
|
validations = $param_validations
|
||||||
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
|
|
||||||
}
|
|
||||||
|
@ -1,48 +0,0 @@
|
|||||||
[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 {}
|
|
@ -32,7 +32,7 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_powershell_file() {
|
fn test_parse_powershell_file() {
|
||||||
let got = parse_powershell_file("powershell/test-script.ps1");
|
let got = parse_powershell_file("powershell/get-metadata.ps1");
|
||||||
assert!(got.is_ok());
|
assert!(got.is_ok());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user