Forgotten files

This commit is contained in:
Pavel Krymets 2016-01-08 14:51:21 -08:00
parent 566790577d
commit b5c48de651
3 changed files with 268 additions and 0 deletions

View File

@ -0,0 +1,118 @@
// 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.Diagnostics;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.Dnx.Compilation.CSharp;
using Microsoft.Extensions.CompilationAbstractions;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.AspNet.Mvc.Razor.Compilation
{
/// <summary>
/// A type that uses Roslyn to compile C# content and <see cref="ILibraryExporter"/> to find out references.
/// </summary>
public class DefaultRoslynCompilationService : RoslynCompilationService
{
private readonly IApplicationEnvironment _environment;
private readonly ILibraryExporter _libraryExporter;
/// <summary>
/// Initalizes a new instance of the <see cref="DefaultRoslynCompilationService"/> class.
/// </summary>
/// <param name="environment">The environment for the executing application.</param>
/// <param name="libraryExporter">The library manager that provides export and reference information.</param>
/// <param name="host">The <see cref="IMvcRazorHost"/> that was used to generate the code.</param>
/// <param name="optionsAccessor">Accessor to <see cref="RazorViewEngineOptions"/>.</param>
/// <param name="fileProviderAccessor">The <see cref="IRazorViewEngineFileProviderAccessor"/>.</param>
public DefaultRoslynCompilationService(IApplicationEnvironment environment,
ILibraryExporter libraryExporter,
IMvcRazorHost host,
IOptions<RazorViewEngineOptions> optionsAccessor,
IRazorViewEngineFileProviderAccessor fileProviderAccessor)
: base(environment, host, optionsAccessor, fileProviderAccessor)
{
_environment = environment;
_libraryExporter = libraryExporter;
}
protected override List<MetadataReference> GetApplicationReferences()
{
var references = new List<MetadataReference>();
// Get the MetadataReference for the executing application. If it's a Roslyn reference,
// we can copy the references created when compiling the application to the Razor page being compiled.
// This avoids performing expensive calls to MetadataReference.CreateFromImage.
var libraryExport = _libraryExporter.GetExport(_environment.ApplicationName);
if (libraryExport?.MetadataReferences != null && libraryExport.MetadataReferences.Count > 0)
{
Debug.Assert(libraryExport.MetadataReferences.Count == 1,
"Expected 1 MetadataReferences, found " + libraryExport.MetadataReferences.Count);
var roslynReference = libraryExport.MetadataReferences[0] as IRoslynMetadataReference;
var compilationReference = roslynReference?.MetadataReference as CompilationReference;
if (compilationReference != null)
{
references.AddRange(compilationReference.Compilation.References);
references.Add(roslynReference.MetadataReference);
return references;
}
}
var export = _libraryExporter.GetAllExports(_environment.ApplicationName);
if (export != null)
{
foreach (var metadataReference in export.MetadataReferences)
{
// Taken from https://github.com/aspnet/KRuntime/blob/757ba9bfdf80bd6277e715d6375969a7f44370ee/src/...
// Microsoft.Extensions.Runtime.Roslyn/RoslynCompiler.cs#L164
// We don't want to take a dependency on the Roslyn bit directly since it pulls in more dependencies
// than the view engine needs (Microsoft.Extensions.Runtime) for example
references.Add(ConvertMetadataReference(metadataReference));
}
}
return references;
}
private MetadataReference ConvertMetadataReference(IMetadataReference metadataReference)
{
var roslynReference = metadataReference as IRoslynMetadataReference;
if (roslynReference != null)
{
return roslynReference.MetadataReference;
}
var embeddedReference = metadataReference as IMetadataEmbeddedReference;
if (embeddedReference != null)
{
return MetadataReference.CreateFromImage(embeddedReference.Contents);
}
var fileMetadataReference = metadataReference as IMetadataFileReference;
if (fileMetadataReference != null)
{
return CreateMetadataFileReference(fileMetadataReference.Path);
}
var projectReference = metadataReference as IMetadataProjectReference;
if (projectReference != null)
{
using (var ms = new MemoryStream())
{
projectReference.EmitReferenceAssembly(ms);
return MetadataReference.CreateFromImage(ms.ToArray());
}
}
throw new NotSupportedException();
}
}
}

View File

@ -0,0 +1,62 @@
// 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.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.CompilationAbstractions;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.AspNet.Mvc.Razor.Compilation
{
/// <summary>
/// A type that uses Roslyn to compile C# content and <see cref="DependencyContext"/> to find out references.
/// </summary>
public class DependencyContextCompilationService : RoslynCompilationService
{
private DependencyContext _dependencyContext;
/// <summary>
/// Initalizes a new instance of the <see cref="DependencyContextCompilationService"/> class.
/// </summary>
/// <param name="environment">The environment for the executing application.</param>
/// <param name="host">The <see cref="IMvcRazorHost"/> that was used to generate the code.</param>
/// <param name="optionsAccessor">Accessor to <see cref="RazorViewEngineOptions"/>.</param>
/// <param name="fileProviderAccessor">The <see cref="IRazorViewEngineFileProviderAccessor"/>.</param>
public DependencyContextCompilationService(IApplicationEnvironment environment,
IMvcRazorHost host,
IOptions<RazorViewEngineOptions> optionsAccessor,
IRazorViewEngineFileProviderAccessor fileProviderAccessor)
: this(DependencyContext.Default, environment, host, optionsAccessor, fileProviderAccessor)
{
}
/// <summary>
/// Initalizes a new instance of the <see cref="DependencyContextCompilationService"/> class.
/// </summary>
/// <param name="dependencyContext"><see cref="DependencyContext"/> to use for reference resolution.</param>
/// <param name="environment">The environment for the executing application.</param>
/// <param name="host">The <see cref="IMvcRazorHost"/> that was used to generate the code.</param>
/// <param name="optionsAccessor">Accessor to <see cref="RazorViewEngineOptions"/>.</param>
/// <param name="fileProviderAccessor">The <see cref="IRazorViewEngineFileProviderAccessor"/>.</param>
public DependencyContextCompilationService(DependencyContext dependencyContext,
IApplicationEnvironment environment,
IMvcRazorHost host,
IOptions<RazorViewEngineOptions> optionsAccessor,
IRazorViewEngineFileProviderAccessor fileProviderAccessor)
: base(environment, host, optionsAccessor, fileProviderAccessor)
{
_dependencyContext = dependencyContext;
}
protected override List<MetadataReference> GetApplicationReferences()
{
return _dependencyContext.CompileLibraries
.SelectMany(library => library.ResolveReferencePaths())
.Select(CreateMetadataFileReference)
.ToList();
}
}
}

View File

@ -0,0 +1,88 @@
// 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 Microsoft.AspNet.Mvc.Razor;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.DependencyModel;
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Sets up compilation and parse option default options for <see cref="RazorViewEngineOptions"/> using <see cref="DependencyContext"/>
/// </summary>
public class DependencyContextRazorViewEngineOptionsSetup : ConfigureOptions<RazorViewEngineOptions>
{
/// <summary>
/// Initializes a new instance of <see cref="DependencyContextRazorViewEngineOptionsSetup"/>.
/// </summary>
public DependencyContextRazorViewEngineOptionsSetup() : this(DependencyContext.Default)
{
}
/// <summary>
/// Initializes a new instance of <see cref="DependencyContextRazorViewEngineOptionsSetup"/>.
/// </summary>
/// <param name="dependencyContext"><see cref="DependencyContext"/> to use as compilation and parse option source.</param>
public DependencyContextRazorViewEngineOptionsSetup(DependencyContext dependencyContext) : base(options => ConfigureRazor(options, dependencyContext))
{
}
private static void ConfigureRazor(RazorViewEngineOptions options, DependencyContext dependencyContext)
{
var compilationOptions = dependencyContext.CompilationOptions;
SetParseOptions(options, compilationOptions);
SetCompilationOptions(options, compilationOptions);
}
private static void SetCompilationOptions(RazorViewEngineOptions options, Microsoft.Extensions.DependencyModel.CompilationOptions compilationOptions)
{
var roslynOptions = options.CompilationOptions;
// Disable 1702 until roslyn turns this off by default
roslynOptions = roslynOptions.WithSpecificDiagnosticOptions(
new Dictionary<string, ReportDiagnostic>
{
{"CS1701", ReportDiagnostic.Suppress}, // Binding redirects
{"CS1702", ReportDiagnostic.Suppress},
{"CS1705", ReportDiagnostic.Suppress}
});
if (compilationOptions.AllowUnsafe.HasValue)
{
roslynOptions = roslynOptions.WithAllowUnsafe(compilationOptions.AllowUnsafe.Value);
}
if (compilationOptions.Optimize.HasValue)
{
var optimizationLevel = compilationOptions.Optimize.Value ? OptimizationLevel.Debug : OptimizationLevel.Release;
roslynOptions = roslynOptions.WithOptimizationLevel(optimizationLevel);
}
if (compilationOptions.WarningsAsErrors.HasValue)
{
var reportDiagnostic = compilationOptions.WarningsAsErrors.Value ? ReportDiagnostic.Error : ReportDiagnostic.Default;
roslynOptions = roslynOptions.WithGeneralDiagnosticOption(reportDiagnostic);
}
options.CompilationOptions = roslynOptions;
}
private static void SetParseOptions(RazorViewEngineOptions options, Microsoft.Extensions.DependencyModel.CompilationOptions compilationOptions)
{
var roslynParseOptions = options.ParseOptions;
roslynParseOptions = roslynParseOptions.WithPreprocessorSymbols(compilationOptions.Defines);
var languageVersion = roslynParseOptions.LanguageVersion;
if (Enum.TryParse(compilationOptions.LanguageVersion, ignoreCase: true, result: out languageVersion))
{
roslynParseOptions = roslynParseOptions.WithLanguageVersion(languageVersion);
}
options.ParseOptions = roslynParseOptions;
}
}
}