React to dnx confguration change

This commit is contained in:
Pavel Krymets 2015-11-24 10:21:01 -08:00
parent 1c34d88f4c
commit 5ef14e95b7
8 changed files with 95 additions and 38 deletions

View File

@ -23,10 +23,14 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
/// <param name="applicationEnvironment"> /// <param name="applicationEnvironment">
/// The <see cref="IApplicationEnvironment"/> for the executing application. /// The <see cref="IApplicationEnvironment"/> for the executing application.
/// </param> /// </param>
/// <param name="configuration">
/// The configuration name to use for compilation.
/// </param>
/// <returns>The <see cref="CompilationSettings"/> for the current application.</returns> /// <returns>The <see cref="CompilationSettings"/> for the current application.</returns>
public static CompilationSettings GetCompilationSettings( public static CompilationSettings GetCompilationSettings(
this ICompilerOptionsProvider compilerOptionsProvider, this ICompilerOptionsProvider compilerOptionsProvider,
IApplicationEnvironment applicationEnvironment) IApplicationEnvironment applicationEnvironment,
string configuration)
{ {
if (compilerOptionsProvider == null) if (compilerOptionsProvider == null)
{ {
@ -38,10 +42,16 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
throw new ArgumentNullException(nameof(applicationEnvironment)); throw new ArgumentNullException(nameof(applicationEnvironment));
} }
return compilerOptionsProvider.GetCompilerOptions(applicationEnvironment.ApplicationName, if (string.IsNullOrEmpty(configuration))
applicationEnvironment.RuntimeFramework, {
applicationEnvironment.Configuration) throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(configuration));
.ToCompilationSettings(applicationEnvironment.RuntimeFramework, applicationEnvironment.ApplicationBasePath); }
return compilerOptionsProvider.GetCompilerOptions(
applicationEnvironment.ApplicationName,
applicationEnvironment.RuntimeFramework,
configuration)
.ToCompilationSettings(applicationEnvironment.RuntimeFramework, applicationEnvironment.ApplicationBasePath);
} }
} }
} }

View File

@ -39,6 +39,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
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;
#if DOTNET5_5 #if DOTNET5_5
private readonly RazorLoadContext _razorLoadContext; private readonly RazorLoadContext _razorLoadContext;
@ -69,6 +70,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
_compilerOptionsProvider = compilerOptionsProvider; _compilerOptionsProvider = compilerOptionsProvider;
_fileProvider = optionsAccessor.Value.FileProvider; _fileProvider = optionsAccessor.Value.FileProvider;
_classPrefix = host.MainClassNamePrefix; _classPrefix = host.MainClassNamePrefix;
_configuration = optionsAccessor.Value.Configuration;
#if DOTNET5_5 #if DOTNET5_5
_razorLoadContext = new RazorLoadContext(); _razorLoadContext = new RazorLoadContext();
@ -89,7 +91,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
} }
var assemblyName = Path.GetRandomFileName(); var assemblyName = Path.GetRandomFileName();
var compilationSettings = _compilerOptionsProvider.GetCompilationSettings(_environment); var compilationSettings = _compilerOptionsProvider.GetCompilationSettings(_environment, _configuration);
var syntaxTree = SyntaxTreeGenerator.Generate( var syntaxTree = SyntaxTreeGenerator.Generate(
compilationContent, compilationContent,
assemblyName, assemblyName,

View File

@ -14,6 +14,8 @@ namespace Microsoft.AspNet.Mvc.Razor
{ {
private IFileProvider _fileProvider; private IFileProvider _fileProvider;
private string _configuration;
/// <summary> /// <summary>
/// Get a <see cref="IList{IViewLocationExpander}"/> used by the <see cref="RazorViewEngine"/>. /// Get a <see cref="IList{IViewLocationExpander}"/> used by the <see cref="RazorViewEngine"/>.
/// </summary> /// </summary>
@ -42,5 +44,26 @@ namespace Microsoft.AspNet.Mvc.Razor
_fileProvider = value; _fileProvider = value;
} }
} }
/// <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;
}
}
} }
} }

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.FileProviders; using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Mvc.Razor; using Microsoft.AspNet.Mvc.Razor;
using Microsoft.Extensions.OptionsModel; using Microsoft.Extensions.OptionsModel;
using Microsoft.Extensions.PlatformAbstractions; using Microsoft.Extensions.PlatformAbstractions;
@ -17,16 +18,26 @@ namespace Microsoft.AspNet.Mvc
/// Initializes a new instance of <see cref="RazorViewEngineOptions"/>. /// Initializes a new instance of <see cref="RazorViewEngineOptions"/>.
/// </summary> /// </summary>
/// <param name="applicationEnvironment"><see cref="IApplicationEnvironment"/> for the application.</param> /// <param name="applicationEnvironment"><see cref="IApplicationEnvironment"/> for the application.</param>
public RazorViewEngineOptionsSetup(IApplicationEnvironment applicationEnvironment) /// <param name="hostingEnvironment"><see cref="IHostingEnvironment"/> for the application.</param>
: base(options => ConfigureRazor(options, applicationEnvironment)) public RazorViewEngineOptionsSetup(IApplicationEnvironment applicationEnvironment, IHostingEnvironment hostingEnvironment)
: base(options => ConfigureRazor(options, applicationEnvironment, hostingEnvironment))
{ {
} }
private static void ConfigureRazor( private static void ConfigureRazor(
RazorViewEngineOptions razorOptions, RazorViewEngineOptions razorOptions,
IApplicationEnvironment applicationEnvironment) IApplicationEnvironment applicationEnvironment,
IHostingEnvironment hostingEnvironment)
{ {
razorOptions.FileProvider = new PhysicalFileProvider(applicationEnvironment.ApplicationBasePath); razorOptions.FileProvider = new PhysicalFileProvider(applicationEnvironment.ApplicationBasePath);
if (hostingEnvironment.IsDevelopment())
{
razorOptions.Configuration = "Debug";
}
else
{
razorOptions.Configuration = "Release";
}
} }
} }
} }

View File

@ -33,14 +33,6 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
public string ApplicationBasePath { get; } public string ApplicationBasePath { get; }
public string Configuration
{
get
{
return _original.Configuration;
}
}
public FrameworkName RuntimeFramework public FrameworkName RuntimeFramework
{ {
get get
@ -48,15 +40,5 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
return _original.RuntimeFramework; return _original.RuntimeFramework;
} }
} }
public object GetData(string name)
{
return _original.GetData(name);
}
public void SetData(string name, object value)
{
_original.SetData(name, value);
}
} }
} }

View File

@ -19,6 +19,8 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
{ {
public class RoslynCompilationServiceTest public class RoslynCompilationServiceTest
{ {
private const string ConfigurationName = "Release";
[Fact] [Fact]
public void Compile_ReturnsCompilationResult() public void Compile_ReturnsCompilationResult()
{ {
@ -33,7 +35,7 @@ public class MyTestType {}";
.Setup(p => p.GetCompilerOptions( .Setup(p => p.GetCompilerOptions(
applicationEnvironment.ApplicationName, applicationEnvironment.ApplicationName,
applicationEnvironment.RuntimeFramework, applicationEnvironment.RuntimeFramework,
applicationEnvironment.Configuration)) ConfigurationName))
.Returns(new CompilerOptions()); .Returns(new CompilerOptions());
var mvcRazorHost = new Mock<IMvcRazorHost>(); var mvcRazorHost = new Mock<IMvcRazorHost>();
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix) mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
@ -73,7 +75,7 @@ this should fail";
.Setup(p => p.GetCompilerOptions( .Setup(p => p.GetCompilerOptions(
applicationEnvironment.ApplicationName, applicationEnvironment.ApplicationName,
applicationEnvironment.RuntimeFramework, applicationEnvironment.RuntimeFramework,
applicationEnvironment.Configuration)) ConfigurationName))
.Returns(new CompilerOptions()); .Returns(new CompilerOptions());
var mvcRazorHost = Mock.Of<IMvcRazorHost>(); var mvcRazorHost = Mock.Of<IMvcRazorHost>();
var fileProvider = new TestFileProvider(); var fileProvider = new TestFileProvider();
@ -112,7 +114,7 @@ this should fail";
.Setup(p => p.GetCompilerOptions( .Setup(p => p.GetCompilerOptions(
applicationEnvironment.ApplicationName, applicationEnvironment.ApplicationName,
applicationEnvironment.RuntimeFramework, applicationEnvironment.RuntimeFramework,
applicationEnvironment.Configuration)) ConfigurationName))
.Returns(new CompilerOptions()); .Returns(new CompilerOptions());
var mvcRazorHost = Mock.Of<IMvcRazorHost>(); var mvcRazorHost = Mock.Of<IMvcRazorHost>();
@ -154,7 +156,7 @@ this should fail";
.Setup(p => p.GetCompilerOptions( .Setup(p => p.GetCompilerOptions(
applicationEnvironment.ApplicationName, applicationEnvironment.ApplicationName,
applicationEnvironment.RuntimeFramework, applicationEnvironment.RuntimeFramework,
applicationEnvironment.Configuration)) ConfigurationName))
.Returns(new CompilerOptions()); .Returns(new CompilerOptions());
var mvcRazorHost = Mock.Of<IMvcRazorHost>(); var mvcRazorHost = Mock.Of<IMvcRazorHost>();
@ -203,7 +205,7 @@ public class MyNonCustomDefinedClass {}
.Setup(p => p.GetCompilerOptions( .Setup(p => p.GetCompilerOptions(
applicationEnvironment.ApplicationName, applicationEnvironment.ApplicationName,
applicationEnvironment.RuntimeFramework, applicationEnvironment.RuntimeFramework,
applicationEnvironment.Configuration)) ConfigurationName))
.Returns(new CompilerOptions { Defines = new[] { "MY_CUSTOM_DEFINE" } }); .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)
@ -242,7 +244,7 @@ public class NotRazorPrefixType {}";
.Setup(p => p.GetCompilerOptions( .Setup(p => p.GetCompilerOptions(
applicationEnvironment.ApplicationName, applicationEnvironment.ApplicationName,
applicationEnvironment.RuntimeFramework, applicationEnvironment.RuntimeFramework,
applicationEnvironment.Configuration)) ConfigurationName))
.Returns(new CompilerOptions()); .Returns(new CompilerOptions());
var mvcRazorHost = new Mock<IMvcRazorHost>(); var mvcRazorHost = new Mock<IMvcRazorHost>();
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix) mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
@ -397,9 +399,6 @@ public class NotRazorPrefixType {}";
applicationEnvironment applicationEnvironment
.SetupGet(a => a.RuntimeFramework) .SetupGet(a => a.RuntimeFramework)
.Returns(new FrameworkName("ASPNET", new Version(5, 0))); .Returns(new FrameworkName("ASPNET", new Version(5, 0)));
applicationEnvironment
.SetupGet(a => a.Configuration)
.Returns("Debug");
applicationEnvironment applicationEnvironment
.SetupGet(a => a.ApplicationBasePath) .SetupGet(a => a.ApplicationBasePath)
.Returns("MyBasePath"); .Returns("MyBasePath");
@ -411,7 +410,8 @@ public class NotRazorPrefixType {}";
{ {
var razorViewEngineOptions = new RazorViewEngineOptions var razorViewEngineOptions = new RazorViewEngineOptions
{ {
FileProvider = fileProvider ?? new TestFileProvider() FileProvider = fileProvider ?? new TestFileProvider(),
Configuration = ConfigurationName
}; };
var options = new Mock<IOptions<RazorViewEngineOptions>>(); var options = new Mock<IOptions<RazorViewEngineOptions>>();
options options

View File

@ -5,6 +5,7 @@ using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Xml.Linq; using System.Xml.Linq;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Mvc.Formatters; using Microsoft.AspNet.Mvc.Formatters;
using Microsoft.AspNet.Mvc.Internal; using Microsoft.AspNet.Mvc.Internal;
using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.ModelBinding;
@ -237,6 +238,7 @@ namespace Microsoft.AspNet.Mvc
serviceCollection.AddSingleton(Mock.Of<ILibraryExporter>()); serviceCollection.AddSingleton(Mock.Of<ILibraryExporter>());
serviceCollection.AddSingleton(Mock.Of<ICompilerOptionsProvider>()); serviceCollection.AddSingleton(Mock.Of<ICompilerOptionsProvider>());
serviceCollection.AddSingleton(Mock.Of<IAssemblyLoadContextAccessor>()); serviceCollection.AddSingleton(Mock.Of<IAssemblyLoadContextAccessor>());
serviceCollection.AddSingleton(Mock.Of<IHostingEnvironment>());
var applicationEnvironment = new Mock<IApplicationEnvironment>(); var applicationEnvironment = new Mock<IApplicationEnvironment>();
// ApplicationBasePath is used to set up a PhysicalFileProvider which requires // ApplicationBasePath is used to set up a PhysicalFileProvider which requires

View File

@ -3,6 +3,7 @@
using System.IO; using System.IO;
using Microsoft.AspNet.FileProviders; using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Mvc.Razor; using Microsoft.AspNet.Mvc.Razor;
using Microsoft.Extensions.PlatformAbstractions; using Microsoft.Extensions.PlatformAbstractions;
using Moq; using Moq;
@ -20,7 +21,10 @@ namespace Microsoft.AspNet.Mvc
var appEnv = new Mock<IApplicationEnvironment>(); var appEnv = new Mock<IApplicationEnvironment>();
appEnv.SetupGet(e => e.ApplicationBasePath) appEnv.SetupGet(e => e.ApplicationBasePath)
.Returns(Directory.GetCurrentDirectory()); .Returns(Directory.GetCurrentDirectory());
var optionsSetup = new RazorViewEngineOptionsSetup(appEnv.Object); var hostingEnv = new Mock<IHostingEnvironment>();
hostingEnv.SetupGet(e => e.EnvironmentName)
.Returns("Development");
var optionsSetup = new RazorViewEngineOptionsSetup(appEnv.Object, hostingEnv.Object);
// Act // Act
optionsSetup.Configure(options); optionsSetup.Configure(options);
@ -29,5 +33,28 @@ namespace Microsoft.AspNet.Mvc
Assert.NotNull(options.FileProvider); Assert.NotNull(options.FileProvider);
Assert.IsType<PhysicalFileProvider>(options.FileProvider); Assert.IsType<PhysicalFileProvider>(options.FileProvider);
} }
[Theory]
[InlineData("Development", "Debug")]
[InlineData("Staging", "Release")]
[InlineData("Production", "Release")]
public void RazorViewEngineOptionsSetup_SetsCorrectConfiguration(string environment, string expectedConfiguration)
{
// 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(expectedConfiguration, options.Configuration);
}
} }
} }