-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCodeFactoryConfigurationLoader.cs
More file actions
449 lines (364 loc) · 16.9 KB
/
CodeFactoryConfigurationLoader.cs
File metadata and controls
449 lines (364 loc) · 16.9 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//*****************************************************************************
//* 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
{
/// <summary>
/// Utility class that loads a CFX file libraries into memory and creates instances of all the visual studio actions and makes them available to visual studio for usage.
/// </summary>
public static class CodeFactoryConfigurationLoader
{
//Logger for the class.
private static readonly ILogger _logger = LogManager.GetLogger(typeof(CodeFactoryConfigurationLoader));
/// <summary>
/// Constant that holds the name of the code factory directory.
/// </summary>
public const string CodeFactoryDirectoryName = "CodeFactory";
/// <summary>
/// Constant that holds the name of the code factory file extension.
/// </summary>
public const string CodeFactoryExtension = ".cfx";
/// <summary>
/// Helper method that determines if a code factory extension file is located in the directory. This will return the first CFX file that is located in the directory.
/// </summary>
/// <param name="sourceDirectory">The directory to search for the code factory extension file.</param>
/// <returns>The fully qualified path to the code factory extension file, or null if no file was found. </returns>
public static string LocateFactoryPackage(string sourceDirectory)
{
_logger.DebugEnter();
if (string.IsNullOrEmpty(sourceDirectory))
{
_logger.DebugExit();
return null;
}
string result = null;
try
{
var directoryData = new DirectoryInfo(sourceDirectory);
if (!directoryData.Exists)
{
_logger.DebugExit();
return null;
}
var files = directoryData.GetFiles("*.cfx");
var fileData = files.FirstOrDefault();
if (fileData != null) result = fileData.FullName;
}
catch (Exception locateFactoryPackageError)
{
_logger.Error("Exception occurred while locating the factory package. See exception for details.",
locateFactoryPackageError);
result = null;
}
_logger.DebugExit();
return result;
}
/// <summary>
/// Helper method that gets the list of all the assembly files that need to copied from the code factory package.
/// </summary>
/// <param name="packageDirectory">The target directory all files are to be copied to.</param>
/// <param name="config">The factory configuration to load the files from.</param>
/// <returns>List of the assembly and support files to load, or null if a load failure error occurred.</returns>
public static List<string> GetAssembliesToLoadFromPackage(string packageDirectory, VsFactoryConfiguration config)
{
_logger.DebugEnter();
if (string.IsNullOrEmpty(packageDirectory) | config == null)
{
_logger.DebugExit();
return null;
}
var result = new List<string>();
var libraries = new List<VsLibraryConfiguration>();
try
{
if (config.SupportLibraries != null) libraries.AddRange(config.SupportLibraries);
if (config.CodeFactoryLibraries != null) libraries.AddRange(config.CodeFactoryLibraries);
if (!libraries.Any()) return result;
foreach (var library in libraries)
{
if (library == null) continue;
if (library.IsStoredInGac) continue;
var libraryPath = Path.Combine(packageDirectory, library.AssemblyFilePath.Trim());
if (!File.Exists(libraryPath)) result.Add(libraryPath);
if (!library.HasDebugInformation) continue;
var debugPath = Path.Combine(packageDirectory,$"{Path.GetFileNameWithoutExtension(library.AssemblyFilePath.Trim())}.pdb");
if (!File.Exists(debugPath)) result.Add(debugPath);
}
}
catch (Exception getAssembliesToLoadFromPackageError)
{
_logger.Error("Unhandled error occurred check exception for details.",
getAssembliesToLoadFromPackageError);
result = null;
}
_logger.DebugExit();
return result;
}
/// <summary>
/// Loads the factory libraries into the running visual studio instance.
/// </summary>
/// <param name="config">The code factory configuration to load assemblies from.</param>
/// <param name="packageDirectory">The directory where the packages are found.</param>
/// <returns>Data object that holds the status of loaded factory libraries. This will always return an instance of the status.</returns>
public static LibraryLoadStatus LoadFactoryLibraries(VsFactoryConfiguration config, string packageDirectory)
{
_logger.DebugEnter();
var result = new LibraryLoadStatus();
if (config == null)
{
result.HasErrors = true;
result.Errors.Add(ConfigurationMessages.LibraryNotLoaded);
_logger.DebugExit();
return result;
}
if (string.IsNullOrEmpty(packageDirectory))
{
result.HasErrors = true;
result.Errors.Add(ConfigurationMessages.LibraryNotLoaded);
_logger.DebugExit();
return result;
}
try
{
if (!Directory.Exists(packageDirectory))
{
result.HasErrors = true;
result.Errors.Add(ConfigurationMessages.LibraryNotLoaded);
_logger.DebugExit();
return result;
}
var libraries = config.CodeFactoryLibraries;
if (libraries == null)
{
result.HasErrors = true;
result.Errors.Add(ConfigurationMessages.LibraryNotLoaded);
_logger.DebugExit();
return result;
}
foreach (var libraryConfiguration in libraries)
{
if (libraryConfiguration == null) continue;
Assembly loadedLibrary = null;
if (libraryConfiguration.IsStoredInGac)
{
loadedLibrary = Assembly.Load(libraryConfiguration.AssemblyStrongName);
}
else
{
var assemblyPath = Path.Combine(packageDirectory, libraryConfiguration.AssemblyFilePath.Trim());
loadedLibrary = Assembly.LoadFrom(assemblyPath);
}
if (loadedLibrary != null)
{
SdkSupport.SupportedAssembly(loadedLibrary);
continue;
}
result.HasErrors = true;
result.Errors.Add(ConfigurationMessages.LibraryNotLoaded);
_logger.DebugExit();
return result;
}
}
catch (UnsupportedSdkLibraryException)
{
//Throwing to the caller to notify which library could not be loaded due to SDK being out of date.
throw;
}
catch (Exception loadFactoryLibrariesError)
{
_logger.Error("Unhandled exception occurred see exception for details.", loadFactoryLibrariesError);
result.HasErrors = true;
result.Errors.Add(ConfigurationMessages.LibraryNotLoaded);
}
_logger.DebugExit();
return result;
}
/// <summary>
/// Loads a code factory configuration into memory and returns the target visual studio actions to be used by code factory.
/// </summary>
/// <param name="codeFactoryPackageFile">The fully qualified path to the code factory package file.</param>
/// <param name="unpackDirectory">The fully qualified path to the unpackDirectory for the code factory libraries.</param>
/// <param name="actions">The implementation of the actions interface to be injected into each created visual studio action.</param>
/// <returns></returns>
public static IVsCodeFactoryLoadStatus LoadCodeFactoryConfiguration(string codeFactoryPackageFile, string unpackDirectory, IVsActions actions)
{
_logger.DebugEnter();
var result = new VsCodeFactoryLoadStatus
{
IsLoaded = false,
VisualStudioFactoryActions = new List<IVsCommandInformation>()
};
if (string.IsNullOrEmpty(codeFactoryPackageFile))
{
result.HasErrors = true;
result.ErrorMessages.Add(ConfigurationMessages.NoPackageFile);
_logger.DebugExit();
return result;
}
if (string.IsNullOrEmpty(unpackDirectory))
{
result.HasErrors = true;
result.ErrorMessages.Add(ConfigurationMessages.NoPackageDirectory);
_logger.DebugExit();
return result;
}
if (actions == null)
{
result.HasErrors = true;
result.ErrorMessages.Add(ConfigurationMessages.NoVisualStudioActions);
}
if (!File.Exists(codeFactoryPackageFile))
{
result.HasErrors = true;
result.ErrorMessages.Add(string.Format(ConfigurationMessages.PackageFileNotFound,codeFactoryPackageFile));
_logger.DebugExit();
return result;
}
if (!Directory.Exists(unpackDirectory))
{
result.HasErrors = true;
result.ErrorMessages.Add(string.Format(ConfigurationMessages.CannotLocatePackageDirectory,unpackDirectory));
_logger.DebugExit();
return result;
}
try
{
var packageResult = ConfigManager.LoadFactoryConfiguration(codeFactoryPackageFile);
if (packageResult.HasError)
{
result.HasErrors = true;
result.ErrorMessages.Add(packageResult.Error);
_logger.DebugExit();
return result;
}
var packageConfig = packageResult.Result;
if (packageConfig == null)
{
result.HasErrors = true;
result.ErrorMessages.Add(ConfigurationMessages.PackageLoadError);
_logger.DebugExit();
return result;
}
var factoryActions = packageConfig.CodeFactoryActions;
if (factoryActions == null)
{
result.HasErrors = false;
result.IsLoaded = true;
_logger.DebugExit();
return result;
}
var packageDirectory = Path.Combine(unpackDirectory, CodeFactoryDirectoryName,
packageConfig.Id.ToString());
bool hasPackageDirectory = Directory.Exists(packageDirectory);
if (!hasPackageDirectory)
{
var packageDirectoryInfo = Directory.CreateDirectory(packageDirectory);
if (!packageDirectoryInfo.Exists)
{
result.HasErrors = true;
result.ErrorMessages.Add(string.Format(ConfigurationMessages.PackageDirectoryCannotBeCreated,
Path.GetFileName(codeFactoryPackageFile)));
_logger.DebugExit();
return result;
}
}
var assembliesToExtract = GetAssembliesToLoadFromPackage(packageDirectory, packageConfig);
if (assembliesToExtract == null)
{
result.HasErrors = true;
result.ErrorMessages.Add(ConfigurationMessages.PackageLoadError);
_logger.DebugExit();
return result;
}
if (assembliesToExtract.Any())
{
var assemblyExtractResults = ConfigManager.ExtractLibrariesFromPackage(codeFactoryPackageFile,
packageDirectory,
assembliesToExtract);
if (assemblyExtractResults.HasErrors)
{
result.HasErrors = true;
result.ErrorMessages.AddRange(assemblyExtractResults.Errors);
_logger.DebugExit();
return result;
}
}
var assemblyLoadResults = LoadFactoryLibraries(packageConfig, packageDirectory);
if (assemblyLoadResults.HasErrors)
{
result.HasErrors = true;
result.ErrorMessages.AddRange(assemblyLoadResults.Errors);
_logger.DebugExit();
return result;
}
var loadedActions = new List<IVsCommandInformation>();
foreach (var factoryAction in factoryActions)
{
var assemblyPath = factoryAction.ActionAssemblyFullName;
if (string.IsNullOrEmpty(assemblyPath))
{
result.HasErrors = true;
result.ErrorMessages.Add(string.Format(ConfigurationMessages.InvalidAssemblyPath,
factoryAction.Title));
continue;
}
try
{
var actionType = Type.GetType(assemblyPath, false);
if (actionType == null)
{
result.HasErrors = true;
result.ErrorMessages.Add(string.Format(ConfigurationMessages.InvalidAssemblyPath,
factoryAction.Title));
continue;
}
ILogger logger = LogManager.GetLogger(actionType);
object[] constructorArgs = {logger,actions};
var action =
Activator.CreateInstance(actionType, constructorArgs) as IVsCommandInformation;
if (action == null)
{
result.HasErrors = true;
result.ErrorMessages.Add(string.Format(ConfigurationMessages.FailedToLoadAction,
factoryAction.Title));
continue;
}
loadedActions.Add(action);
}
catch (Exception actionFailedToLoad)
{
_logger.Error("Unhandled error occurred see exception for details.", actionFailedToLoad);
result.HasErrors = true;
result.ErrorMessages.Add(string.Format(ConfigurationMessages.FailedToLoadAction,
factoryAction.Title));
continue;
}
}
if (loadedActions.Any())
{
result.IsLoaded = true;
result.VisualStudioFactoryActions = loadedActions;
}
}
catch (UnsupportedSdkLibraryException)
{
//Throwing to the caller to notify which library could not be loaded due to SDK being out of date.
throw;
}
catch (Exception loadPackageError)
{
_logger.Error("An unhandled exception has occurred see the exception for details", loadPackageError);
result.HasErrors = true;
result.ErrorMessages.Add(ConfigurationMessages.PackageLoadError);
}
return result;
}
}
}