From ea5d10509ecb55c523126120cdb1fb2d4e5e0d02 Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Mon, 1 Apr 2019 20:59:11 -0700 Subject: [PATCH] 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 https://github.com/dotnet/aspnetcore-tooling/commit/2dd34b8dd82a3214ba9242ee627ba13dc55e915a --- .../src/DefaultRazorProjectFileSystem.cs | 10 ++++++++-- .../src/EmptyProjectFileSystem.cs | 9 ++++++++- .../src/NotFoundProjectItem.cs | 7 ++++++- .../src/RazorProject.cs | 18 +++++++++++++++++- .../src/VirtualRazorProjectFileSystem.cs | 8 +++++++- .../src/CompositeRazorProjectFileSystem.cs | 9 +++++++-- .../src/GenerateCommand.cs | 2 +- .../CompositeRazorProjectFileSystemTest.cs | 10 +++++----- .../Language/TestRazorProjectFileSystem.cs | 8 +++++++- 9 files changed, 66 insertions(+), 15 deletions(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorProjectFileSystem.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorProjectFileSystem.cs index 26b03a14ef..889c8cdb75 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorProjectFileSystem.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorProjectFileSystem.cs @@ -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) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/EmptyProjectFileSystem.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/EmptyProjectFileSystem.cs index 61ced1271b..50849709af 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/EmptyProjectFileSystem.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/EmptyProjectFileSystem.cs @@ -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(); } + [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); } } } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/NotFoundProjectItem.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/NotFoundProjectItem.cs index 6989ce7f63..627b7b52e5 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/NotFoundProjectItem.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/NotFoundProjectItem.cs @@ -16,10 +16,12 @@ namespace Microsoft.AspNetCore.Razor.Language /// /// The base path. /// The path. - public NotFoundProjectItem(string basePath, string path) + /// The file kind + public NotFoundProjectItem(string basePath, string path, string fileKind) { BasePath = basePath; FilePath = path; + FileKind = fileKind ?? FileKinds.GetFileKindFromFilePath(path); } /// @@ -28,6 +30,9 @@ namespace Microsoft.AspNetCore.Razor.Language /// public override string FilePath { get; } + /// + public override string FileKind { get; } + /// public override bool Exists => false; diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorProject.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorProject.cs index 931d38abc6..b75d8ee7f0 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorProject.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorProject.cs @@ -18,6 +18,9 @@ namespace Microsoft.AspNetCore.Razor.Language /// /// The base path. /// The sequence of . + /// + /// Project items returned by this method have inferred FileKinds from their corresponding file paths. + /// public abstract IEnumerable EnumerateItems(string basePath); /// @@ -25,8 +28,17 @@ namespace Microsoft.AspNetCore.Razor.Language /// /// The path. /// The . + [Obsolete("Use GetItem(string path, string fileKind) instead.")] public abstract RazorProjectItem GetItem(string path); + /// + /// Gets a for the specified path. + /// + /// The path. + /// The file kind + /// The . + public abstract RazorProjectItem GetItem(string path, string fileKind); + /// /// Gets the sequence of files named that are applicable to the specified path. /// @@ -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. /// public IEnumerable FindHierarchicalItems(string path, string fileName) { @@ -56,6 +70,8 @@ namespace Microsoft.AspNetCore.Razor.Language /// traverses to the . /// 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. /// public virtual IEnumerable 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); } } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/VirtualRazorProjectFileSystem.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/VirtualRazorProjectFileSystem.cs index 6b3737fb73..c6bf87efb0 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/VirtualRazorProjectFileSystem.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/VirtualRazorProjectFileSystem.cs @@ -19,10 +19,16 @@ namespace Microsoft.AspNetCore.Razor.Language return directory?.EnumerateItems() ?? Enumerable.Empty(); } + [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) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/CompositeRazorProjectFileSystem.cs b/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/CompositeRazorProjectFileSystem.cs index 63fad347ee..092851d311 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/CompositeRazorProjectFileSystem.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/CompositeRazorProjectFileSystem.cs @@ -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; diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/GenerateCommand.cs b/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/GenerateCommand.cs index 5715739c67..637421409d 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/GenerateCommand.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/GenerateCommand.cs @@ -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); }); diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Tools/test/CompositeRazorProjectFileSystemTest.cs b/src/Razor/Microsoft.AspNetCore.Razor.Tools/test/CompositeRazorProjectFileSystemTest.cs index 68969a5c7b..01a3e1b6a8 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Tools/test/CompositeRazorProjectFileSystemTest.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Tools/test/CompositeRazorProjectFileSystemTest.cs @@ -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( - f => f.GetItem(filePath) == file1); + f => f.GetItem(filePath, null) == file1); var fileSystem2 = Mock.Of( - f => f.GetItem(filePath) == nullItem); + f => f.GetItem(filePath, null) == nullItem); var fileSystem3 = Mock.Of( - 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); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common/Language/TestRazorProjectFileSystem.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common/Language/TestRazorProjectFileSystem.cs index f16fff0274..ed8383a9f5 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common/Language/TestRazorProjectFileSystem.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common/Language/TestRazorProjectFileSystem.cs @@ -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;