This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register DependencyContextRazorViewEngineOptionsSetup after RazorView…
…EngineOptionsSetup Fixes #4902
- Loading branch information
Showing
6 changed files
with
367 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,34 +10,54 @@ | |
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.Extensions.DependencyModel; | ||
using Microsoft.Extensions.Options; | ||
using DependencyContextOptions = Microsoft.Extensions.DependencyModel.CompilationOptions; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Razor.Internal | ||
{ | ||
/// <summary> | ||
/// Sets up compilation and parse option default options for <see cref="RazorViewEngineOptions"/> using | ||
/// <see cref="DependencyContext"/> | ||
/// </summary> | ||
public class DependencyContextRazorViewEngineOptionsSetup : ConfigureOptions<RazorViewEngineOptions> | ||
public class DependencyContextRazorViewEngineOptionsSetup : IConfigureOptions<RazorViewEngineOptions> | ||
{ | ||
private readonly IHostingEnvironment _hostingEnvironment; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of <see cref="DependencyContextRazorViewEngineOptionsSetup"/>. | ||
/// </summary> | ||
public DependencyContextRazorViewEngineOptionsSetup(IHostingEnvironment hostingEnvironment) | ||
: base(options => ConfigureRazor(options, hostingEnvironment)) | ||
{ | ||
_hostingEnvironment = hostingEnvironment; | ||
} | ||
|
||
private static void ConfigureRazor(RazorViewEngineOptions options, IHostingEnvironment hostingEnvironment) | ||
/// <inheritdoc /> | ||
public void Configure(RazorViewEngineOptions options) | ||
{ | ||
var applicationAssembly = Assembly.Load(new AssemblyName(hostingEnvironment.ApplicationName)); | ||
var dependencyContext = DependencyContext.Load(applicationAssembly); | ||
var compilationOptions = dependencyContext?.CompilationOptions ?? Extensions.DependencyModel.CompilationOptions.Default; | ||
var compilationOptions = GetCompilationOptions(); | ||
|
||
SetParseOptions(options, compilationOptions); | ||
SetCompilationOptions(options, compilationOptions); | ||
} | ||
|
||
private static void SetCompilationOptions(RazorViewEngineOptions options, Extensions.DependencyModel.CompilationOptions compilationOptions) | ||
// Internal for unit testing. | ||
protected internal virtual DependencyContextOptions GetCompilationOptions() | ||
{ | ||
if (!string.IsNullOrEmpty(_hostingEnvironment.ApplicationName)) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
pranavkm
Author
Contributor
|
||
{ | ||
var applicationAssembly = Assembly.Load(new AssemblyName(_hostingEnvironment.ApplicationName)); | ||
var dependencyContext = DependencyContext.Load(applicationAssembly); | ||
if (dependencyContext?.CompilationOptions != null) | ||
{ | ||
return dependencyContext.CompilationOptions; | ||
} | ||
} | ||
|
||
return DependencyContextOptions.Default; | ||
} | ||
|
||
private static void SetCompilationOptions( | ||
RazorViewEngineOptions options, | ||
DependencyContextOptions compilationOptions) | ||
{ | ||
var roslynOptions = options.CompilationOptions; | ||
|
||
|
@@ -57,17 +77,17 @@ private static void SetCompilationOptions(RazorViewEngineOptions options, Extens | |
|
||
if (compilationOptions.Optimize.HasValue) | ||
{ | ||
var optimizationLevel = compilationOptions.Optimize.Value | ||
? OptimizationLevel.Debug | ||
: OptimizationLevel.Release; | ||
var optimizationLevel = compilationOptions.Optimize.Value ? | ||
OptimizationLevel.Release : | ||
OptimizationLevel.Debug; | ||
roslynOptions = roslynOptions.WithOptimizationLevel(optimizationLevel); | ||
} | ||
|
||
if (compilationOptions.WarningsAsErrors.HasValue) | ||
{ | ||
var reportDiagnostic = compilationOptions.WarningsAsErrors.Value | ||
? ReportDiagnostic.Error | ||
: ReportDiagnostic.Default; | ||
var reportDiagnostic = compilationOptions.WarningsAsErrors.Value ? | ||
ReportDiagnostic.Error : | ||
ReportDiagnostic.Default; | ||
roslynOptions = roslynOptions.WithGeneralDiagnosticOption(reportDiagnostic); | ||
} | ||
|
||
|
@@ -76,7 +96,7 @@ private static void SetCompilationOptions(RazorViewEngineOptions options, Extens | |
|
||
private static void SetParseOptions( | ||
RazorViewEngineOptions options, | ||
Extensions.DependencyModel.CompilationOptions compilationOptions) | ||
DependencyContextOptions compilationOptions) | ||
{ | ||
var parseOptions = options.ParseOptions; | ||
parseOptions = parseOptions.WithPreprocessorSymbols( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
When is this null?