Remove dependency on ICompilationOptionsProvider
- Removed Configuration from RazorViewEngineOptions - Added ParseOptions and CompilationOptions to RazorViewEngineOptions
This commit is contained in:
parent
80e25344f6
commit
99f501152b
|
|
@ -35,12 +35,12 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||||
|
|
||||||
private readonly ILibraryExporter _libraryExporter;
|
private readonly ILibraryExporter _libraryExporter;
|
||||||
private readonly IApplicationEnvironment _environment;
|
private readonly IApplicationEnvironment _environment;
|
||||||
private readonly ICompilerOptionsProvider _compilerOptionsProvider;
|
|
||||||
private readonly IFileProvider _fileProvider;
|
private readonly IFileProvider _fileProvider;
|
||||||
private readonly Lazy<List<MetadataReference>> _applicationReferences;
|
private readonly Lazy<List<MetadataReference>> _applicationReferences;
|
||||||
private readonly string _classPrefix;
|
private readonly string _classPrefix;
|
||||||
private readonly string _configuration;
|
private readonly Action<RoslynCompilationContext> _compilationCallback;
|
||||||
private Action<RoslynCompilationContext> _compilationCallback;
|
private readonly CSharpParseOptions _parseOptions;
|
||||||
|
private readonly CSharpCompilationOptions _compilationOptions;
|
||||||
|
|
||||||
#if DOTNET5_5
|
#if DOTNET5_5
|
||||||
private readonly RazorLoadContext _razorLoadContext;
|
private readonly RazorLoadContext _razorLoadContext;
|
||||||
|
|
@ -61,18 +61,18 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||||
public RoslynCompilationService(
|
public RoslynCompilationService(
|
||||||
IApplicationEnvironment environment,
|
IApplicationEnvironment environment,
|
||||||
ILibraryExporter libraryExporter,
|
ILibraryExporter libraryExporter,
|
||||||
ICompilerOptionsProvider compilerOptionsProvider,
|
|
||||||
IMvcRazorHost host,
|
IMvcRazorHost host,
|
||||||
IOptions<RazorViewEngineOptions> optionsAccessor)
|
IOptions<RazorViewEngineOptions> optionsAccessor)
|
||||||
{
|
{
|
||||||
_environment = environment;
|
_environment = environment;
|
||||||
_libraryExporter = libraryExporter;
|
_libraryExporter = libraryExporter;
|
||||||
_applicationReferences = new Lazy<List<MetadataReference>>(GetApplicationReferences);
|
_applicationReferences = new Lazy<List<MetadataReference>>(GetApplicationReferences);
|
||||||
_compilerOptionsProvider = compilerOptionsProvider;
|
|
||||||
_fileProvider = optionsAccessor.Value.FileProvider;
|
_fileProvider = optionsAccessor.Value.FileProvider;
|
||||||
_classPrefix = host.MainClassNamePrefix;
|
_classPrefix = host.MainClassNamePrefix;
|
||||||
_configuration = optionsAccessor.Value.Configuration;
|
|
||||||
_compilationCallback = optionsAccessor.Value.CompilationCallback;
|
_compilationCallback = optionsAccessor.Value.CompilationCallback;
|
||||||
|
_parseOptions = optionsAccessor.Value.ParseOptions;
|
||||||
|
_compilationOptions = optionsAccessor.Value.CompilationOptions;
|
||||||
|
|
||||||
|
|
||||||
#if DOTNET5_5
|
#if DOTNET5_5
|
||||||
_razorLoadContext = new RazorLoadContext();
|
_razorLoadContext = new RazorLoadContext();
|
||||||
|
|
@ -93,21 +93,17 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||||
}
|
}
|
||||||
|
|
||||||
var assemblyName = Path.GetRandomFileName();
|
var assemblyName = Path.GetRandomFileName();
|
||||||
var compilationSettings = _compilerOptionsProvider.GetCompilationSettings(_environment, _configuration);
|
|
||||||
var syntaxTree = SyntaxTreeGenerator.Generate(
|
var syntaxTree = SyntaxTreeGenerator.Generate(
|
||||||
compilationContent,
|
compilationContent,
|
||||||
assemblyName,
|
assemblyName,
|
||||||
compilationSettings);
|
_parseOptions);
|
||||||
|
|
||||||
var references = _applicationReferences.Value;
|
var references = _applicationReferences.Value;
|
||||||
|
|
||||||
var compilationOptions = compilationSettings
|
|
||||||
.CompilationOptions
|
|
||||||
.WithOutputKind(OutputKind.DynamicallyLinkedLibrary);
|
|
||||||
|
|
||||||
var compilation = CSharpCompilation.Create(
|
var compilation = CSharpCompilation.Create(
|
||||||
assemblyName,
|
assemblyName,
|
||||||
options: compilationOptions,
|
options: _compilationOptions,
|
||||||
syntaxTrees: new[] { syntaxTree },
|
syntaxTrees: new[] { syntaxTree },
|
||||||
references: references);
|
references: references);
|
||||||
|
|
||||||
|
|
@ -258,13 +254,16 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||||
}
|
}
|
||||||
|
|
||||||
var export = _libraryExporter.GetAllExports(_environment.ApplicationName);
|
var export = _libraryExporter.GetAllExports(_environment.ApplicationName);
|
||||||
foreach (var metadataReference in export.MetadataReferences)
|
if (export != null)
|
||||||
{
|
{
|
||||||
// Taken from https://github.com/aspnet/KRuntime/blob/757ba9bfdf80bd6277e715d6375969a7f44370ee/src/...
|
foreach (var metadataReference in export.MetadataReferences)
|
||||||
// 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
|
// Taken from https://github.com/aspnet/KRuntime/blob/757ba9bfdf80bd6277e715d6375969a7f44370ee/src/...
|
||||||
// than the view engine needs (Microsoft.Extensions.Runtime) for example
|
// Microsoft.Extensions.Runtime.Roslyn/RoslynCompiler.cs#L164
|
||||||
references.Add(ConvertMetadataReference(metadataReference));
|
// 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;
|
return references;
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,19 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||||
string text,
|
string text,
|
||||||
string path,
|
string path,
|
||||||
CompilationSettings compilationSettings)
|
CompilationSettings compilationSettings)
|
||||||
|
{
|
||||||
|
if (compilationSettings == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(compilationSettings));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Generate(text, path, GetParseOptions(compilationSettings));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SyntaxTree Generate(
|
||||||
|
string text,
|
||||||
|
string path,
|
||||||
|
CSharpParseOptions parseOptions)
|
||||||
{
|
{
|
||||||
if (text == null)
|
if (text == null)
|
||||||
{
|
{
|
||||||
|
|
@ -27,15 +40,15 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||||
throw new ArgumentNullException(nameof(path));
|
throw new ArgumentNullException(nameof(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (compilationSettings == null)
|
if (parseOptions == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(compilationSettings));
|
throw new ArgumentNullException(nameof(parseOptions));
|
||||||
}
|
}
|
||||||
|
|
||||||
var sourceText = SourceText.From(text, Encoding.UTF8);
|
var sourceText = SourceText.From(text, Encoding.UTF8);
|
||||||
var syntaxTree = CSharpSyntaxTree.ParseText(sourceText,
|
var syntaxTree = CSharpSyntaxTree.ParseText(sourceText,
|
||||||
path: path,
|
path: path,
|
||||||
options: GetParseOptions(compilationSettings));
|
options: parseOptions);
|
||||||
|
|
||||||
return syntaxTree;
|
return syntaxTree;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -123,7 +123,6 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
if (CompilationServices.Default != null)
|
if (CompilationServices.Default != null)
|
||||||
{
|
{
|
||||||
services.TryAdd(ServiceDescriptor.Instance(CompilationServices.Default.LibraryExporter));
|
services.TryAdd(ServiceDescriptor.Instance(CompilationServices.Default.LibraryExporter));
|
||||||
services.TryAdd(ServiceDescriptor.Instance(CompilationServices.Default.CompilerOptionsProvider));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
services.TryAddEnumerable(
|
services.TryAddEnumerable(
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.AspNet.FileProviders;
|
using Microsoft.AspNet.FileProviders;
|
||||||
using Microsoft.AspNet.Mvc.Razor.Compilation;
|
using Microsoft.AspNet.Mvc.Razor.Compilation;
|
||||||
|
using Microsoft.CodeAnalysis.CSharp;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.Razor
|
namespace Microsoft.AspNet.Mvc.Razor
|
||||||
{
|
{
|
||||||
|
|
@ -15,7 +16,9 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
{
|
{
|
||||||
private IFileProvider _fileProvider;
|
private IFileProvider _fileProvider;
|
||||||
|
|
||||||
private string _configuration;
|
private CSharpParseOptions _parseOptions = new CSharpParseOptions(LanguageVersion.CSharp6);
|
||||||
|
|
||||||
|
private CSharpCompilationOptions _compilationOptions = new CSharpCompilationOptions(CodeAnalysis.OutputKind.DynamicallyLinkedLibrary);
|
||||||
|
|
||||||
private Action<RoslynCompilationContext> _compilationCallback = c => { };
|
private Action<RoslynCompilationContext> _compilationCallback = c => { };
|
||||||
|
|
||||||
|
|
@ -48,27 +51,6 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the configuration name used by <see cref="RazorViewEngine"/> to compile razor views.
|
|
||||||
/// </summary>
|
|
||||||
/// <remarks>
|
|
||||||
/// At startup, this is initialized to "Debug" if <see cref="Microsoft.AspNet.Hosting.IHostingEnvironment"/> service is
|
|
||||||
/// registred and environment is development (<see cref="Microsoft.AspNet.Hosting.HostingEnvironmentExtensions.IsDevelopment"/>) else it is set to
|
|
||||||
/// "Release".
|
|
||||||
/// </remarks>
|
|
||||||
public string Configuration
|
|
||||||
{
|
|
||||||
get { return _configuration; }
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(value))
|
|
||||||
{
|
|
||||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(value));
|
|
||||||
}
|
|
||||||
_configuration = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the callback that is used to customize Razor compilation
|
/// Gets or sets the callback that is used to customize Razor compilation
|
||||||
/// to change compilation settings you can update <see cref="RoslynCompilationContext.Compilation"/> property.
|
/// to change compilation settings you can update <see cref="RoslynCompilationContext.Compilation"/> property.
|
||||||
|
|
@ -86,5 +68,38 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
_compilationCallback = value;
|
_compilationCallback = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the <see cref="CSharpParseOptions"/> options used by Razor view compilation.
|
||||||
|
/// </summary>
|
||||||
|
public CSharpParseOptions ParseOptions
|
||||||
|
{
|
||||||
|
get { return _parseOptions; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(value));
|
||||||
|
}
|
||||||
|
_parseOptions = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the <see cref="CSharpCompilationOptions"/> used by Razor view compilation.
|
||||||
|
/// </summary>
|
||||||
|
public CSharpCompilationOptions CompilationOptions
|
||||||
|
{
|
||||||
|
get { return _compilationOptions; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(value));
|
||||||
|
}
|
||||||
|
_compilationOptions = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@
|
||||||
using Microsoft.AspNet.FileProviders;
|
using Microsoft.AspNet.FileProviders;
|
||||||
using Microsoft.AspNet.Hosting;
|
using Microsoft.AspNet.Hosting;
|
||||||
using Microsoft.AspNet.Mvc.Razor;
|
using Microsoft.AspNet.Mvc.Razor;
|
||||||
using Microsoft.AspNet.Mvc.Razor.Compilation;
|
using Microsoft.CodeAnalysis;
|
||||||
using Microsoft.Extensions.CompilationAbstractions;
|
using Microsoft.CodeAnalysis.CSharp;
|
||||||
using Microsoft.Extensions.OptionsModel;
|
using Microsoft.Extensions.OptionsModel;
|
||||||
using Microsoft.Extensions.PlatformAbstractions;
|
using Microsoft.Extensions.PlatformAbstractions;
|
||||||
|
|
||||||
|
|
@ -22,7 +22,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
/// <param name="applicationEnvironment"><see cref="IApplicationEnvironment"/> for the application.</param>
|
/// <param name="applicationEnvironment"><see cref="IApplicationEnvironment"/> for the application.</param>
|
||||||
/// <param name="hostingEnvironment"><see cref="IHostingEnvironment"/> for the application.</param>
|
/// <param name="hostingEnvironment"><see cref="IHostingEnvironment"/> for the application.</param>
|
||||||
public RazorViewEngineOptionsSetup(IApplicationEnvironment applicationEnvironment,
|
public RazorViewEngineOptionsSetup(IApplicationEnvironment applicationEnvironment,
|
||||||
IHostingEnvironment hostingEnvironment)
|
IHostingEnvironment hostingEnvironment)
|
||||||
: base(options => ConfigureRazor(options, applicationEnvironment, hostingEnvironment))
|
: base(options => ConfigureRazor(options, applicationEnvironment, hostingEnvironment))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
@ -32,13 +32,19 @@ namespace Microsoft.AspNet.Mvc
|
||||||
IHostingEnvironment hostingEnvironment)
|
IHostingEnvironment hostingEnvironment)
|
||||||
{
|
{
|
||||||
razorOptions.FileProvider = new PhysicalFileProvider(applicationEnvironment.ApplicationBasePath);
|
razorOptions.FileProvider = new PhysicalFileProvider(applicationEnvironment.ApplicationBasePath);
|
||||||
|
|
||||||
|
var parseOptions = new CSharpParseOptions(LanguageVersion.CSharp6);
|
||||||
|
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
|
||||||
|
|
||||||
if (hostingEnvironment.IsDevelopment())
|
if (hostingEnvironment.IsDevelopment())
|
||||||
{
|
{
|
||||||
razorOptions.Configuration = "Debug";
|
razorOptions.ParseOptions = parseOptions.WithPreprocessorSymbols("DEBUG");
|
||||||
|
razorOptions.CompilationOptions = compilationOptions.WithOptimizationLevel(OptimizationLevel.Debug);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
razorOptions.Configuration = "Release";
|
razorOptions.ParseOptions = parseOptions.WithPreprocessorSymbols("RELEASE");
|
||||||
|
razorOptions.CompilationOptions = compilationOptions.WithOptimizationLevel(OptimizationLevel.Release);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,14 +27,6 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||||
public class MyTestType {}";
|
public class MyTestType {}";
|
||||||
var applicationEnvironment = PlatformServices.Default.Application;
|
var applicationEnvironment = PlatformServices.Default.Application;
|
||||||
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
||||||
|
|
||||||
var compilerOptionsProvider = new Mock<ICompilerOptionsProvider>();
|
|
||||||
compilerOptionsProvider
|
|
||||||
.Setup(p => p.GetCompilerOptions(
|
|
||||||
applicationEnvironment.ApplicationName,
|
|
||||||
applicationEnvironment.RuntimeFramework,
|
|
||||||
ConfigurationName))
|
|
||||||
.Returns(new CompilerOptions());
|
|
||||||
var mvcRazorHost = new Mock<IMvcRazorHost>();
|
var mvcRazorHost = new Mock<IMvcRazorHost>();
|
||||||
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
||||||
.Returns(string.Empty);
|
.Returns(string.Empty);
|
||||||
|
|
@ -42,7 +34,6 @@ public class MyTestType {}";
|
||||||
var compilationService = new RoslynCompilationService(
|
var compilationService = new RoslynCompilationService(
|
||||||
applicationEnvironment,
|
applicationEnvironment,
|
||||||
libraryExporter,
|
libraryExporter,
|
||||||
compilerOptionsProvider.Object,
|
|
||||||
mvcRazorHost.Object,
|
mvcRazorHost.Object,
|
||||||
GetOptions());
|
GetOptions());
|
||||||
var relativeFileInfo = new RelativeFileInfo(
|
var relativeFileInfo = new RelativeFileInfo(
|
||||||
|
|
@ -67,14 +58,6 @@ public class MyTestType {}";
|
||||||
this should fail";
|
this should fail";
|
||||||
var applicationEnvironment = PlatformServices.Default.Application;
|
var applicationEnvironment = PlatformServices.Default.Application;
|
||||||
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
||||||
|
|
||||||
var compilerOptionsProvider = new Mock<ICompilerOptionsProvider>();
|
|
||||||
compilerOptionsProvider
|
|
||||||
.Setup(p => p.GetCompilerOptions(
|
|
||||||
applicationEnvironment.ApplicationName,
|
|
||||||
applicationEnvironment.RuntimeFramework,
|
|
||||||
ConfigurationName))
|
|
||||||
.Returns(new CompilerOptions());
|
|
||||||
var mvcRazorHost = Mock.Of<IMvcRazorHost>();
|
var mvcRazorHost = Mock.Of<IMvcRazorHost>();
|
||||||
var fileProvider = new TestFileProvider();
|
var fileProvider = new TestFileProvider();
|
||||||
var fileInfo = fileProvider.AddFile(viewPath, fileContent);
|
var fileInfo = fileProvider.AddFile(viewPath, fileContent);
|
||||||
|
|
@ -82,7 +65,6 @@ this should fail";
|
||||||
var compilationService = new RoslynCompilationService(
|
var compilationService = new RoslynCompilationService(
|
||||||
applicationEnvironment,
|
applicationEnvironment,
|
||||||
libraryExporter,
|
libraryExporter,
|
||||||
compilerOptionsProvider.Object,
|
|
||||||
mvcRazorHost,
|
mvcRazorHost,
|
||||||
GetOptions(fileProvider));
|
GetOptions(fileProvider));
|
||||||
var relativeFileInfo = new RelativeFileInfo(fileInfo, "some-relative-path");
|
var relativeFileInfo = new RelativeFileInfo(fileInfo, "some-relative-path");
|
||||||
|
|
@ -106,20 +88,11 @@ this should fail";
|
||||||
var content = @"this should fail";
|
var content = @"this should fail";
|
||||||
var applicationEnvironment = PlatformServices.Default.Application;
|
var applicationEnvironment = PlatformServices.Default.Application;
|
||||||
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
||||||
|
|
||||||
var compilerOptionsProvider = new Mock<ICompilerOptionsProvider>();
|
|
||||||
compilerOptionsProvider
|
|
||||||
.Setup(p => p.GetCompilerOptions(
|
|
||||||
applicationEnvironment.ApplicationName,
|
|
||||||
applicationEnvironment.RuntimeFramework,
|
|
||||||
ConfigurationName))
|
|
||||||
.Returns(new CompilerOptions());
|
|
||||||
var mvcRazorHost = Mock.Of<IMvcRazorHost>();
|
var mvcRazorHost = Mock.Of<IMvcRazorHost>();
|
||||||
|
|
||||||
var compilationService = new RoslynCompilationService(
|
var compilationService = new RoslynCompilationService(
|
||||||
applicationEnvironment,
|
applicationEnvironment,
|
||||||
libraryExporter,
|
libraryExporter,
|
||||||
compilerOptionsProvider.Object,
|
|
||||||
mvcRazorHost,
|
mvcRazorHost,
|
||||||
GetOptions());
|
GetOptions());
|
||||||
var relativeFileInfo = new RelativeFileInfo(
|
var relativeFileInfo = new RelativeFileInfo(
|
||||||
|
|
@ -148,14 +121,6 @@ this should fail";
|
||||||
this should fail";
|
this should fail";
|
||||||
var applicationEnvironment = PlatformServices.Default.Application;
|
var applicationEnvironment = PlatformServices.Default.Application;
|
||||||
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
||||||
|
|
||||||
var compilerOptionsProvider = new Mock<ICompilerOptionsProvider>();
|
|
||||||
compilerOptionsProvider
|
|
||||||
.Setup(p => p.GetCompilerOptions(
|
|
||||||
applicationEnvironment.ApplicationName,
|
|
||||||
applicationEnvironment.RuntimeFramework,
|
|
||||||
ConfigurationName))
|
|
||||||
.Returns(new CompilerOptions());
|
|
||||||
var mvcRazorHost = Mock.Of<IMvcRazorHost>();
|
var mvcRazorHost = Mock.Of<IMvcRazorHost>();
|
||||||
|
|
||||||
var mockFileInfo = new Mock<IFileInfo>();
|
var mockFileInfo = new Mock<IFileInfo>();
|
||||||
|
|
@ -167,7 +132,6 @@ this should fail";
|
||||||
var compilationService = new RoslynCompilationService(
|
var compilationService = new RoslynCompilationService(
|
||||||
applicationEnvironment,
|
applicationEnvironment,
|
||||||
libraryExporter,
|
libraryExporter,
|
||||||
compilerOptionsProvider.Object,
|
|
||||||
mvcRazorHost,
|
mvcRazorHost,
|
||||||
GetOptions(fileProvider));
|
GetOptions(fileProvider));
|
||||||
|
|
||||||
|
|
@ -197,24 +161,18 @@ public class MyNonCustomDefinedClass {}
|
||||||
";
|
";
|
||||||
var applicationEnvironment = PlatformServices.Default.Application;
|
var applicationEnvironment = PlatformServices.Default.Application;
|
||||||
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
||||||
|
|
||||||
var compilerOptionsProvider = new Mock<ICompilerOptionsProvider>();
|
|
||||||
compilerOptionsProvider
|
|
||||||
.Setup(p => p.GetCompilerOptions(
|
|
||||||
applicationEnvironment.ApplicationName,
|
|
||||||
applicationEnvironment.RuntimeFramework,
|
|
||||||
ConfigurationName))
|
|
||||||
.Returns(new CompilerOptions { Defines = new[] { "MY_CUSTOM_DEFINE" } });
|
|
||||||
var mvcRazorHost = new Mock<IMvcRazorHost>();
|
var mvcRazorHost = new Mock<IMvcRazorHost>();
|
||||||
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
||||||
.Returns("My");
|
.Returns("My");
|
||||||
|
|
||||||
|
var options = GetOptions();
|
||||||
|
options.Value.ParseOptions = options.Value.ParseOptions.WithPreprocessorSymbols("MY_CUSTOM_DEFINE");
|
||||||
|
|
||||||
var compilationService = new RoslynCompilationService(
|
var compilationService = new RoslynCompilationService(
|
||||||
applicationEnvironment,
|
applicationEnvironment,
|
||||||
libraryExporter,
|
libraryExporter,
|
||||||
compilerOptionsProvider.Object,
|
|
||||||
mvcRazorHost.Object,
|
mvcRazorHost.Object,
|
||||||
GetOptions());
|
options);
|
||||||
var relativeFileInfo = new RelativeFileInfo(
|
var relativeFileInfo = new RelativeFileInfo(
|
||||||
new TestFileInfo { PhysicalPath = "SomePath" },
|
new TestFileInfo { PhysicalPath = "SomePath" },
|
||||||
"some-relative-path");
|
"some-relative-path");
|
||||||
|
|
@ -236,14 +194,6 @@ public class RazorPrefixType {}
|
||||||
public class NotRazorPrefixType {}";
|
public class NotRazorPrefixType {}";
|
||||||
var applicationEnvironment = PlatformServices.Default.Application;
|
var applicationEnvironment = PlatformServices.Default.Application;
|
||||||
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
||||||
|
|
||||||
var compilerOptionsProvider = new Mock<ICompilerOptionsProvider>();
|
|
||||||
compilerOptionsProvider
|
|
||||||
.Setup(p => p.GetCompilerOptions(
|
|
||||||
applicationEnvironment.ApplicationName,
|
|
||||||
applicationEnvironment.RuntimeFramework,
|
|
||||||
ConfigurationName))
|
|
||||||
.Returns(new CompilerOptions());
|
|
||||||
var mvcRazorHost = new Mock<IMvcRazorHost>();
|
var mvcRazorHost = new Mock<IMvcRazorHost>();
|
||||||
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
||||||
.Returns("RazorPrefix");
|
.Returns("RazorPrefix");
|
||||||
|
|
@ -251,7 +201,6 @@ public class NotRazorPrefixType {}";
|
||||||
var compilationService = new RoslynCompilationService(
|
var compilationService = new RoslynCompilationService(
|
||||||
applicationEnvironment,
|
applicationEnvironment,
|
||||||
libraryExporter,
|
libraryExporter,
|
||||||
compilerOptionsProvider.Object,
|
|
||||||
mvcRazorHost.Object,
|
mvcRazorHost.Object,
|
||||||
GetOptions());
|
GetOptions());
|
||||||
|
|
||||||
|
|
@ -284,7 +233,6 @@ public class NotRazorPrefixType {}";
|
||||||
var compilationService = new RoslynCompilationService(
|
var compilationService = new RoslynCompilationService(
|
||||||
PlatformServices.Default.Application,
|
PlatformServices.Default.Application,
|
||||||
CompilationServices.Default.LibraryExporter,
|
CompilationServices.Default.LibraryExporter,
|
||||||
Mock.Of<ICompilerOptionsProvider>(),
|
|
||||||
Mock.Of<IMvcRazorHost>(),
|
Mock.Of<IMvcRazorHost>(),
|
||||||
options.Object);
|
options.Object);
|
||||||
|
|
||||||
|
|
@ -368,15 +316,6 @@ public class NotRazorPrefixType {}";
|
||||||
var content = "public class MyTestType {}";
|
var content = "public class MyTestType {}";
|
||||||
var applicationEnvironment = PlatformServices.Default.Application;
|
var applicationEnvironment = PlatformServices.Default.Application;
|
||||||
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
||||||
|
|
||||||
var compilerOptionsProvider = new Mock<ICompilerOptionsProvider>();
|
|
||||||
compilerOptionsProvider
|
|
||||||
.Setup(p => p.GetCompilerOptions(
|
|
||||||
applicationEnvironment.ApplicationName,
|
|
||||||
applicationEnvironment.RuntimeFramework,
|
|
||||||
ConfigurationName))
|
|
||||||
.Returns(new CompilerOptions());
|
|
||||||
|
|
||||||
RoslynCompilationContext usedCompilation = null;
|
RoslynCompilationContext usedCompilation = null;
|
||||||
var mvcRazorHost = new Mock<IMvcRazorHost>();
|
var mvcRazorHost = new Mock<IMvcRazorHost>();
|
||||||
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
||||||
|
|
@ -385,7 +324,6 @@ public class NotRazorPrefixType {}";
|
||||||
var compilationService = new RoslynCompilationService(
|
var compilationService = new RoslynCompilationService(
|
||||||
applicationEnvironment,
|
applicationEnvironment,
|
||||||
libraryExporter,
|
libraryExporter,
|
||||||
compilerOptionsProvider.Object,
|
|
||||||
mvcRazorHost.Object,
|
mvcRazorHost.Object,
|
||||||
GetOptions(callback: c => usedCompilation = c));
|
GetOptions(callback: c => usedCompilation = c));
|
||||||
|
|
||||||
|
|
@ -418,7 +356,6 @@ public class NotRazorPrefixType {}";
|
||||||
var razorViewEngineOptions = new RazorViewEngineOptions
|
var razorViewEngineOptions = new RazorViewEngineOptions
|
||||||
{
|
{
|
||||||
FileProvider = fileProvider ?? new TestFileProvider(),
|
FileProvider = fileProvider ?? new TestFileProvider(),
|
||||||
Configuration = ConfigurationName,
|
|
||||||
CompilationCallback = callback ?? (c => { })
|
CompilationCallback = callback ?? (c => { })
|
||||||
};
|
};
|
||||||
var options = new Mock<IOptions<RazorViewEngineOptions>>();
|
var options = new Mock<IOptions<RazorViewEngineOptions>>();
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ using System.IO;
|
||||||
using Microsoft.AspNet.FileProviders;
|
using Microsoft.AspNet.FileProviders;
|
||||||
using Microsoft.AspNet.Hosting;
|
using Microsoft.AspNet.Hosting;
|
||||||
using Microsoft.AspNet.Mvc.Razor;
|
using Microsoft.AspNet.Mvc.Razor;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
using Microsoft.Extensions.PlatformAbstractions;
|
using Microsoft.Extensions.PlatformAbstractions;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
@ -35,10 +36,10 @@ namespace Microsoft.AspNet.Mvc
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("Development", "Debug")]
|
[InlineData("Development", "DEBUG")]
|
||||||
[InlineData("Staging", "Release")]
|
[InlineData("Staging", "RELEASE")]
|
||||||
[InlineData("Production", "Release")]
|
[InlineData("Production", "RELEASE")]
|
||||||
public void RazorViewEngineOptionsSetup_SetsCorrectConfiguration(string environment, string expectedConfiguration)
|
public void RazorViewEngineOptionsSetup_SetsPreprocessorSymbols(string environment, string expectedConfiguration)
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var options = new RazorViewEngineOptions();
|
var options = new RazorViewEngineOptions();
|
||||||
|
|
@ -54,7 +55,30 @@ namespace Microsoft.AspNet.Mvc
|
||||||
optionsSetup.Configure(options);
|
optionsSetup.Configure(options);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(expectedConfiguration, options.Configuration);
|
Assert.Equal(new[] { expectedConfiguration }, options.ParseOptions.PreprocessorSymbolNames);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("Development", OptimizationLevel.Debug)]
|
||||||
|
[InlineData("Staging", OptimizationLevel.Release)]
|
||||||
|
[InlineData("Production", OptimizationLevel.Release)]
|
||||||
|
public void RazorViewEngineOptionsSetup_SetsOptimizationLevel(string environment, OptimizationLevel expectedOptimizationLevel)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var options = new RazorViewEngineOptions();
|
||||||
|
var appEnv = new Mock<IApplicationEnvironment>();
|
||||||
|
appEnv.SetupGet(e => e.ApplicationBasePath)
|
||||||
|
.Returns(Directory.GetCurrentDirectory());
|
||||||
|
var hostingEnv = new Mock<IHostingEnvironment>();
|
||||||
|
hostingEnv.SetupGet(e => e.EnvironmentName)
|
||||||
|
.Returns(environment);
|
||||||
|
var optionsSetup = new RazorViewEngineOptionsSetup(appEnv.Object, hostingEnv.Object);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
optionsSetup.Configure(options);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(expectedOptimizationLevel, options.CompilationOptions.OptimizationLevel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -19,6 +19,11 @@ namespace RazorWebSite
|
||||||
.AddRazorOptions(options =>
|
.AddRazorOptions(options =>
|
||||||
{
|
{
|
||||||
options.ViewLocationExpanders.Add(new NonMainPageViewLocationExpander());
|
options.ViewLocationExpanders.Add(new NonMainPageViewLocationExpander());
|
||||||
|
#if DNX451
|
||||||
|
options.ParseOptions = options.ParseOptions.WithPreprocessorSymbols("DNX451", "DNX451_CUSTOM_DEFINE");
|
||||||
|
#else
|
||||||
|
options.ParseOptions = options.ParseOptions.WithPreprocessorSymbols("DNXCORE50", "DNXCORE50_CUSTOM_DEFINE");
|
||||||
|
#endif
|
||||||
})
|
})
|
||||||
.AddViewOptions(options =>
|
.AddViewOptions(options =>
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue