1
1
[CmdletBinding ()]
2
2
param (
3
- [Parameter (Mandatory = $true )]
4
- [string ]$dllFolder ,
3
+ [Parameter (Mandatory = $true )]
4
+ [string ]$dllFolder ,
5
5
6
- [Parameter (Mandatory = $true )]
7
- [string ]$outFolder ,
8
-
9
- # The base cmdlet from which all automation cmdlets derive
10
- [Parameter (Mandatory = $false )]
11
- [string ]$baseCmdlet = ' ServiceManagementBaseCmdlet' ,
6
+ [Parameter (Mandatory = $true )]
7
+ [string ]$outFolder ,
8
+
9
+ # The base cmdlet from which all automation cmdlets derive
10
+ [Parameter (Mandatory = $false )]
11
+ [string ]$baseCmdlet = ' ServiceManagementBaseCmdlet' ,
12
+
13
+ # The property field to access the client wrapper class from the base cmdlet
14
+ [Parameter (Mandatory = $false )]
15
+ [string ]$base_class_client_field = ' ComputeClient' ,
12
16
13
- # Cmdlet Code Generation Style
14
- # 1. Invoke (default) that uses Invoke as the verb, and Operation + Method (e.g. VirtualMachine + Get)
15
- # 2. Verb style that maps the method name to a certain common PS verb (e.g. CreateOrUpdate -> New)
16
- [Parameter (Mandatory = $false )]
17
- [string ]$cmdletStyle = ' Invoke'
17
+ # Cmdlet Code Generation Style
18
+ # 1. Invoke (default) that uses Invoke as the verb, and Operation + Method (e.g. VirtualMachine + Get)
19
+ # 2. Verb style that maps the method name to a certain common PS verb (e.g. CreateOrUpdate -> New)
20
+ [Parameter (Mandatory = $false )]
21
+ [string ]$cmdletStyle = ' Invoke'
18
22
)
19
23
24
+ $client_library_namespace = ' Microsoft.WindowsAzure.Management.Compute' ;
25
+ $code_common_namespace = ($client_library_namespace.Replace (' .Management.' , ' .Commands.' )) + ' .Automation' ;
26
+
27
+ $code_common_usings = @ (
28
+ ' System.Management.Automation' ,
29
+ ' Microsoft.Azure' ,
30
+ ' Microsoft.WindowsAzure.Commands.ServiceManagement' ,
31
+ ' Microsoft.WindowsAzure.Commands.Utilities.Common'
32
+ );
33
+
20
34
$code_common_header =
21
35
@"
22
36
//
@@ -41,12 +55,35 @@ $code_common_header =
41
55
// code is regenerated.
42
56
"@ ;
43
57
44
- $client_library_namespace = ' Microsoft.WindowsAzure.Management.Compute' ;
45
- $code_common_namespace = ($client_library_namespace.Replace (' .Management.' , ' .Commands.' )) + ' .Automation' ;
58
+ $new_line_str = " `r`n " ;
59
+
60
+ function Get-SortedUsings
61
+ {
62
+ param (
63
+ # Sample: @('System.Management.Automation', 'Microsoft.Azure', ...)
64
+ [Parameter (Mandatory = $true )]
65
+ $common_using_str_list ,
66
+
67
+ # Sample: 'Microsoft.WindowsAzure.Management.Compute'
68
+ [Parameter (Mandatory = $true )]
69
+ $client_library_namespace
70
+ )
71
+
72
+ $a1 = @ () + $common_using_str_list + $client_library_namespace ;
73
+ $a2 = Sort-Object - Descending - Unique - InputObject $a1 ;
74
+ $a3 = $a2 | foreach { " using ${_} ;" };
75
+
76
+ $text = [string ]::Join($new_line_str , $a3 );
77
+
78
+ return $text ;
79
+ }
80
+
81
+ $code_using_strs = Get-SortedUsings $code_common_usings $client_library_namespace ;
46
82
47
83
function Get-NormalizedName
48
84
{
49
85
param (
86
+ # Sample: 'vmName' => 'VMName', 'resourceGroup' => 'ResourceGroup', etc.
50
87
[Parameter (Mandatory = $True )]
51
88
[string ]$inputName
52
89
)
@@ -73,6 +110,8 @@ function Get-NormalizedName
73
110
function Get-OperationShortName
74
111
{
75
112
param (
113
+ # Sample #1: 'IVirtualMachineOperations' => 'VirtualMachine'
114
+ # Sample #2: 'IDeploymentOperations' => 'Deployment'
76
115
[Parameter (Mandatory = $True )]
77
116
[string ]$opFullName
78
117
)
@@ -83,7 +122,8 @@ function Get-OperationShortName
83
122
84
123
if ($opFullName.StartsWith ($prefix ) -and $opShortName.EndsWith ($suffix ))
85
124
{
86
- $opShortName = $opShortName.Substring ($prefix.Length , ($opShortName.Length - $prefix.Length - $suffix.Length ));
125
+ $lenOpShortName = ($opShortName.Length - $prefix.Length - $suffix.Length );
126
+ $opShortName = $opShortName.Substring ($prefix.Length , $lenOpShortName );
87
127
}
88
128
89
129
return $opShortName ;
@@ -93,20 +133,19 @@ function Write-BaseCmdletFile
93
133
{
94
134
param (
95
135
[Parameter (Mandatory = $True )]
96
- [string ]$fileFullPath ,
136
+ [string ]$file_full_path ,
97
137
98
138
[Parameter (Mandatory = $True )]
99
- $operationNameList ,
139
+ $operation_name_list ,
100
140
101
141
[Parameter (Mandatory = $True )]
102
- $clientClass
142
+ $client_class_info
103
143
)
104
144
105
-
106
- [System.Reflection.PropertyInfo []]$propItems = $clientClass.GetProperties ();
145
+ [System.Reflection.PropertyInfo []]$propItems = $client_class_info.GetProperties ();
107
146
108
147
$operation_get_code = " " ;
109
- foreach ($opFullName in $operationNameList )
148
+ foreach ($opFullName in $operation_name_list )
110
149
{
111
150
[string ]$sOpFullName = $opFullName ;
112
151
Write-Output (' $sOpFullName = ' + $sOpFullName );
@@ -131,26 +170,25 @@ function Write-BaseCmdletFile
131
170
{
132
171
get
133
172
{
134
- return ComputeClient .${opPropName} ;
173
+ return ${base_class_client_field} .${opPropName} ;
135
174
}
136
175
}
137
176
"@ ;
138
177
139
- if (-not ($operation_get_code -eq ' ' ))
178
+ if (-not ($operation_get_code -eq " " ))
140
179
{
141
- $operation_get_code += " `r`n " ;
180
+ $operation_get_code += ( $new_line_str * 2 ) ;
142
181
}
143
182
144
183
$operation_get_code += $operation_get_template ;
145
184
}
146
185
}
147
186
148
- $source_template =
187
+ $cmdlet_source_code_text =
149
188
@"
150
189
${code_common_header}
151
190
152
- using ${client_library_namespace} ;
153
- using Microsoft.WindowsAzure.Commands.Utilities.Common;
191
+ $code_using_strs
154
192
155
193
namespace ${code_common_namespace}
156
194
{
@@ -161,7 +199,7 @@ ${operation_get_code}
161
199
}
162
200
"@ ;
163
201
164
- Set-Content - Path $fileFullPath - Value ( $source_template | Out-String ) - Force;
202
+ $st = Set-Content - Path $file_full_path - Value $cmdlet_source_code_text - Force;
165
203
}
166
204
167
205
function Write-OperationCmdletFile
@@ -179,15 +217,16 @@ function Write-OperationCmdletFile
179
217
180
218
$methodName = ($operationMethodInfo.Name.Replace (' Async' , ' ' ));
181
219
$cmdlet_verb = " Invoke" ;
182
- $cmdlet_noun = " Azure" + $opShortName + $methodName ;
183
- $cmdlet_class_name = $cmdlet_verb + $cmdlet_noun + ' Cmdlet' ;
220
+ $cmdlet_noun_prefix = ' Azure' ;
221
+ $cmdlet_noun_suffix = ' Method' ;
222
+ $cmdlet_noun = $cmdlet_noun_prefix + $opShortName + $methodName + $cmdlet_noun_suffix ;
223
+ $cmdlet_class_name = $cmdlet_verb + $cmdlet_noun ;
184
224
185
225
$indents = " " * 8 ;
186
- $new_line = " `r`n " ;
187
226
$get_set_block = ' { get; set; }' ;
188
227
189
228
$cmdlet_generated_code = ' ' ;
190
- # $cmdlet_generated_code += $indents + '// ' + $operationMethodInfo + $new_line ;
229
+ # $cmdlet_generated_code += $indents + '// ' + $operationMethodInfo + $new_line_str ;
191
230
192
231
$params = $operationMethodInfo.GetParameters ();
193
232
[System.Collections.ArrayList ]$param_names = @ ();
@@ -200,13 +239,13 @@ function Write-OperationCmdletFile
200
239
201
240
Write-Output (' ' + $paramTypeFullName + ' ' + $normalized_param_name );
202
241
203
- $param_attributes = $indents + " [Parameter(Mandatory = true), ValidateNotNullOrEmpty ]" + $new_line ;
204
- $param_definition = $indents + " public ${paramTypeFullName} ${normalized_param_name} " + $get_set_block + $new_line ;
242
+ $param_attributes = $indents + " [Parameter(Mandatory = true)]" + $new_line_str ;
243
+ $param_definition = $indents + " public ${paramTypeFullName} ${normalized_param_name} " + $get_set_block + $new_line_str ;
205
244
$param_code_content = $param_attributes + $param_definition ;
206
245
207
- $cmdlet_generated_code += $param_code_content + $new_line ;
246
+ $cmdlet_generated_code += $param_code_content + $new_line_str ;
208
247
209
- $param_names.Add ($normalized_param_name );
248
+ $st = $ param_names.Add ($normalized_param_name );
210
249
}
211
250
}
212
251
@@ -233,13 +272,9 @@ function Write-OperationCmdletFile
233
272
@"
234
273
${code_common_header}
235
274
236
- using Microsoft.Azure;
237
- using Microsoft.WindowsAzure.Commands.ServiceManagement;
238
- using Microsoft.WindowsAzure.Commands.Utilities.Common;
239
- using Microsoft.WindowsAzure.Management.Compute;
240
- using System.Management.Automation;
275
+ $code_using_strs
241
276
242
- namespace Microsoft.WindowsAzure.Commands.Compute.Automation
277
+ namespace ${code_common_namespace}
243
278
{
244
279
[Cmdlet(`" ${cmdlet_verb} `" , `" ${cmdlet_noun} `" )]
245
280
public class ${cmdlet_class_name} : ComputeAutomationBaseCmdlet
@@ -249,10 +284,11 @@ ${cmdlet_generated_code}
249
284
}
250
285
"@ ;
251
286
252
- $fileFullPath = $fileOutputFolder + ' /' + $cmdlet_class_name + ' .cs' ;
253
- Set-Content - Path $fileFullPath - Value $cmdlt_source_template - Force;
287
+ $file_full_path = $fileOutputFolder + ' /' + $cmdlet_class_name + ' .cs' ;
288
+ $st = Set-Content - Path $file_full_path - Value $cmdlt_source_template - Force;
254
289
}
255
290
291
+ # Code Generation Main:
256
292
Write-Output $dllFolder ;
257
293
Write-Output $outFolder ;
258
294
@@ -264,7 +300,7 @@ $output = Get-ChildItem -Path $dllFolder | Out-String;
264
300
Write-Output $output ;
265
301
266
302
267
- $dllname = ' Microsoft.WindowsAzure.Management.Compute ' ;
303
+ $dllname = $client_library_namespace ;
268
304
$dllfile = $dllname + ' .dll' ;
269
305
$dllFileFullPath = $dllFolder + ' \' + $dllfile ;
270
306
302
338
$methods = $ft.GetMethods ();
303
339
foreach ($mt in $methods )
304
340
{
305
- Write-Output ($mt.Name.Replace (' Async' , ' ' ));
341
+ Write-Output ($new_line_str + $ mt.Name.Replace (' Async' , ' ' ));
306
342
Write-OperationCmdletFile $opOutFolder $opShortName $mt ;
307
343
}
308
344
}
0 commit comments