1
- using System ;
1
+ using Microsoft . Azure . PowerShell . Tools . AzPredictor . Utilities ;
2
+ using System ;
3
+ using System . IO ;
4
+ using System . Text ;
5
+ using System . Text . Json ;
6
+ using System . Text . Json . Serialization ;
2
7
using System . Collections . Concurrent ;
3
8
using System . Collections . Generic ;
4
9
using System . Linq ;
5
10
using System . Management . Automation . Language ;
6
11
12
+
13
+
7
14
namespace Microsoft . Azure . PowerShell . Tools . AzPredictor
8
15
{
9
16
/// <summary>
10
17
/// A predictor to learn and provide values for Azure PowerShell commands' parameters values.
11
18
/// </summary>
12
19
sealed class ParameterValuePredictor
13
20
{
14
- /// <summary>
15
- /// The collections of the parameter names that is used directly as the key in local parameter collection.
16
- /// </summary>
17
- private static readonly IReadOnlyCollection < string > _specialLocalParameterNames = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) { "location" , "credential" , "addressprefix" } ;
18
-
19
21
private readonly ConcurrentDictionary < string , string > _localParameterValues = new ConcurrentDictionary < string , string > ( ) ;
20
22
23
+ private readonly Dictionary < string , Dictionary < string , string > > _command_param_to_resource_map ;
24
+
25
+ public ParameterValuePredictor ( )
26
+ {
27
+ var fileInfo = new FileInfo ( typeof ( Settings ) . Assembly . Location ) ;
28
+ var directory = fileInfo . DirectoryName ;
29
+ var mappingFilePath = Path . Join ( directory , "command_param_to_resource_map.json" ) ;
30
+ _command_param_to_resource_map = JsonSerializer . Deserialize < Dictionary < string , Dictionary < string , string > > > ( File . ReadAllText ( mappingFilePath ) , JsonUtilities . DefaultSerializerOptions ) ;
31
+ }
32
+
21
33
/// <summary>
22
34
/// Process the command from history
23
35
/// </summary>
@@ -38,30 +50,28 @@ public void ProcessHistoryCommand(CommandAst command)
38
50
/// > Get-AzVM -VMName <TestVM>
39
51
/// "TestVM" is predicted for Get-AzVM.
40
52
/// </summary>
53
+ /// <param name="commandNoun">The command noun</param>
41
54
/// <param name="parameterName">The parameter name</param>
42
55
/// <returns>The parameter value from the history command. Null if that is not available.</returns>
43
- public string GetParameterValueFromAzCommand ( string parameterName )
56
+ public string GetParameterValueFromAzCommand ( string commandNoun , string parameterName )
44
57
{
45
- if ( _localParameterValues . TryGetValue ( parameterName . ToUpper ( ) , out var value ) )
58
+ if ( _command_param_to_resource_map . ContainsKey ( commandNoun ) )
46
59
{
47
- return value ;
60
+ parameterName = parameterName . ToLower ( ) ;
61
+ if ( _command_param_to_resource_map [ commandNoun ] . ContainsKey ( parameterName ) )
62
+ {
63
+ var key = _command_param_to_resource_map [ commandNoun ] [ parameterName ] ;
64
+ if ( _localParameterValues . TryGetValue ( key , out var value ) )
65
+ {
66
+ return value ;
67
+ }
68
+ }
48
69
}
49
-
50
70
return null ;
51
71
}
52
72
53
- /// <summary>
54
- /// Gets the key to the local parameter dictionary from the command noun and the parameter name.
55
- /// </summary>
56
- /// <param name="commandNoun">The noun in the PowerShell command, e.g. the noun for command New-AzVM is VM.</param>
57
- /// <param name="parameterName">The command's parameter name, e.g. "New-AzVM -Name" the parameter name is Name</param>
58
- /// <returns></returns>
59
- private static string GetLocalParameterKey ( string commandNoun , string parameterName )
60
- {
61
- return _specialLocalParameterNames . Contains ( parameterName ) ? parameterName . ToUpper ( ) : string . Concat ( commandNoun , parameterName ) . ToUpper ( ) ;
62
- }
63
-
64
- private static string GetAzCommandNoun ( string commandName )
73
+
74
+ public static string GetAzCommandNoun ( string commandName )
65
75
{
66
76
var monikerIndex = commandName ? . IndexOf ( AzPredictorConstants . AzCommandMoniker , StringComparison . OrdinalIgnoreCase ) ;
67
77
@@ -91,7 +101,7 @@ private void ExtractLocalParameters(System.Collections.ObjectModel.ReadOnlyColle
91
101
// We need to extract the noun to construct the parameter name.
92
102
93
103
var commandName = command . FirstOrDefault ( ) ? . ToString ( ) ;
94
- var commandNoun = ParameterValuePredictor . GetAzCommandNoun ( commandName ) ;
104
+ var commandNoun = ParameterValuePredictor . GetAzCommandNoun ( commandName ) . ToLower ( ) ;
95
105
if ( commandNoun == null )
96
106
{
97
107
return ;
@@ -101,11 +111,17 @@ private void ExtractLocalParameters(System.Collections.ObjectModel.ReadOnlyColle
101
111
{
102
112
if ( command [ i - 1 ] is CommandParameterAst parameterAst && command [ i ] is StringConstantExpressionAst )
103
113
{
104
- var parameterName = parameterAst . ParameterName ;
105
- var key = ParameterValuePredictor . GetLocalParameterKey ( commandNoun , parameterName ) ;
106
- var parameterValue = command [ i ] . ToString ( ) ;
107
- this . _localParameterValues . AddOrUpdate ( key , parameterValue , ( k , v ) => parameterValue ) ;
108
- }
114
+ var parameterName = command [ i - 1 ] . ToString ( ) . ToLower ( ) . Trim ( '-' ) ;
115
+ if ( _command_param_to_resource_map . ContainsKey ( commandNoun ) )
116
+ {
117
+ if ( _command_param_to_resource_map [ commandNoun ] . ContainsKey ( parameterName ) )
118
+ {
119
+ var key = _command_param_to_resource_map [ commandNoun ] [ parameterName ] ;
120
+ var parameterValue = command [ i ] . ToString ( ) ;
121
+ this . _localParameterValues . AddOrUpdate ( key , parameterValue , ( k , v ) => parameterValue ) ;
122
+ }
123
+ }
124
+ }
109
125
}
110
126
}
111
127
}
0 commit comments