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:
parent
d365a92ca4
commit
ea5d10509e
|
|
@ -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 absolutePath = NormalizeAndEnsureValidPath(path);
|
||||
|
|
@ -58,7 +58,13 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
var relativePhysicalPath = file.FullName.Substring(absoluteBasePath.Length + 1); // Include leading separator
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
// 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;
|
||||
|
||||
|
|
@ -14,10 +15,16 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
return Enumerable.Empty<RazorProjectItem>();
|
||||
}
|
||||
|
||||
[Obsolete("Use GetItem(string path, string fileKind) instead.")]
|
||||
public override RazorProjectItem GetItem(string path)
|
||||
{
|
||||
return GetItem(path, fileKind: null);
|
||||
}
|
||||
|
||||
public override RazorProjectItem GetItem(string path, string fileKind)
|
||||
{
|
||||
NormalizeAndEnsureValidPath(path);
|
||||
return new NotFoundProjectItem(string.Empty, path);
|
||||
return new NotFoundProjectItem(string.Empty, path, fileKind);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,10 +16,12 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
/// </summary>
|
||||
/// <param name="basePath">The base 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;
|
||||
FilePath = path;
|
||||
FileKind = fileKind ?? FileKinds.GetFileKindFromFilePath(path);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
|
@ -28,6 +30,9 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
/// <inheritdoc />
|
||||
public override string FilePath { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string FileKind { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Exists => false;
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,9 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
/// </summary>
|
||||
/// <param name="basePath">The base path.</param>
|
||||
/// <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);
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -25,8 +28,17 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <returns>The <see cref="RazorProjectItem"/>.</returns>
|
||||
[Obsolete("Use GetItem(string path, string fileKind) instead.")]
|
||||
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>
|
||||
/// Gets the sequence of files named <paramref name="fileName"/> that are applicable to the specified path.
|
||||
/// </summary>
|
||||
|
|
@ -38,6 +50,8 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
/// traverses to the project root.
|
||||
/// e.g.
|
||||
/// /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>
|
||||
public IEnumerable<RazorProjectItem> FindHierarchicalItems(string path, string fileName)
|
||||
{
|
||||
|
|
@ -56,6 +70,8 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
/// traverses to the <paramref name="basePath"/>.
|
||||
/// e.g.
|
||||
/// (/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>
|
||||
public virtual IEnumerable<RazorProjectItem> FindHierarchicalItems(string basePath, string path, string fileName)
|
||||
{
|
||||
|
|
@ -101,7 +117,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
builder.Append(fileName);
|
||||
|
||||
var itemPath = builder.ToString();
|
||||
yield return GetItem(itemPath);
|
||||
yield return GetItem(itemPath, fileKind: null);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,10 +19,16 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
return directory?.EnumerateItems() ?? Enumerable.Empty<RazorProjectItem>();
|
||||
}
|
||||
|
||||
[Obsolete("Use GetItem(string path, string fileKind) instead.")]
|
||||
public override RazorProjectItem GetItem(string path)
|
||||
{
|
||||
return GetItem(path, fileKind: null);
|
||||
}
|
||||
|
||||
public override RazorProjectItem GetItem(string path, string fileKind)
|
||||
{
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Razor.Language;
|
||||
|
||||
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)
|
||||
{
|
||||
return GetItem(path, fileKind: null);
|
||||
}
|
||||
|
||||
public override RazorProjectItem GetItem(string path, string fileKind)
|
||||
{
|
||||
RazorProjectItem razorProjectItem = null;
|
||||
foreach (var fileSystem in FileSystems)
|
||||
{
|
||||
razorProjectItem = fileSystem.GetItem(path);
|
||||
razorProjectItem = fileSystem.GetItem(path, fileKind);
|
||||
if (razorProjectItem != null && razorProjectItem.Exists)
|
||||
{
|
||||
return razorProjectItem;
|
||||
|
|
|
|||
|
|
@ -305,7 +305,7 @@ namespace Microsoft.AspNetCore.Razor.Tools
|
|||
{
|
||||
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();
|
||||
outputs[i] = new OutputItem(inputItem, csharpDocument);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -59,20 +59,20 @@ namespace Microsoft.AspNetCore.Razor.Tools
|
|||
// Arrange
|
||||
var basePath = "base-path";
|
||||
var filePath = "file-path";
|
||||
var file1 = new NotFoundProjectItem(basePath, filePath);
|
||||
var file1 = new NotFoundProjectItem(basePath, filePath, fileKind: null);
|
||||
var file2 = new TestRazorProjectItem(filePath);
|
||||
RazorProjectItem nullItem = null;
|
||||
var fileSystem1 = Mock.Of<RazorProjectFileSystem>(
|
||||
f => f.GetItem(filePath) == file1);
|
||||
f => f.GetItem(filePath, null) == file1);
|
||||
var fileSystem2 = Mock.Of<RazorProjectFileSystem>(
|
||||
f => f.GetItem(filePath) == nullItem);
|
||||
f => f.GetItem(filePath, null) == nullItem);
|
||||
var fileSystem3 = Mock.Of<RazorProjectFileSystem>(
|
||||
f => f.GetItem(filePath) == file2);
|
||||
f => f.GetItem(filePath, null) == file2);
|
||||
|
||||
var compositeRazorProjectFileSystem = new CompositeRazorProjectFileSystem(new[] { fileSystem1, fileSystem2, fileSystem3 });
|
||||
|
||||
// Act
|
||||
var result = compositeRazorProjectFileSystem.GetItem(filePath);
|
||||
var result = compositeRazorProjectFileSystem.GetItem(filePath, fileKind: null);
|
||||
|
||||
// Assert
|
||||
Assert.Same(file2, result);
|
||||
|
|
|
|||
|
|
@ -28,11 +28,17 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
[Obsolete("Use GetItem(string path, string fileKind) instead.")]
|
||||
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))
|
||||
{
|
||||
value = new NotFoundProjectItem("", path);
|
||||
value = new NotFoundProjectItem("", path, fileKind);
|
||||
}
|
||||
|
||||
return value;
|
||||
|
|
|
|||
Loading…
Reference in New Issue