Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 5253fb4

Browse files
deathly809bmanikm
authored andcommitted
Support NPM installed Autorest (#271)
1 parent 59b33ef commit 5253fb4

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

PSSwagger/PSSwagger.Resources.psd1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ConvertFrom-StringData @'
2424
SwaggerSpecPathNotExist=Swagger file $SwaggerSpecPath does not exist. Check the path
2525
SamePropertyName=Same property name '{0}' is defined in a definition '{1}' with AllOf inheritance.
2626
DataTypeNotImplemented=Please get an implementation of '{0}' for '{1}'
27-
AutoRestNotInPath=Unable to find AutoRest.exe in PATH environment. Ensure the PATH is updated.
27+
AutoRestNotInPath=Unable to find AutoRest in PATH environment. Ensure the PATH is updated.
2828
AutoRestError=AutoRest resulted in an error
2929
SwaggerParamsMissing=No parameters in the Swagger
3030
SwaggerDefinitionsMissing=No definitions in the Swagger
@@ -62,11 +62,11 @@ ConvertFrom-StringData @'
6262
InvalidPathParameterType=Extracted an invalid parameter type '{0}' for parameter '{1}' in path operation.
6363
InvalidDefinitionParameterType=Extracted an invalid parameter type '{0}' for parameter '{1}' in definition '{2}'.
6464
AutoRestParameterIgnored=The parameter '{0}' with value '{1}' specified in the Swagger specification will be overwritten by PSSwagger during code generation.
65-
InvokingAutoRestWithParams=Invoking AutoRest.exe with the following parameters:
65+
InvokingAutoRestWithParams=Invoking AutoRest with the following parameters:
6666
AutoRestParam= {0} = {1}
6767
OutputDirectoryMustBeEmpty=Code output directory '{0}' must not contain any .cs files in order for PSSwagger to correctly determine the files from which the SDK should be built.
6868
CustomNamespaceNotRecommended=Custom namespaces are not recommended when using PSSwagger, as the generated modules may not support side-by-side.
69-
CodeDirectoryWillBeCreated=Code output directory '{0}' will be created by AutoRest.exe and contain unused generated code. You may delete this directory after the module has finished generating if you wish.
69+
CodeDirectoryWillBeCreated=Code output directory '{0}' will be created by AutoRest and contain unused generated code. You may delete this directory after the module has finished generating if you wish.
7070
MultiplePageReturnTypes=Multiple page return types found, unable to generate -Page parameter with a strong type for cmdlet '{0}'.
7171
FailedToFindPagingOperation=Failed to find specified next page operation with operationId '{0}' for cmdlet '{1}'.
7272
InvalidPagingOperationSchema=PSSwagger requires that the NextLink operation contains exactly one parameter different than the original operation, where the different parameter is used to pass the nextLink value to the NextLink operation. Current cmdlet: '{0}'. NextLink operation: '{1}'

PSSwagger/PSSwagger.psm1

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,9 @@ function ConvertTo-CsharpCode
528528
Write-Verbose -Message $LocalizedData.GenerateCodeUsingAutoRest
529529
$info = $SwaggerDict['Info']
530530

531-
$autoRestExePath = "autorest.exe"
532-
if (-not (get-command -name autorest.exe))
533-
{
534-
throw $LocalizedData.AutoRestNotInPath
531+
$autoRestExePath = "AutoRest"
532+
if (-not (get-command -name $autoRestExePath)) {
533+
throw $LocalizedData.AutoRestNotInPath
535534
}
536535

537536
$outputDirectory = $SwaggerMetaDict['outputDirectory']

PSSwagger/SwaggerUtils.psm1

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,41 @@ function Get-Response
15591559
return $responseBody, $outputType
15601560
}
15611561

1562+
<#
1563+
1564+
Get-ToolsPath
1565+
1566+
#>
1567+
function Get-ToolsPath {
1568+
[string]$AutoRestToolsPath = $null
1569+
1570+
# Note: DLLs are automatically downloaded and extracted into the folder
1571+
# "$env:USERPROFILE/.autorest/plugins/autorest/$VERSION" if they do not
1572+
# exist for newer versions of autorest.
1573+
[string]$basePath = Join-Path -Path $env:USERPROFILE -ChildPath ".autorest" | Join-Path -ChildPath "plugins" | Join-Path -ChildPath "autorest"
1574+
1575+
1576+
if(Test-Path $basePath) { # Try to load newer version of autorest
1577+
$versions = @(Get-ChildItem -Directory $basePath | ForEach-Object {[System.Version]$_.Name} | Sort-Object -Descending)
1578+
1579+
if($versions.Length -ne 0) {
1580+
[string]$version = $versions[0] # Get newest
1581+
$AutoRestToolsPath = Join-Path -Path $basePath -ChildPath $version
1582+
}
1583+
} else { # Fallback to old version of autorest
1584+
$AutoRestToolsPath = Get-Command -Name 'AutoRest.exe' |
1585+
Select-Object -First 1 -ErrorAction Ignore |
1586+
ForEach-Object {Split-Path -LiteralPath $_.Source}
1587+
}
1588+
1589+
if (-not ($AutoRestToolsPath))
1590+
{
1591+
throw $LocalizedData.AutoRestNotInPath
1592+
}
1593+
1594+
return $AutoRestToolsPath
1595+
}
1596+
15621597
function Get-CSharpModelName
15631598
{
15641599
param(
@@ -1574,15 +1609,8 @@ function Get-CSharpModelName
15741609
[string]
15751610
$AssemblyName
15761611
)
1577-
1578-
$AutoRestToolsPath = Get-Command -Name 'AutoRest.exe' |
1579-
Select-Object -First 1 -ErrorAction Ignore |
1580-
ForEach-Object {Split-Path -LiteralPath $_.Source}
1581-
1582-
if (-not ($AutoRestToolsPath))
1583-
{
1584-
throw $LocalizedData.AutoRestNotInPath
1585-
}
1612+
1613+
[string]$AutoRestToolsPath = Get-ToolsPath
15861614

15871615
$AssemblyFilePath = Join-Path -Path $AutoRestToolsPath -ChildPath $AssemblyName
15881616
if(-not $AssemblyFilePath -or -not (Test-Path -LiteralPath $AssemblyFilePath -PathType Leaf))

Tests/PSSwaggerScenario.Tests.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,14 @@ Describe "Optional parameter tests" -Tag ScenarioTest {
226226
$results.Length | should be 2
227227
}
228228

229+
It "Generates cmdlet using optional query parameters (flavor and maker)" {
230+
$results = Get-Cupcake -Flavor "chocolate" -Maker "bob"
231+
$results.Length | should be 1
232+
}
233+
229234
It "Generates cmdlet using optional path parameters" {
230-
Get-CupcakeByMaker -Flavor "chocolate" -Maker "bob"
235+
$results = Get-CupcakeByMaker -Flavor "chocolate" -Maker "bob"
236+
$results.Length | should be 1
231237
}
232238

233239
It "Sets default value when specified in spec" {

0 commit comments

Comments
 (0)