Skip to content

Commit ec7463c

Browse files
StartAutomatingStartAutomating
authored andcommitted
Adding TypeConstraintAST get_ResolvedCommand (#162)
1 parent 33750f7 commit ec7463c

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

PipeScript.types.ps1xml

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,57 @@ foreach ($attributeArg in $argsInOrder) {
110110
return $Parameter
111111
</GetScriptBlock>
112112
</ScriptProperty>
113+
<ScriptProperty>
114+
<Name>ResolvedCommand</Name>
115+
<GetScriptBlock>
116+
&lt;#
117+
.SYNOPSIS
118+
Resolves an Attribute to a CommandInfo
119+
.DESCRIPTION
120+
Resolves an Attribute to one or more CommandInfo.
121+
.EXAMPLE
122+
{
123+
[InvokePipeScript()]$null
124+
}.Ast.EndBlock.Statements[0].PipelineElements[0].Expression.Attribute.ResolvedCommand
125+
.EXAMPLE
126+
{
127+
[Microsoft.PowerShell.Core.GetCommand()]$null
128+
}.Ast.EndBlock.Statements[0].PipelineElements[0].Expression.Attribute.ResolvedCommand
129+
.EXAMPLE
130+
{
131+
[Get_Command()]$null
132+
}.Ast.EndBlock.Statements[0].PipelineElements[0].Expression.Attribute.ResolvedCommand
133+
.EXAMPLE
134+
{
135+
[GetCommand()]$null
136+
}.Ast.EndBlock.Statements[0].PipelineElements[0].Expression.Attribute.ResolvedCommand
137+
.EXAMPLE
138+
{
139+
[cmd()]$null
140+
}.EndBlock.Statements[0].PipelineElements[0].Expression.Attribute.ResolvedCommand
141+
#&gt;
142+
# Get the name of the transpiler.
143+
$transpilerStepName =
144+
if ($this.TypeName.IsGeneric) {
145+
$this.TypeName.TypeName.Name
146+
} else {
147+
$this.TypeName.Name
148+
}
149+
$decamelCase = [Regex]::new('(?&lt;=[a-z])(?=[A-Z])')
150+
@(
151+
# If a Transpiler exists by that name, it will be returned first.
152+
Get-Transpiler -TranspilerName $transpilerStepName
153+
# Then, any periods in the attribute name will be converted to slashes,
154+
$fullCommandName = $transpilerStepName -replace '\.','\' -replace
155+
'_','-' # and any underscores to dashes.
156+
157+
# Then, the first CamelCased code will have a - injected in between the CamelCase.
158+
$fullCommandName = $decamelCase.Replace($fullCommandName, '-', 1)
159+
# Now we will try to find the command.
160+
$ExecutionContext.SessionState.InvokeCommand.GetCommand($fullCommandName, 'All')
161+
)
162+
</GetScriptBlock>
163+
</ScriptProperty>
113164
</Members>
114165
</Type>
115166
<Type>
@@ -292,6 +343,62 @@ else {
292343
</ScriptMethod>
293344
</Members>
294345
</Type>
346+
<Type>
347+
<Name>System.Management.Automation.Language.TypeConstraintAst</Name>
348+
<Members>
349+
<ScriptProperty>
350+
<Name>ResolvedCommand</Name>
351+
<GetScriptBlock>
352+
&lt;#
353+
.SYNOPSIS
354+
Resolves an TypeConstraintAST to a CommandInfo
355+
.DESCRIPTION
356+
Resolves an TypeConstraintAST to one or more CommandInfo Objects.
357+
.EXAMPLE
358+
{
359+
[InvokePipeScript[a]]$null
360+
}.Ast.EndBlock.Statements[0].PipelineElements[0].Expression.Attribute.ResolvedCommand
361+
.EXAMPLE
362+
{
363+
[Microsoft.PowerShell.Core.GetCommand]$null
364+
}.Ast.EndBlock.Statements[0].PipelineElements[0].Expression.Attribute.ResolvedCommand
365+
.EXAMPLE
366+
{
367+
[Get_Command]$null
368+
}.Ast.EndBlock.Statements[0].PipelineElements[0].Expression.Attribute.ResolvedCommand
369+
.EXAMPLE
370+
{
371+
[GetCommand]$null
372+
}.Ast.EndBlock.Statements[0].PipelineElements[0].Expression.Attribute.ResolvedCommand
373+
.EXAMPLE
374+
{
375+
[cmd]$null
376+
}.EndBlock.Statements[0].PipelineElements[0].Expression.Attribute.ResolvedCommand
377+
#&gt;
378+
# Get the name of the transpiler.
379+
$transpilerStepName =
380+
if ($this.TypeName.IsGeneric) {
381+
$this.TypeName.TypeName.Name
382+
} else {
383+
$this.TypeName.Name
384+
}
385+
$decamelCase = [Regex]::new('(?&lt;=[a-z])(?=[A-Z])')
386+
@(
387+
# If a Transpiler exists by that name, it will be returned first.
388+
Get-Transpiler -TranspilerName $transpilerStepName
389+
# Then, any periods in the attribute name will be converted to slashes,
390+
$fullCommandName = $transpilerStepName -replace '\.','\' -replace
391+
'_','-' # and any underscores to dashes.
392+
393+
# Then, the first CamelCased code will have a - injected in between the CamelCase.
394+
$fullCommandName = $decamelCase.Replace($fullCommandName, '-', 1)
395+
# Now we will try to find the command.
396+
$ExecutionContext.SessionState.InvokeCommand.GetCommand($fullCommandName, 'All')
397+
)
398+
</GetScriptBlock>
399+
</ScriptProperty>
400+
</Members>
401+
</Type>
295402
<Type>
296403
<Name>System.Management.Automation.Language.VariableExpressionAst</Name>
297404
<Members>

0 commit comments

Comments
 (0)