Add RelativePhysicalPath
The FilePath property on RPI isn't what we want for use in the compiler, it's more of a view engine concept. Adding a property that does what we want.
This commit is contained in:
parent
b68d9cf01f
commit
b7415502bf
|
|
@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
{
|
{
|
||||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(root));
|
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(root));
|
||||||
}
|
}
|
||||||
|
|
||||||
Root = root.Replace('\\', '/').TrimEnd('/');
|
Root = root.Replace('\\', '/').TrimEnd('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,16 +36,24 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
.EnumerateFiles("*.cshtml", SearchOption.AllDirectories)
|
.EnumerateFiles("*.cshtml", SearchOption.AllDirectories)
|
||||||
.Select(file =>
|
.Select(file =>
|
||||||
{
|
{
|
||||||
var relativePath = file.FullName.Substring(absoluteBasePath.Length).Replace(Path.DirectorySeparatorChar, '/');
|
var relativePhysicalPath = file.FullName.Substring(absoluteBasePath.Length + 1); // Include leading separator
|
||||||
return new FileSystemRazorProjectItem(basePath, relativePath, file);
|
var filePath = "/" + relativePhysicalPath.Replace(Path.DirectorySeparatorChar, '/');
|
||||||
|
|
||||||
|
return new FileSystemRazorProjectItem(basePath, filePath, relativePhysicalPath, file);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public override RazorProjectItem GetItem(string path)
|
public override RazorProjectItem GetItem(string path)
|
||||||
{
|
{
|
||||||
|
var absoluteBasePath = NormalizeAndEnsureValidPath("/");
|
||||||
var absolutePath = NormalizeAndEnsureValidPath(path);
|
var absolutePath = NormalizeAndEnsureValidPath(path);
|
||||||
|
|
||||||
return new FileSystemRazorProjectItem("/", path, new FileInfo(absolutePath));
|
var file = new FileInfo(absolutePath);
|
||||||
|
|
||||||
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override string NormalizeAndEnsureValidPath(string path)
|
protected override string NormalizeAndEnsureValidPath(string path)
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,14 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
/// Initializes a new instance of <see cref="FileSystemRazorProjectItem"/>.
|
/// Initializes a new instance of <see cref="FileSystemRazorProjectItem"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="basePath">The base path.</param>
|
/// <param name="basePath">The base path.</param>
|
||||||
/// <param name="path">The 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>
|
/// <param name="file">The <see cref="FileInfo"/>.</param>
|
||||||
public FileSystemRazorProjectItem(string basePath, string path, FileInfo file)
|
public FileSystemRazorProjectItem(string basePath, string filePath, string relativePhysicalPath, FileInfo file)
|
||||||
{
|
{
|
||||||
BasePath = basePath;
|
BasePath = basePath;
|
||||||
FilePath = path;
|
FilePath = filePath;
|
||||||
|
RelativePhysicalPath = relativePhysicalPath;
|
||||||
File = file;
|
File = file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -30,6 +32,8 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
|
|
||||||
public override string PhysicalPath => File.FullName;
|
public override string PhysicalPath => File.FullName;
|
||||||
|
|
||||||
|
public override string RelativePhysicalPath { get; }
|
||||||
|
|
||||||
public override Stream Read() => new FileStream(PhysicalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
|
public override Stream Read() => new FileStream(PhysicalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
|
|
@ -18,15 +19,23 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
public abstract string BasePath { get; }
|
public abstract string BasePath { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// File path relative to <see cref="BasePath"/>.
|
/// File path relative to <see cref="BasePath"/>. This property uses the project path syntax,
|
||||||
|
/// using <c>/</c> as a path separator and does not follow the operating system's file system
|
||||||
|
/// conventions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract string FilePath { get; }
|
public abstract string FilePath { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The absolute path to the file, including the file name.
|
/// The absolute physical (file system) path to the file, including the file name.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract string PhysicalPath { get; }
|
public abstract string PhysicalPath { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The relative physical (file system) path to the file, including the file name. Relative to the
|
||||||
|
/// physical path of the <see cref="BasePath"/>.
|
||||||
|
/// </summary>
|
||||||
|
public virtual string RelativePhysicalPath => null;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the file contents as readonly <see cref="Stream"/>.
|
/// Gets the file contents as readonly <see cref="Stream"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -20,14 +20,15 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
var fileInfo = new FileInfo(Path.Combine(TestFolder, "Home.cshtml"));
|
var fileInfo = new FileInfo(Path.Combine(TestFolder, "Home.cshtml"));
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var projectItem = new FileSystemRazorProjectItem("/Views", "/Home.cshtml", fileInfo);
|
var projectItem = new FileSystemRazorProjectItem("/", "/Home.cshtml", "Home.cshtml", fileInfo);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal("/Home.cshtml", projectItem.FilePath);
|
Assert.Equal("/Home.cshtml", projectItem.FilePath);
|
||||||
Assert.Equal("/Views", projectItem.BasePath);
|
Assert.Equal("/", projectItem.BasePath);
|
||||||
Assert.True(projectItem.Exists);
|
Assert.True(projectItem.Exists);
|
||||||
Assert.Equal("Home.cshtml", projectItem.FileName);
|
Assert.Equal("Home.cshtml", projectItem.FileName);
|
||||||
Assert.Equal(fileInfo.FullName, projectItem.PhysicalPath);
|
Assert.Equal(fileInfo.FullName, projectItem.PhysicalPath);
|
||||||
|
Assert.Equal("Home.cshtml", projectItem.RelativePhysicalPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -37,7 +38,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
var fileInfo = new FileInfo(Path.Combine(TestFolder, "Views", "FileDoesNotExist.cshtml"));
|
var fileInfo = new FileInfo(Path.Combine(TestFolder, "Views", "FileDoesNotExist.cshtml"));
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var projectItem = new FileSystemRazorProjectItem("/Views", "/FileDoesNotExist.cshtml", fileInfo);
|
var projectItem = new FileSystemRazorProjectItem("/Views", "/FileDoesNotExist.cshtml", Path.Combine("Views", "FileDoesNotExist.cshtml"), fileInfo);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.False(projectItem.Exists);
|
Assert.False(projectItem.Exists);
|
||||||
|
|
@ -48,7 +49,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var fileInfo = new FileInfo(Path.Combine(TestFolder, "Home.cshtml"));
|
var fileInfo = new FileInfo(Path.Combine(TestFolder, "Home.cshtml"));
|
||||||
var projectItem = new FileSystemRazorProjectItem("/", "/Home.cshtml", fileInfo);
|
var projectItem = new FileSystemRazorProjectItem("/", "/Home.cshtml", "Home.cshtml", fileInfo);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var stream = projectItem.Read();
|
var stream = projectItem.Read();
|
||||||
|
|
|
||||||
|
|
@ -73,24 +73,31 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
var fileSystemProject = new FileSystemRazorProject(TestFolder);
|
var fileSystemProject = new FileSystemRazorProject(TestFolder);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var files = fileSystemProject.EnumerateItems("/");
|
var items = fileSystemProject.EnumerateItems("/");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Collection(files.OrderBy(f => f.FilePath),
|
Assert.Collection(items.OrderBy(f => f.FilePath),
|
||||||
file =>
|
item =>
|
||||||
{
|
{
|
||||||
Assert.Equal("/Home.cshtml", file.FilePath);
|
Assert.Equal("/Home.cshtml", item.FilePath);
|
||||||
Assert.Equal("/", file.BasePath);
|
Assert.Equal("/", item.BasePath);
|
||||||
|
Assert.Equal(Path.Combine(TestFolder, "Home.cshtml"), item.PhysicalPath);
|
||||||
|
Assert.Equal("Home.cshtml", item.RelativePhysicalPath);
|
||||||
|
|
||||||
},
|
},
|
||||||
file =>
|
item =>
|
||||||
{
|
{
|
||||||
Assert.Equal("/Views/About/About.cshtml", file.FilePath);
|
Assert.Equal("/Views/About/About.cshtml", item.FilePath);
|
||||||
Assert.Equal("/", file.BasePath);
|
Assert.Equal("/", item.BasePath);
|
||||||
|
Assert.Equal(Path.Combine(TestFolder, "Views", "About", "About.cshtml"), item.PhysicalPath);
|
||||||
|
Assert.Equal(Path.Combine("Views", "About", "About.cshtml"), item.RelativePhysicalPath);
|
||||||
},
|
},
|
||||||
file =>
|
item =>
|
||||||
{
|
{
|
||||||
Assert.Equal("/Views/Home/Index.cshtml", file.FilePath);
|
Assert.Equal("/Views/Home/Index.cshtml", item.FilePath);
|
||||||
Assert.Equal("/", file.BasePath);
|
Assert.Equal("/", item.BasePath);
|
||||||
|
Assert.Equal(Path.Combine(TestFolder, "Views", "Home", "Index.cshtml"), item.PhysicalPath);
|
||||||
|
Assert.Equal(Path.Combine("Views", "Home", "Index.cshtml"), item.RelativePhysicalPath);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -101,19 +108,23 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
var fileSystemProject = new FileSystemRazorProject(TestFolder);
|
var fileSystemProject = new FileSystemRazorProject(TestFolder);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var files = fileSystemProject.EnumerateItems("/Views");
|
var items = fileSystemProject.EnumerateItems("/Views");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Collection(files.OrderBy(f => f.FilePath),
|
Assert.Collection(items.OrderBy(f => f.FilePath),
|
||||||
file =>
|
item =>
|
||||||
{
|
{
|
||||||
Assert.Equal("/About/About.cshtml", file.FilePath);
|
Assert.Equal("/About/About.cshtml", item.FilePath);
|
||||||
Assert.Equal("/Views", file.BasePath);
|
Assert.Equal("/Views", item.BasePath);
|
||||||
|
Assert.Equal(Path.Combine(TestFolder, "Views", "About", "About.cshtml"), item.PhysicalPath);
|
||||||
|
Assert.Equal(Path.Combine("About", "About.cshtml"), item.RelativePhysicalPath);
|
||||||
},
|
},
|
||||||
file =>
|
item =>
|
||||||
{
|
{
|
||||||
Assert.Equal("/Home/Index.cshtml", file.FilePath);
|
Assert.Equal("/Home/Index.cshtml", item.FilePath);
|
||||||
Assert.Equal("/Views", file.BasePath);
|
Assert.Equal("/Views", item.BasePath);
|
||||||
|
Assert.Equal(Path.Combine(TestFolder, "Views", "Home", "Index.cshtml"), item.PhysicalPath);
|
||||||
|
Assert.Equal(Path.Combine("Home", "Index.cshtml"), item.RelativePhysicalPath);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -124,25 +135,28 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
var fileSystemProject = new FileSystemRazorProject(TestFolder);
|
var fileSystemProject = new FileSystemRazorProject(TestFolder);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var files = fileSystemProject.EnumerateItems("/Does-Not-Exist");
|
var items = fileSystemProject.EnumerateItems("/Does-Not-Exist");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Empty(files);
|
Assert.Empty(items);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void GetItem_ReturnsFileFromDisk()
|
public void GetItem_ReturnsFileFromDisk()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var path = "/Views/About/About.cshtml";
|
var filePath = "/Views/About/About.cshtml";
|
||||||
var fileSystemProject = new FileSystemRazorProject(TestFolder);
|
var fileSystemProject = new FileSystemRazorProject(TestFolder);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var file = fileSystemProject.GetItem(path);
|
var item = fileSystemProject.GetItem(filePath);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.True(file.Exists);
|
Assert.True(item.Exists);
|
||||||
Assert.Equal(path, file.FilePath);
|
Assert.Equal(filePath, item.FilePath);
|
||||||
|
Assert.Equal("/", item.BasePath);
|
||||||
|
Assert.Equal(Path.Combine(TestFolder, "Views", "About", "About.cshtml"), item.PhysicalPath);
|
||||||
|
Assert.Equal(Path.Combine("Views", "About", "About.cshtml"), item.RelativePhysicalPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -153,10 +167,10 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
var fileSystemProject = new FileSystemRazorProject(TestFolder);
|
var fileSystemProject = new FileSystemRazorProject(TestFolder);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var file = fileSystemProject.GetItem(path);
|
var item = fileSystemProject.GetItem(path);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.False(file.Exists);
|
Assert.False(item.Exists);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TestFileSystemRazorProject : FileSystemRazorProject
|
private class TestFileSystemRazorProject : FileSystemRazorProject
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue