Remove RazorTemplateEngine and friends.
- Also nuked tests and resource entries specific to the template engine.
Addresses aspnet/AspNetCoredotnet/aspnetcore-tooling#5079
\n\nCommit migrated from d5f0b4c0b4
This commit is contained in:
parent
439349899b
commit
060c8e63b1
|
|
@ -1,60 +0,0 @@
|
|||
// 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.IO;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Razor.Language;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="RazorTemplateEngine"/> for Mvc Razor views.
|
||||
/// </summary>
|
||||
public class MvcRazorTemplateEngine : RazorTemplateEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of <see cref="MvcRazorTemplateEngine"/>.
|
||||
/// </summary>
|
||||
/// <param name="engine">The <see cref="RazorEngine"/>.</param>
|
||||
/// <param name="project">The <see cref="RazorProject"/>.</param>
|
||||
public MvcRazorTemplateEngine(
|
||||
RazorEngine engine,
|
||||
RazorProject project)
|
||||
: base(engine, project)
|
||||
{
|
||||
Options.ImportsFileName = "_ViewImports.cshtml";
|
||||
Options.DefaultImports = GetDefaultImports();
|
||||
}
|
||||
|
||||
public override RazorCodeDocument CreateCodeDocument(RazorProjectItem projectItem)
|
||||
{
|
||||
return base.CreateCodeDocument(projectItem);
|
||||
}
|
||||
|
||||
// Internal for testing.
|
||||
internal static RazorSourceDocument GetDefaultImports()
|
||||
{
|
||||
using (var stream = new MemoryStream())
|
||||
using (var writer = new StreamWriter(stream, Encoding.UTF8))
|
||||
{
|
||||
writer.WriteLine("@using System");
|
||||
writer.WriteLine("@using System.Collections.Generic");
|
||||
writer.WriteLine("@using System.Linq");
|
||||
writer.WriteLine("@using System.Threading.Tasks");
|
||||
writer.WriteLine("@using Microsoft.AspNetCore.Mvc");
|
||||
writer.WriteLine("@using Microsoft.AspNetCore.Mvc.Rendering");
|
||||
writer.WriteLine("@using Microsoft.AspNetCore.Mvc.ViewFeatures");
|
||||
writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<TModel> Html");
|
||||
writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json");
|
||||
writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component");
|
||||
writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.IUrlHelper Url");
|
||||
writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider");
|
||||
writer.WriteLine("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNetCore.Mvc.Razor");
|
||||
writer.Flush();
|
||||
|
||||
stream.Position = 0;
|
||||
return RazorSourceDocument.ReadFrom(stream, fileName: null, encoding: Encoding.UTF8);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
// 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.Linq;
|
||||
using Microsoft.AspNetCore.Razor.Language;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
|
||||
{
|
||||
public class MvcRazorTemplateEngineTest : RazorProjectEngineTestBase
|
||||
{
|
||||
protected override RazorLanguageVersion Version => RazorLanguageVersion.Version_1_1;
|
||||
|
||||
[Fact]
|
||||
public void GetDefaultImports_IncludesDefaultImports()
|
||||
{
|
||||
// Arrange
|
||||
var expectedImports = new[]
|
||||
{
|
||||
"@using System",
|
||||
"@using System.Collections.Generic",
|
||||
"@using System.Linq",
|
||||
"@using System.Threading.Tasks",
|
||||
"@using Microsoft.AspNetCore.Mvc",
|
||||
"@using Microsoft.AspNetCore.Mvc.Rendering",
|
||||
"@using Microsoft.AspNetCore.Mvc.ViewFeatures",
|
||||
};
|
||||
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
|
||||
CreateProjectEngine().Engine,
|
||||
new TestRazorProjectFileSystem());
|
||||
|
||||
// Act
|
||||
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
|
||||
|
||||
// Assert
|
||||
var importContent = GetContent(imports)
|
||||
.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
|
||||
.Where(line => line.StartsWith("@using"));
|
||||
Assert.Equal(expectedImports, importContent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDefaultImports_IncludesDefaulInjects()
|
||||
{
|
||||
// Arrange
|
||||
var expectedImports = new[]
|
||||
{
|
||||
"@inject global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<TModel> Html",
|
||||
"@inject global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json",
|
||||
"@inject global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component",
|
||||
"@inject global::Microsoft.AspNetCore.Mvc.IUrlHelper Url",
|
||||
"@inject global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider",
|
||||
};
|
||||
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
|
||||
CreateProjectEngine().Engine,
|
||||
new TestRazorProjectFileSystem());
|
||||
|
||||
// Act
|
||||
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
|
||||
|
||||
// Assert
|
||||
var importContent = GetContent(imports)
|
||||
.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
|
||||
.Where(line => line.StartsWith("@inject"));
|
||||
Assert.Equal(expectedImports, importContent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDefaultImports_IncludesDefaultTagHelpers()
|
||||
{
|
||||
// Arrange
|
||||
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
|
||||
CreateProjectEngine().Engine,
|
||||
new TestRazorProjectFileSystem());
|
||||
|
||||
// Act
|
||||
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
|
||||
|
||||
// Assert
|
||||
var importContent = GetContent(imports)
|
||||
.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
|
||||
.Where(line => line.StartsWith("@addTagHelper"));
|
||||
Assert.Contains("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNetCore.Mvc.Razor", importContent);
|
||||
}
|
||||
|
||||
private string GetContent(RazorSourceDocument imports)
|
||||
{
|
||||
var contentChars = new char[imports.Length];
|
||||
imports.CopyTo(0, contentChars, 0, imports.Length);
|
||||
return new string(contentChars);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
// 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.IO;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Razor.Language;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="RazorTemplateEngine"/> for Mvc Razor views.
|
||||
/// </summary>
|
||||
public class MvcRazorTemplateEngine : RazorTemplateEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of <see cref="MvcRazorTemplateEngine"/>.
|
||||
/// </summary>
|
||||
/// <param name="engine">The <see cref="RazorEngine"/>.</param>
|
||||
/// <param name="project">The <see cref="RazorProject"/>.</param>
|
||||
public MvcRazorTemplateEngine(
|
||||
RazorEngine engine,
|
||||
RazorProject project)
|
||||
: base(engine, project)
|
||||
{
|
||||
Options.ImportsFileName = "_ViewImports.cshtml";
|
||||
Options.DefaultImports = GetDefaultImports();
|
||||
}
|
||||
|
||||
public override RazorCodeDocument CreateCodeDocument(RazorProjectItem projectItem)
|
||||
{
|
||||
return base.CreateCodeDocument(projectItem);
|
||||
}
|
||||
|
||||
// Internal for testing.
|
||||
internal static RazorSourceDocument GetDefaultImports()
|
||||
{
|
||||
using (var stream = new MemoryStream())
|
||||
using (var writer = new StreamWriter(stream, Encoding.UTF8))
|
||||
{
|
||||
writer.WriteLine("@using System");
|
||||
writer.WriteLine("@using System.Collections.Generic");
|
||||
writer.WriteLine("@using System.Linq");
|
||||
writer.WriteLine("@using System.Threading.Tasks");
|
||||
writer.WriteLine("@using Microsoft.AspNetCore.Mvc");
|
||||
writer.WriteLine("@using Microsoft.AspNetCore.Mvc.Rendering");
|
||||
writer.WriteLine("@using Microsoft.AspNetCore.Mvc.ViewFeatures");
|
||||
writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<TModel> Html");
|
||||
writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json");
|
||||
writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component");
|
||||
writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.IUrlHelper Url");
|
||||
writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider");
|
||||
writer.WriteLine("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNetCore.Mvc.Razor");
|
||||
writer.WriteLine("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.HeadTagHelper, Microsoft.AspNetCore.Mvc.Razor");
|
||||
writer.WriteLine("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.BodyTagHelper, Microsoft.AspNetCore.Mvc.Razor");
|
||||
writer.Flush();
|
||||
|
||||
stream.Position = 0;
|
||||
return RazorSourceDocument.ReadFrom(stream, fileName: null, encoding: Encoding.UTF8);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,97 +0,0 @@
|
|||
// 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.Linq;
|
||||
using Microsoft.AspNetCore.Razor.Language;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
|
||||
{
|
||||
public class MvcRazorTemplateEngineTest : RazorProjectEngineTestBase
|
||||
{
|
||||
protected override RazorLanguageVersion Version => RazorLanguageVersion.Version_3_0;
|
||||
|
||||
[Fact]
|
||||
public void GetDefaultImports_IncludesDefaultImports()
|
||||
{
|
||||
// Arrange
|
||||
var expectedImports = new[]
|
||||
{
|
||||
"@using System",
|
||||
"@using System.Collections.Generic",
|
||||
"@using System.Linq",
|
||||
"@using System.Threading.Tasks",
|
||||
"@using Microsoft.AspNetCore.Mvc",
|
||||
"@using Microsoft.AspNetCore.Mvc.Rendering",
|
||||
"@using Microsoft.AspNetCore.Mvc.ViewFeatures",
|
||||
};
|
||||
|
||||
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
|
||||
CreateProjectEngine().Engine,
|
||||
new TestRazorProjectFileSystem());
|
||||
|
||||
// Act
|
||||
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
|
||||
|
||||
// Assert
|
||||
var importContent = GetContent(imports)
|
||||
.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
|
||||
.Where(line => line.StartsWith("@using"));
|
||||
Assert.Equal(expectedImports, importContent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDefaultImports_IncludesDefaulInjects()
|
||||
{
|
||||
// Arrange
|
||||
var expectedImports = new[]
|
||||
{
|
||||
"@inject global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<TModel> Html",
|
||||
"@inject global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json",
|
||||
"@inject global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component",
|
||||
"@inject global::Microsoft.AspNetCore.Mvc.IUrlHelper Url",
|
||||
"@inject global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider",
|
||||
};
|
||||
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
|
||||
CreateProjectEngine().Engine,
|
||||
new TestRazorProjectFileSystem());
|
||||
|
||||
// Act
|
||||
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
|
||||
|
||||
// Assert
|
||||
var importContent = GetContent(imports)
|
||||
.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
|
||||
.Where(line => line.StartsWith("@inject"));
|
||||
Assert.Equal(expectedImports, importContent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDefaultImports_IncludesDefaultTagHelpers()
|
||||
{
|
||||
// Arrange
|
||||
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
|
||||
CreateProjectEngine().Engine,
|
||||
new TestRazorProjectFileSystem());
|
||||
|
||||
// Act
|
||||
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
|
||||
|
||||
// Assert
|
||||
var importContent = GetContent(imports)
|
||||
.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
|
||||
.Where(line => line.StartsWith("@addTagHelper"));
|
||||
Assert.Contains("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNetCore.Mvc.Razor", importContent);
|
||||
Assert.Contains("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.HeadTagHelper, Microsoft.AspNetCore.Mvc.Razor", importContent);
|
||||
Assert.Contains("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.BodyTagHelper, Microsoft.AspNetCore.Mvc.Razor", importContent);
|
||||
}
|
||||
|
||||
private string GetContent(RazorSourceDocument imports)
|
||||
{
|
||||
var contentChars = new char[imports.Length];
|
||||
imports.CopyTo(0, contentChars, 0, imports.Length);
|
||||
return new string(contentChars);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1208,20 +1208,6 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
internal static string FormatRazorProject_PathMustStartWithForwardSlash()
|
||||
=> GetString("RazorProject_PathMustStartWithForwardSlash");
|
||||
|
||||
/// <summary>
|
||||
/// The item '{0}' could not be found.
|
||||
/// </summary>
|
||||
internal static string RazorTemplateEngine_ItemCouldNotBeFound
|
||||
{
|
||||
get => GetString("RazorTemplateEngine_ItemCouldNotBeFound");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The item '{0}' could not be found.
|
||||
/// </summary>
|
||||
internal static string FormatRazorTemplateEngine_ItemCouldNotBeFound(object p0)
|
||||
=> string.Format(CultureInfo.CurrentCulture, GetString("RazorTemplateEngine_ItemCouldNotBeFound"), p0);
|
||||
|
||||
/// <summary>
|
||||
/// Remove Tag Helpers for use in the current document.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -1,241 +0,0 @@
|
|||
// 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;
|
||||
using System.Linq;
|
||||
|
||||
namespace Microsoft.AspNetCore.Razor.Language
|
||||
{
|
||||
/// <summary>
|
||||
/// Entry point to parse Razor files and generate code.
|
||||
/// </summary>
|
||||
public class RazorTemplateEngine
|
||||
{
|
||||
private RazorTemplateEngineOptions _options;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of <see cref="RazorTemplateEngine"/>.
|
||||
/// </summary>
|
||||
/// <param name="engine">The <see cref="RazorEngine"/>.</param>
|
||||
/// <param name="project">The <see cref="RazorProject"/>.</param>
|
||||
public RazorTemplateEngine(
|
||||
RazorEngine engine,
|
||||
RazorProject project)
|
||||
{
|
||||
if (engine == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(engine));
|
||||
}
|
||||
|
||||
if (project == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(project));
|
||||
}
|
||||
|
||||
Engine = engine;
|
||||
Project = project;
|
||||
_options = new RazorTemplateEngineOptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="RazorEngine"/>.
|
||||
/// </summary>
|
||||
public RazorEngine Engine { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="RazorProject"/>.
|
||||
/// </summary>
|
||||
public RazorProject Project { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Options to configure <see cref="RazorTemplateEngine"/>.
|
||||
/// </summary>
|
||||
public RazorTemplateEngineOptions Options
|
||||
{
|
||||
get
|
||||
{
|
||||
return _options;
|
||||
}
|
||||
set
|
||||
{
|
||||
_options = value ?? throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the template specified by the project item <paramref name="path"/>.
|
||||
/// </summary>
|
||||
/// <param name="path">The template path.</param>
|
||||
/// <returns>The <see cref="RazorCSharpDocument"/>.</returns>
|
||||
public RazorCSharpDocument GenerateCode(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(path));
|
||||
}
|
||||
|
||||
var projectItem = Project.GetItem(path);
|
||||
return GenerateCode(projectItem);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the template specified by <paramref name="projectItem"/>.
|
||||
/// </summary>
|
||||
/// <param name="projectItem">The <see cref="RazorProjectItem"/>.</param>
|
||||
/// <returns>The <see cref="RazorCSharpDocument"/>.</returns>
|
||||
public RazorCSharpDocument GenerateCode(RazorProjectItem projectItem)
|
||||
{
|
||||
if (projectItem == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(projectItem));
|
||||
}
|
||||
|
||||
if (!projectItem.Exists)
|
||||
{
|
||||
throw new InvalidOperationException(Resources.FormatRazorTemplateEngine_ItemCouldNotBeFound(projectItem.FilePath));
|
||||
}
|
||||
|
||||
var codeDocument = CreateCodeDocument(projectItem);
|
||||
return GenerateCode(codeDocument);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the template specified by <paramref name="codeDocument"/>.
|
||||
/// </summary>
|
||||
/// <param name="codeDocument">The <see cref="RazorProjectItem"/>.</param>
|
||||
/// <returns>The <see cref="RazorCSharpDocument"/>.</returns>
|
||||
public virtual RazorCSharpDocument GenerateCode(RazorCodeDocument codeDocument)
|
||||
{
|
||||
if (codeDocument == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(codeDocument));
|
||||
}
|
||||
|
||||
Engine.Process(codeDocument);
|
||||
return codeDocument.GetCSharpDocument();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a <see cref="RazorCodeDocument"/> for the specified <paramref name="path"/>.
|
||||
/// </summary>
|
||||
/// <param name="path">The template path.</param>
|
||||
/// <returns>The created <see cref="RazorCodeDocument"/>.</returns>
|
||||
public virtual RazorCodeDocument CreateCodeDocument(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(path));
|
||||
}
|
||||
|
||||
var projectItem = Project.GetItem(path);
|
||||
return CreateCodeDocument(projectItem);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a <see cref="RazorCodeDocument"/> for the specified <paramref name="projectItem"/>.
|
||||
/// </summary>
|
||||
/// <param name="projectItem">The <see cref="RazorProjectItem"/>.</param>
|
||||
/// <returns>The created <see cref="RazorCodeDocument"/>.</returns>
|
||||
public virtual RazorCodeDocument CreateCodeDocument(RazorProjectItem projectItem)
|
||||
{
|
||||
if (projectItem == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(projectItem));
|
||||
}
|
||||
|
||||
if (!projectItem.Exists)
|
||||
{
|
||||
throw new InvalidOperationException(Resources.FormatRazorTemplateEngine_ItemCouldNotBeFound(projectItem.FilePath));
|
||||
}
|
||||
|
||||
var source = RazorSourceDocument.ReadFrom(projectItem);
|
||||
var imports = GetImports(projectItem);
|
||||
|
||||
return RazorCodeDocument.Create(source, imports);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets <see cref="RazorSourceDocument"/> that are applicable to the specified <paramref name="path"/>.
|
||||
/// </summary>
|
||||
/// <param name="path">The template path.</param>
|
||||
/// <returns>The sequence of applicable <see cref="RazorSourceDocument"/>.</returns>
|
||||
public IEnumerable<RazorSourceDocument> GetImports(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(path));
|
||||
}
|
||||
|
||||
var projectItem = Project.GetItem(path);
|
||||
return GetImports(projectItem);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets <see cref="RazorSourceDocument"/> that are applicable to the specified <paramref name="projectItem"/>.
|
||||
/// </summary>
|
||||
/// <param name="projectItem">The <see cref="RazorProjectItem"/>.</param>
|
||||
/// <returns>The sequence of applicable <see cref="RazorSourceDocument"/>.</returns>
|
||||
public virtual IEnumerable<RazorSourceDocument> GetImports(RazorProjectItem projectItem)
|
||||
{
|
||||
if (projectItem == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(projectItem));
|
||||
}
|
||||
var result = new List<RazorSourceDocument>();
|
||||
|
||||
var importProjectItems = GetImportItems(projectItem);
|
||||
foreach (var importItem in importProjectItems)
|
||||
{
|
||||
if (importItem.Exists)
|
||||
{
|
||||
// We want items in descending order. FindHierarchicalItems returns items in ascending order.
|
||||
result.Insert(0, RazorSourceDocument.ReadFrom(importItem));
|
||||
}
|
||||
}
|
||||
|
||||
if (Options.DefaultImports != null)
|
||||
{
|
||||
result.Insert(0, Options.DefaultImports);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the sequence of imports with the name specified by <see cref="RazorTemplateEngineOptions.ImportsFileName" />
|
||||
/// that apply to <paramref name="path"/>.
|
||||
/// </summary>
|
||||
/// <param name="path">The path to look up import items for.</param>
|
||||
/// <returns>A sequence of <see cref="RazorProjectItem"/> instances that apply to the
|
||||
/// <paramref name="path"/>.</returns>
|
||||
public IEnumerable<RazorProjectItem> GetImportItems(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(path));
|
||||
}
|
||||
|
||||
var projectItem = Project.GetItem(path);
|
||||
return GetImportItems(projectItem);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the sequence of imports with the name specified by <see cref="RazorTemplateEngineOptions.ImportsFileName" />
|
||||
/// that apply to <paramref name="projectItem"/>.
|
||||
/// </summary>
|
||||
/// <param name="projectItem">The <see cref="RazorProjectItem"/> to look up import items for.</param>
|
||||
/// <returns>A sequence of <see cref="RazorProjectItem"/> instances that apply to the
|
||||
/// <paramref name="projectItem"/>.</returns>
|
||||
public virtual IEnumerable<RazorProjectItem> GetImportItems(RazorProjectItem projectItem)
|
||||
{
|
||||
var importsFileName = Options.ImportsFileName;
|
||||
if (!string.IsNullOrEmpty(importsFileName))
|
||||
{
|
||||
return Project.FindHierarchicalItems(projectItem.FilePath, importsFileName);
|
||||
}
|
||||
|
||||
return Enumerable.Empty<RazorProjectItem>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
// 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.
|
||||
|
||||
namespace Microsoft.AspNetCore.Razor.Language
|
||||
{
|
||||
/// <summary>
|
||||
/// Options for code generation in the <see cref="RazorTemplateEngine"/>.
|
||||
/// </summary>
|
||||
public sealed class RazorTemplateEngineOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the file name of the imports file (e.g. _ViewImports.cshtml).
|
||||
/// </summary>
|
||||
public string ImportsFileName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the default set of imports.
|
||||
/// </summary>
|
||||
public RazorSourceDocument DefaultImports { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -392,9 +392,6 @@ Instead, wrap the contents of the block in "{{}}":
|
|||
<data name="RazorProject_PathMustStartWithForwardSlash" xml:space="preserve">
|
||||
<value>Path must begin with a forward slash '/'.</value>
|
||||
</data>
|
||||
<data name="RazorTemplateEngine_ItemCouldNotBeFound" xml:space="preserve">
|
||||
<value>The item '{0}' could not be found.</value>
|
||||
</data>
|
||||
<data name="RemoveTagHelperDirective_Description" xml:space="preserve">
|
||||
<value>Remove Tag Helpers for use in the current document.</value>
|
||||
</data>
|
||||
|
|
|
|||
Loading…
Reference in New Issue