Expand RazorProject.GetItem to take in FileKinds when getting items.

- Obsoleted old `GetItem` API.
- Updated tests to take new API.
- Added a new test to verify the broken scenario.

dotnet/aspnetcore-tooling#8972
\n\nCommit migrated from 2dd34b8dd8
This commit is contained in:
N. Taylor Mullen 2019-04-01 20:59:11 -07:00
parent d365a92ca4
commit ea5d10509e
9 changed files with 66 additions and 15 deletions

View File

@ -44,7 +44,7 @@ namespace Microsoft.AspNetCore.Razor.Language
}); });
} }
public override RazorProjectItem GetItem(string path) public override RazorProjectItem GetItem(string path, string fileKind)
{ {
var absoluteBasePath = NormalizeAndEnsureValidPath("/"); var absoluteBasePath = NormalizeAndEnsureValidPath("/");
var absolutePath = NormalizeAndEnsureValidPath(path); var absolutePath = NormalizeAndEnsureValidPath(path);
@ -58,7 +58,13 @@ namespace Microsoft.AspNetCore.Razor.Language
var relativePhysicalPath = file.FullName.Substring(absoluteBasePath.Length + 1); // Include leading separator var relativePhysicalPath = file.FullName.Substring(absoluteBasePath.Length + 1); // Include leading separator
var filePath = "/" + relativePhysicalPath.Replace(Path.DirectorySeparatorChar, '/'); var filePath = "/" + relativePhysicalPath.Replace(Path.DirectorySeparatorChar, '/');
return new DefaultRazorProjectItem("/", filePath, relativePhysicalPath, fileKind: null, new FileInfo(absolutePath)); return new DefaultRazorProjectItem("/", filePath, relativePhysicalPath, fileKind, new FileInfo(absolutePath));
}
[Obsolete("Use GetItem(string path, string fileKind) instead.")]
public override RazorProjectItem GetItem(string path)
{
return GetItem(path, fileKind: null);
} }
protected override string NormalizeAndEnsureValidPath(string path) protected override string NormalizeAndEnsureValidPath(string path)

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // 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.Collections.Generic;
using System.Linq; using System.Linq;
@ -14,10 +15,16 @@ namespace Microsoft.AspNetCore.Razor.Language
return Enumerable.Empty<RazorProjectItem>(); return Enumerable.Empty<RazorProjectItem>();
} }
[Obsolete("Use GetItem(string path, string fileKind) instead.")]
public override RazorProjectItem GetItem(string path) public override RazorProjectItem GetItem(string path)
{
return GetItem(path, fileKind: null);
}
public override RazorProjectItem GetItem(string path, string fileKind)
{ {
NormalizeAndEnsureValidPath(path); NormalizeAndEnsureValidPath(path);
return new NotFoundProjectItem(string.Empty, path); return new NotFoundProjectItem(string.Empty, path, fileKind);
} }
} }
} }

View File

@ -16,10 +16,12 @@ namespace Microsoft.AspNetCore.Razor.Language
/// </summary> /// </summary>
/// <param name="basePath">The base path.</param> /// <param name="basePath">The base path.</param>
/// <param name="path">The path.</param> /// <param name="path">The path.</param>
public NotFoundProjectItem(string basePath, string path) /// <param name="fileKind">The file kind</param>
public NotFoundProjectItem(string basePath, string path, string fileKind)
{ {
BasePath = basePath; BasePath = basePath;
FilePath = path; FilePath = path;
FileKind = fileKind ?? FileKinds.GetFileKindFromFilePath(path);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -28,6 +30,9 @@ namespace Microsoft.AspNetCore.Razor.Language
/// <inheritdoc /> /// <inheritdoc />
public override string FilePath { get; } public override string FilePath { get; }
/// <inheritdoc />
public override string FileKind { get; }
/// <inheritdoc /> /// <inheritdoc />
public override bool Exists => false; public override bool Exists => false;

View File

@ -18,6 +18,9 @@ namespace Microsoft.AspNetCore.Razor.Language
/// </summary> /// </summary>
/// <param name="basePath">The base path.</param> /// <param name="basePath">The base path.</param>
/// <returns>The sequence of <see cref="RazorProjectItem"/>.</returns> /// <returns>The sequence of <see cref="RazorProjectItem"/>.</returns>
/// <remarks>
/// Project items returned by this method have inferred FileKinds from their corresponding file paths.
/// </remarks>
public abstract IEnumerable<RazorProjectItem> EnumerateItems(string basePath); public abstract IEnumerable<RazorProjectItem> EnumerateItems(string basePath);
/// <summary> /// <summary>
@ -25,8 +28,17 @@ namespace Microsoft.AspNetCore.Razor.Language
/// </summary> /// </summary>
/// <param name="path">The path.</param> /// <param name="path">The path.</param>
/// <returns>The <see cref="RazorProjectItem"/>.</returns> /// <returns>The <see cref="RazorProjectItem"/>.</returns>
[Obsolete("Use GetItem(string path, string fileKind) instead.")]
public abstract RazorProjectItem GetItem(string path); public abstract RazorProjectItem GetItem(string path);
/// <summary>
/// Gets a <see cref="RazorProjectItem"/> for the specified path.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="fileKind">The file kind</param>
/// <returns>The <see cref="RazorProjectItem"/>.</returns>
public abstract RazorProjectItem GetItem(string path, string fileKind);
/// <summary> /// <summary>
/// Gets the sequence of files named <paramref name="fileName"/> that are applicable to the specified path. /// Gets the sequence of files named <paramref name="fileName"/> that are applicable to the specified path.
/// </summary> /// </summary>
@ -38,6 +50,8 @@ namespace Microsoft.AspNetCore.Razor.Language
/// traverses to the project root. /// traverses to the project root.
/// e.g. /// e.g.
/// /Views/Home/View.cshtml -> [ /Views/Home/FileName.cshtml, /Views/FileName.cshtml, /FileName.cshtml ] /// /Views/Home/View.cshtml -> [ /Views/Home/FileName.cshtml, /Views/FileName.cshtml, /FileName.cshtml ]
///
/// Project items returned by this method have inferred FileKinds from their corresponding file paths.
/// </remarks> /// </remarks>
public IEnumerable<RazorProjectItem> FindHierarchicalItems(string path, string fileName) public IEnumerable<RazorProjectItem> FindHierarchicalItems(string path, string fileName)
{ {
@ -56,6 +70,8 @@ namespace Microsoft.AspNetCore.Razor.Language
/// traverses to the <paramref name="basePath"/>. /// traverses to the <paramref name="basePath"/>.
/// e.g. /// e.g.
/// (/Views, /Views/Home/View.cshtml) -> [ /Views/Home/FileName.cshtml, /Views/FileName.cshtml ] /// (/Views, /Views/Home/View.cshtml) -> [ /Views/Home/FileName.cshtml, /Views/FileName.cshtml ]
///
/// Project items returned by this method have inferred FileKinds from their corresponding file paths.
/// </remarks> /// </remarks>
public virtual IEnumerable<RazorProjectItem> FindHierarchicalItems(string basePath, string path, string fileName) public virtual IEnumerable<RazorProjectItem> FindHierarchicalItems(string basePath, string path, string fileName)
{ {
@ -101,7 +117,7 @@ namespace Microsoft.AspNetCore.Razor.Language
builder.Append(fileName); builder.Append(fileName);
var itemPath = builder.ToString(); var itemPath = builder.ToString();
yield return GetItem(itemPath); yield return GetItem(itemPath, fileKind: null);
} }
} }

View File

@ -19,10 +19,16 @@ namespace Microsoft.AspNetCore.Razor.Language
return directory?.EnumerateItems() ?? Enumerable.Empty<RazorProjectItem>(); return directory?.EnumerateItems() ?? Enumerable.Empty<RazorProjectItem>();
} }
[Obsolete("Use GetItem(string path, string fileKind) instead.")]
public override RazorProjectItem GetItem(string path) public override RazorProjectItem GetItem(string path)
{
return GetItem(path, fileKind: null);
}
public override RazorProjectItem GetItem(string path, string fileKind)
{ {
path = NormalizeAndEnsureValidPath(path); path = NormalizeAndEnsureValidPath(path);
return _root.GetItem(path) ?? new NotFoundProjectItem(string.Empty, path); return _root.GetItem(path) ?? new NotFoundProjectItem(string.Empty, path, fileKind);
} }
public void Add(RazorProjectItem projectItem) public void Add(RazorProjectItem projectItem)

View File

@ -3,7 +3,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
namespace Microsoft.AspNetCore.Razor.Tools namespace Microsoft.AspNetCore.Razor.Tools
@ -28,12 +27,18 @@ namespace Microsoft.AspNetCore.Razor.Tools
} }
} }
[Obsolete("Use GetItem(string path, string fileKind) instead.")]
public override RazorProjectItem GetItem(string path) public override RazorProjectItem GetItem(string path)
{
return GetItem(path, fileKind: null);
}
public override RazorProjectItem GetItem(string path, string fileKind)
{ {
RazorProjectItem razorProjectItem = null; RazorProjectItem razorProjectItem = null;
foreach (var fileSystem in FileSystems) foreach (var fileSystem in FileSystems)
{ {
razorProjectItem = fileSystem.GetItem(path); razorProjectItem = fileSystem.GetItem(path, fileKind);
if (razorProjectItem != null && razorProjectItem.Exists) if (razorProjectItem != null && razorProjectItem.Exists)
{ {
return razorProjectItem; return razorProjectItem;

View File

@ -305,7 +305,7 @@ namespace Microsoft.AspNetCore.Razor.Tools
{ {
var inputItem = inputs[i]; var inputItem = inputs[i];
var codeDocument = engine.Process(engine.FileSystem.GetItem(inputItem.FilePath)); var codeDocument = engine.Process(engine.FileSystem.GetItem(inputItem.FilePath, inputItem.FileKind));
var csharpDocument = codeDocument.GetCSharpDocument(); var csharpDocument = codeDocument.GetCSharpDocument();
outputs[i] = new OutputItem(inputItem, csharpDocument); outputs[i] = new OutputItem(inputItem, csharpDocument);
}); });

View File

@ -59,20 +59,20 @@ namespace Microsoft.AspNetCore.Razor.Tools
// Arrange // Arrange
var basePath = "base-path"; var basePath = "base-path";
var filePath = "file-path"; var filePath = "file-path";
var file1 = new NotFoundProjectItem(basePath, filePath); var file1 = new NotFoundProjectItem(basePath, filePath, fileKind: null);
var file2 = new TestRazorProjectItem(filePath); var file2 = new TestRazorProjectItem(filePath);
RazorProjectItem nullItem = null; RazorProjectItem nullItem = null;
var fileSystem1 = Mock.Of<RazorProjectFileSystem>( var fileSystem1 = Mock.Of<RazorProjectFileSystem>(
f => f.GetItem(filePath) == file1); f => f.GetItem(filePath, null) == file1);
var fileSystem2 = Mock.Of<RazorProjectFileSystem>( var fileSystem2 = Mock.Of<RazorProjectFileSystem>(
f => f.GetItem(filePath) == nullItem); f => f.GetItem(filePath, null) == nullItem);
var fileSystem3 = Mock.Of<RazorProjectFileSystem>( var fileSystem3 = Mock.Of<RazorProjectFileSystem>(
f => f.GetItem(filePath) == file2); f => f.GetItem(filePath, null) == file2);
var compositeRazorProjectFileSystem = new CompositeRazorProjectFileSystem(new[] { fileSystem1, fileSystem2, fileSystem3 }); var compositeRazorProjectFileSystem = new CompositeRazorProjectFileSystem(new[] { fileSystem1, fileSystem2, fileSystem3 });
// Act // Act
var result = compositeRazorProjectFileSystem.GetItem(filePath); var result = compositeRazorProjectFileSystem.GetItem(filePath, fileKind: null);
// Assert // Assert
Assert.Same(file2, result); Assert.Same(file2, result);

View File

@ -28,11 +28,17 @@ namespace Microsoft.AspNetCore.Razor.Language
throw new NotImplementedException(); throw new NotImplementedException();
} }
[Obsolete("Use GetItem(string path, string fileKind) instead.")]
public override RazorProjectItem GetItem(string path) public override RazorProjectItem GetItem(string path)
{
return GetItem(path, fileKind: null);
}
public override RazorProjectItem GetItem(string path, string fileKind)
{ {
if (!_lookup.TryGetValue(path, out var value)) if (!_lookup.TryGetValue(path, out var value))
{ {
value = new NotFoundProjectItem("", path); value = new NotFoundProjectItem("", path, fileKind);
} }
return value; return value;