forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssemblyResolver.cs
More file actions
80 lines (70 loc) · 3.14 KB
/
AssemblyResolver.cs
File metadata and controls
80 lines (70 loc) · 3.14 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ScriptCs.Contracts;
namespace ScriptCs
{
public class AssemblyResolver : IAssemblyResolver
{
private readonly Dictionary<string, List<string>> _assemblyPathCache = new Dictionary<string, List<string>>();
private readonly IFileSystem _fileSystem;
private readonly IPackageAssemblyResolver _packageAssemblyResolver;
private readonly IAssemblyUtility _assemblyUtility;
private readonly ILog _logger;
public AssemblyResolver(
IFileSystem fileSystem,
IPackageAssemblyResolver packageAssemblyResolver,
IAssemblyUtility assemblyUtility,
ILogProvider logProvider)
{
Guard.AgainstNullArgument("fileSystem", fileSystem);
Guard.AgainstNullArgumentProperty("fileSystem", "PackagesFolder", fileSystem.PackagesFolder);
Guard.AgainstNullArgumentProperty("fileSystem", "BinFolder", fileSystem.BinFolder);
Guard.AgainstNullArgument("packageAssemblyResolver", packageAssemblyResolver);
Guard.AgainstNullArgument("assemblyUtility", assemblyUtility);
Guard.AgainstNullArgument("logProvider", logProvider);
_fileSystem = fileSystem;
_packageAssemblyResolver = packageAssemblyResolver;
_assemblyUtility = assemblyUtility;
_logger = logProvider.ForCurrentType();
}
public IEnumerable<string> GetAssemblyPaths(string path, bool binariesOnly = false)
{
Guard.AgainstNullArgument("path", path);
List<string> assemblies;
if (!_assemblyPathCache.TryGetValue(path, out assemblies))
{
assemblies = GetPackageAssemblyNames(path).Union(GetBinAssemblyPaths(path)).ToList();
_assemblyPathCache.Add(path, assemblies);
}
return binariesOnly
? assemblies.Where(m =>
m.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase) ||
m.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase))
: assemblies.ToArray();
}
private IEnumerable<string> GetBinAssemblyPaths(string path)
{
var binFolder = Path.Combine(path, _fileSystem.BinFolder);
if (!_fileSystem.DirectoryExists(binFolder))
{
yield break;
}
foreach (var assembly in _fileSystem.EnumerateBinaries(binFolder, SearchOption.TopDirectoryOnly)
.Where(f => _assemblyUtility.IsManagedAssembly(f)))
{
_logger.DebugFormat("Found assembly in bin folder: {0}", Path.GetFileName(assembly));
yield return assembly;
}
}
private IEnumerable<string> GetPackageAssemblyNames(string path)
{
foreach (var assembly in _packageAssemblyResolver.GetAssemblyNames(path))
{
_logger.DebugFormat("Found package assembly: {0}", Path.GetFileName(assembly));
yield return assembly;
}
}
}
}