This repository has been archived by the owner on Dec 18, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
/
ApplicationHostContext.cs
277 lines (222 loc) · 11.5 KB
/
ApplicationHostContext.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Versioning;
using Microsoft.Dnx.Runtime.Internals;
using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.Dnx.Runtime
{
public class ApplicationHostContext
{
public Project Project { get; set; }
public LockFile LockFile { get; set; }
// TODO: Remove this, it's kinda hacky
public ProjectDescription MainProject { get; set; }
public FrameworkName TargetFramework { get; set; }
public IEnumerable<string> RuntimeIdentifiers { get; set; } = Enumerable.Empty<string>();
public string RootDirectory { get; set; }
public string ProjectDirectory { get; set; }
public string PackagesDirectory { get; set; }
public FrameworkReferenceResolver FrameworkResolver { get; set; }
public LibraryManager LibraryManager { get; private set; }
public static IList<LibraryDescription> GetRuntimeLibraries(ApplicationHostContext context)
{
return GetRuntimeLibraries(context, throwOnInvalidLockFile: false);
}
public static IList<LibraryDescription> GetRuntimeLibraries(ApplicationHostContext context, bool throwOnInvalidLockFile)
{
var result = InitializeCore(context);
if (throwOnInvalidLockFile)
{
if (!result.ValidLockFile)
{
throw new InvalidOperationException($"{result.LockFileValidationMessage}. Please run \"dnu restore\" to generate a new lock file.");
}
}
return result.Libraries;
}
public static void Initialize(ApplicationHostContext context)
{
var result = InitializeCore(context);
// Map dependencies
var lookup = result.Libraries.ToDictionary(l => l.Identity.Name);
var frameworkReferenceResolver = context.FrameworkResolver ?? new FrameworkReferenceResolver();
var referenceAssemblyDependencyResolver = new ReferenceAssemblyDependencyResolver(frameworkReferenceResolver);
var gacDependencyResolver = new GacDependencyResolver();
var unresolvedDependencyProvider = new UnresolvedDependencyProvider();
// Fix up dependencies
foreach (var library in lookup.Values.ToList())
{
if (string.Equals(library.Type, LibraryTypes.Package) &&
!Directory.Exists(library.Path))
{
// If the package path doesn't exist then mark this dependency as unresolved
library.Resolved = false;
}
library.Framework = library.Framework ?? context.TargetFramework;
foreach (var dependency in library.Dependencies)
{
LibraryDescription dep;
if (!lookup.TryGetValue(dependency.Name, out dep))
{
if (dependency.LibraryRange.IsGacOrFrameworkReference)
{
dep = referenceAssemblyDependencyResolver.GetDescription(dependency.LibraryRange, context.TargetFramework) ??
gacDependencyResolver.GetDescription(dependency.LibraryRange, context.TargetFramework) ??
unresolvedDependencyProvider.GetDescription(dependency.LibraryRange, context.TargetFramework);
dep.Framework = context.TargetFramework;
lookup[dependency.Name] = dep;
}
else
{
dep = unresolvedDependencyProvider.GetDescription(dependency.LibraryRange, context.TargetFramework);
lookup[dependency.Name] = dep;
}
}
// REVIEW: This isn't quite correct but there's a many to one relationship here.
// Different ranges can resolve to this dependency but only one wins
dep.RequestedRange = dependency.LibraryRange;
dependency.Library = dep;
}
}
var libraries = lookup.Values.ToList();
context.LibraryManager = new LibraryManager(context.Project.ProjectFilePath, context.TargetFramework, libraries);
AddLockFileDiagnostics(context, result);
// Clear all the temporary memory aggressively here if we don't care about reuse
// e.g. runtime scenarios
lookup.Clear();
}
private static Result InitializeCore(ApplicationHostContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
context.ProjectDirectory = context.Project?.ProjectDirectory ?? context.ProjectDirectory;
context.RootDirectory = context.RootDirectory ?? ProjectRootResolver.ResolveRootDirectory(context.ProjectDirectory);
context.PackagesDirectory = context.PackagesDirectory ?? PackageDependencyProvider.ResolveRepositoryPath(context.RootDirectory);
LockFileLookup lockFileLookup = null;
if (context.Project == null)
{
Project project;
if (Project.TryGetProject(context.ProjectDirectory, out project))
{
context.Project = project;
}
else
{
throw new InvalidOperationException($"Unable to resolve project from {context.ProjectDirectory}");
}
}
var projectLockJsonPath = Path.Combine(context.ProjectDirectory, LockFileReader.LockFileName);
if (context.LockFile == null && File.Exists(projectLockJsonPath))
{
var lockFileReader = new LockFileReader();
context.LockFile = lockFileReader.Read(projectLockJsonPath);
}
var validLockFile = true;
string lockFileValidationMessage = null;
if (context.LockFile != null)
{
validLockFile = (context.LockFile.Version == Constants.LockFileVersion) && context.LockFile.IsValidForProject(context.Project, out lockFileValidationMessage);
lockFileLookup = new LockFileLookup(context.LockFile);
}
var libraries = new List<LibraryDescription>();
var packageResolver = new PackageDependencyProvider(context.PackagesDirectory);
var projectResolver = new ProjectDependencyProvider();
context.MainProject = projectResolver.GetDescription(context.TargetFramework, context.Project);
// Add the main project
libraries.Add(context.MainProject);
if (lockFileLookup != null)
{
var target = SelectTarget(context, context.LockFile);
if (target != null)
{
if (Logger.IsEnabled && string.IsNullOrEmpty(target.RuntimeIdentifier))
{
// REVIEW(anurse): Is there ever a reason we want to use the RID-less target now?
Logger.TraceWarning($"[{nameof(ApplicationHostContext)}] Lock File Target is Runtime-agnostic! This is generally not good.");
}
Logger.TraceInformation($"[{nameof(ApplicationHostContext)}] Using Lock File Target: {target.TargetFramework}/{target.RuntimeIdentifier}");
foreach (var library in target.Libraries)
{
if (string.Equals(library.Type, "project"))
{
var projectLibrary = lockFileLookup.GetProject(library.Name);
var path = Path.GetFullPath(Path.Combine(context.ProjectDirectory, projectLibrary.Path));
var projectDescription = projectResolver.GetDescription(library.Name, path, library);
libraries.Add(projectDescription);
}
else
{
var packageEntry = lockFileLookup.GetPackage(library.Name, library.Version);
var packageDescription = packageResolver.GetDescription(packageEntry, library);
libraries.Add(packageDescription);
}
}
}
}
lockFileLookup?.Clear();
var lockFilePresent = context.LockFile != null;
context.LockFile = null; // LockFile is no longer needed
return new Result(libraries, lockFilePresent, validLockFile, lockFileValidationMessage);
}
private static LockFileTarget SelectTarget(ApplicationHostContext context, LockFile lockFile)
{
foreach (var runtimeIdentifier in context.RuntimeIdentifiers)
{
foreach (var scanTarget in lockFile.Targets)
{
if (scanTarget.TargetFramework == context.TargetFramework && string.Equals(scanTarget.RuntimeIdentifier, runtimeIdentifier, StringComparison.Ordinal))
{
return scanTarget;
}
}
}
foreach (var scanTarget in lockFile.Targets)
{
if (scanTarget.TargetFramework == context.TargetFramework && string.IsNullOrEmpty(scanTarget.RuntimeIdentifier))
{
return scanTarget;
}
}
return null;
}
private static void AddLockFileDiagnostics(ApplicationHostContext context, Result result)
{
if (!result.LockFileExists)
{
context.LibraryManager.AddGlobalDiagnostics(new DiagnosticMessage(
DiagnosticMonikers.NU1009,
$"The expected lock file doesn't exist. Please run \"dnu restore\" to generate a new lock file.",
Path.Combine(context.Project.ProjectDirectory, LockFileReader.LockFileName),
DiagnosticMessageSeverity.Error));
}
if (!result.ValidLockFile)
{
context.LibraryManager.AddGlobalDiagnostics(new DiagnosticMessage(
DiagnosticMonikers.NU1006,
$"{result.LockFileValidationMessage}. Please run \"dnu restore\" to generate a new lock file.",
Path.Combine(context.Project.ProjectDirectory, LockFileReader.LockFileName),
DiagnosticMessageSeverity.Error));
}
}
private struct Result
{
public List<LibraryDescription> Libraries { get; }
public string LockFileValidationMessage { get; }
public bool LockFileExists { get; }
public bool ValidLockFile { get; }
public Result(List<LibraryDescription> libraries, bool lockFileExists, bool validLockFile, string lockFileValidationMessage)
{
Libraries = libraries;
LockFileExists = lockFileExists;
ValidLockFile = validLockFile;
LockFileValidationMessage = lockFileValidationMessage;
}
}
}
}