Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#
# Licensed under the MIT license.
#
# PSSwagger Module
#
#########################################################################################

ConvertFrom-StringData @'
Expand Down
97 changes: 97 additions & 0 deletions PSSwagger/AssemblyGenerationHelpers.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#########################################################################################
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# Licensed under the MIT license.
#
#########################################################################################

Microsoft.PowerShell.Core\Set-StrictMode -Version Latest
Microsoft.PowerShell.Utility\Import-LocalizedData LocalizedData -FileName AssemblyGenerationHelpers.Resources.psd1

<#
.DESCRIPTION
Compiles AutoRest generated C# code using the framework of the current PowerShell process.

.PARAMETER AssemblyFileName
Full Path to the output assembly.

.PARAMETER IsAzureSDK
C# code generated by Azure.CSharp AutoRest code generator.
#>
function New-SDKAssembly {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]
$AssemblyFileName,

[Parameter(Mandatory = $false)]
[switch]
$IsAzureSDK
)

$OperatingSystemInfo = Get-OperatingSystemInfo
if ($OperatingSystemInfo.IsCore) {
$clr = 'coreclr'
$framework = 'netstandard1'
}
else {
$clr = 'fullclr'
$framework = 'net4'
}

$ClrPath = Join-Path -Path $PSScriptRoot -ChildPath 'ref' | Join-Path -ChildPath $clr
$dllFullName = Join-Path -Path $ClrPath -ChildPath $AssemblyFileName
$UserConsent = $false
if (-not (Test-Path -Path $dllFullName -PathType Leaf)) {
Write-Verbose -Message ($LocalizedData.CompilingBinaryComponent -f ($dllFullName))

$generatedCSharpFilePath = Join-Path -Path $PSScriptRoot -ChildPath 'Generated.Csharp'
if (-not (Test-Path -Path $generatedCSharpFilePath -PathType Container)) {
Write-Error -ErrorId 'CSharpFilesNotFound' -Message ($LocalizedData.CSharpFilesNotFound -f ($generatedCSharpFilePath))
return
}

$CodePs1FilePath = Join-Path -Path $generatedCSharpFilePath -ChildPath '*.Code.ps1'
$AllCSharpFiles = Get-ChildItem -Path $CodePs1FilePath -Recurse -File
if ($OperatingSystemInfo.IsWindows) {
$AllCSharpFiles | ForEach-Object {
$AuthenticodeSignature = Get-AuthenticodeSignature -FilePath $_.FullName
if (('NotSigned' -ne $AuthenticodeSignature.Status) -and ('Valid' -ne $AuthenticodeSignature.Status)) {
Write-Error -Message $LocalizedData.HashValidationFailed -ErrorId 'HashValidationFailed'
return
}
}
Write-Verbose -Message $LocalizedData.HashValidationSuccessful
}

$dependencies = Get-PSSwaggerExternalDependencies -Azure:$IsAzureSDK -Framework $framework
$UserConsent = Initialize-PSSwaggerLocalTool -Azure:$IsAzureSDK -Framework $framework
$microsoftRestClientRuntimeAzureRequiredVersion = ''
if ($dependencies.ContainsKey('Microsoft.Rest.ClientRuntime.Azure')) {
$microsoftRestClientRuntimeAzureRequiredVersion = $dependencies['Microsoft.Rest.ClientRuntime.Azure'].RequiredVersion
}

$microsoftRestClientRuntimeRequiredVersion = $dependencies['Microsoft.Rest.ClientRuntime'].RequiredVersion
$newtonsoftJsonRequiredVersion = $dependencies['Newtonsoft.Json'].RequiredVersion

$AddPSSwaggerClientType_params = @{
CSharpFiles = $AllCSharpFiles
NewtonsoftJsonRequiredVersion = $newtonsoftJsonRequiredVersion
MicrosoftRestClientRuntimeRequiredVersion = $microsoftRestClientRuntimeRequiredVersion
MicrosoftRestClientRuntimeAzureRequiredVersion = $microsoftRestClientRuntimeAzureRequiredVersion
ClrPath = $ClrPath
BootstrapConsent = $UserConsent
CodeCreatedByAzureGenerator = $IsAzureSDK
}
$success = Add-PSSwaggerClientType @AddPSSwaggerClientType_params
if (-not $success) {
Write-Error -ErrorId 'UnableToGenerateAssembly' -Message ($LocalizedData.CompilationFailed -f ($dllFullName))
return
}

$message = $LocalizedData.CompilationSucceeded -f ($dllFullName)
Write-Verbose -Message $message
}
}
72 changes: 19 additions & 53 deletions PSSwagger/PSSwagger.Constants.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ $parameterDefString = @'

$parameterDefaultValueString = ' = $parameterDefaultValue'

$DynamicAssemblyGenerationBlock = @'
`$dllFullName = Join-Path -Path `$ClrPath -ChildPath '$DllFileName'
if(-not (Test-Path -Path `$dllFullName -PathType Leaf)) {
. (Join-Path -Path `$PSScriptRoot -ChildPath 'AssemblyGenerationHelpers.ps1')
New-SDKAssembly -AssemblyFileName '$DllFileName' -IsAzureSDK:`$$UseAzureCSharpGenerator
}
'@

$RootModuleContents = @'
Microsoft.PowerShell.Core\Set-StrictMode -Version Latest
Microsoft.PowerShell.Utility\Import-LocalizedData LocalizedData -filename $Name.Resources.psd1

# If the user supplied -Prefix to Import-Module, that applies to the nested module as well
# Force import the nested module again without -Prefix
if (-not (Get-Command Get-OperatingSystemInfo -Module PSSwaggerUtility -ErrorAction Ignore)) {
Expand All @@ -35,61 +43,20 @@ if (-not (Get-Command Get-OperatingSystemInfo -Module PSSwaggerUtility -ErrorAct

if ((Get-OperatingSystemInfo).IsCore) {
$testCoreModuleRequirements`$clr = 'coreclr'
`$framework = 'netstandard1'
} else {
$testFullModuleRequirements`$clr = 'fullclr'
`$framework = 'net4'
}

`$clrPath = Join-Path -Path `$PSScriptRoot -ChildPath 'ref' | Join-Path -ChildPath `$clr
`$dllFullName = Join-Path -Path `$clrPath -ChildPath '$Namespace.dll'
`$isAzureCSharp = `$$UseAzureCSharpGenerator
`$consent = `$false
if (-not (Test-Path -Path `$dllFullName)) {
`$message = `$LocalizedData.CompilingBinaryComponent -f (`$dllFullName)
Write-Verbose -Message `$message
`$generatedCSharpFilePath = (Join-Path -Path "`$PSScriptRoot" -ChildPath "Generated.Csharp")
if (-not (Test-Path -Path `$generatedCSharpFilePath)) {
throw `$LocalizedData.CSharpFilesNotFound -f (`$generatedCSharpFilePath)
}

`$allCSharpFiles = Get-ChildItem -Path (Join-Path -Path `$generatedCSharpFilePath -ChildPath "*.Code.ps1") -Recurse -Exclude Program.cs,TemporaryGeneratedFile* -File | Where-Object DirectoryName -notlike '*Azure.Csharp.Generated*'
if ((Get-OperatingSystemInfo).IsWindows) {
`$allCSharpFiles | ForEach-Object {
`$sig = Get-AuthenticodeSignature -FilePath `$_.FullName
if (('NotSigned' -ne `$sig.Status) -and ('Valid' -ne `$sig.Status)) {
throw `$LocalizedData.HashValidationFailed
}
}

`$message = `$LocalizedData.HashValidationSuccessful
Write-Verbose -Message `$message -Verbose
}

`$dependencies = Get-PSSwaggerExternalDependencies -Azure:`$isAzureCSharp -Framework `$framework
`$consent = Initialize-PSSwaggerLocalTool -Azure:`$isAzureCSharp -Framework `$framework
`$microsoftRestClientRuntimeAzureRequiredVersion = ''
if (`$dependencies.ContainsKey('Microsoft.Rest.ClientRuntime.Azure')) {
`$microsoftRestClientRuntimeAzureRequiredVersion = `$dependencies['Microsoft.Rest.ClientRuntime.Azure'].RequiredVersion
}

`$microsoftRestClientRuntimeRequiredVersion = `$dependencies['Microsoft.Rest.ClientRuntime'].RequiredVersion
`$newtonsoftJsonRequiredVersion = `$dependencies['Newtonsoft.Json'].RequiredVersion

`$success = Add-PSSwaggerClientType -CSharpFiles `$allCSharpFiles -NewtonsoftJsonRequiredVersion `$newtonsoftJsonRequiredVersion -MicrosoftRestClientRuntimeRequiredVersion `$microsoftRestClientRuntimeRequiredVersion -MicrosoftRestClientRuntimeAzureRequiredVersion "`$microsoftRestClientRuntimeAzureRequiredVersion" -ClrPath `$clrPath -BootstrapConsent:`$consent -CodeCreatedByAzureGenerator:`$isAzureCSharp
if (-not `$success) {
`$message = `$LocalizedData.CompilationFailed -f (`$dllFullName)
throw `$message
}

`$message = `$LocalizedData.CompilationFailed -f (`$dllFullName)
Write-Verbose -Message `$message
else {
$testFullModuleRequirements`$clr = 'fullclr'
}

`$ClrPath = Join-Path -Path `$PSScriptRoot -ChildPath 'ref' | Join-Path -ChildPath `$clr
$DynamicAssemblyGenerationCode
`$allDllsPath = Join-Path -Path `$ClrPath -ChildPath '*.dll'
Get-ChildItem -Path `$allDllsPath -File | ForEach-Object { Add-Type -Path `$_.FullName -ErrorAction SilentlyContinue }

. (Join-Path -Path `$PSScriptRoot -ChildPath 'GeneratedHelpers.ps1')

Get-ChildItem -Path (Join-Path -Path "`$PSScriptRoot" -ChildPath "ref" | Join-Path -ChildPath "`$clr" | Join-Path -ChildPath "*.dll") -File | ForEach-Object { Add-Type -Path `$_.FullName -ErrorAction SilentlyContinue }
Get-ChildItem -Path "`$PSScriptRoot\$GeneratedCommandsName\*.ps1" -Recurse -File | ForEach-Object { . `$_.FullName}
`$allPs1FilesPath = Join-Path -Path `$PSScriptRoot -ChildPath '$GeneratedCommandsName' | Join-Path -ChildPath '*.ps1'
Get-ChildItem -Path `$allPs1FilesPath -Recurse -File | ForEach-Object { . `$_.FullName}
'@

$advFnSignatureForDefintion = @'
Expand All @@ -114,7 +81,6 @@ $AsJobParameterString = @'


$advFnSignatureForPath = @'
Import-Module -Name (Join-Path -Path `$PSScriptRoot -ChildPath .. | Join-Path -ChildPath .. | Join-Path -ChildPath "GeneratedHelpers.psm1")
<#
$commandHelp
$paramHelp
Expand Down Expand Up @@ -193,7 +159,7 @@ $functionBodyStr = @'
`$ErrorActionPreference = 'Stop'
$securityBlock

$clientName = New-Object -TypeName $fullModuleName -ArgumentList $clientArgumentList$apiVersion
$clientName = New-Object -TypeName $FullClientTypeName -ArgumentList $clientArgumentList$apiVersion
$overrideBaseUriBlock
$GlobalParameterBlock
$oDataExpressionBlock
Expand Down
Loading