11using System ;
22using System . Collections ;
33using System . Collections . Generic ;
4+ using System . Linq ;
45using System . Management . Automation ;
56using System . Management . Automation . Language ;
67using System . Management . Automation . Runspaces ;
@@ -10,52 +11,67 @@ namespace PSParallelPipeline;
1011
1112internal static class Extensions
1213{
13- internal static void AddFunctions (
14+ internal static InitialSessionState AddFunctions (
1415 this InitialSessionState initialSessionState ,
15- string [ ] functionsToAdd ,
16+ string [ ] ? functionsToAdd ,
1617 PSCmdlet cmdlet )
1718 {
18- foreach ( string function in functionsToAdd )
19+ if ( functionsToAdd is not null )
1920 {
20- CommandInfo ? commandInfo = cmdlet
21- . InvokeCommand
22- . GetCommand ( function , CommandTypes . Function ) ;
23-
24- if ( commandInfo is null )
21+ foreach ( string function in functionsToAdd )
2522 {
26- continue ;
23+ CommandInfo ? commandInfo = cmdlet
24+ . InvokeCommand
25+ . GetCommand ( function , CommandTypes . Function ) ;
26+
27+ if ( commandInfo is null )
28+ {
29+ continue ;
30+ }
31+
32+ initialSessionState . Commands . Add ( new SessionStateFunctionEntry (
33+ name : function ,
34+ definition : commandInfo . Definition ) ) ;
2735 }
28-
29- initialSessionState . Commands . Add ( new SessionStateFunctionEntry (
30- name : function ,
31- definition : commandInfo . Definition ) ) ;
3236 }
37+
38+ return initialSessionState ;
3339 }
3440
35- internal static void AddVariables (
41+ internal static InitialSessionState AddVariables (
3642 this InitialSessionState initialSessionState ,
37- Hashtable variables ,
43+ Hashtable ? variables ,
3844 PSCmdlet cmdlet )
3945 {
40- foreach ( DictionaryEntry pair in variables )
46+ if ( variables is not null )
4147 {
42- cmdlet . ThrowIfVariableIsScriptBlock ( pair . Value ) ;
43- initialSessionState . Variables . Add ( new SessionStateVariableEntry (
44- name : LanguagePrimitives . ConvertTo < string > ( pair . Key ) ,
45- value : pair . Value ,
46- description : null ) ) ;
48+ foreach ( DictionaryEntry pair in variables )
49+ {
50+ cmdlet . ThrowIfVariableIsScriptBlock ( pair . Value ) ;
51+ initialSessionState . Variables . Add ( new SessionStateVariableEntry (
52+ name : LanguagePrimitives . ConvertTo < string > ( pair . Key ) ,
53+ value : pair . Value ,
54+ description : null ) ) ;
55+ }
4756 }
57+
58+ return initialSessionState ;
4859 }
4960
5061 internal static Dictionary < string , object ? > GetUsingParameters (
5162 this ScriptBlock script ,
5263 PSCmdlet cmdlet )
5364 {
5465 Dictionary < string , object ? > usingParams = [ ] ;
66+ IEnumerable < UsingExpressionAst > usingExpressionAsts = script . Ast
67+ . FindAll ( ( a ) => a is UsingExpressionAst , true )
68+ . Cast < UsingExpressionAst > ( ) ;
5569
56- foreach ( UsingExpressionAst usingStatement in script . Ast . FindAll ( ( a ) => a is UsingExpressionAst , true ) )
70+ foreach ( UsingExpressionAst usingStatement in usingExpressionAsts )
5771 {
58- VariableExpressionAst backingVariableAst = UsingExpressionAst . ExtractUsingVariable ( usingStatement ) ;
72+ VariableExpressionAst backingVariableAst = UsingExpressionAst
73+ . ExtractUsingVariable ( usingStatement ) ;
74+
5975 string varPath = backingVariableAst . VariablePath . UserPath ;
6076
6177 string varText = usingStatement . ToString ( ) ;
@@ -89,7 +105,9 @@ internal static void AddVariables(
89105 object ? value ,
90106 ExpressionAst ast )
91107 {
92- VariableExpressionAst usingVariable = ( VariableExpressionAst ) ast . Find ( a => a is VariableExpressionAst , false ) ;
108+ VariableExpressionAst usingVariable = ( VariableExpressionAst ) ast
109+ . Find ( a => a is VariableExpressionAst , false ) ;
110+
93111 ExpressionAst lookupAst = new ConstantExpressionAst ( ast . Extent , value ) ;
94112 Ast ? currentAst = usingVariable ;
95113
0 commit comments