1- <#
1+ $Global :parameters = @ {}
2+
3+ <#
24. DESCRIPTION
35 Decodes the swagger spec and generates PowerShell cmdlets.
46
@@ -20,8 +22,7 @@ function Export-CommandFromSwagger
2022
2123 [Parameter (Mandatory = $true )]
2224 [string ] $moduleName
23-
24- )
25+ )
2526
2627 if (-not (Test-path $swaggerSpecPath ))
2728 {
@@ -41,6 +42,8 @@ function Export-CommandFromSwagger
4142 $null = New-Item - ItemType Directory $outputDirectory - ErrorAction Stop
4243
4344 $namespace = " Microsoft.PowerShell.$moduleName "
45+ $Global :parameters.Add (' namespace' , $namespace )
46+
4447 GenerateCsharpCode - swaggerSpecPath $swaggerSpecPath - path $outputDirectory - moduleName $moduleName - nameSpace $namespace
4548 GenerateModuleManifest - path $outputDirectory - moduleName $moduleName - rootModule " $moduleName .psm1"
4649
@@ -49,6 +52,12 @@ function Export-CommandFromSwagger
4952 $cmds = [System.Collections.ObjectModel.Collection [string ]]::new()
5053
5154 $jsonObject = ConvertFrom-Json ((Get-Content $swaggerSpecPath ) -join [Environment ]::NewLine) - ErrorAction Stop
55+
56+ # Populate the global parameters
57+ $null = ProcessGlobalParams - globalParams $jsonObject.parameters - info $jsonObject.info
58+ $null = ProcessDefinitions - definitions $jsonObject.definitions
59+
60+ # Handle the paths
5261 $jsonObject.Paths.PSObject.Properties | % {
5362 $jsonPathObject = $_.Value
5463 $jsonPathObject.psobject.Properties | % {
@@ -84,29 +93,12 @@ function $commandName
8493 [CmdletBinding()]
8594 param($paramblock
8695 )
96+
97+ $body
8798}
8899'@
89100
90- $commandName = ProcessOperationId $jsonPathItemObject.operationId
91- $description = $jsonPathItemObject.description
92- $commandHelp = $executionContext.InvokeCommand.ExpandString ($helpDescStr )
93-
94- [string ]$paramHelp = " "
95- $paramblock , $paramHelp = ProcessParameters $jsonPathItemObject.parameters
96-
97- $executionContext.InvokeCommand.ExpandString ($advFnSignature )
98-
99- }
100- <#
101- . DESCRIPTION
102- Returns a string that can be used in a parameter block.
103- #>
104- function ProcessParameters
105- {
106- param (
107- [object []] $parametersSpec )
108-
109- $parameterDefString = @'
101+ $parameterDefString = @'
110102
111103 [Parameter(Mandatory = $isParamMandatory)]
112104 [$paramType] $paramName,
@@ -119,34 +111,89 @@ $helpParamStr = @'
119111
120112'@
121113
122- $paramBlockToReturn = " "
123- [string ]$tempParamHelp = " "
124-
125- $parametersSpec | % {
126- # TODO: What to do with $ref
127- if ($_.Name )
128- {
114+ $functionBodyStr = @'
115+ $serviceCredentials = Get-AzCredentials
116+ $subscriptionId = Get-SubscriptionId
117+
118+ $clientName = [$fullModuleName]::new($serviceCredentials, [System.Net.Http.DelegatingHandler[]]::new(0))
119+ $clientName.ApiVersion = $infoVersion
120+ $clientName.SubscriptionId = $subscriptionId
121+ $operationVar = $clientName.$operations.$methodName($requiredParamList)
122+ '@
123+
124+ $commandName = ProcessOperationId $jsonPathItemObject.operationId
125+ $description = $jsonPathItemObject.description
126+ $commandHelp = $executionContext.InvokeCommand.ExpandString ($helpDescStr )
127+
128+ [string ]$paramHelp = " "
129+ $paramblock = " "
130+ $requiredParamList = " "
131+ $optionalParamList = " "
132+ $body = " "
133+
134+ # Handle the function parameters
135+ # region Function Parameters
136+
137+ $jsonPathItemObject.parameters | ForEach-Object {
138+ if ($_.name )
139+ {
129140 $isParamMandatory = ' $false'
130141 $paramName = ' $' + $_.Name
131142 $paramType = if ($_.type ) { $_.type } else { " object" }
132- if ($_.required ) { $isParamMandatory = ' $true' }
133- $paramBlockToReturn += $executionContext.InvokeCommand.ExpandString ($parameterDefString )
143+ if ($_.required )
144+ {
145+ $isParamMandatory = ' $true'
146+ $requiredParamList += $paramName + " , "
147+ }
148+ else
149+ {
150+ $optionalParamList += $paramName + " , "
151+ }
152+
153+ $paramblock += $executionContext.InvokeCommand.ExpandString ($parameterDefString )
134154 if ($_.description )
135155 {
136156 $pDescription = $_.description
137- # $tempParamHelp += $executionContext.InvokeCommand.ExpandString($helpParamStr)
138- $tempParamHelp += @"
157+ $paramHelp += @"
139158
140159.PARAMETER $ ( $_.Name )
141160 $pDescription
142161
143162"@
144163 }
145- }
146- } # $parametersSpec
164+ }
165+ elseif ($_ .' $ref' )
166+ {
167+ }
168+ }# $parametersSpec
169+
170+ $paramblock = $paramBlock.TrimEnd (" ," )
171+ $requiredParamList = $requiredParamList.TrimEnd (" , " )
172+ $optionalParamList = $optionalParamList.TrimEnd (" , " )
173+
174+ # endregion Function Parameters
175+
176+ # Handle the function body
177+ # region Function Body
178+ $infoVersion = $Global :parameters [' infoVersion' ]
179+ $modulePostfix = $Global :parameters [' infoTitle' ] + ' .' + $Global :parameters [' infoName' ]
180+ $fullModuleName = $Global :parameters [' namespace' ] + ' .' + $modulePostfix
181+ $clientName = ' $' + $modulePostfix.Split (' .' )[$_.count - 1 ]
182+
183+ $operationName = $jsonPathItemObject.operationId.Split (' _' )[0 ]
184+ $operationType = $jsonPathItemObject.operationId.Split (' _' )[1 ]
185+ $operations = $operationName + ' Operations'
186+ $methodName = $operationType + ' WithHttpMessagesAsync'
187+ $operationVar = ' $' + $operationName
188+
189+ $serviceCredentials = ' $' + ' serviceCredentials'
190+ $subscriptionId = ' $' + ' subscriptionId'
191+
192+ $body = $executionContext.InvokeCommand.ExpandString ($functionBodyStr )
147193
148- $paramBlockToReturn.TrimEnd (" ," )
149- $tempParamHelp
194+ # endregion Function Body
195+
196+ $executionContext.InvokeCommand.ExpandString ($advFnSignature )
150197}
151198
152199<#
@@ -184,92 +231,50 @@ function ProcessOperationId
184231 return " $cmdVerb -$cmdNoun "
185232}
186233
187- # endregion
188-
189- # region Module Generation Helpers
190-
191- function GenerateCsharpCode
234+ function ProcessGlobalParams
192235{
193236 param (
194- [Parameter (Mandatory = $true )]
195- [string ] $swaggerSpecPath ,
196-
197- [Parameter (Mandatory = $true )]
198- [string ] $path ,
199-
200- [Parameter (Mandatory = $true )]
201- [string ] $moduleName ,
202-
203- [Parameter (Mandatory = $true )]
204- [string ] $nameSpace
205-
206- )
207-
208- Write-Verbose " Generating CSharp Code using AutoRest"
209-
210- $autoRestExePath = get-command autorest.exe | % source
211- if (-not $autoRestExePath )
212- {
213- throw " Unable to find AutoRest.exe in PATH environment. Ensure the PATH is updated."
214- }
215-
216- $outputDirectory = $path
217- $outAssembly = join-path $outputDirectory azure.csharp.ps.generated.dll
218- $net45Dir = join-path $outputDirectory " Net45"
219- $generatedCSharpPath = Join-Path $outputDirectory " Generated.Csharp"
220- $startUpScriptFile = (join-path $outputDirectory $moduleName ) + " .StartupScript.ps1"
221- $moduleManifestFile = (join-path $outputDirectory $moduleName ) + " .psd1"
222-
223- if (Test-Path $outAssembly )
224- {
225- del $outAssembly - Force
226- }
227-
228- if (Test-Path $net45Dir )
229- {
230- del $net45Dir - Force - Recurse
231- }
237+ [PSCustomObject ] $globalParams ,
238+ [PSCustomObject ] $info
239+ )
232240
233- & $autoRestExePath - input $swaggerSpecPath - CodeGenerator CSharp - OutputDirectory $generatedCSharpPath - NameSpace $nameSpace
234- if ($LastExitCode )
235- {
236- throw " AutoRest resulted in an error"
241+ $globalParams.parameters.PSObject.Properties | % {
242+ $name = removeSpecialChars - strWithSpecialChars $_.name
243+ $Global :parameters.Add ($name , $jsonObject.parameters .$name )
237244 }
238245
239- Write-Verbose " Generating assembly from the CSharp code"
240-
241- $srcContent = dir $generatedCSharpPath - Filter * .cs - Recurse - Exclude Program.cs, TemporaryGeneratedFile* | ? DirectoryName -notlike ' *Azure.Csharp.Generated*' | % { " // File $ ( $_.FullName ) " ; get-content $_.FullName }
242- $oneSrc = $srcContent -join " `n "
243-
244- $refassemlbiles = @ (" System.dll" , " System.Core.dll" , " System.Net.Http.dll" ,
245- " System.Net.Http.WebRequest" , " System.Runtime.Serialization.dll" , " System.Xml.dll" ,
246- " $PSScriptRoot \Generated.Azure.Common.Helpers\Net45\Microsoft.Rest.ClientRuntime.dll" ,
247- " $PSScriptRoot \Generated.Azure.Common.Helpers\Net45\Newtonsoft.Json.dll" )
246+ $infoVersion = $info.version
247+ $infoTitle = $info.title
248+ $infoName = $info .' x-ms-code-generation-settings' .name
248249
249- Add-Type - TypeDefinition $oneSrc - ReferencedAssemblies $refassemlbiles - OutputAssembly $outAssembly
250+ $Global :parameters.Add (' infoVersion' , $infoVersion )
251+ $Global :parameters.Add (' infoTitle' , $infoTitle )
252+ $Global :parameters.Add (' infoName' , $infoName )
250253}
251254
252- function GenerateModuleManifest
255+ function ProcessDefinitions
253256{
254257 param (
255- [Parameter (Mandatory = $true )]
256- [string ] $path ,
258+ [PSCustomObject ] $definitions
259+ )
260+
261+ $definitionList = @ {}
262+ $definitions.PSObject.Properties | % {
263+ $name = $_.name
264+ $definitionList.Add ($name , $_ )
265+ }
257266
258- [ Parameter ( Mandatory = $true )]
259- [ string ] $moduleName ,
267+ $ Global :parameters .Add ( ' definitionList ' , $definitionList )
268+ }
260269
261- [ Parameter ( Mandatory = $true )]
262- [ string ] $rootModule
263- )
270+ function removeSpecialChars
271+ {
272+ param ([ string ] $strWithSpecialChars )
264273
265- $startUpScriptFile = (join-path $path $moduleName ) + " .StartupScript.ps1"
266- $moduleManifestFile = (join-path $path $moduleName ) + " .psd1"
267-
268- @'
269- Add-Type -LiteralPath "$PSScriptRoot\azure.csharp.ps.generated.dll"
270- '@ | out-file - Encoding Ascii $startUpScriptFile
274+ $pattern = ' [^a-zA-Z]'
275+ $resultStr = $strWithSpecialChars -replace $pattern , ' '
271276
272- New-ModuleManifest - Path $moduleManifestFile - Guid ( New-Guid ) - Author (whoami) - ScriptsToProcess ( $moduleName + " .StartupScript.ps1 " ) - RequiredModules " Generated.Azure.Common.Helpers " - RootModule " $rootModule "
277+ return $resultStr
273278}
274279
275280# endregion
0 commit comments