-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLibraryManager.cs
More file actions
270 lines (228 loc) · 11.6 KB
/
LibraryManager.cs
File metadata and controls
270 lines (228 loc) · 11.6 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
//*****************************************************************************
//* Code Factory SDK
//* Copyright (c) 2021 CodeFactory, LLC
//*****************************************************************************
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using CodeFactory.Logging;
namespace CodeFactory.VisualStudio.Loader
{
public static class LibraryManager
{
/// <summary>
/// Logger for the class.
/// </summary>
private static readonly ILogger _logger = LogManager.GetLogger(typeof(LibraryManager));
private const string CODE_FACTORY = "CodeFactory";
private const string CODE_FACTORY_DOT_NET = "CodeFactory.DotNet";
private const string CODE_FACTORY_LOGGING = "CodeFactory.Logging";
private const string CODE_FACTORY_VISUAL_STUDIO = "CodeFactory.VisualStudio";
private const string CODE_FACTORY_FORMATTING_CSHARP = "CodeFactory.Formatting.CSharp";
/// <summary>
/// Loads the information about an automation library.
/// </summary>
/// <param name="filePath">The fully qualified path to the automation library file to load information from.</param>
/// <returns>Fully populated automation library information, or null if the library could not be loaded.</returns>
public static VsAutomationLibrary GetLibraryInformation(string filePath)
{
_logger.DebugEnter();
if (string.IsNullOrEmpty(filePath))
{
_logger.Information(
"No file path was provided for the library, no library information will be loaded.");
_logger.DebugExit();
return null;
}
var result = new VsAutomationLibrary();
try
{
if (!File.Exists(filePath))
{
_logger.Information(
$"The library file '{filePath}' was not found, no library information will be loaded.");
_logger.DebugExit();
return null;
}
var assemblyDirectory = Path.GetDirectoryName(filePath);
if (!Directory.Exists(assemblyDirectory))
{
_logger.Information(
$"The directory path could not be extracted from the library file path of '{filePath}', no library information will be loaded.");
_logger.DebugExit();
return null;
}
var libraryAssembly = Assembly.LoadFrom(filePath);
result.LibraryActions = libraryAssembly.GetLibraryActions();
result.SupportLibraries = libraryAssembly.GetExternalDependentLibraries(assemblyDirectory);
result.LibraryFilePath = 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 null if the assembly information can't be loaded.</returns>
public static List<VsActionConfiguration> GetLibraryActions(this Assembly source)
{
_logger.DebugEnter();
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;
}
var result = new List<VsActionConfiguration>();
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)))
{
//var constructor = publicType.GetConstructor(BindingFlags.Public,null,constructorArgTypes, null);
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 = null;
}
if (result == null) return null;
if (!result.Any()) result = null;
_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>();
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 (string.Compare(assemblyName, CODE_FACTORY, StringComparison.InvariantCulture) == 0) continue;
if (string.Compare(assemblyName, CODE_FACTORY_DOT_NET, StringComparison.InvariantCulture) == 0) continue;
if (string.Compare(assemblyName, CODE_FACTORY_LOGGING, StringComparison.InvariantCulture) == 0) continue;
if (string.Compare(assemblyName, CODE_FACTORY_VISUAL_STUDIO, StringComparison.InvariantCulture) == 0) continue;
if (string.Compare(assemblyName,CODE_FACTORY_FORMATTING_CSHARP,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;
externalAssemblies.Add(new VsLibraryConfiguration
{
AssemblyFilePath = referenceAssemblyInfo.Location,
AssemblyStrongName = referenceAssemblyInfo.FullName,
IsStoredInGac = referenceAssemblyInfo.GlobalAssemblyCache,
HasErrors = false,
ErrorType = LibraryErrorType.None,
ErrorDetails = null
});
}
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;
}
}
}