forked from Azure/azure-powershell
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCheckAssemblies.ps1
107 lines (91 loc) · 4.99 KB
/
CheckAssemblies.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# ----------------------------------------------------------------------------------
# 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.
# ----------------------------------------------------------------------------------
param(
[ValidateNotNullOrEmpty()]
[ValidateSet('Debug', 'Release')]
[System.String]$BuildConfig
)
function Get-PreloadAssemblies{
param(
[Parameter(Mandatory)]
[string] $BuildFolder,
[Parameter(Mandatory=$True)]
[string] $ModuleFolder
)
Write-Host "Getting preload assemblies in $BuildFolder for $ModuleFolder"
Add-Type -Path ([System.IO.Path]::Combine($BuildFolder, "Az.Accounts", "Microsoft.Azure.PowerShell.AssemblyLoading.dll"))
$assemblyRootPath = [System.IO.Path]::Combine($BuildFolder, "Az.Accounts", "lib")
$conditionalAssemblyContext = [Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyContext]::new($PSVersionTable.PSEdition, $PSVersionTable.PSVersion)
[Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyProvider]::Initialize($assemblyRootPath, $conditionalAssemblyContext)
$assemblyDict = [Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyProvider]::GetAssemblies()
return $assemblyDict.Keys
}
$ProjectPaths = @( "$PSScriptRoot\..\artifacts\$BuildConfig" )
$DependencyMapPath = "$PSScriptRoot\..\artifacts\StaticAnalysisResults\DependencyMap.csv"
if (-not (Test-Path $DependencyMapPath)) {
Write-Host "$DependencyMapPath does not exist. Skip it."
return
}
$DependencyMap = Import-Csv -Path $DependencyMapPath
.($PSScriptRoot + "\PreloadToolDll.ps1")
$ModuleManifestFiles = $ProjectPaths | ForEach-Object { Get-Item "Az.*.psd1" | Where-Object { $_.FullName -like "*$($BuildConfig)*" -and `
$_.FullName -notlike "*Netcore*" -and `
$_.FullName -notlike "*dll-Help.psd1*" -and `
(-not [Tools.Common.Utilities.ModuleFilter]::IsAzureStackModule($_.FullName)) } }
foreach ($ModuleManifest in $ModuleManifestFiles) {
Write-Host "checking $($ModuleManifest.Fullname)"
$ModuleName = $ModuleManifest.Name.Replace(".psd1", "")
if ("Az.Resources" -eq $ModuleName)
{
Continue;
}
$Assemblies = $DependencyMap | Where-Object { $_.Directory.EndsWith($ModuleName) }
Import-LocalizedData -BindingVariable ModuleMetadata -BaseDirectory $ModuleManifest.DirectoryName -FileName $ModuleManifest.Name
$LoadedAssemblies = @()
if ($ModuleMetadata.RequiredAssemblies.Count -gt 0) {
$LoadedAssemblies += $ModuleMetadata.RequiredAssemblies
}
$LoadedAssemblies += Get-PreloadAssemblies -BuildFolder "$PSScriptRoot\..\artifacts\$BuildConfig" -ModuleFolder $ModuleManifest.Directory
$LoadedAssemblies += $ModuleMetadata.NestedModules
if ($ModuleMetadata.RequiredModules) {
$RequiredModules = $ModuleMetadata.RequiredModules | ForEach-Object { $_["ModuleName"] }
foreach ($RequiredModule in $RequiredModules) {
Write-Output ("ModuleManifest: " + $RequiredModuleManifest)
Write-Output ("Required Module: " + $RequiredModule)
$RequiredModuleManifest = $ModuleManifestFiles | Where-Object { $_.Name.Replace(".psd1", "") -eq $RequiredModule } | Select-Object -First 1
if (-not $RequiredModuleManifest) {
continue
}
$RequiredModuleManifest | ForEach-Object {
Import-LocalizedData -BindingVariable ModuleMetadata -BaseDirectory $_.DirectoryName -FileName $_.Name
if ($ModuleMetadata.RequiredAssemblies.Count -gt 0) {
$LoadedAssemblies += $ModuleMetadata.RequiredAssemblies
}
$LoadedAssemblies += $ModuleMetadata.NestedModules
}
$LoadedAssemblies += Get-PreloadAssemblies -BuildFolder "$PSScriptRoot\..\artifacts\$BuildConfig" -ModuleFolder $RequiredModuleManifest.Directory
}
}
$LoadedAssemblies = $LoadedAssemblies | Where-Object { $_ }
$LoadedAssemblies = $LoadedAssemblies | ForEach-Object { $_.Replace(".dll", "") }
$Found = @()
foreach ($Assembly in $Assemblies) {
if ($Found -notcontains $Assembly.AssemblyName -and $LoadedAssemblies -notcontains $Assembly.AssemblyName -and $Assembly.AssemblyName -notlike "System.Management.Automation*") {
$Found += $Assembly.AssemblyName
Write-Error "ERROR: Assembly $($Assembly.AssemblyName) was not included in the required assemblies field for module $ModuleName"
}
}
if ($Found.Count -gt 0) {
throw
}
}