Updated roslyn compilation to use new host interface.

This commit is contained in:
David Fowler 2014-03-10 23:17:52 -07:00
parent 3e049fed75
commit e991e86863
6 changed files with 33 additions and 77 deletions

View File

@ -12,17 +12,17 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
{ {
public class RoslynCompilationService : ICompilationService public class RoslynCompilationService : ICompilationService
{ {
private readonly IDependencyExporter _exporter; private readonly ILibraryExportProvider _exportProvider;
private readonly IApplicationEnvironment _environment; private readonly IApplicationEnvironment _environment;
private readonly IAssemblyLoaderEngine _loader; private readonly IAssemblyLoaderEngine _loader;
public RoslynCompilationService(IApplicationEnvironment environment, public RoslynCompilationService(IApplicationEnvironment environment,
IAssemblyLoaderEngine loaderEngine, IAssemblyLoaderEngine loaderEngine,
IDependencyExporter exporter) ILibraryExportProvider exportProvider)
{ {
_environment = environment; _environment = environment;
_loader = loaderEngine; _loader = loaderEngine;
_exporter = exporter; _exportProvider = exportProvider;
} }
public Task<CompilationResult> Compile(string content) public Task<CompilationResult> Compile(string content)
@ -67,76 +67,30 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
{ {
var references = new List<MetadataReference>(); var references = new List<MetadataReference>();
// TODO: We need a way to get the current application's dependencies var export = _exportProvider.GetLibraryExport(_environment.ApplicationName, _environment.TargetFramework);
var assemblies = new[] { foreach (var metadataReference in export.MetadataReferences)
_environment.ApplicationName,
#if NET45
"mscorlib",
"System",
"System.Core",
"Microsoft.CSharp",
#else
"System.Linq",
"System.Collections",
"System.Dynamic.Runtime",
"System.Collections.Generic",
#endif
"Microsoft.AspNet.Mvc",
"Microsoft.AspNet.Mvc.Razor",
"Microsoft.AspNet.Mvc.Rendering",
};
var exports = new List<IDependencyExport>();
foreach (var assemblyName in assemblies)
{ {
var export = _exporter.GetDependencyExport(assemblyName, _environment.TargetFramework); var fileMetadataReference = metadataReference as IMetadataFileReference;
if (export == null) if (fileMetadataReference != null)
{ {
continue; references.Add(CreateMetadataFileReference(fileMetadataReference.Path));
} }
else
{
var roslynReference = metadataReference as IRoslynMetadataReference;
exports.Add(export); if (roslynReference != null)
{
references.Add(roslynReference.MetadataReference);
}
}
} }
ExtractReferences(exports, references);
return references; return references;
} }
private void ExtractReferences(List<IDependencyExport> exports, List<MetadataReference> references)
{
var paths = new HashSet<string>();
foreach (var export in exports)
{
foreach (var metadataReference in export.MetadataReferences)
{
var fileMetadataReference = metadataReference as IMetadataFileReference;
if (fileMetadataReference != null)
{
string path = fileMetadataReference.Path;
paths.Add(path);
}
else
{
var roslynReference = metadataReference as IRoslynMetadataReference;
if (roslynReference != null)
{
references.Add(roslynReference.MetadataReference);
}
}
}
}
references.AddRange(paths.Select(CreateMetadataFileReference));
}
private MetadataReference CreateMetadataFileReference(string path) private MetadataReference CreateMetadataFileReference(string path)
{ {
#if NET45 #if NET45

View File

@ -1,10 +0,0 @@
using System.Runtime.Versioning;
namespace Microsoft.Net.Runtime
{
[AssemblyNeutral]
public interface IDependencyExporter
{
IDependencyExport GetDependencyExport(string name, FrameworkName targetFramework);
}
}

View File

@ -4,9 +4,9 @@ using System.Collections.Generic;
namespace Microsoft.Net.Runtime namespace Microsoft.Net.Runtime
{ {
[AssemblyNeutral] [AssemblyNeutral]
public interface IDependencyExport public interface ILibraryExport
{ {
IList<IMetadataReference> MetadataReferences { get; } IList<IMetadataReference> MetadataReferences { get; }
IList<ISourceReference> SourceReferences { get; } IList<ISourceReference> SourceReferences { get; }
} }
} }

View File

@ -0,0 +1,10 @@
using System.Runtime.Versioning;
namespace Microsoft.Net.Runtime
{
[AssemblyNeutral]
public interface ILibraryExportProvider
{
ILibraryExport GetLibraryExport(string name, FrameworkName targetFramework);
}
}

View File

@ -1,8 +1,9 @@
namespace Microsoft.Net.Runtime 
namespace Microsoft.Net.Runtime
{ {
[AssemblyNeutral] [AssemblyNeutral]
public interface IMetadataFileReference : IMetadataReference public interface IMetadataFileReference : IMetadataReference
{ {
string Path { get; } string Path { get; }
} }
} }

View File

@ -4,5 +4,6 @@ namespace Microsoft.Net.Runtime
[AssemblyNeutral] [AssemblyNeutral]
public interface IMetadataReference public interface IMetadataReference
{ {
string Name { get; }
} }
} }