forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackageAssemblyResolver.cs
More file actions
137 lines (111 loc) · 5.07 KB
/
PackageAssemblyResolver.cs
File metadata and controls
137 lines (111 loc) · 5.07 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Common.Logging;
using ScriptCs.Contracts;
using ScriptCs.Exceptions;
using ScriptCs.Package;
namespace ScriptCs
{
public class PackageAssemblyResolver : IPackageAssemblyResolver
{
private readonly IFileSystem _fileSystem;
private readonly IPackageContainer _packageContainer;
private readonly ILog _logger;
private List<IPackageReference> _topLevelPackages;
public PackageAssemblyResolver(IFileSystem fileSystem, IPackageContainer packageContainer, ILog logger)
{
_fileSystem = fileSystem;
_packageContainer = packageContainer;
_logger = logger;
}
public void SavePackages()
{
var packagesFile = Path.Combine(_fileSystem.CurrentDirectory, Constants.PackagesFile);
var packagesFolder = Path.Combine(_fileSystem.CurrentDirectory, Constants.PackagesFolder);
if (_fileSystem.FileExists(packagesFile))
{
_logger.Info("Packages.config already exists!");
return;
}
if (!_fileSystem.DirectoryExists(packagesFolder))
{
_logger.Info("Packages directory does not exist!");
return;
}
var result = _packageContainer.CreatePackageFile().ToList();
if (!result.Any())
{
_logger.Info("No packages found!");
return;
}
result.ForEach(i => _logger.Info(string.Format("Added {0}", i)));
_logger.Info("Packages.config successfully created!");
}
public IEnumerable<IPackageReference> GetPackages(string workingDirectory)
{
var packageFile = Path.Combine(workingDirectory, Constants.PackagesFile);
var packages = _packageContainer.FindReferences(packageFile).ToList();
_topLevelPackages = packages;
return packages;
}
public IEnumerable<string> GetAssemblyNames(string workingDirectory)
{
var packages = GetPackages(workingDirectory).ToList();
if (!packages.Any())
{
return Enumerable.Empty<string>();
}
var packageFile = Path.Combine(workingDirectory, Constants.PackagesFile);
var packageDir = Path.Combine(workingDirectory, Constants.PackagesFolder);
var foundAssemblyPaths = new List<string>();
var missingAssemblies = new List<IPackageReference>();
LoadFiles(packageDir, packages, missingAssemblies, foundAssemblyPaths, _fileSystem.FileExists(packageFile));
if (missingAssemblies.Count > 0)
{
var missingAssembliesString = string.Join(",", missingAssemblies.Select(i => i.PackageId + " " + i.FrameworkName.FullName));
throw new MissingAssemblyException(string.Format("Missing: {0}", missingAssembliesString));
}
return foundAssemblyPaths;
}
private void LoadFiles(string packageDir, IEnumerable<IPackageReference> packageReferences, List<IPackageReference> missingAssemblies, List<string> foundAssemblies, bool strictLoad = true)
{
foreach (var packageRef in packageReferences)
{
var nugetPackage = _packageContainer.FindPackage(packageDir, packageRef);
if (nugetPackage == null)
{
missingAssemblies.Add(packageRef);
_logger.Info("Cannot find: " + packageRef.PackageId + " " + packageRef.Version);
continue;
}
var compatibleFiles = nugetPackage.GetCompatibleDlls(packageRef.FrameworkName);
if (compatibleFiles == null)
{
missingAssemblies.Add(packageRef);
_logger.Info("Cannot find binaries for " + packageRef.FrameworkName + " in: " + packageRef.PackageId + " " + packageRef.Version);
continue;
}
var compatibleFilePaths = compatibleFiles.Select(packageFile => Path.Combine(packageDir, nugetPackage.FullName, packageFile));
foreach (var path in compatibleFilePaths)
{
if (foundAssemblies.Contains(path))
{
continue;
}
foundAssemblies.Add(path);
_logger.Info("Found: " + path);
}
if (nugetPackage.Dependencies == null || !nugetPackage.Dependencies.Any() || !strictLoad)
{
continue;
}
var dependencyReferences = nugetPackage.Dependencies
.Where(i => _topLevelPackages.All(x => x.PackageId != i.Id))
.Select(i => new PackageReference(i.Id, i.FrameworkName, i.Version));
LoadFiles(packageDir, dependencyReferences, missingAssemblies, foundAssemblies, true);
}
}
}
}