-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Adding support for Razor precompilation #5160
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; } | ||
} | ||
} |
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; } | ||
} | ||
} |
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; } | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may want to be more specific here so people don't think this enables custom TagHelper resolution in their cshtml editors. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, let's figure out the naming bit in a second iteration. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
/// (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); | ||
} | ||
} |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm, don't we use semantics equivalent to the current file system for view lookup? Wouldn't doing IgnoreCase here potentially break file systems that are case sensitive? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use case insensitive lookups in other view lookup places (for instance the |
||
} | ||
} |
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; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just double checking, this name change hasn't been updated in the other PR yet right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup.