@@ -35,6 +35,7 @@ public class Helper
35
35
private IOutputWriter outputWriter ;
36
36
private Object getCommandLock = new object ( ) ;
37
37
private readonly static Version minSupportedPSVersion = new Version ( 3 , 0 ) ;
38
+ private Dictionary < string , Dictionary < string , object > > ruleArguments ;
38
39
39
40
#endregion
40
41
@@ -113,14 +114,14 @@ internal set
113
114
/// </summary>
114
115
private Helper ( )
115
116
{
116
-
117
+
117
118
}
118
119
119
120
/// <summary>
120
121
/// Initializes the Helper class.
121
122
/// </summary>
122
123
/// <param name="invokeCommand">
123
- /// A CommandInvocationIntrinsics instance for use in gathering
124
+ /// A CommandInvocationIntrinsics instance for use in gathering
124
125
/// information about available commands and aliases.
125
126
/// </param>
126
127
/// <param name="outputWriter">
@@ -145,6 +146,7 @@ public void Initialize()
145
146
AliasToCmdletDictionary = new Dictionary < String , String > ( StringComparer . OrdinalIgnoreCase ) ;
146
147
KeywordBlockDictionary = new Dictionary < String , List < Tuple < int , int > > > ( StringComparer . OrdinalIgnoreCase ) ;
147
148
VariableAnalysisDictionary = new Dictionary < Ast , VariableAnalysis > ( ) ;
149
+ ruleArguments = new Dictionary < string , Dictionary < string , object > > ( StringComparer . OrdinalIgnoreCase ) ;
148
150
149
151
IEnumerable < CommandInfo > aliases = this . invokeCommand . GetCommands ( "*" , CommandTypes . Alias , true ) ;
150
152
@@ -163,6 +165,59 @@ public void Initialize()
163
165
}
164
166
}
165
167
168
+ /// <summary>
169
+ /// Returns all the rule arguments
170
+ /// </summary>
171
+ /// <returns>Dictionary that maps between rule name to their named arguments</returns>
172
+ public Dictionary < string , Dictionary < string , object > > GetRuleArguments ( )
173
+ {
174
+ return ruleArguments ;
175
+ }
176
+
177
+ /// <summary>
178
+ /// Get the parameters corresponding to the given rule name
179
+ /// </summary>
180
+ /// <param name="ruleName"></param>
181
+ /// <returns>Dictionary of argument names mapped to values. If ruleName is not a valid key, returns null</returns>
182
+ public Dictionary < string , object > GetRuleArguments ( string ruleName )
183
+ {
184
+ if ( ruleArguments . ContainsKey ( ruleName ) )
185
+ {
186
+ return ruleArguments [ ruleName ] ;
187
+ }
188
+ return null ;
189
+ }
190
+
191
+ /// <summary>
192
+ /// Sets the arguments for consumption by rules
193
+ /// </summary>
194
+ /// <param name="ruleArgs">A hashtable with rule names as keys</param>
195
+ public void SetRuleArguments ( Dictionary < string , object > ruleArgs )
196
+ {
197
+ if ( ruleArgs == null )
198
+ {
199
+ return ;
200
+ }
201
+
202
+ if ( ruleArgs . Comparer != StringComparer . OrdinalIgnoreCase )
203
+ {
204
+ throw new ArgumentException (
205
+ "Input dictionary should have OrdinalIgnoreCase comparer." ,
206
+ "ruleArgs" ) ;
207
+ }
208
+ var ruleArgsDict = new Dictionary < string , Dictionary < string , object > > ( ) ;
209
+ foreach ( var rule in ruleArgs . Keys )
210
+ {
211
+ var argsDict = ruleArgs [ rule ] as Dictionary < string , object > ;
212
+ if ( argsDict == null )
213
+ {
214
+ return ;
215
+ }
216
+ ruleArgsDict [ rule ] = argsDict ;
217
+ }
218
+ ruleArguments = ruleArgsDict ;
219
+ }
220
+
166
221
/// <summary>
167
222
/// Given a cmdlet, return the list of all the aliases.
168
223
/// Also include the original name in the list.
@@ -231,7 +286,7 @@ public bool IsDscResourceModule(string filePath)
231
286
232
287
return false ;
233
288
}
234
-
289
+
235
290
/// <summary>
236
291
/// Gets the module manifest
237
292
/// </summary>
@@ -471,13 +526,13 @@ private string NameWithoutScope(string name, string[] scopes)
471
526
if ( String . IsNullOrWhiteSpace ( name ) || scopes == null )
472
527
{
473
528
return name ;
474
- }
529
+ }
475
530
476
531
// checks whether function name starts with scope
477
532
foreach ( string scope in scopes )
478
533
{
479
534
// trim the scope part
480
- if ( name . IndexOf ( scope , StringComparison . OrdinalIgnoreCase ) == 0 )
535
+ if ( name . IndexOf ( scope , StringComparison . OrdinalIgnoreCase ) == 0 )
481
536
482
537
{
483
538
return name . Substring ( scope . Length ) ;
@@ -598,7 +653,7 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanThreePositio
598
653
{
599
654
arguments += 1 ;
600
655
}
601
-
656
+
602
657
}
603
658
604
659
// if not the first element in a pipeline, increase the number of arguments by 1
@@ -724,10 +779,10 @@ public IScriptExtent GetScriptExtentForFunctionName(FunctionDefinitionAst functi
724
779
if ( null == functionDefinitionAst )
725
780
{
726
781
return null ;
727
- }
782
+ }
728
783
var funcNameTokens = Tokens . Where (
729
- token =>
730
- ContainsExtent ( functionDefinitionAst . Extent , token . Extent )
784
+ token =>
785
+ ContainsExtent ( functionDefinitionAst . Extent , token . Extent )
731
786
&& token . Text . Equals ( functionDefinitionAst . Name ) ) ;
732
787
var funcNameToken = funcNameTokens . FirstOrDefault ( ) ;
733
788
return funcNameToken == null ? null : funcNameToken . Extent ;
@@ -919,7 +974,7 @@ internal VariableAnalysis InitializeVariableAnalysisHelper(Ast ast, VariableAnal
919
974
/// <param name="classes"></param>
920
975
/// <param name="scriptAst"></param>
921
976
/// <returns></returns>
922
-
977
+
923
978
#if PSV3
924
979
925
980
public string GetTypeFromReturnStatementAst ( Ast funcAst , ReturnStatementAst ret )
@@ -990,7 +1045,7 @@ public string GetTypeFromReturnStatementAst(Ast funcAst, ReturnStatementAst ret,
990
1045
/// <param name="scopeAst"></param>
991
1046
/// <param name="classes"></param>
992
1047
/// <returns></returns>
993
-
1048
+
994
1049
#if PSV3
995
1050
996
1051
public string GetTypeFromMemberExpressionAst ( MemberExpressionAst memberAst , Ast scopeAst )
@@ -1050,9 +1105,9 @@ public string GetTypeFromMemberExpressionAst(MemberExpressionAst memberAst, Ast
1050
1105
/// <param name="psClass"></param>
1051
1106
/// <param name="analysisDetails"></param>
1052
1107
/// <returns></returns>
1053
-
1108
+
1054
1109
#if PSV3
1055
-
1110
+
1056
1111
internal string GetTypeFromMemberExpressionAstHelper ( MemberExpressionAst memberAst , VariableAnalysisDetails analysisDetails )
1057
1112
1058
1113
#else
@@ -1241,8 +1296,8 @@ internal List<RuleSuppression> GetSuppressionsClass(TypeDefinitionAst typeAst)
1241
1296
1242
1297
if ( typeAst . Members == null )
1243
1298
{
1244
- return result ;
1245
- }
1299
+ return result ;
1300
+ }
1246
1301
1247
1302
foreach ( var member in typeAst . Members )
1248
1303
{
@@ -1469,11 +1524,11 @@ public static string[] ProcessCustomRulePaths(string[] rulePaths, SessionState s
1469
1524
}
1470
1525
outPaths . Add ( path ) ;
1471
1526
}
1472
-
1527
+
1473
1528
return outPaths . ToArray ( ) ;
1474
-
1529
+
1475
1530
}
1476
-
1531
+
1477
1532
/// <summary>
1478
1533
/// Check if the function name starts with one of potentailly state changing verbs
1479
1534
/// </summary>
@@ -1671,8 +1726,8 @@ public static IEnumerable<string> GetDeprecatedModuleManifestKeys()
1671
1726
1672
1727
/// <summary>
1673
1728
/// Get a mapping between string type keys and StatementAsts from module manifest hashtable ast
1674
- ///
1675
- /// This is a workaround as SafeGetValue is not supported on PS v5 and below.
1729
+ ///
1730
+ /// This is a workaround as SafeGetValue is not supported on PS v4 and below.
1676
1731
/// </summary>
1677
1732
/// <param name="hast">Hashtable Ast obtained from module manifest</param>
1678
1733
/// <returns>A dictionary that maps string keys to values of StatementAst type</returns>
@@ -1693,7 +1748,7 @@ private static Dictionary<string, StatementAst> GetMapFromHashtableAst(Hashtable
1693
1748
1694
1749
/// <summary>
1695
1750
/// Checks if the version is supported
1696
- ///
1751
+ ///
1697
1752
/// PowerShell versions with Major greater than 3 are supported
1698
1753
/// </summary>
1699
1754
/// <param name="version">PowerShell version</param>
@@ -1910,7 +1965,7 @@ private object VisitStatementHelper(StatementAst statementAst)
1910
1965
#if PSV3
1911
1966
1912
1967
statementAst . Visit ( this ) ;
1913
-
1968
+
1914
1969
#else
1915
1970
1916
1971
TypeDefinitionAst typeAst = statementAst as TypeDefinitionAst ;
@@ -2664,7 +2719,7 @@ static FindPipelineOutput()
2664
2719
/// Find the pipeline output
2665
2720
/// </summary>
2666
2721
/// <param name="ast"></param>
2667
-
2722
+
2668
2723
#if PSV3
2669
2724
2670
2725
public FindPipelineOutput ( FunctionDefinitionAst ast )
@@ -2695,7 +2750,7 @@ public FindPipelineOutput(FunctionDefinitionAst ast, IEnumerable<TypeDefinitionA
2695
2750
/// Get list of outputTypes from functiondefinitionast funcast
2696
2751
/// </summary>
2697
2752
/// <returns></returns>
2698
-
2753
+
2699
2754
#if PSV3
2700
2755
2701
2756
public static List < Tuple < string , StatementAst > > OutputTypes ( FunctionDefinitionAst funcAst )
0 commit comments