Skip to content

Commit

Permalink
Move to RazorProjectFileSystem.
Browse files Browse the repository at this point in the history
- Changed all existing APIs to utilize `RazorProjectFileSystem`. This was possible because `RazorProjectFileSystem` inherits from RazorProject.
- Renamed `FileSystemRazorProject` to `DefaultRazorProjectFileSystem`.
- Renamed FileSystemRazorProjectItem` to `DefaultRazorProjectItem`.
- Obsoleted `RazorProject.Create`

#1828
  • Loading branch information
NTaylorMullen committed Feb 9, 2018
1 parent 06c2f0c commit 84bc74e
Show file tree
Hide file tree
Showing 25 changed files with 98 additions and 91 deletions.
Expand Up @@ -24,10 +24,10 @@ public CodeGenerationBenchmark()

var engine = RazorEngine.Create(b => { RazorExtensions.Register(b); });

var project = RazorProject.Create(root.FullName);
var fileSystem = RazorProjectFileSystem.Create(root.FullName);

DesignTimeTemplateEngine = new MvcRazorTemplateEngine(RazorEngine.CreateDesignTime(b => { RazorExtensions.Register(b); }), project);
RuntimeTemplateEngine = new MvcRazorTemplateEngine(RazorEngine.Create(b => { RazorExtensions.Register(b); }), project);
DesignTimeTemplateEngine = new MvcRazorTemplateEngine(RazorEngine.CreateDesignTime(b => { RazorExtensions.Register(b); }), fileSystem);
RuntimeTemplateEngine = new MvcRazorTemplateEngine(RazorEngine.Create(b => { RazorExtensions.Register(b); }), fileSystem);

var codeDocument = RuntimeTemplateEngine.CreateCodeDocument(Path.Combine(root.FullName, "MSN.cshtml"));

Expand Down
Expand Up @@ -8,9 +8,9 @@

namespace Microsoft.AspNetCore.Razor.Language
{
internal class FileSystemRazorProject : RazorProjectFileSystem
internal class DefaultRazorProjectFileSystem : RazorProjectFileSystem
{
public FileSystemRazorProject(string root)
public DefaultRazorProjectFileSystem(string root)
{
if (string.IsNullOrEmpty(root))
{
Expand Down Expand Up @@ -39,7 +39,7 @@ public override IEnumerable<RazorProjectItem> EnumerateItems(string basePath)
var relativePhysicalPath = file.FullName.Substring(absoluteBasePath.Length + 1); // Include leading separator
var filePath = "/" + relativePhysicalPath.Replace(Path.DirectorySeparatorChar, '/');
return new FileSystemRazorProjectItem(basePath, filePath, relativePhysicalPath, file);
return new DefaultRazorProjectItem(basePath, filePath, relativePhysicalPath, file);
});
}

Expand All @@ -53,7 +53,7 @@ public override RazorProjectItem GetItem(string path)
var relativePhysicalPath = file.FullName.Substring(absoluteBasePath.Length + 1); // Include leading separator
var filePath = "/" + relativePhysicalPath.Replace(Path.DirectorySeparatorChar, '/');

return new FileSystemRazorProjectItem("/", filePath, relativePhysicalPath, new FileInfo(absolutePath));
return new DefaultRazorProjectItem("/", filePath, relativePhysicalPath, new FileInfo(absolutePath));
}

protected override string NormalizeAndEnsureValidPath(string path)
Expand Down
Expand Up @@ -5,16 +5,16 @@

namespace Microsoft.AspNetCore.Razor.Language
{
internal class FileSystemRazorProjectItem : RazorProjectItem
internal class DefaultRazorProjectItem : RazorProjectItem
{
/// <summary>
/// Initializes a new instance of <see cref="FileSystemRazorProjectItem"/>.
/// Initializes a new instance of <see cref="DefaultRazorProjectItem"/>.
/// </summary>
/// <param name="basePath">The base path.</param>
/// <param name="relativePhysicalPath">The physical path of the base path.</param>
/// <param name="filePath">The path.</param>
/// <param name="file">The <see cref="FileInfo"/>.</param>
public FileSystemRazorProjectItem(string basePath, string filePath, string relativePhysicalPath, FileInfo file)
public DefaultRazorProjectItem(string basePath, string filePath, string relativePhysicalPath, FileInfo file)
{
BasePath = basePath;
FilePath = filePath;
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.AspNetCore.Razor.Language/RazorProject.cs
Expand Up @@ -129,9 +129,10 @@ protected virtual string NormalizeAndEnsureValidPath(string path)
/// </summary>
/// <param name="rootDirectoryPath">The directory to root the file system at.</param>
/// <returns>A <see cref="RazorProject"/></returns>
[Obsolete("This method is obsolete and will be removed in a future version. Please use " + nameof(RazorProjectFileSystem) + "." + nameof(RazorProjectFileSystem.Create) + " instead.")]
public static RazorProject Create(string rootDirectoryPath)
{
return new FileSystemRazorProject(rootDirectoryPath);
return new DefaultRazorProjectFileSystem(rootDirectoryPath);
}
}
}
15 changes: 11 additions & 4 deletions src/Microsoft.AspNetCore.Razor.Language/RazorProjectFileSystem.cs
@@ -1,18 +1,25 @@
// 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;

namespace Microsoft.AspNetCore.Razor.Language
{
public abstract class RazorProjectFileSystem : RazorProject
{
/// <summary>
/// Create a Razor project based on a physical file system.
/// Create a Razor project file system based off of a root directory.
/// </summary>
/// <param name="rootDirectoryPath">The directory to root the file system at.</param>
/// <returns>A <see cref="RazorProject"/></returns>
public static new RazorProjectFileSystem Create(string rootDirectoryPath)
/// <returns>A <see cref="RazorProjectFileSystem"/></returns>
public new static RazorProjectFileSystem Create(string rootDirectoryPath)
{
return new FileSystemRazorProject(rootDirectoryPath);
if (string.IsNullOrEmpty(rootDirectoryPath))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(rootDirectoryPath));
}

return new DefaultRazorProjectFileSystem(rootDirectoryPath);
}
}
}
3 changes: 1 addition & 2 deletions src/Microsoft.AspNetCore.Razor.Language/RazorProjectItem.cs
@@ -1,14 +1,13 @@
// 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.Diagnostics;
using System.IO;

namespace Microsoft.AspNetCore.Razor.Language
{
/// <summary>
/// An item in <see cref="RazorProject"/>.
/// An item in a <see cref="RazorProjectFileSystem"/>.
/// </summary>
[DebuggerDisplay("{" + nameof(DebuggerToString) + "()}")]
public abstract class RazorProjectItem
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNetCore.Razor.Tools/GenerateCommand.cs
Expand Up @@ -136,7 +136,7 @@ private VirtualRazorProjectFileSystem GetVirtualRazorProjectSystem(SourceItem[]
var project = new VirtualRazorProjectFileSystem();
foreach (var item in inputItems)
{
var projectItem = new FileSystemRazorProjectItem(
var projectItem = new DefaultRazorProjectItem(
basePath: "/",
filePath: item.FilePath,
relativePhysicalPath: item.RelativePhysicalPath,
Expand Down
Expand Up @@ -60,7 +60,7 @@ public override RazorTemplateEngine Create(string projectPath, Action<IRazorEngi
}
});

var templateEngine = new Mvc1_X.MvcRazorTemplateEngine(engine, RazorProject.Create(projectPath));
var templateEngine = new Mvc1_X.MvcRazorTemplateEngine(engine, RazorProjectFileSystem.Create(projectPath));
templateEngine.Options.ImportsFileName = "_ViewImports.cshtml";
return templateEngine;
}
Expand All @@ -73,7 +73,7 @@ public override RazorTemplateEngine Create(string projectPath, Action<IRazorEngi
MvcLatest.RazorExtensions.Register(b);
});

var templateEngine = new MvcLatest.MvcRazorTemplateEngine(engine, RazorProject.Create(projectPath));
var templateEngine = new MvcLatest.MvcRazorTemplateEngine(engine, RazorProjectFileSystem.Create(projectPath));
templateEngine.Options.ImportsFileName = "_ViewImports.cshtml";
return templateEngine;
}
Expand Down
2 changes: 1 addition & 1 deletion src/RazorPageGenerator/Program.cs
Expand Up @@ -73,7 +73,7 @@ public static RazorEngine CreateRazorEngine(string rootNamespace, Action<IRazorE
public static IList<RazorPageGeneratorResult> MainCore(RazorEngine razorEngine, string targetProjectDirectory)
{
var viewDirectories = Directory.EnumerateDirectories(targetProjectDirectory, "Views", SearchOption.AllDirectories);
var razorProject = RazorProject.Create(targetProjectDirectory);
var razorProject = RazorProjectFileSystem.Create(targetProjectDirectory);
var templateEngine = new RazorTemplateEngine(razorEngine, razorProject);
templateEngine.Options.DefaultImports = RazorSourceDocument.Create(@"
@using System
Expand Down
Expand Up @@ -26,7 +26,7 @@ public void GetDefaultImports_IncludesDefaultImports()
};
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
RazorEngine.Create(),
new TestRazorProject());
new TestRazorProjectFileSystem());

// Act
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
Expand All @@ -52,7 +52,7 @@ public void GetDefaultImports_IncludesDefaulInjects()
};
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
RazorEngine.Create(),
new TestRazorProject());
new TestRazorProjectFileSystem());

// Act
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
Expand All @@ -70,7 +70,7 @@ public void GetDefaultImports_IncludesDefaultTagHelpers()
// Arrange
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
RazorEngine.Create(),
new TestRazorProject());
new TestRazorProjectFileSystem());

// Act
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
Expand Down
Expand Up @@ -26,7 +26,7 @@ public void GetDefaultImports_IncludesDefaultImports()
};
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
RazorEngine.Create(),
new TestRazorProject());
new TestRazorProjectFileSystem());

// Act
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
Expand All @@ -52,7 +52,7 @@ public void GetDefaultImports_IncludesDefaulInjects()
};
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
RazorEngine.Create(),
new TestRazorProject());
new TestRazorProjectFileSystem());

// Act
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
Expand All @@ -70,7 +70,7 @@ public void GetDefaultImports_IncludesDefaultTagHelpers()
// Arrange
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
RazorEngine.Create(),
new TestRazorProject());
new TestRazorProjectFileSystem());

// Act
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
Expand Down
Expand Up @@ -8,33 +8,33 @@

namespace Microsoft.AspNetCore.Razor.Language
{
public class FileSystemRazorProjectTest
public class DefaultRazorProjectFileSystemTest
{
private static string TestFolder { get; } = Path.Combine(
TestProject.GetProjectDirectory(typeof(FileSystemRazorProjectTest)),
"TestFiles",
"FileSystemRazorProject");
TestProject.GetProjectDirectory(typeof(DefaultRazorProjectFileSystemTest)),
"TestFiles",
"DefaultRazorProjectFileSystem");

[Theory]
[InlineData(null)]
[InlineData("")]
public void NormalizeAndEnsureValidPath_ThrowsIfPathIsNullOrEmpty(string path)
{
// Arrange
var project = new TestFileSystemRazorProject("C:/some/test/path/root");
var fileSystem = new TestRazorProjectFileSystem("C:/some/test/path/root");

// Act and Assert
ExceptionAssert.ThrowsArgumentNullOrEmptyString(() => project.NormalizeAndEnsureValidPath(path), "path");
ExceptionAssert.ThrowsArgumentNullOrEmptyString(() => fileSystem.NormalizeAndEnsureValidPath(path), "path");
}

[Fact]
public void NormalizeAndEnsureValidPath_NormalizesToAbsolutePath()
{
// Arrange
var project = new TestFileSystemRazorProject("C:/some/test/path/root");
var fileSystem = new TestRazorProjectFileSystem("C:/some/test/path/root");

// Act
var absolutePath = project.NormalizeAndEnsureValidPath("file.cshtml");
var absolutePath = fileSystem.NormalizeAndEnsureValidPath("file.cshtml");

// Assert
Assert.Equal("C:/some/test/path/root/file.cshtml", absolutePath);
Expand All @@ -44,10 +44,10 @@ public void NormalizeAndEnsureValidPath_NormalizesToAbsolutePath()
public void NormalizeAndEnsureValidPath_NormalizesToAbsolutePathWithoutForwardSlash()
{
// Arrange
var project = new TestFileSystemRazorProject("C:/some/test/path/root");
var fileSystem = new TestRazorProjectFileSystem("C:/some/test/path/root");

// Act
var absolutePath = project.NormalizeAndEnsureValidPath("/file.cshtml");
var absolutePath = fileSystem.NormalizeAndEnsureValidPath("/file.cshtml");

// Assert
Assert.Equal("C:/some/test/path/root/file.cshtml", absolutePath);
Expand All @@ -57,10 +57,10 @@ public void NormalizeAndEnsureValidPath_NormalizesToAbsolutePathWithoutForwardSl
public void NormalizeAndEnsureValidPath_NormalizesToForwardSlashes()
{
// Arrange
var project = new TestFileSystemRazorProject(@"C:\some\test\path\root");
var fileSystem = new TestRazorProjectFileSystem(@"C:\some\test\path\root");

// Act
var absolutePath = project.NormalizeAndEnsureValidPath(@"something\file.cshtml");
var absolutePath = fileSystem.NormalizeAndEnsureValidPath(@"something\file.cshtml");

// Assert
Assert.Equal("C:/some/test/path/root/something/file.cshtml", absolutePath);
Expand All @@ -70,10 +70,10 @@ public void NormalizeAndEnsureValidPath_NormalizesToForwardSlashes()
public void EnumerateItems_DiscoversAllCshtmlFiles()
{
// Arrange
var fileSystemProject = new FileSystemRazorProject(TestFolder);
var fileSystem = new DefaultRazorProjectFileSystem(TestFolder);

// Act
var items = fileSystemProject.EnumerateItems("/");
var items = fileSystem.EnumerateItems("/");

// Assert
Assert.Collection(
Expand Down Expand Up @@ -130,10 +130,10 @@ public void EnumerateItems_DiscoversAllCshtmlFiles()
public void EnumerateItems_DiscoversAllCshtmlFiles_UnderSpecifiedBasePath()
{
// Arrange
var fileSystemProject = new FileSystemRazorProject(TestFolder);
var fileSystem = new DefaultRazorProjectFileSystem(TestFolder);

// Act
var items = fileSystemProject.EnumerateItems("/Views");
var items = fileSystem.EnumerateItems("/Views");

// Assert
Assert.Collection(
Expand Down Expand Up @@ -172,10 +172,10 @@ public void EnumerateItems_DiscoversAllCshtmlFiles_UnderSpecifiedBasePath()
public void EnumerateItems_ReturnsEmptySequence_WhenBasePathDoesNotExist()
{
// Arrange
var fileSystemProject = new FileSystemRazorProject(TestFolder);
var fileSystem = new DefaultRazorProjectFileSystem(TestFolder);

// Act
var items = fileSystemProject.EnumerateItems("/Does-Not-Exist");
var items = fileSystem.EnumerateItems("/Does-Not-Exist");

// Assert
Assert.Empty(items);
Expand All @@ -185,10 +185,10 @@ public void EnumerateItems_ReturnsEmptySequence_WhenBasePathDoesNotExist()
public void FindHierarchicalItems_FindsItemsWithMatchingNames()
{
// Arrange
var fileSystemProject = new FileSystemRazorProject(TestFolder);
var fileSystem = new DefaultRazorProjectFileSystem(TestFolder);

// Act
var items = fileSystemProject.FindHierarchicalItems("/Views/Home/Index.cshtml", "_ViewImports.cshtml");
var items = fileSystem.FindHierarchicalItems("/Views/Home/Index.cshtml", "_ViewImports.cshtml");

// Assert
Assert.Collection(
Expand Down Expand Up @@ -224,10 +224,10 @@ public void GetItem_ReturnsFileFromDisk()
{
// Arrange
var filePath = "/Views/About/About.cshtml";
var fileSystemProject = new FileSystemRazorProject(TestFolder);
var fileSystem = new DefaultRazorProjectFileSystem(TestFolder);

// Act
var item = fileSystemProject.GetItem(filePath);
var item = fileSystem.GetItem(filePath);

// Assert
Assert.True(item.Exists);
Expand All @@ -242,18 +242,18 @@ public void GetItem_ReturnsNotFoundResult()
{
// Arrange
var path = "/NotFound.cshtml";
var fileSystemProject = new FileSystemRazorProject(TestFolder);
var fileSystem = new DefaultRazorProjectFileSystem(TestFolder);

// Act
var item = fileSystemProject.GetItem(path);
var item = fileSystem.GetItem(path);

// Assert
Assert.False(item.Exists);
}

private class TestFileSystemRazorProject : FileSystemRazorProject
private class TestRazorProjectFileSystem : DefaultRazorProjectFileSystem
{
public TestFileSystemRazorProject(string root) : base(root)
public TestRazorProjectFileSystem(string root) : base(root)
{
}

Expand Down

0 comments on commit 84bc74e

Please sign in to comment.