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.
Adding support for Razor precompilation
Fixes #3917
- Loading branch information
Showing
20 changed files
with
502 additions
and
179 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
18 changes: 18 additions & 0 deletions
18
src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/IViewsProvider.cs
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// 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; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ApplicationParts | ||
{ | ||
/// <summary> | ||
/// Exposes a sequence of views associated with an <see cref="ApplicationPart"/> . | ||
/// </summary> | ||
public interface IViewsProvider | ||
{ | ||
/// <summary> | ||
/// Gets the sequence of <see cref="ViewInfo"/>. | ||
/// </summary> | ||
IEnumerable<ViewInfo> Views { get; } | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/ViewInfo.cs
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ApplicationParts | ||
{ | ||
/// <summary> | ||
/// Provides information for precompiled views. | ||
/// </summary> | ||
public class ViewInfo | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of <see cref="ViewInfo" />. | ||
/// </summary> | ||
/// <param name="path">The path of the view.</param> | ||
/// <param name="type">The view <see cref="System.Type"/>.</param> | ||
public ViewInfo(string path, Type type) | ||
{ | ||
Path = path; | ||
Type = type; | ||
} | ||
|
||
/// <summary> | ||
/// The path of the view. | ||
/// </summary> | ||
public string Path { get; } | ||
|
||
/// <summary> | ||
/// The view <see cref="System.Type"/>. | ||
/// </summary> | ||
public Type Type { get; } | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/ViewInfoContainer.cs
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// 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; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ApplicationParts | ||
{ | ||
/// <summary> | ||
/// A container for <see cref="ViewInfo"/> instances. | ||
/// </summary> | ||
public class ViewInfoContainer | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of <see cref="ViewInfos"/>. | ||
/// </summary> | ||
/// <param name="views">The sequence of <see cref="ViewInfo"/>.</param> | ||
public ViewInfoContainer(IReadOnlyList<ViewInfo> views) | ||
{ | ||
ViewInfos = views; | ||
} | ||
|
||
/// <summary> | ||
/// The <see cref="IReadOnlyList{T}"/> of <see cref="ViewInfo"/>. | ||
/// </summary> | ||
public IReadOnlyList<ViewInfo> ViewInfos { get; } | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Microsoft.AspNetCore.Mvc.Core/IDesignTimeMvcBuilderConfiguration.cs
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 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 Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.Mvc | ||
{ | ||
/// <summary> | ||
/// Configures the <see cref="IMvcBuilder"/>. Implement this interface to enable design-time configuration | ||
/// (for instance during pre-compilation of views) of <see cref="IMvcBuilder"/>. | ||
/// </summary> | ||
public interface IDesignTimeMvcBuilderConfiguration | ||
{ | ||
/// <summary> | ||
/// Configures the <see cref="IMvcBuilder"/>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IMvcBuilder"/>.</param> | ||
void ConfigureMvc(IMvcBuilder builder); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewsFeature.cs
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// 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; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Razor.Compilation | ||
{ | ||
public class ViewsFeature | ||
{ | ||
public IDictionary<string, Type> Views { get; } = | ||
new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewsFeatureProvider.cs
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 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.AspNetCore.Mvc.ApplicationParts; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Razor.Compilation | ||
{ | ||
/// <summary> | ||
/// An <see cref="IApplicationFeatureProvider{TFeature}"/> for <see cref="ViewsFeature"/>. | ||
/// </summary> | ||
public class ViewsFeatureProvider : IApplicationFeatureProvider<ViewsFeature> | ||
{ | ||
/// <inheritdoc /> | ||
public void PopulateFeature(IEnumerable<ApplicationPart> parts, ViewsFeature feature) | ||
{ | ||
foreach (var provider in parts.OfType<IViewsProvider>()) | ||
{ | ||
var precompiledViews = provider.Views; | ||
if (precompiledViews != null) | ||
{ | ||
foreach (var viewInfo in precompiledViews) | ||
{ | ||
feature.Views[viewInfo.Path] = viewInfo.Type; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
src/Microsoft.AspNetCore.Mvc.Razor/Internal/CSharpCompiler.cs
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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 Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.Emit; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Razor.Internal | ||
{ | ||
public class CSharpCompiler | ||
{ | ||
private readonly CSharpCompilationOptions _compilationOptions; | ||
private readonly CSharpParseOptions _parseOptions; | ||
private readonly RazorReferenceManager _referenceManager; | ||
private readonly DebugInformationFormat _pdbFormat = | ||
#if NET451 | ||
SymbolsUtility.SupportsFullPdbGeneration() ? | ||
DebugInformationFormat.Pdb : | ||
DebugInformationFormat.PortablePdb; | ||
#else | ||
DebugInformationFormat.PortablePdb; | ||
#endif | ||
|
||
public CSharpCompiler(RazorReferenceManager manager, IOptions<RazorViewEngineOptions> optionsAccessor) | ||
{ | ||
_referenceManager = manager; | ||
_compilationOptions = optionsAccessor.Value.CompilationOptions; | ||
_parseOptions = optionsAccessor.Value.ParseOptions; | ||
EmitOptions = new EmitOptions(debugInformationFormat: _pdbFormat); | ||
} | ||
|
||
public EmitOptions EmitOptions { get; } | ||
|
||
public SyntaxTree CreateSyntaxTree(SourceText sourceText) | ||
{ | ||
return CSharpSyntaxTree.ParseText( | ||
sourceText, | ||
options: _parseOptions); | ||
} | ||
|
||
public CSharpCompilation CreateCompilation(string assemblyName) | ||
{ | ||
return CSharpCompilation.Create( | ||
assemblyName, | ||
options: _compilationOptions, | ||
references: _referenceManager.CompilationReferences); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.