2021-09-20 16:50:33 +03:00
# ----------------------------------------------------------------------------------
#
# Copyright Microsoft Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------------
<#
. SYNOPSIS
Generate InitializeAssemblyLoadContext . ps1 and put it under output folder .
. PARAMETER ModuleFolder
The output folder for module , e . g . C: \ azure-powershell \ artifacts \ Debug \ Az . Compute
. PARAMETER AlcEntryAssembly
Entry assembly name of current module Assembly Load Context , e . g . " Azure. "
. PARAMETER AlcRefAssembly
Assembly referenced by module entry assemblies , separated by ';' , e . g . " Azure.Storage.Blobs;Azure.Storage.Common "
#>
param (
2021-09-28 09:29:19 +03:00
[ Parameter ( Mandatory = $true ) ]
2021-09-20 16:50:33 +03:00
[ string ]
$ModuleFolder ,
2021-09-28 09:29:19 +03:00
[ Parameter ( Mandatory = $true ) ]
2021-09-20 16:50:33 +03:00
[ string ]
$AlcEntryAssembly ,
2021-09-28 09:29:19 +03:00
[ Parameter ( Mandatory = $true ) ]
2021-09-20 16:50:33 +03:00
[ string ]
$AlcRefAssembly ,
2021-09-28 09:29:19 +03:00
[ Parameter ( Mandatory = $true ) ]
2021-09-20 16:50:33 +03:00
[ string ]
$Configuration
)
$alcTemplate = @ '
if ( $PSVersionTable . PSEdition -eq 'Core' )
{
try {
$assemblyLoadContextFolder = [ System.IO.Path ] :: Combine ( $PSScriptRoot , " .. " , " ModuleAlcAssemblies " )
2021-09-28 09:29:19 +03:00
Write-Debug " Registering module AssemblyLoadContext for path: ' $assemblyLoadContextFolder '. "
2021-09-20 16:50:33 +03:00
[ Microsoft.Azure.PowerShell.AuthenticationAssemblyLoadContext.AzAssemblyLoadContextInitializer ] :: RegisterModuleAssemblyLoadContext ( " %ASSEMBLY_LOAD_CONTEXT_ENTRY_ASSEMBLY% " , $assemblyLoadContextFolder )
2021-09-28 09:29:19 +03:00
Write-Debug " AssemblyLoadContext registered. "
} catch {
Write-Warning $_
2021-09-20 16:50:33 +03:00
}
}
' @
$alcTemplate = $alcTemplate . Replace ( " %ASSEMBLY_LOAD_CONTEXT_ENTRY_ASSEMBLY% " , $AlcEntryAssembly )
$scriptPath = [ System.IO.Path ] :: Combine ( $ModuleFolder , " PostImportScripts " , " InitializeAssemblyLoadContext.ps1 " )
2021-09-28 09:29:19 +03:00
if ( -not ( Test-Path $scriptPath ) ) {
2021-09-20 16:50:33 +03:00
New-Item -ItemType File -Path $scriptPath -Force
}
$alcTemplate | Out-File -FilePath $scriptPath -Encoding utf8 -Force
$isReleaseConfiguration = $Configuration -eq 'Release'
$alcDestinationDir = [ System.IO.Path ] :: Combine ( $ModuleFolder , " ModuleAlcAssemblies " )
$entryAssemblyPath = [ System.IO.Path ] :: Combine ( $ModuleFolder , $AlcEntryAssembly + " .dll " )
2021-09-28 09:29:19 +03:00
if ( -not ( Test-Path $alcDestinationDir ) ) {
2021-09-20 16:50:33 +03:00
New-Item -ItemType Directory -Path $alcDestinationDir -Force
}
2021-09-28 09:29:19 +03:00
if ( $isReleaseConfiguration ) {
2021-09-20 16:50:33 +03:00
Move-Item -Path $entryAssemblyPath -Destination $alcDestinationDir -Force
}
2021-09-28 09:29:19 +03:00
else {
2021-09-20 16:50:33 +03:00
Copy-Item -Path $entryAssemblyPath -Destination $alcDestinationDir -Force #Move-Item cause test project build error: CS0006 metadata file AlcWrapper.dll could not be found
}
2021-09-28 09:29:19 +03:00
if ( -not [ System.String ] :: IsNullOrEmpty ( $AlcRefAssembly ) ) {
2021-09-20 16:50:33 +03:00
$refAssemblies = $AlcRefAssembly . Split ( ';' )
$refAssemblies | ForEach-Object {
$refAssemblyPath = [ System.IO.Path ] :: Combine ( $ModuleFolder , $_ + " .dll " )
2021-09-28 09:29:19 +03:00
if ( $isReleaseConfiguration ) {
2021-09-20 16:50:33 +03:00
Move-Item -Path $refAssemblyPath -Destination $alcDestinationDir -Force
}
else {
Copy-Item -Path $refAssemblyPath -Destination $alcDestinationDir -Force
}
}
2021-11-16 10:11:43 +03:00
}