forked from microsoft/WindowsAppSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.psm1
180 lines (150 loc) · 5.66 KB
/
Utils.psm1
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# There's a bug in Powershell where it doesn't properly handle bad paths in the LIB environment variable
# when loading C# reference assemblies. In order to work around that bug, we'll manually test the paths in LIB
# and remove ones that don't actually exist.
if ($env:LIB -ne $null -and $env:LIB.Length -gt 0)
{
[System.Collections.Generic.List[string]]$libPaths = @()
foreach ($libPath in $env:LIB.Split(';', [System.StringSplitOptions]::RemoveEmptyEntries))
{
if (Test-Path $libPath)
{
$libPaths.Add($libPath)
}
}
$env:LIB = $libPaths -join ";"
}
Add-Type -Language CSharp @"
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices.WindowsRuntime;
namespace WinmdHelper
{
public class LoadWinmdTypes
{
public static List<Type> FromFile(string assemblyPath, string referenceWinmds)
{
var references = new List<string>(referenceWinmds.Replace("\\\\", "\\").Split(';'));
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += (sender, eventArgs) =>
{
Assembly assembly = null;
foreach (var reference in references)
{
string name = eventArgs.Name.Split(',')[0];
if (reference.Contains(name))
{
assembly = Assembly.ReflectionOnlyLoadFrom(reference);
break;
}
}
if (assembly == null)
{
string fullName = eventArgs.Name;
assembly = Assembly.ReflectionOnlyLoad(fullName);
}
return assembly;
};
WindowsRuntimeMetadata.ReflectionOnlyNamespaceResolve += (sender, eventArgs) =>
{
foreach (var refer in references)
{
eventArgs.ResolvedAssemblies.Add(Assembly.ReflectionOnlyLoadFrom(refer));
}
return;
};
string fullAssemblyPath = Path.GetFullPath(assemblyPath);
Assembly loadFrom = null;
foreach (var loadedAssembly in AppDomain.CurrentDomain.ReflectionOnlyGetAssemblies())
{
if (String.Equals(loadedAssembly.Location, fullAssemblyPath, StringComparison.OrdinalIgnoreCase))
{
loadFrom = loadedAssembly;
break;
}
}
if (loadFrom == null)
{
loadFrom = Assembly.ReflectionOnlyLoadFrom(fullAssemblyPath);
}
return loadFrom.GetExportedTypes().ToList();
}
public static List<Type> GetActivatableTypesFromFile(string assemblyPath, string referenceWinmds)
{
var exportedTypes = FromFile(assemblyPath, referenceWinmds);
List<Type> activatableTypes = new List<Type>();
foreach(var type in exportedTypes)
{
var attributes = CustomAttributeData.GetCustomAttributes(type);
foreach(var attrib in attributes)
{
var attributeString = attrib.ToString();
if(attributeString.Contains("Windows.Foundation.Metadata.ComposableAttribute") ||
attributeString.Contains("Windows.Foundation.Metadata.ActivatableAttribute") ||
attributeString.Contains("Windows.Foundation.Metadata.StaticAttribute"))
{
activatableTypes.Add(type);
break;
}
}
}
return activatableTypes;
}
}
}
"@
function Get-ActivatableTypes
{
param(
[Parameter(Mandatory=$true, HelpMessage="Paths to the WinMD files whose types need to be added to the manifest.")]
[string]$WinmdPaths,
[Parameter(Mandatory=$true, HelpMessage="Semicolon-delimited list of reference WinMD files.")]
[string]$ReferenceWinmds
)
$winmdPathList = $WinmdPaths.Split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
[System.Collections.Generic.List[System.Type]]$typeList = @()
foreach ($winmdPath in $winmdPathList)
{
$typeList.AddRange([WinmdHelper.LoadWinmdTypes]::GetActivatableTypesFromFile($winmdPath, $ReferenceWinmds))
}
$typeList
}
function Get-WinmdTypes
{
param(
[Parameter(Mandatory=$true, HelpMessage="Paths to the WinMD files whose types need to be added to the manifest.")]
[string]$WinmdPaths,
[Parameter(Mandatory=$true, HelpMessage="Semicolon-delimited list of reference WinMD files.")]
[string]$ReferenceWinmds
)
$winmdPathList = $WinmdPaths.Split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
[System.Collections.Generic.List[System.Type]]$typeList = @()
foreach ($winmdPath in $winmdPathList)
{
$typeList.AddRange([WinmdHelper.LoadWinmdTypes]::FromFile($winmdPath, $ReferenceWinmds))
}
$typeList
}
function Get-WindowsNamespace
{
param(
[Parameter(Mandatory=$true)]
[string]$Namespace
)
throw "this is supposed to be unused in Project Reunion and microsoft.ui.xaml"
}
function Write-ErrorInFile
{
param(
[Parameter(Mandatory=$true)]
[string]$Message,
[Parameter(Mandatory=$true)]
[string]$FilePath,
[Parameter(Mandatory=$false)]
[int]$LineNumber = 0,
[Parameter(Mandatory=$false)]
[int]$ColumnNumber = 0
)
Write-Information "{ERROR:$message|$FilePath|$LineNumber|$ColumnNumber}"
}