Skip to content

Commit 02033ef

Browse files
committed
Update
1 parent 8817f40 commit 02033ef

File tree

5 files changed

+350
-18
lines changed

5 files changed

+350
-18
lines changed

src/ServiceManagement/Compute/Commands.ServiceManagement.Preview/Automation/Generate-FunctionCommand.ps1

Lines changed: 289 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,295 @@ param
3333

3434
. "$PSScriptRoot\Import-StringFunction.ps1";
3535
. "$PSScriptRoot\Import-TypeFunction.ps1";
36-
36+
. "$PSScriptRoot\Import-WriterFunction.ps1";
3737

3838
# Sample: VirtualMachineGetMethod.cs
3939
function Generate-PsFunctionCommandImpl
40+
{
41+
param
42+
(
43+
[Parameter(Mandatory = $true)]
44+
[string]$OperationName,
45+
46+
[Parameter(Mandatory = $true)]
47+
[System.Reflection.MethodInfo]$MethodInfo,
48+
49+
[Parameter(Mandatory = $true)]
50+
[string]$FileOutputFolder,
51+
52+
[Parameter(Mandatory = $false)]
53+
[System.Reflection.MethodInfo]$FriendMethodInfo = $null
54+
)
55+
56+
# e.g. Compute
57+
$componentName = Get-ComponentName $ModelClassNameSpace;
58+
# e.g. CreateOrUpdate, Get, ...
59+
$methodName = ($MethodInfo.Name.Replace('Async', ''));
60+
# e.g. VirtualMachine, System.Void, ...
61+
$returnTypeInfo = $MethodInfo.ReturnType;
62+
$normalizedOutputTypeName = Get-NormalizedTypeName $returnTypeInfo;
63+
$nounPrefix = 'Azure';
64+
$nounSuffix = 'Method';
65+
# e.g. VirtualMachines => VirtualMachine
66+
$opSingularName = Get-SingularNoun $OperationName;
67+
# e.g. AzureVirtualMachineGetMethod
68+
$cmdletNoun = $nounPrefix + $opSingularName + $methodName + $nounSuffix;
69+
# e.g. InvokeAzureVirtualMachineGetMethod
70+
$invokeVerb = "Invoke";
71+
$invokeCmdletName = $invokeVerb + $cmdletNoun;
72+
$invokeParamSetName = $opSingularName + $methodName;
73+
# e.g. Generated/InvokeAzureVirtualMachineGetMethod.cs
74+
$fileNameExt = $invokeParamSetName + $nounSuffix + '.cs';
75+
$fileFullPath = $FileOutputFolder + '/' + $fileNameExt;
76+
77+
# The folder and files shall be removed beforehand.
78+
# It will exist, if the target file already exists.
79+
if (Test-Path $fileFullPath)
80+
{
81+
return;
82+
}
83+
84+
# Common Variables
85+
$indents_8 = ' ' * 8;
86+
$getSetCodeBlock = '{ get; set; }';
87+
88+
# Iterate through Param List
89+
$methodParamList = $MethodInfo.GetParameters();
90+
$positionIndex = 1;
91+
foreach ($methodParam in $methodParamList)
92+
{
93+
# Filter Out Helper Parameters
94+
if (($methodParam.ParameterType.Name -like "I*Operations") -and ($methodParam.Name -eq 'operations'))
95+
{
96+
continue;
97+
}
98+
elseif ($methodParam.ParameterType.Name.EndsWith('CancellationToken'))
99+
{
100+
continue;
101+
}
102+
103+
# e.g. vmName => VMName, resourceGroup => ResourceGroup, etc.
104+
$paramName = Get-CamelCaseName $methodParam.Name;
105+
$paramTypeName = Get-NormalizedTypeName $methodParam.ParameterType;
106+
$paramCtorCode = Get-ConstructorCode -InputName $paramTypeName;
107+
108+
}
109+
110+
# Construct Code
111+
$code = '';
112+
$code += Get-InvokeMethodCmdletCode -ComponentName $componentName -OperationName $OperationName -MethodInfo $MethodInfo;
113+
$code += $NEW_LINE;
114+
$code += Get-ArgumentListCmdletCode -ComponentName $componentName -OperationName $OperationName -MethodInfo $MethodInfo;
115+
116+
# Write Code to File
117+
Write-CmdletCodeFile $fileFullPath $code;
118+
}
119+
120+
# Get Partial Code for Invoke Method
121+
function Get-InvokeMethodCmdletCode
122+
{
123+
param
124+
(
125+
[Parameter(Mandatory = $true)]
126+
[string]$ComponentName,
127+
128+
[Parameter(Mandatory = $true)]
129+
[string]$OperationName,
130+
131+
[Parameter(Mandatory = $true)]
132+
[System.Reflection.MethodInfo]$MethodInfo
133+
)
134+
135+
# e.g. CreateOrUpdate, Get, ...
136+
$methodName = ($MethodInfo.Name.Replace('Async', ''));
137+
# e.g. VirtualMachines => VirtualMachine
138+
$opSingularName = Get-SingularNoun $OperationName;
139+
# e.g. InvokeAzureComputeMethodCmdlet
140+
$invoke_cmdlet_class_name = 'InvokeAzure' + $ComponentName + 'MethodCmdlet';
141+
$invoke_param_set_name = $opSingularName + $methodName;
142+
$method_return_type = $MethodInfo.ReturnType;
143+
$invoke_input_params_name = 'invokeMethodInputParameters';
144+
145+
# 1. Start
146+
$code = "";
147+
$code += " public partial class ${invoke_cmdlet_class_name} : ${ComponentName}AutomationBaseCmdlet" + $NEW_LINE;
148+
$code += " {" + $NEW_LINE;
149+
150+
# 2. Iterate through Param List
151+
$methodParamList = $MethodInfo.GetParameters();
152+
$paramNameList = @();
153+
$paramLocalNameList = @();
154+
foreach ($methodParam in $methodParamList)
155+
{
156+
# Filter Out Helper Parameters
157+
if (($methodParam.ParameterType.Name -like "I*Operations") -and ($methodParam.Name -eq 'operations'))
158+
{
159+
continue;
160+
}
161+
elseif ($methodParam.ParameterType.Name.EndsWith('CancellationToken'))
162+
{
163+
continue;
164+
}
165+
166+
# e.g. vmName => VMName, resourceGroup => ResourceGroup, etc.
167+
$paramName = Get-CamelCaseName $methodParam.Name;
168+
# Save the parameter's camel name (in upper case) and local name (in lower case).
169+
$paramNameList += $paramName;
170+
$paramLocalNameList += $methodParam.Name;
171+
}
172+
173+
$invoke_params_join_str = [string]::Join(', ', $paramLocalNameList);
174+
175+
# 2.1
176+
$dynamic_param_source_template =
177+
@"
178+
protected object Create${invoke_param_set_name}DynamicParameters()
179+
{
180+
dynamicParameters = new RuntimeDefinedParameterDictionary();
181+
$dynamic_param_assignment_code
182+
return dynamicParameters;
183+
}
184+
"@;
185+
186+
$code += $dynamic_param_source_template + $NEW_LINE;
187+
188+
# 2.2
189+
$invoke_cmdlt_source_template = '';
190+
if ($method_return_type.FullName -eq 'System.Void')
191+
{
192+
$invoke_cmdlt_source_template =
193+
@"
194+
protected void Execute${invoke_param_set_name}Method(object[] ${invoke_input_params_name})
195+
{
196+
${invoke_local_param_code_content}
197+
${OperationName}Client.${methodName}(${invoke_params_join_str});
198+
}
199+
"@;
200+
}
201+
else
202+
{
203+
$invoke_cmdlt_source_template =
204+
@"
205+
protected void Execute${invoke_param_set_name}Method(object[] ${invoke_input_params_name})
206+
{
207+
${invoke_local_param_code_content}
208+
var result = ${OperationName}Client.${methodName}(${invoke_params_join_str});
209+
WriteObject(result);
210+
}
211+
"@;
212+
}
213+
214+
$code += $invoke_cmdlt_source_template + $NEW_LINE;
215+
216+
# End
217+
$code += " }" + $NEW_LINE;
218+
219+
return $code;
220+
}
221+
222+
# Get Partial Code for Creating New Argument List
223+
function Get-ArgumentListCmdletCode
224+
{
225+
param
226+
(
227+
[Parameter(Mandatory = $true)]
228+
[string]$ComponentName,
229+
230+
[Parameter(Mandatory = $true)]
231+
[string]$OperationName,
232+
233+
[Parameter(Mandatory = $true)]
234+
[System.Reflection.MethodInfo]$MethodInfo
235+
)
236+
237+
# e.g. CreateOrUpdate, Get, ...
238+
$methodName = ($MethodInfo.Name.Replace('Async', ''));
239+
# e.g. VirtualMachines => VirtualMachine
240+
$opSingularName = Get-SingularNoun $OperationName;
241+
242+
# 1. Construct Code - Starting
243+
$code = "";
244+
$code += " public partial class NewAzure${ComponentName}ArgumentListCmdlet : ${ComponentName}AutomationBaseCmdlet" + $NEW_LINE;
245+
$code += " {" + $NEW_LINE;
246+
$code += " protected PSArgument[] Create" + $opSingularName + $methodName + "Parameters()" + $NEW_LINE;
247+
$code += " {" + $NEW_LINE;
248+
249+
# 2. Iterate through Param List
250+
$methodParamList = $MethodInfo.GetParameters();
251+
$paramNameList = @();
252+
$paramLocalNameList = @();
253+
foreach ($methodParam in $methodParamList)
254+
{
255+
# Filter Out Helper Parameters
256+
if (($methodParam.ParameterType.Name -like "I*Operations") -and ($methodParam.Name -eq 'operations'))
257+
{
258+
continue;
259+
}
260+
elseif ($methodParam.ParameterType.Name.EndsWith('CancellationToken'))
261+
{
262+
continue;
263+
}
264+
265+
# e.g. vmName => VMName, resourceGroup => ResourceGroup, etc.
266+
$paramName = Get-CamelCaseName $methodParam.Name;
267+
# Save the parameter's camel name (in upper case) and local name (in lower case).
268+
$paramNameList += $paramName;
269+
$paramLocalNameList += $methodParam.Name;
270+
271+
# i.e. System.Int32 => int, Microsoft.Azure.Management.Compute.VirtualMachine => VirtualMachine
272+
$paramTypeName = Get-NormalizedTypeName $methodParam.ParameterType;
273+
$paramCtorCode = Get-ConstructorCode -InputName $paramTypeName;
274+
275+
$isStringList = Is-ListStringType $methodParam.ParameterType;
276+
$strTypeList = Get-StringTypes $methodParam.ParameterType;
277+
$containsOnlyStrings = ($strTypeList -ne $null) -and ($strTypeList.Count -ne 0);
278+
279+
# 2.1 Construct Code - Local Constructor Initialization
280+
if ($containsOnlyStrings)
281+
{
282+
# Case 2.1.1: the parameter type contains only string types.
283+
foreach ($param in $strTypeList)
284+
{
285+
$code += $indents + (' ' * 4) + "var p${param} = string.Empty;" + $NEW_LINE;
286+
$param_index += 1;
287+
$position_index += 1;
288+
$param_names += ${param};
289+
$invoke_local_param_names += "p${param}";
290+
}
291+
}
292+
elseif ($isStringList)
293+
{
294+
# Case 2.1.2: the parameter type contains only a list of strings.
295+
$code += " var " + $methodParam.Name + " = new string[0];" + $NEW_LINE;
296+
}
297+
elseif ($paramName -eq 'ODataQuery')
298+
{
299+
# Case 2.1.3: ODataQuery.
300+
$paramTypeName = "Microsoft.Rest.Azure.OData.ODataQuery<${opSingularName}>";
301+
$code += " ${paramTypeName} " + $methodParam.Name + " = new ${paramTypeName}();" + $NEW_LINE;
302+
}
303+
else
304+
{
305+
# Case 2.1.4: Most General Constructor Case
306+
$code += " ${paramTypeName} " + $methodParam.Name + " = ${paramCtorCode};" + $NEW_LINE;
307+
}
308+
}
309+
310+
# Construct Code - 2.2 Return Argument List
311+
$code += $NEW_LINE;
312+
$code += " return ConvertFromObjectsToArguments(" + $NEW_LINE;
313+
$code += " new string[] { `"" + ([string]::Join("`", `"", $paramNameList)) + "`" }," + $NEW_LINE;
314+
$code += " new object[] { " + ([string]::Join(", ", $paramLocalNameList)) + " });" + $NEW_LINE;
315+
316+
# Construct Code - Ending
317+
$code += " }" + $NEW_LINE;
318+
$code += " }" + $NEW_LINE;
319+
320+
return $code;
321+
}
322+
323+
# Sample: VirtualMachineGetMethod.cs
324+
function Generate-PsFunctionVerbCommandImpl
40325
{
41326
param
42327
(
@@ -125,7 +410,7 @@ function Generate-PsFunctionCommandImpl
125410
#Write-Verbose (' ' + $paramTypeFullName + ' ' + $normalized_param_name);
126411

127412
$paramTypeNormalizedName = Get-NormalizedTypeName $pt.ParameterType;
128-
$param_constructor_code = Get-ConstructorCodeByNormalizedTypeName -inputName $paramTypeNormalizedName;
413+
$param_constructor_code = Get-ConstructorCode -inputName $paramTypeNormalizedName;
129414

130415
$has_properties = $true;
131416
$is_string_list = Is-ListStringType $pt.ParameterType;
@@ -323,6 +608,7 @@ function Generate-PsFunctionCommandImpl
323608

324609
if (($does_contain_only_strings -eq $null) -or ($does_contain_only_strings.Count -eq 0))
325610
{
611+
# Complex Class Parameters
326612
$dynamic_param_assignment_code_lines +=
327613
@"
328614
var p${param_name} = new RuntimeDefinedParameter();
@@ -354,6 +640,7 @@ function Generate-PsFunctionCommandImpl
354640
}
355641
else
356642
{
643+
# String Parameters
357644
foreach ($s in $does_contain_only_strings)
358645
{
359646
$s = Get-SingularNoun $s;

src/ServiceManagement/Compute/Commands.ServiceManagement.Preview/Automation/Import-TypeFunction.ps1

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,35 +308,35 @@ function Get-StringTypes
308308
}
309309

310310

311-
function Get-ConstructorCodeByNormalizedTypeName
311+
function Get-ConstructorCode
312312
{
313313
param(
314314
# Sample: 'string' => 'string.Empty', 'HostedServiceCreateParameters' => 'new HostedServiceCreateParameters()', etc.
315315
[Parameter(Mandatory = $True)]
316-
[string]$inputName
316+
[string]$InputName
317317
)
318318

319-
if ([string]::IsNullOrEmpty($inputName))
319+
if ([string]::IsNullOrEmpty($InputName))
320320
{
321321
return 'null';
322322
}
323323

324-
if ($inputName -eq 'string')
324+
if ($InputName -eq 'string')
325325
{
326326
$outputName = 'string.Empty';
327327
}
328328
else
329329
{
330-
if ($inputName.StartsWith($client_model_namespace + "."))
330+
if ($InputName.StartsWith($client_model_namespace + "."))
331331
{
332-
$inputName = $inputName.Replace($client_model_namespace + ".", '');
332+
$InputName = $InputName.Replace($client_model_namespace + ".", '');
333333
}
334-
elseif ($inputName.StartsWith('System.Collections.Generic.'))
334+
elseif ($InputName.StartsWith('System.Collections.Generic.'))
335335
{
336-
$inputName = $inputName.Replace('System.Collections.Generic.', '');
336+
$InputName = $InputName.Replace('System.Collections.Generic.', '');
337337
}
338338

339-
$outputName = 'new ' + $inputName + "()";
339+
$outputName = 'new ' + $InputName + "()";
340340
}
341341

342342
return $outputName;

0 commit comments

Comments
 (0)