parent
dd9eab551d
commit
209729332c
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Razor.Evolution
|
namespace Microsoft.AspNetCore.Razor.Evolution
|
||||||
{
|
{
|
||||||
|
|
@ -189,17 +190,14 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
||||||
|
|
||||||
var result = new List<RazorSourceDocument>();
|
var result = new List<RazorSourceDocument>();
|
||||||
|
|
||||||
var importsFileName = Options.ImportsFileName;
|
|
||||||
if (!string.IsNullOrEmpty(importsFileName))
|
var importProjectItems = GetImportItems(projectItem);
|
||||||
|
foreach (var importItem in importProjectItems)
|
||||||
{
|
{
|
||||||
var importProjectItems = Project.FindHierarchicalItems(projectItem.Path, importsFileName);
|
if (importItem.Exists)
|
||||||
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));
|
||||||
// We want items in descending order. FindHierarchicalItems returns items in ascending order.
|
|
||||||
result.Insert(0, RazorSourceDocument.ReadFrom(importItem));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -210,5 +208,41 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
||||||
|
|
||||||
return result;
|
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.Path, importsFileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Enumerable.Empty<RazorProjectItem>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using Microsoft.AspNetCore.Testing;
|
using Microsoft.AspNetCore.Testing;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|
@ -215,5 +216,52 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
||||||
Assert.Collection(codeDocument.Imports,
|
Assert.Collection(codeDocument.Imports,
|
||||||
import => Assert.Same(defaultImport, import));
|
import => Assert.Same(defaultImport, import));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetImportItems_WithPath_ReturnsAllImportsForFile()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var expected = new[] { "/Views/Home/MyImport.cshtml", "/Views/MyImport.cshtml", "/MyImport.cshtml" };
|
||||||
|
var project = new TestRazorProject();
|
||||||
|
var razorEngine = RazorEngine.Create();
|
||||||
|
var templateEngine = new RazorTemplateEngine(razorEngine, project)
|
||||||
|
{
|
||||||
|
Options =
|
||||||
|
{
|
||||||
|
ImportsFileName = "MyImport.cshtml"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var imports = templateEngine.GetImportItems("/Views/Home/Index.cshtml");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var paths = imports.Select(i => i.Path);
|
||||||
|
Assert.Equal(expected, paths);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetImportItems_WithItem_ReturnsAllImportsForFile()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var expected = new[] { "/Views/Home/MyImport.cshtml", "/Views/MyImport.cshtml", "/MyImport.cshtml" };
|
||||||
|
var projectItem = new TestRazorProjectItem("/Views/Home/Index.cshtml");
|
||||||
|
var project = new TestRazorProject(new[] { projectItem });
|
||||||
|
var razorEngine = RazorEngine.Create();
|
||||||
|
var templateEngine = new RazorTemplateEngine(razorEngine, project)
|
||||||
|
{
|
||||||
|
Options =
|
||||||
|
{
|
||||||
|
ImportsFileName = "MyImport.cshtml"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var imports = templateEngine.GetImportItems(projectItem);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var paths = imports.Select(i => i.Path);
|
||||||
|
Assert.Equal(expected, paths);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue