React to dnx confguration change
This commit is contained in:
parent
1c34d88f4c
commit
5ef14e95b7
|
|
@ -23,10 +23,14 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
/// <param name="applicationEnvironment">
|
||||
/// The <see cref="IApplicationEnvironment"/> for the executing application.
|
||||
/// </param>
|
||||
/// <param name="configuration">
|
||||
/// The configuration name to use for compilation.
|
||||
/// </param>
|
||||
/// <returns>The <see cref="CompilationSettings"/> for the current application.</returns>
|
||||
public static CompilationSettings GetCompilationSettings(
|
||||
this ICompilerOptionsProvider compilerOptionsProvider,
|
||||
IApplicationEnvironment applicationEnvironment)
|
||||
IApplicationEnvironment applicationEnvironment,
|
||||
string configuration)
|
||||
{
|
||||
if (compilerOptionsProvider == null)
|
||||
{
|
||||
|
|
@ -38,10 +42,16 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
throw new ArgumentNullException(nameof(applicationEnvironment));
|
||||
}
|
||||
|
||||
return compilerOptionsProvider.GetCompilerOptions(applicationEnvironment.ApplicationName,
|
||||
applicationEnvironment.RuntimeFramework,
|
||||
applicationEnvironment.Configuration)
|
||||
.ToCompilationSettings(applicationEnvironment.RuntimeFramework, applicationEnvironment.ApplicationBasePath);
|
||||
if (string.IsNullOrEmpty(configuration))
|
||||
{
|
||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(configuration));
|
||||
}
|
||||
|
||||
return compilerOptionsProvider.GetCompilerOptions(
|
||||
applicationEnvironment.ApplicationName,
|
||||
applicationEnvironment.RuntimeFramework,
|
||||
configuration)
|
||||
.ToCompilationSettings(applicationEnvironment.RuntimeFramework, applicationEnvironment.ApplicationBasePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -39,6 +39,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
private readonly IFileProvider _fileProvider;
|
||||
private readonly Lazy<List<MetadataReference>> _applicationReferences;
|
||||
private readonly string _classPrefix;
|
||||
private readonly string _configuration;
|
||||
|
||||
#if DOTNET5_5
|
||||
private readonly RazorLoadContext _razorLoadContext;
|
||||
|
|
@ -69,6 +70,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
_compilerOptionsProvider = compilerOptionsProvider;
|
||||
_fileProvider = optionsAccessor.Value.FileProvider;
|
||||
_classPrefix = host.MainClassNamePrefix;
|
||||
_configuration = optionsAccessor.Value.Configuration;
|
||||
|
||||
#if DOTNET5_5
|
||||
_razorLoadContext = new RazorLoadContext();
|
||||
|
|
@ -89,7 +91,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
}
|
||||
|
||||
var assemblyName = Path.GetRandomFileName();
|
||||
var compilationSettings = _compilerOptionsProvider.GetCompilationSettings(_environment);
|
||||
var compilationSettings = _compilerOptionsProvider.GetCompilationSettings(_environment, _configuration);
|
||||
var syntaxTree = SyntaxTreeGenerator.Generate(
|
||||
compilationContent,
|
||||
assemblyName,
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
{
|
||||
private IFileProvider _fileProvider;
|
||||
|
||||
private string _configuration;
|
||||
|
||||
/// <summary>
|
||||
/// Get a <see cref="IList{IViewLocationExpander}"/> used by the <see cref="RazorViewEngine"/>.
|
||||
/// </summary>
|
||||
|
|
@ -42,5 +44,26 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
_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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
// 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.Hosting;
|
||||
using Microsoft.AspNet.Mvc.Razor;
|
||||
using Microsoft.Extensions.OptionsModel;
|
||||
using Microsoft.Extensions.PlatformAbstractions;
|
||||
|
|
@ -17,16 +18,26 @@ namespace Microsoft.AspNet.Mvc
|
|||
/// Initializes a new instance of <see cref="RazorViewEngineOptions"/>.
|
||||
/// </summary>
|
||||
/// <param name="applicationEnvironment"><see cref="IApplicationEnvironment"/> for the application.</param>
|
||||
public RazorViewEngineOptionsSetup(IApplicationEnvironment applicationEnvironment)
|
||||
: base(options => ConfigureRazor(options, applicationEnvironment))
|
||||
/// <param name="hostingEnvironment"><see cref="IHostingEnvironment"/> for the application.</param>
|
||||
public RazorViewEngineOptionsSetup(IApplicationEnvironment applicationEnvironment, IHostingEnvironment hostingEnvironment)
|
||||
: base(options => ConfigureRazor(options, applicationEnvironment, hostingEnvironment))
|
||||
{
|
||||
}
|
||||
|
||||
private static void ConfigureRazor(
|
||||
RazorViewEngineOptions razorOptions,
|
||||
IApplicationEnvironment applicationEnvironment)
|
||||
IApplicationEnvironment applicationEnvironment,
|
||||
IHostingEnvironment hostingEnvironment)
|
||||
{
|
||||
razorOptions.FileProvider = new PhysicalFileProvider(applicationEnvironment.ApplicationBasePath);
|
||||
if (hostingEnvironment.IsDevelopment())
|
||||
{
|
||||
razorOptions.Configuration = "Debug";
|
||||
}
|
||||
else
|
||||
{
|
||||
razorOptions.Configuration = "Release";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -33,14 +33,6 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
|||
|
||||
public string ApplicationBasePath { get; }
|
||||
|
||||
public string Configuration
|
||||
{
|
||||
get
|
||||
{
|
||||
return _original.Configuration;
|
||||
}
|
||||
}
|
||||
|
||||
public FrameworkName RuntimeFramework
|
||||
{
|
||||
get
|
||||
|
|
@ -48,15 +40,5 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
|||
return _original.RuntimeFramework;
|
||||
}
|
||||
}
|
||||
|
||||
public object GetData(string name)
|
||||
{
|
||||
return _original.GetData(name);
|
||||
}
|
||||
|
||||
public void SetData(string name, object value)
|
||||
{
|
||||
_original.SetData(name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,8 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
{
|
||||
public class RoslynCompilationServiceTest
|
||||
{
|
||||
private const string ConfigurationName = "Release";
|
||||
|
||||
[Fact]
|
||||
public void Compile_ReturnsCompilationResult()
|
||||
{
|
||||
|
|
@ -33,7 +35,7 @@ public class MyTestType {}";
|
|||
.Setup(p => p.GetCompilerOptions(
|
||||
applicationEnvironment.ApplicationName,
|
||||
applicationEnvironment.RuntimeFramework,
|
||||
applicationEnvironment.Configuration))
|
||||
ConfigurationName))
|
||||
.Returns(new CompilerOptions());
|
||||
var mvcRazorHost = new Mock<IMvcRazorHost>();
|
||||
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
||||
|
|
@ -73,7 +75,7 @@ this should fail";
|
|||
.Setup(p => p.GetCompilerOptions(
|
||||
applicationEnvironment.ApplicationName,
|
||||
applicationEnvironment.RuntimeFramework,
|
||||
applicationEnvironment.Configuration))
|
||||
ConfigurationName))
|
||||
.Returns(new CompilerOptions());
|
||||
var mvcRazorHost = Mock.Of<IMvcRazorHost>();
|
||||
var fileProvider = new TestFileProvider();
|
||||
|
|
@ -112,7 +114,7 @@ this should fail";
|
|||
.Setup(p => p.GetCompilerOptions(
|
||||
applicationEnvironment.ApplicationName,
|
||||
applicationEnvironment.RuntimeFramework,
|
||||
applicationEnvironment.Configuration))
|
||||
ConfigurationName))
|
||||
.Returns(new CompilerOptions());
|
||||
var mvcRazorHost = Mock.Of<IMvcRazorHost>();
|
||||
|
||||
|
|
@ -154,7 +156,7 @@ this should fail";
|
|||
.Setup(p => p.GetCompilerOptions(
|
||||
applicationEnvironment.ApplicationName,
|
||||
applicationEnvironment.RuntimeFramework,
|
||||
applicationEnvironment.Configuration))
|
||||
ConfigurationName))
|
||||
.Returns(new CompilerOptions());
|
||||
var mvcRazorHost = Mock.Of<IMvcRazorHost>();
|
||||
|
||||
|
|
@ -203,7 +205,7 @@ public class MyNonCustomDefinedClass {}
|
|||
.Setup(p => p.GetCompilerOptions(
|
||||
applicationEnvironment.ApplicationName,
|
||||
applicationEnvironment.RuntimeFramework,
|
||||
applicationEnvironment.Configuration))
|
||||
ConfigurationName))
|
||||
.Returns(new CompilerOptions { Defines = new[] { "MY_CUSTOM_DEFINE" } });
|
||||
var mvcRazorHost = new Mock<IMvcRazorHost>();
|
||||
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
||||
|
|
@ -242,7 +244,7 @@ public class NotRazorPrefixType {}";
|
|||
.Setup(p => p.GetCompilerOptions(
|
||||
applicationEnvironment.ApplicationName,
|
||||
applicationEnvironment.RuntimeFramework,
|
||||
applicationEnvironment.Configuration))
|
||||
ConfigurationName))
|
||||
.Returns(new CompilerOptions());
|
||||
var mvcRazorHost = new Mock<IMvcRazorHost>();
|
||||
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
||||
|
|
@ -397,9 +399,6 @@ public class NotRazorPrefixType {}";
|
|||
applicationEnvironment
|
||||
.SetupGet(a => a.RuntimeFramework)
|
||||
.Returns(new FrameworkName("ASPNET", new Version(5, 0)));
|
||||
applicationEnvironment
|
||||
.SetupGet(a => a.Configuration)
|
||||
.Returns("Debug");
|
||||
applicationEnvironment
|
||||
.SetupGet(a => a.ApplicationBasePath)
|
||||
.Returns("MyBasePath");
|
||||
|
|
@ -411,7 +410,8 @@ public class NotRazorPrefixType {}";
|
|||
{
|
||||
var razorViewEngineOptions = new RazorViewEngineOptions
|
||||
{
|
||||
FileProvider = fileProvider ?? new TestFileProvider()
|
||||
FileProvider = fileProvider ?? new TestFileProvider(),
|
||||
Configuration = ConfigurationName
|
||||
};
|
||||
var options = new Mock<IOptions<RazorViewEngineOptions>>();
|
||||
options
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ using System;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using Microsoft.AspNet.Hosting;
|
||||
using Microsoft.AspNet.Mvc.Formatters;
|
||||
using Microsoft.AspNet.Mvc.Internal;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
|
|
@ -237,6 +238,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
serviceCollection.AddSingleton(Mock.Of<ILibraryExporter>());
|
||||
serviceCollection.AddSingleton(Mock.Of<ICompilerOptionsProvider>());
|
||||
serviceCollection.AddSingleton(Mock.Of<IAssemblyLoadContextAccessor>());
|
||||
serviceCollection.AddSingleton(Mock.Of<IHostingEnvironment>());
|
||||
var applicationEnvironment = new Mock<IApplicationEnvironment>();
|
||||
|
||||
// ApplicationBasePath is used to set up a PhysicalFileProvider which requires
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
using System.IO;
|
||||
using Microsoft.AspNet.FileProviders;
|
||||
using Microsoft.AspNet.Hosting;
|
||||
using Microsoft.AspNet.Mvc.Razor;
|
||||
using Microsoft.Extensions.PlatformAbstractions;
|
||||
using Moq;
|
||||
|
|
@ -20,7 +21,10 @@ namespace Microsoft.AspNet.Mvc
|
|||
var appEnv = new Mock<IApplicationEnvironment>();
|
||||
appEnv.SetupGet(e => e.ApplicationBasePath)
|
||||
.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
|
||||
optionsSetup.Configure(options);
|
||||
|
|
@ -29,5 +33,28 @@ namespace Microsoft.AspNet.Mvc
|
|||
Assert.NotNull(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue