Skip to content

Commit 0e60907

Browse files
author
James Brundage
committed
Adding CommandAst.ResolvedCommand (#157)
1 parent a2c51f5 commit 0e60907

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

New-PipeScript.ps1.ps1

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,18 @@
5151
# The script header.
5252
[Parameter(ValueFromPipelineByPropertyName)]
5353
[string]
54-
$Header
54+
$Header,
55+
56+
# If provided, will automatically create parameters.
57+
# Parameters will be automatically created for any unassigned variables.
58+
[Alias('AutoParameterize','AutoParameters')]
59+
[switch]
60+
$AutoParameter,
61+
62+
# The type used for automatically generated parameters.
63+
# By default, ```[PSObject]```.
64+
[type]
65+
$AutoParameterType = [PSObject]
5566
)
5667

5768
begin {
@@ -164,6 +175,24 @@
164175
$allEndBlocks += $end
165176
}
166177

178+
if ($AutoParameter) {
179+
$variableDefinitions = $Begin, $Process, $End |
180+
Where-Object { $_ } |
181+
Search-PipeScript -AstType VariableExpressionAST |
182+
Select-Object -ExpandProperty Result
183+
foreach ($var in $variableDefinitions) {
184+
$assigned = $var.GetAssignments()
185+
if ($assigned) { continue }
186+
$varName = $var.VariablePath.userPath.ToString()
187+
$ParametersToCreate[$varName] = @(
188+
@(
189+
"[Parameter(ValueFromPipelineByPropertyName)]"
190+
"[$($AutoParameterType.FullName -replace '^System\.')]"
191+
"$var"
192+
) -join [Environment]::NewLine
193+
)
194+
}
195+
}
167196
}
168197

169198
end {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$ExecutionContext.SessionState.InvokeCommand.GetCommand($this.CommandElements[0].ToString(), 'All')

0 commit comments

Comments
 (0)