Skip to content

Commit 74c142f

Browse files
committed
Auto Generate Invoke Cmdlets for Compute
1 parent ef76abf commit 74c142f

File tree

45 files changed

+2385
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2385
-0
lines changed
Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
[CmdletBinding()]
2+
param(
3+
[Parameter(Mandatory = $True)]
4+
[string]$dllFolder,
5+
6+
[Parameter(Mandatory = $True)]
7+
[string]$outFolder
8+
)
9+
10+
$code_common_header =
11+
@"
12+
//
13+
// Copyright (c) Microsoft and contributors. All rights reserved.
14+
//
15+
// Licensed under the Apache License, Version 2.0 (the "License");
16+
// you may not use this file except in compliance with the License.
17+
// You may obtain a copy of the License at
18+
// http://www.apache.org/licenses/LICENSE-2.0
19+
//
20+
// Unless required by applicable law or agreed to in writing, software
21+
// distributed under the License is distributed on an "AS IS" BASIS,
22+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23+
//
24+
// See the License for the specific language governing permissions and
25+
// limitations under the License.
26+
//
27+
28+
// Warning: This code was generated by a tool.
29+
//
30+
// Changes to this file may cause incorrect behavior and will be lost if the
31+
// code is regenerated.
32+
"@;
33+
34+
$client_library_namespace = 'Microsoft.Azure.Management.Compute';
35+
$code_common_namespace = ($client_library_namespace.Replace('.Management.', '.Commands.')) + '.Automation';
36+
37+
function Get-NormalizedName
38+
{
39+
param(
40+
[Parameter(Mandatory = $True)]
41+
[string]$inputName
42+
)
43+
44+
if ([string]::IsNullOrEmpty($inputName))
45+
{
46+
return $inputName;
47+
}
48+
49+
if ($inputName.StartsWith('vm'))
50+
{
51+
$outputName = 'VM' + $inputName.Substring(2);
52+
}
53+
else
54+
{
55+
[char]$firstChar = $inputName[0];
56+
$firstChar = [System.Char]::ToUpper($firstChar);
57+
$outputName = $firstChar + $inputName.Substring(1);
58+
}
59+
60+
return $outputName;
61+
}
62+
63+
function Get-OperationShortName
64+
{
65+
param(
66+
[Parameter(Mandatory = $True)]
67+
[string]$opFullName
68+
)
69+
70+
$prefix = 'I';
71+
$suffix = 'Operations';
72+
$opShortName = $opFullName;
73+
74+
if ($opFullName.StartsWith($prefix) -and $opShortName.EndsWith($suffix))
75+
{
76+
$opShortName = $opShortName.Substring($prefix.Length, ($opShortName.Length - $prefix.Length - $suffix.Length));
77+
}
78+
79+
return $opShortName;
80+
}
81+
82+
function Write-BaseCmdletFile
83+
{
84+
param(
85+
[Parameter(Mandatory = $True)]
86+
[string]$fileFullPath,
87+
88+
[Parameter(Mandatory = $True)]
89+
$operationNameList,
90+
91+
[Parameter(Mandatory = $True)]
92+
$clientClass
93+
)
94+
95+
96+
[System.Reflection.PropertyInfo[]]$propItems = $clientClass.GetProperties();
97+
98+
$operation_get_code = "";
99+
foreach ($opFullName in $operationNameList)
100+
{
101+
[string]$sOpFullName = $opFullName;
102+
Write-Output ('$sOpFullName = ' + $sOpFullName);
103+
$prefix = 'I';
104+
$suffix = 'Operations';
105+
if ($sOpFullName.StartsWith($prefix) -and $sOpFullName.EndsWith($suffix))
106+
{
107+
$opShortName = Get-OperationShortName $sOpFullName;
108+
$opPropName = $opShortName;
109+
foreach ($propItem in $propItems)
110+
{
111+
if ($propItem.PropertyType.Name -eq $opFullName)
112+
{
113+
$opPropName = $propItem.Name;
114+
break;
115+
}
116+
}
117+
118+
$operation_get_template =
119+
@"
120+
public I${opShortName}Operations ${opShortName}Client
121+
{
122+
get
123+
{
124+
return ComputeClient.ComputeManagementClient.${opPropName};
125+
}
126+
}
127+
"@;
128+
129+
if (-not ($operation_get_code -eq ''))
130+
{
131+
$operation_get_code += "`r`n";
132+
}
133+
134+
$operation_get_code += $operation_get_template;
135+
}
136+
}
137+
138+
$source_template =
139+
@"
140+
${code_common_header}
141+
142+
using ${client_library_namespace};
143+
144+
namespace ${code_common_namespace}
145+
{
146+
public abstract class ComputeAutomationBaseCmdlet : ComputeClientBaseCmdlet
147+
{
148+
${operation_get_code}
149+
}
150+
}
151+
"@;
152+
153+
Set-Content -Path $fileFullPath -Value ($source_template | Out-String) -Force;
154+
}
155+
156+
function Write-OperationCmdletFile
157+
{
158+
param(
159+
[Parameter(Mandatory = $True)]
160+
[string]$fileOutputFolder,
161+
162+
[Parameter(Mandatory = $True)]
163+
$opShortName,
164+
165+
[Parameter(Mandatory = $True)]
166+
[System.Reflection.MethodInfo]$operationMethodInfo
167+
)
168+
169+
$methodName = ($operationMethodInfo.Name.Replace('Async', ''));
170+
$cmdlet_verb = "Invoke";
171+
$cmdlet_noun = "Azure" + $opShortName + $methodName;
172+
$cmdlet_class_name = $cmdlet_verb + $cmdlet_noun + 'Cmdlet';
173+
174+
$indents = " " * 8;
175+
$new_line = "`r`n";
176+
$get_set_block = '{ get; set; }';
177+
178+
$cmdlet_generated_code = '';
179+
# $cmdlet_generated_code += $indents + '// ' + $operationMethodInfo + $new_line;
180+
181+
$params = $operationMethodInfo.GetParameters();
182+
[System.Collections.ArrayList]$param_names = @();
183+
foreach ($pt in $params)
184+
{
185+
$paramTypeFullName = $pt.ParameterType.FullName;
186+
if (-not ($paramTypeFullName.EndsWith('CancellationToken')))
187+
{
188+
$normalized_param_name = Get-NormalizedName $pt.Name;
189+
190+
Write-Output (' ' + $paramTypeFullName + ' ' + $normalized_param_name);
191+
192+
$param_attributes = $indents + "[Parameter(Mandatory = true), ValidateNotNullOrEmpty]" + $new_line;
193+
$param_definition = $indents + "public ${paramTypeFullName} ${normalized_param_name} " + $get_set_block + $new_line;
194+
$param_code_content = $param_attributes + $param_definition;
195+
196+
$cmdlet_generated_code += $param_code_content + $new_line;
197+
198+
$param_names.Add($normalized_param_name);
199+
}
200+
}
201+
202+
$params_join_str = [string]::Join(', ', $param_names.ToArray());
203+
204+
$cmdlet_client_call_template =
205+
@"
206+
public override void ExecuteCmdlet()
207+
{
208+
base.ExecuteCmdlet();
209+
210+
ExecuteClientAction(() =>
211+
{
212+
var result = ${opShortName}Client.${methodName}(${params_join_str});
213+
WriteObject(result, true);
214+
});
215+
}
216+
"@;
217+
218+
$cmdlet_generated_code += $cmdlet_client_call_template;
219+
220+
$cmdlt_source_template =
221+
@"
222+
${code_common_header}
223+
224+
using Microsoft.Azure.Management.Compute;
225+
using System.Management.Automation;
226+
227+
namespace Microsoft.Azure.Commands.Compute.Automation
228+
{
229+
[Cmdlet(`"${cmdlet_verb}`", `"${cmdlet_noun}`")]
230+
public class ${cmdlet_class_name} : ComputeAutomationBaseCmdlet
231+
{
232+
${cmdlet_generated_code}
233+
}
234+
}
235+
"@;
236+
237+
$fileFullPath = $fileOutputFolder + '/' + $cmdlet_class_name + '.cs';
238+
Set-Content -Path $fileFullPath -Value $cmdlt_source_template -Force;
239+
}
240+
241+
Write-Output $dllFolder;
242+
Write-Output $outFolder;
243+
244+
$outFolder += '/Generated';
245+
246+
$output = Get-ChildItem -Path $dllFolder | Out-String;
247+
248+
# Set-Content -Path ($outFolder + '/Output.txt');
249+
Write-Output $output;
250+
251+
252+
$dllname = 'Microsoft.Azure.Management.Compute';
253+
$dllfile = $dllname + '.dll';
254+
$dllFileFullPath = $dllFolder + '\' + $dllfile;
255+
256+
if (-not (Test-Path -Path $dllFileFullPath))
257+
{
258+
Write-Output "DLL file `'$dllFileFullPath`' not found. Exit.";
259+
}
260+
else
261+
{
262+
$assembly = [System.Reflection.Assembly]::LoadFrom($dllFileFullPath);
263+
264+
# All original types
265+
$types = $assembly.GetTypes();
266+
$filtered_types = $types | where { $_.Namespace -eq $dllname -and $_.Name -like 'I*Operations' };
267+
Write-Output ($filtered_types | select Namespace, Name);
268+
269+
# Write Base Cmdlet File
270+
$baseCmdletFileFullName = $outFolder + '\' + 'ComputeAutomationBaseCmdlet.cs';
271+
$opNameList = ($filtered_types | select -ExpandProperty Name);
272+
$clientClassType = $types | where { $_.Namespace -eq $dllname -and $_.Name -eq 'IComputeManagementClient' };
273+
Write-BaseCmdletFile $baseCmdletFileFullName $opNameList $clientClassType;
274+
275+
# Write Cmdlet Files
276+
foreach ($ft in $filtered_types)
277+
{
278+
Write-Output '=============================================';
279+
Write-Output $ft.Name;
280+
Write-Output '=============================================';
281+
282+
$opShortName = Get-OperationShortName $ft.Name;
283+
$opOutFolder = $outFolder + '/' + $opShortName;
284+
$st = rmdir -Recurse -Force $opOutFolder;
285+
$st = mkdir -Force $opOutFolder;
286+
287+
$methods = $ft.GetMethods();
288+
foreach ($mt in $methods)
289+
{
290+
Write-Output ($mt.Name.Replace('Async', ''));
291+
Write-OperationCmdletFile $opOutFolder $opShortName $mt;
292+
}
293+
}
294+
295+
Write-Output "=============================================";
296+
Write-Output "Finished.";
297+
Write-Output "=============================================";
298+
}

src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,49 @@
163163
<Compile Include="Extension\SetAzureVMExtensionCommand.cs" />
164164
<Compile Include="Extension\RemoveAzureVMExtensionCommand.cs" />
165165
<Compile Include="Extension\GetAzureVMExtensionCommand.cs" />
166+
<Compile Include="Generated\AvailabilitySet\InvokeAzureAvailabilitySetCreateOrUpdateCmdlet.cs" />
167+
<Compile Include="Generated\AvailabilitySet\InvokeAzureAvailabilitySetDeleteCmdlet.cs" />
168+
<Compile Include="Generated\AvailabilitySet\InvokeAzureAvailabilitySetGetCmdlet.cs" />
169+
<Compile Include="Generated\AvailabilitySet\InvokeAzureAvailabilitySetListAvailableSizesCmdlet.cs" />
170+
<Compile Include="Generated\AvailabilitySet\InvokeAzureAvailabilitySetListCmdlet.cs" />
171+
<Compile Include="Generated\ComputeAutomationBaseCmdlet.cs" />
172+
<Compile Include="Generated\Usage\InvokeAzureUsageListCmdlet.cs" />
173+
<Compile Include="Generated\VirtualMachineExtensionImage\InvokeAzureVirtualMachineExtensionImageGetCmdlet.cs" />
174+
<Compile Include="Generated\VirtualMachineExtensionImage\InvokeAzureVirtualMachineExtensionImageListTypesCmdlet.cs" />
175+
<Compile Include="Generated\VirtualMachineExtensionImage\InvokeAzureVirtualMachineExtensionImageListVersionsCmdlet.cs" />
176+
<Compile Include="Generated\VirtualMachineExtension\InvokeAzureVirtualMachineExtensionBeginCreatingOrUpdatingCmdlet.cs" />
177+
<Compile Include="Generated\VirtualMachineExtension\InvokeAzureVirtualMachineExtensionBeginDeletingCmdlet.cs" />
178+
<Compile Include="Generated\VirtualMachineExtension\InvokeAzureVirtualMachineExtensionCreateOrUpdateCmdlet.cs" />
179+
<Compile Include="Generated\VirtualMachineExtension\InvokeAzureVirtualMachineExtensionDeleteCmdlet.cs" />
180+
<Compile Include="Generated\VirtualMachineExtension\InvokeAzureVirtualMachineExtensionGetCmdlet.cs" />
181+
<Compile Include="Generated\VirtualMachineExtension\InvokeAzureVirtualMachineExtensionGetWithInstanceViewCmdlet.cs" />
182+
<Compile Include="Generated\VirtualMachineImage\InvokeAzureVirtualMachineImageGetCmdlet.cs" />
183+
<Compile Include="Generated\VirtualMachineImage\InvokeAzureVirtualMachineImageListCmdlet.cs" />
184+
<Compile Include="Generated\VirtualMachineImage\InvokeAzureVirtualMachineImageListOffersCmdlet.cs" />
185+
<Compile Include="Generated\VirtualMachineImage\InvokeAzureVirtualMachineImageListPublishersCmdlet.cs" />
186+
<Compile Include="Generated\VirtualMachineImage\InvokeAzureVirtualMachineImageListSkusCmdlet.cs" />
187+
<Compile Include="Generated\VirtualMachineSize\InvokeAzureVirtualMachineSizeListCmdlet.cs" />
188+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineBeginCapturingCmdlet.cs" />
189+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineBeginCreatingOrUpdatingCmdlet.cs" />
190+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineBeginDeallocatingCmdlet.cs" />
191+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineBeginDeletingCmdlet.cs" />
192+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineBeginPoweringOffCmdlet.cs" />
193+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineBeginRestartingCmdlet.cs" />
194+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineBeginStartingCmdlet.cs" />
195+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineCaptureCmdlet.cs" />
196+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineCreateOrUpdateCmdlet.cs" />
197+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineDeallocateCmdlet.cs" />
198+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineDeleteCmdlet.cs" />
199+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineGeneralizeCmdlet.cs" />
200+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineGetCmdlet.cs" />
201+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineGetWithInstanceViewCmdlet.cs" />
202+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineListAllCmdlet.cs" />
203+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineListAvailableSizesCmdlet.cs" />
204+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineListCmdlet.cs" />
205+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineListNextCmdlet.cs" />
206+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachinePowerOffCmdlet.cs" />
207+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineRestartCmdlet.cs" />
208+
<Compile Include="Generated\VirtualMachine\InvokeAzureVirtualMachineStartCmdlet.cs" />
166209
<Compile Include="Images\GetAzureVMImageCommand.cs" />
167210
<Compile Include="Common\HashTableExtensions.cs" />
168211
<Compile Include="Models\PSComputeLongRunningOperation.cs" />
@@ -251,6 +294,7 @@
251294
</ProjectReference>
252295
</ItemGroup>
253296
<ItemGroup>
297+
<None Include="Automation\RunCodeGeneration.ps1" />
254298
<None Include="Microsoft.Azure.Commands.Compute.dll-Help.psd1">
255299
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
256300
</None>
@@ -305,4 +349,12 @@
305349
<Target Name="AfterBuild">
306350
</Target>
307351
-->
352+
<Target Name="BeforeBuild" Condition="('$(BuildingInsideVisualStudio)'=='true')">
353+
<PropertyGroup>
354+
<PowerShellExe Condition=" '$(PowerShellExe)'=='' ">%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe</PowerShellExe>
355+
<PSCodeGenScriptPath>".\Automation\RunCodeGeneration.ps1"</PSCodeGenScriptPath>
356+
<PSCodeGenTargetFolder>$(OutputPath)</PSCodeGenTargetFolder>
357+
</PropertyGroup>
358+
<Exec Command="$(PowerShellExe) $(PSCodeGenScriptPath) $(PSCodeGenTargetFolder) $(MSBuildProjectDirectory)" />
359+
</Target>
308360
</Project>

0 commit comments

Comments
 (0)