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
136 lines (108 loc) · 5.22 KB
/
PackageAssemblyResolver.cs
File metadata and controls
136 lines (108 loc) · 5.22 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ScriptCs.Exceptions;
using ScriptCs.Package;
namespace ScriptCs
{
public class PackageAssemblyResolver : IPackageAssemblyResolver
{
private readonly IFileSystem _fileSystem;
private readonly IPackageContainer _packageContainer;
private List<IPackageReference> _topLevelPackages;
public PackageAssemblyResolver(IFileSystem fileSystem, IPackageContainer packageContainer)
{
_fileSystem = fileSystem;
_packageContainer = packageContainer;
}
public void SavePackages(Action<string> output)
{
var packagesFile = Path.Combine(_fileSystem.CurrentDirectory, Constants.PackagesFile);
var packagesFolder = Path.Combine(_fileSystem.CurrentDirectory, Constants.PackagesFolder);
if (_fileSystem.FileExists(packagesFile))
{
output("Packages.config already exists!");
return;
}
if (!_fileSystem.DirectoryExists(packagesFolder))
{
output("Packages directory does not exist!");
return;
}
var result = _packageContainer.CreatePackageFile().ToList();
if (!result.Any())
{
output("No packages found!");
return;
}
result.ForEach(i => output(string.Format("Added {0}", i)));
output("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, Action<string> outputCallback = null)
{
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, ref missingAssemblies, ref foundAssemblyPaths, _fileSystem.FileExists(packageFile), outputCallback);
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, ref List<IPackageReference> missingAssemblies, ref List<string> foundAssemblies, bool strictLoad = true, Action<string> outputCallback = null)
{
foreach (var packageRef in packageReferences)
{
var nugetPackage = _packageContainer.FindPackage(packageDir, packageRef);
if (nugetPackage == null)
{
missingAssemblies.Add(packageRef);
if (outputCallback != null)
{
outputCallback("Cannot find: " + packageRef.PackageId + " " + packageRef.Version);
}
continue;
}
var compatibleFiles = nugetPackage.GetCompatibleDlls(packageRef.FrameworkName);
if (compatibleFiles == null)
{
missingAssemblies.Add(packageRef);
if (outputCallback != null)
{
outputCallback("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);
if (outputCallback != null)
{
outputCallback("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, ref missingAssemblies, ref foundAssemblies, true, outputCallback);
}
}
}
}