-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssemblyDependencyGraph.cs
524 lines (430 loc) · 20.1 KB
/
AssemblyDependencyGraph.cs
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.Extensions.FileSystemGlobbing.Abstractions;
using Serilog;
namespace BindingRedirectR
{
internal class AssemblyDependencyGraph
{
#region Public members
public IList<AssemblyDependencyNode> EnsureNodeWithAssemblySourceOrPatterns(IList<string> assemblySources, DirectoryInfo baseDirectory)
{
var allNodes = new List<AssemblyDependencyNode>();
foreach (var assemblySource in assemblySources)
{
var nodes = EnsureNodeWithAssemblySourceOrPattern(assemblySource, baseDirectory);
allNodes.AddRange(nodes);
}
allNodes = allNodes.Distinct().ToList();
return allNodes;
}
public AssemblyDependencyNode EnsureNodeWithAssemblySource(string assemblySource, DirectoryInfo baseDirectory)
{
if (IsValidAssemblyString(assemblySource, out var name))
return EnsureNodeWithName(name);
if (IsValidPath(assemblySource, baseDirectory, out var fileInfo))
return EnsureNodeWithFile(fileInfo);
throw new ArgumentException($"Assembly source '{assemblySource}' is not valid, it's neither a path, nor an assembly string.", nameof(assemblySource));
}
public void RegisterDependency(AssemblyDependencyNode dependant, AssemblyDependencyNode dependency)
{
_dependencyByNode.AddOrUpdate(dependant, _ => new HashSet<AssemblyDependencyNode> { dependency }, (_, set) =>
{
set.Add(dependency);
return set;
});
_dependantByNode.AddOrUpdate(dependency, _ => new HashSet<AssemblyDependencyNode> { dependant }, (_, set) =>
{
set.Add(dependant);
return set;
});
}
public IEnumerable<AssemblyDependencyNode> GetNodesToLoadFromFile()
=> new NodesToLoadEnumerable(() => _nodes, x => x.LoadedFromFile == AssemblyLoadStatus.NotAttempted && !x.Loaded && x.File != null);
public IEnumerable<AssemblyDependencyNode> GetNodesToLoadFromName()
=> new NodesToLoadEnumerable(() => _nodes, x => x.LoadedFromName == AssemblyLoadStatus.NotAttempted && !x.Loaded && x.Name != null);
public void LoadNodeFromFile(AssemblyDependencyNode node)
{
if (node.File == null)
throw new InvalidOperationException("Cannot load assembly from file, the file is empty.");
switch (node.LoadedFromFile)
{
case AssemblyLoadStatus.Loaded:
throw new InvalidOperationException("Cannot load assembly from file, it's already loaded.");
case AssemblyLoadStatus.Failed:
throw new InvalidOperationException("Cannot load assembly from file, previous attempt failed.");
}
if (node.Loaded)
throw new InvalidOperationException("Cannot load assembly from file, it's already been loaded.");
var fileFullName = node.File.FullName;
try
{
Logger.Debug("Loading from file {File}.", fileFullName);
var assembly = AssemblyMetadataLoader.LoadFrom(fileFullName);
node.MarkAsLoadedFromFile(assembly);
ProcessLoadedNode(node);
}
catch (Exception ex)
{
Logger.Debug(ex, "Failed to load from file {File}.", fileFullName);
node.MarkAsFailedFromFile(ex);
}
}
public void LoadNodeFromName(AssemblyDependencyNode node)
{
if (node.Name == null)
throw new InvalidOperationException("Cannot load assembly from name, the name is empty.");
switch (node.LoadedFromName)
{
case AssemblyLoadStatus.Loaded:
throw new InvalidOperationException("Cannot load assembly from name, it's already loaded.");
case AssemblyLoadStatus.Failed:
throw new InvalidOperationException("Cannot load assembly from name, previous attempt failed.");
}
if (node.Loaded)
throw new InvalidOperationException("Cannot load assembly from name, it's already been loaded.");
var assemblyString = node.Name.ToString();
try
{
Logger.Debug("Loading from name {AssemblyName}.", assemblyString);
var assembly = AssemblyMetadataLoader.Load(node.Name);
if (assembly.AssemblyName != node.Name.FullName)
{
Logger.Warning("Requesting the load of [{RequestedAssemblyName}], but obtained [{LoadedAssemblyName}].", node.Name.FullName, assembly.AssemblyName);
}
node.MarkAsLoadedFromName(assembly);
ProcessLoadedNode(node);
}
catch (Exception ex)
{
Logger.Debug(ex, "Failed to load from name {AssemblyName}.", assemblyString);
node.MarkAsFailedFromName(ex);
}
}
public IEnumerable<AssemblyDependencyNode> GetDirectDependants(AssemblyDependencyNode dependency)
=> _dependantByNode.TryGetValue(dependency, out var result)
? result
: Enumerable.Empty<AssemblyDependencyNode>();
public IEnumerable<AssemblyDependencyNode> GetDirectDependantsByGroup(AssemblyUnversionedIdentity dependencyGroup)
=> _nodes
.Where(x => x.Identity.Unversioned == dependencyGroup)
.SelectMany(GetDirectDependants)
.Distinct();
public IEnumerable<AssemblyDependencyNode> GetDirectDependencies(AssemblyDependencyNode dependant)
=> _dependencyByNode.TryGetValue(dependant, out var result)
? result
: Enumerable.Empty<AssemblyDependencyNode>();
public IEnumerable<AssemblyDependencyNode> GetIndirectDependants(AssemblyDependencyNode dependency)
{
var results = new HashSet<AssemblyDependencyNode>(GetAllDependants(dependency));
// keep only indirect
results.ExceptWith(GetDirectDependants(dependency));
return results;
}
public IEnumerable<AssemblyDependencyNode> GetIndirectDependencies(AssemblyDependencyNode dependant)
{
var results = new HashSet<AssemblyDependencyNode>(GetAllDependencies(dependant));
// keep only indirect
results.ExceptWith(GetDirectDependencies(dependant));
return results;
}
public IEnumerable<AssemblyDependencyNode> GetAllDependants(AssemblyDependencyNode dependency)
{
var results = new HashSet<AssemblyDependencyNode>();
var directDependants = GetDirectDependants(dependency).ToList();
// collect both direct and indirect
var nodesToProcess = new HashSet<AssemblyDependencyNode>(directDependants);
var processedNodes = new HashSet<AssemblyDependencyNode>();
while (nodesToProcess.Any())
{
var node = nodesToProcess.First();
if (!processedNodes.Contains(node))
{
results.Add(node);
foreach (var dependant in GetDirectDependants(node))
{
nodesToProcess.Add(dependant);
}
processedNodes.Add(node);
}
nodesToProcess.Remove(node);
}
return results;
}
public IEnumerable<AssemblyDependencyNode> GetAllDependencies(AssemblyDependencyNode dependant)
{
var results = new HashSet<AssemblyDependencyNode>();
var directDependencies = GetDirectDependencies(dependant).ToList();
// collect both direct and indirect
var nodesToProcess = new HashSet<AssemblyDependencyNode>(directDependencies);
var processedNodes = new HashSet<AssemblyDependencyNode>();
while (nodesToProcess.Any())
{
var node = nodesToProcess.First();
if (!processedNodes.Contains(node))
{
results.Add(node);
foreach (var dependency in GetDirectDependencies(node))
{
nodesToProcess.Add(dependency);
}
processedNodes.Add(node);
}
nodesToProcess.Remove(node);
}
return results;
}
public IList<AssemblyDependencyNode> GetAllDependenciesIncludingEntireGroup(AssemblyDependencyNode dependant)
{
var allDependencies = GetAllDependencies(dependant);
var allDependencyGroups = allDependencies.Select(x => x.Identity.Unversioned).ToHashSet();
var allNodesByGroups = GetAllNodes().Where(x => allDependencyGroups.Contains(x.Identity.Unversioned)).ToList();
return allNodesByGroups;
}
public IEnumerable<AssemblyDependencyNode> GetAllNodes() => _nodes;
#endregion
#region Private members
private static readonly ILogger Logger = Log.ForContext<Program>();
private readonly HashSet<AssemblyDependencyNode> _nodes = new HashSet<AssemblyDependencyNode>();
private readonly ConcurrentDictionary<string, AssemblyDependencyNode> _nodeByName = new ConcurrentDictionary<string, AssemblyDependencyNode>(StringComparer.OrdinalIgnoreCase);
private readonly ConcurrentDictionary<string, AssemblyDependencyNode> _nodeByFile = new ConcurrentDictionary<string, AssemblyDependencyNode>(StringComparer.OrdinalIgnoreCase);
private readonly ConcurrentDictionary<AssemblyDependencyNode, HashSet<AssemblyDependencyNode>> _dependencyByNode = new ConcurrentDictionary<AssemblyDependencyNode, HashSet<AssemblyDependencyNode>>();
private readonly ConcurrentDictionary<AssemblyDependencyNode, HashSet<AssemblyDependencyNode>> _dependantByNode = new ConcurrentDictionary<AssemblyDependencyNode, HashSet<AssemblyDependencyNode>>();
private IList<AssemblyDependencyNode> EnsureNodeWithAssemblySourceOrPattern(string assemblySource, DirectoryInfo baseDirectory)
{
if (IsValidAssemblyString(assemblySource, out var name))
return new[] { EnsureNodeWithName(name) };
if (IsValidGlobPattern(assemblySource, baseDirectory, out var fileInfos))
{
var results = fileInfos.Select(EnsureNodeWithFile).Distinct().ToList();
return results;
}
if (IsValidPath(assemblySource, baseDirectory, out var fileInfo))
return new[] { EnsureNodeWithFile(fileInfo) };
throw new ArgumentException($"Assembly source '{assemblySource}' is not valid, it's neither a path, nor an assembly string, nor a file globbing pattern.", nameof(assemblySource));
}
private AssemblyDependencyNode EnsureNodeWithName(AssemblyName name)
{
var assemblyFullName = GetKeyFromName(name);
var node = _nodeByName.GetOrAdd(assemblyFullName, _ => AssemblyDependencyNode.CreateFromName(name));
_nodes.Add(node);
return node;
}
private AssemblyDependencyNode EnsureNodeWithFile(FileInfo file)
{
var fullPath = GetKeyFromFile(file);
var node = _nodeByFile.GetOrAdd(fullPath, _ => AssemblyDependencyNode.CreateFromFile(file));
_nodes.Add(node);
return node;
}
private static string GetKeyFromName(AssemblyName name) => name.FullName;
private static string GetKeyFromFile(FileInfo file) => Path.GetFullPath(file.FullName);
private static bool IsValidAssemblyString(string assemblyString, out AssemblyName name)
{
try
{
var regex = new Regex(".*, Version=.*, Culture=.*, PublicKeyToken=.*", RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (!regex.IsMatch(assemblyString))
{
name = null;
return false;
}
name = new AssemblyName(assemblyString);
return true;
}
catch (Exception)
{
name = null;
return false;
}
}
private static bool IsValidPath(string path, DirectoryInfo baseDirectory, out FileInfo fileInfo)
{
try
{
var root = Path.GetPathRoot(path);
var isPathFullyQualified = root?.StartsWith(@"\\") == true || root?.EndsWith(@"\") == true;
if (isPathFullyQualified)
{
fileInfo = new FileInfo(path);
return true;
}
var fullyQualifiedPath = Path.Combine(baseDirectory.FullName, path ?? string.Empty);
fileInfo = new FileInfo(fullyQualifiedPath);
return true;
}
catch (Exception)
{
fileInfo = null;
return false;
}
}
private static bool IsValidGlobPattern(string globPattern, DirectoryInfo baseDirectory, out IList<FileInfo> files)
{
files = null;
if (!globPattern.Contains('*'))
return false;
PatternMatchingResult patternMatchingResult;
try
{
var matcher = new Matcher(StringComparison.InvariantCultureIgnoreCase);
matcher.AddInclude(globPattern);
patternMatchingResult = matcher.Execute(new DirectoryInfoWrapper(baseDirectory));
}
catch
{
return false;
}
files = new List<FileInfo>();
if (!patternMatchingResult.HasMatches)
return true;
foreach (var match in patternMatchingResult.Files)
{
if (IsValidPath(match.Path, baseDirectory, out var fileInfo))
{
files.Add(fileInfo);
}
}
return true;
}
private void ProcessLoadedNode(AssemblyDependencyNode node)
{
// process name
var nameKey = GetKeyFromName(node.Name);
AssemblyDependencyNode previousNodeByName = null;
_nodeByName.AddOrUpdate(nameKey, _ => node, (_, previousNode) =>
{
previousNodeByName = previousNode;
return node;
});
if (previousNodeByName != null && previousNodeByName != node)
{
MergeNodeDependencies(node, previousNodeByName);
}
// process file
var fileKey = GetKeyFromFile(node.File);
AssemblyDependencyNode previousNodeByFile = null;
_nodeByFile.AddOrUpdate(fileKey, _ => node, (_, previousNode) =>
{
previousNodeByFile = previousNode;
return node;
});
if (previousNodeByFile != null && previousNodeByFile != node)
{
if (previousNodeByFile.Identity != node.Identity)
{
Logger.Warning("There will be multiple nodes with the same file [{File}].", node.File.FullName);
}
else
{
MergeNodeDependencies(node, previousNodeByFile);
}
}
// process dependencies
RegisterNodeDependencies(node);
}
private void RegisterNodeDependencies(AssemblyDependencyNode dependant)
{
foreach (var dependencyName in dependant.Assembly.ReferenceAssemblyNames)
{
var dependency = EnsureNodeWithName(new AssemblyName(dependencyName));
RegisterDependency(dependant, dependency);
}
}
private void MergeNodeDependencies(AssemblyDependencyNode targetNode, AssemblyDependencyNode sourceNode)
{
if (targetNode == sourceNode)
throw new InvalidOperationException("Cannot merge nodes that are the same.");
// find occurences of source node in internal dictionaries and redirect references to target node
_nodes.Remove(sourceNode);
if (sourceNode.Name != null)
{
var nameKey = GetKeyFromName(sourceNode.Name);
if (_nodeByName.TryRemove(nameKey, out _))
{
_nodeByName[nameKey] = targetNode;
}
}
if (sourceNode.File != null)
{
var fileKey = GetKeyFromFile(sourceNode.File);
if (_nodeByFile.TryRemove(fileKey, out _))
{
_nodeByFile[fileKey] = targetNode;
}
}
if (_dependencyByNode.TryRemove(sourceNode, out var sourceNodeDependencies))
{
foreach (var dependency in sourceNodeDependencies)
{
_dependantByNode[dependency].Remove(sourceNode);
RegisterDependency(targetNode, dependency);
}
}
if (_dependantByNode.TryRemove(sourceNode, out var sourceNodeDependants))
{
foreach (var dependant in sourceNodeDependants)
{
_dependencyByNode[dependant].Remove(sourceNode);
RegisterDependency(dependant, targetNode);
}
}
}
#endregion
#region Nested types
private class NodesToLoadEnumerable : IEnumerable<AssemblyDependencyNode>
{
private readonly Func<HashSet<AssemblyDependencyNode>> _nodesAccessor;
private readonly Func<AssemblyDependencyNode, bool> _filter;
internal NodesToLoadEnumerable(Func<HashSet<AssemblyDependencyNode>> nodesAccessor, Func<AssemblyDependencyNode, bool> filter)
{
_nodesAccessor = nodesAccessor;
_filter = filter;
}
public IEnumerator<AssemblyDependencyNode> GetEnumerator() => new NodesToLoadEnumerator(_nodesAccessor, _filter);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
private class NodesToLoadEnumerator : IEnumerator<AssemblyDependencyNode>
{
private readonly Func<HashSet<AssemblyDependencyNode>> _nodesAccessor;
private readonly Func<AssemblyDependencyNode, bool> _filter;
private HashSet<AssemblyDependencyNode> _processedNodes = new HashSet<AssemblyDependencyNode>();
public NodesToLoadEnumerator(Func<HashSet<AssemblyDependencyNode>> nodesAccessor, Func<AssemblyDependencyNode, bool> filter)
{
_nodesAccessor = nodesAccessor;
_filter = filter;
}
public void Dispose()
{
}
public bool MoveNext()
{
var nodesToProcess = new HashSet<AssemblyDependencyNode>(_nodesAccessor());
nodesToProcess.ExceptWith(_processedNodes);
foreach (var node in nodesToProcess)
{
_processedNodes.Add(node);
if (!_filter(node))
continue;
Current = node;
return true;
}
return false;
}
public void Reset()
{
_processedNodes = new HashSet<AssemblyDependencyNode>();
}
public AssemblyDependencyNode Current { get; private set; }
object IEnumerator.Current => Current;
}
#endregion
}
}