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 224
/
ProjectLibraryExportProvider.cs
153 lines (127 loc) · 7.13 KB
/
ProjectLibraryExportProvider.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
// Copyright (c) Microsoft Open Technologies, Inc. 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 Microsoft.Framework.Runtime.Caching;
using Microsoft.Framework.Runtime.Compilation;
namespace Microsoft.Framework.Runtime
{
public class ProjectLibraryExportProvider : ILibraryExportProvider
{
private readonly IProjectResolver _projectResolver;
private readonly IServiceProvider _serviceProvider;
private readonly Dictionary<TypeInformation, IProjectCompiler> _projectCompilers = new Dictionary<TypeInformation, IProjectCompiler>();
private readonly Lazy<IAssemblyLoadContext> _projectLoadContext;
public ProjectLibraryExportProvider(IProjectResolver projectResolver,
IServiceProvider serviceProvider)
{
_projectResolver = projectResolver;
_serviceProvider = serviceProvider;
// REVIEW: In the future we should be able to throw this away
_projectLoadContext = new Lazy<IAssemblyLoadContext>(() =>
{
var factory = (IAssemblyLoadContextFactory)_serviceProvider.GetService(typeof(IAssemblyLoadContextFactory));
return factory.Create();
});
}
public ILibraryExport GetLibraryExport(ILibraryKey target)
{
Project project;
// Can't find a project file with the name so bail
if (!_projectResolver.TryResolveProject(target.Name, out project))
{
return null;
}
Logger.TraceInformation("[{0}]: GetLibraryExport({1}, {2}, {3}, {4})", GetType().Name, target.Name, target.TargetFramework, target.Configuration, target.Aspect);
var targetFrameworkInformation = project.GetTargetFramework(target.TargetFramework);
// This is the target framework defined in the project. If there were no target frameworks
// defined then this is the targetFramework specified
if (targetFrameworkInformation.FrameworkName != null)
{
target = target.ChangeTargetFramework(targetFrameworkInformation.FrameworkName);
}
var key = Tuple.Create(
target.Name,
target.TargetFramework,
target.Configuration,
target.Aspect);
var cache = (ICache)_serviceProvider.GetService(typeof(ICache));
var cacheContextAccessor = (ICacheContextAccessor)_serviceProvider.GetService(typeof(ICacheContextAccessor));
var namedCacheDependencyProvider = (INamedCacheDependencyProvider)_serviceProvider.GetService(typeof(INamedCacheDependencyProvider));
var loadContextFactory = (IAssemblyLoadContextFactory)_serviceProvider.GetService(typeof(IAssemblyLoadContextFactory));
return cache.Get<ILibraryExport>(key, ctx =>
{
var metadataReferences = new List<IMetadataReference>();
var sourceReferences = new List<ISourceReference>();
if (!string.IsNullOrEmpty(targetFrameworkInformation.AssemblyPath))
{
var assemblyPath = ResolvePath(project, target.Configuration, targetFrameworkInformation.AssemblyPath);
var pdbPath = ResolvePath(project, target.Configuration, targetFrameworkInformation.PdbPath);
metadataReferences.Add(new CompiledProjectMetadataReference(project, assemblyPath, pdbPath));
}
else
{
var provider = project.CompilerServices?.ProjectCompiler ?? Project.DefaultCompiler;
// Find the default project exporter
var projectCompiler = _projectCompilers.GetOrAdd(provider, typeInfo =>
{
return CompilerServices.CreateService<IProjectCompiler>(_serviceProvider, _projectLoadContext.Value, typeInfo);
});
Logger.TraceInformation("[{0}]: GetProjectReference({1}, {2}, {3}, {4})", provider.TypeName, target.Name, target.TargetFramework, target.Configuration, target.Aspect);
// Get the exports for the project dependencies
var projectExport = new Lazy<ILibraryExport>(() =>
{
// TODO: Cache?
var context = new ApplicationHostContext(_serviceProvider,
project.ProjectDirectory,
packagesDirectory: null,
configuration: target.Configuration,
targetFramework: target.TargetFramework,
cache: cache,
cacheContextAccessor: cacheContextAccessor,
namedCacheDependencyProvider: namedCacheDependencyProvider,
loadContextFactory: loadContextFactory);
context.DependencyWalker.Walk(project.Name, project.Version, target.TargetFramework);
return ProjectExportProviderHelper.GetExportsRecursive(
cache,
context.LibraryManager,
context.LibraryExportProvider,
target,
dependenciesOnly: true);
});
// Resolve the project export
IMetadataProjectReference projectReference = projectCompiler.CompileProject(
project,
target,
() => projectExport.Value,
() => CompositeResourceProvider.Default.GetResources(project));
metadataReferences.Add(projectReference);
// Shared sources
foreach (var sharedFile in project.Files.SharedFiles)
{
sourceReferences.Add(new SourceFileReference(sharedFile));
}
}
return new LibraryExport(metadataReferences, sourceReferences);
});
}
private static string ResolvePath(Project project, string configuration, string path)
{
if (string.IsNullOrEmpty(path))
{
return null;
}
if (Path.DirectorySeparatorChar == '/')
{
path = path.Replace('\\', Path.DirectorySeparatorChar);
}
else
{
path = path.Replace('/', Path.DirectorySeparatorChar);
}
path = path.Replace("{configuration}", configuration);
return Path.Combine(project.ProjectDirectory, path);
}
}
}