-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLibraryManagement.cs
More file actions
302 lines (261 loc) · 12.7 KB
/
LibraryManagement.cs
File metadata and controls
302 lines (261 loc) · 12.7 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//*****************************************************************************
//* Code Factory SDK
//* Copyright (c) 2021 CodeFactory, LLC
//*****************************************************************************
using CodeFactory.Logging;
using CodeFactory.VisualStudio.Loader;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace CodeFactory.VisualStudio.Packager
{
/// <summary>
/// Logic to handle discovery and information for libraries needed to package a CodeFactory automation library
/// </summary>
public static class LibraryManagement
{
/// <summary>
/// Logger for the class.
/// </summary>
private static readonly ILogger _logger = LogManager.GetLogger(typeof(LibraryManager));
/// <summary>
/// Loads the assembly data from the target CodeFactory library to be packaged.
/// </summary>
/// <param name="filePath">Fully qualified path to the assembly to be packaged.</param>
/// <returns>The target assembly data or null if the assembly could not be accessed.</returns>
public static Assembly GetAssemblyInformation(string filePath)
{
_logger.DebugEnter();
Assembly result = null;
if (string.IsNullOrEmpty(filePath))
{
_logger.Information(
"No file path was provided for the library, no library information will be loaded.");
_logger.DebugExit();
return null;
}
try
{
if (!File.Exists(filePath))
{
_logger.Information(
$"The library file '{filePath}' was not found, no library information will be loaded.");
_logger.DebugExit();
return null;
}
result = Assembly.LoadFrom(filePath);
}
catch (Exception getLibraryInformationError)
{
_logger.Error(
$"The following unhandled exception occurred while loading the library information for '{filePath}'",
getLibraryInformationError);
result = null;
}
_logger.DebugExit();
return result;
}
/// <summary>
/// Extension method that loads all library commands from the current assembly.
/// </summary>
/// <param name="source">Source assembly that holds the commands to be loaded.</param>
/// <returns>List of the library command information or an empty list is not commands are loaded.</returns>
public static List<VsActionConfiguration> GetLibraryActions(this Assembly source)
{
_logger.DebugEnter();
var result = new List<VsActionConfiguration>();
if (source == null)
{
_logger.Information(
"No target assembly was provided to load the library commands from. No commands will be returned.");
_logger.DebugExit();
return null;
}
try
{
var publicTypes = source.GetExportedTypes();
var vsActionType = typeof(IVsCommandInformation);
IVsActions commands = null;
ILogger logger = null;
object[] commandArgs = { logger, commands };
Type[] constructorArgTypes = { typeof(ILogger), typeof(IVsActions) };
foreach (var publicType in publicTypes.Where(publicType => publicType.IsClass).Where(publicType => publicType.InheritsInterface(vsActionType)))
{
if (!(Activator.CreateInstance(publicType, commandArgs) is IVsCommandInformation vsAction)) continue;
result.Add(new VsActionConfiguration { ActionAssemblyFullName = publicType.AssemblyQualifiedName, Title = vsAction.CommandTitle, VisualStudioActionType = vsAction.CommandType });
}
}
catch (Exception getLibraryActionsError)
{
_logger.Error(
$"An unhandled exception occurred while loading library commands for assembly '{source.FullName}'",
getLibraryActionsError);
result = new List<VsActionConfiguration>();
}
_logger.DebugExit();
return result;
}
/// <summary>
/// Static extension method information configuration information about external assemblies used by the source assembly.
/// </summary>
/// <param name="source">Source assembly to get external dependencies from.</param>
/// <param name="assemblyDirectory">The directory in which the external dependent libraries are stored at.</param>
/// <returns>List of dependent libraries or null if not dependent libraries were found. </returns>
public static List<VsLibraryConfiguration> GetExternalDependentLibraries(
this Assembly source, string assemblyDirectory)
{
_logger.DebugEnter();
if (source == null)
{
_logger.Information(
"The target assembly to load external dependencies from was not provided. No libraries will be loaded.");
_logger.DebugExit();
return null;
}
var referenceAssemblies = source.GetReferencedAssemblies();
var externalAssemblies = new List<VsLibraryConfiguration>();
if (referenceAssemblies == null)
{
_logger.DebugExit();
return externalAssemblies;
}
foreach (var referenceAssembly in referenceAssemblies)
{
try
{
if (referenceAssembly == null) continue;
//Validating that the SDK components are being pulled in.
var assemblyName = referenceAssembly.Name.Trim();
if (PackagerData.IgnoreAssemblies.Any(a =>
string.Compare(assemblyName, a, StringComparison.InvariantCulture) == 0)) continue;
var referenceAssemblyPath = $"{assemblyDirectory}\\{referenceAssembly.Name}.dll";
Assembly referenceAssemblyInfo = null;
referenceAssemblyInfo = File.Exists(referenceAssemblyPath)
? Assembly.ReflectionOnlyLoadFrom(referenceAssemblyPath)
: Assembly.ReflectionOnlyLoad(referenceAssembly.FullName);
if (referenceAssemblyInfo == null) continue;
//Checks to make sure the assembly if a CodeFactory assembly is supported.
SdkSupport.SupportedAssembly(referenceAssemblyInfo);
var assemblyFilePath = referenceAssemblyInfo.Location;
var assemblyStrongName = referenceAssemblyInfo.FullName;
var isStoredInGac = referenceAssemblyInfo.GlobalAssemblyCache;
if (isStoredInGac)
{
if (externalAssemblies.Where(e => e.IsStoredInGac).Any(e =>
string.Compare(e.AssemblyStrongName, assemblyStrongName,
StringComparison.InvariantCulture) == 0)) continue;
}
else
{
if (externalAssemblies.Where(e => !e.IsStoredInGac).Any(e =>
string.Compare(e.AssemblyFilePath, assemblyFilePath,
StringComparison.InvariantCulture) == 0)) continue;
}
externalAssemblies.Add(new VsLibraryConfiguration
{
AssemblyFilePath = !isStoredInGac ? assemblyFilePath : null,
AssemblyStrongName = isStoredInGac ? assemblyStrongName : null,
IsStoredInGac = isStoredInGac,
HasErrors = false,
ErrorType = LibraryErrorType.None,
ErrorDetails = null
});
if (!referenceAssemblyInfo.GlobalAssemblyCache)
{
var childAssemblies = referenceAssemblyInfo.GetExternalDependentLibraries(assemblyDirectory);
if (childAssemblies.Any())
{
foreach (var child in childAssemblies)
{
if (child.IsStoredInGac)
{
if (externalAssemblies.Where(e => e.IsStoredInGac).Any(e =>
string.Compare(e.AssemblyStrongName, child.AssemblyStrongName,
StringComparison.InvariantCulture) == 0)) continue;
}
else
{
if (externalAssemblies.Where(e => !e.IsStoredInGac).Any(e =>
string.Compare(e.AssemblyFilePath, child.AssemblyFilePath,
StringComparison.InvariantCulture) == 0)) continue;
}
externalAssemblies.Add(child);
}
}
}
}
catch (UnsupportedSdkLibraryException)
{
throw;
}
catch (BadImageFormatException badImageError)
{
_logger.Error("A bad image error occurred while trying to load a dependent assembly.",
badImageError);
if (referenceAssembly != null)
{
externalAssemblies.Add(new VsLibraryConfiguration
{
AssemblyFilePath = referenceAssembly.Name,
IsStoredInGac = false,
HasErrors = true,
ErrorType = LibraryErrorType.InvalidLibrary
});
}
}
catch (FileLoadException fileLoadError)
{
_logger.Error("A file load exception occurred while trying to load a dependent assembly.",
fileLoadError);
if (referenceAssembly != null)
{
externalAssemblies.Add(new VsLibraryConfiguration
{
AssemblyFilePath = referenceAssembly.Name,
IsStoredInGac = false,
HasErrors = true,
ErrorType = LibraryErrorType.AssemblyLoadError
});
}
}
catch (FileNotFoundException fileNotFoundError)
{
_logger.Error("An assembly was not found when trying to load a dependent assembly.",
fileNotFoundError);
if (referenceAssembly != null)
{
externalAssemblies.Add(new VsLibraryConfiguration
{
AssemblyFilePath = referenceAssembly.Name,
IsStoredInGac = false,
HasErrors = true,
ErrorType = LibraryErrorType.FileNotFound
});
}
}
catch (Exception referenceAssemblyLoadError)
{
_logger.Error(
"The following unhandled exception occurred while trying to load a dependent assembly.",
referenceAssemblyLoadError);
if (referenceAssembly != null)
{
externalAssemblies.Add(new VsLibraryConfiguration
{
AssemblyFilePath = referenceAssembly.Name,
IsStoredInGac = false,
HasErrors = true,
ErrorType = LibraryErrorType.LoadException,
ErrorDetails = referenceAssemblyLoadError.ToString()
});
}
}
}
if (!externalAssemblies.Any()) externalAssemblies = null;
_logger.DebugExit();
return externalAssemblies;
}
}
}