Skip to content

Commit 9254fdc

Browse files
committed
Update PIR Proj. with Auto-Generated Cmdlets
1 parent 244e98e commit 9254fdc

File tree

160 files changed

+8821
-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.

160 files changed

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

0 commit comments

Comments
 (0)