|
| 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 | +} |
0 commit comments