diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..a597b67 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "C#: OrchardCoreContrib.PoExtractor Debug", + "type": "coreclr", + "request": "launch", + "program": "${workspaceFolder}/src/OrchardCoreContrib.PoExtractor/bin/Debug/net8.0/OrchardCoreContrib.PoExtractor.dll", + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} diff --git a/src/OrchardCoreContrib.PoExtractor.Razor/MetadataProviders/RazorMetadataProvider.cs b/src/OrchardCoreContrib.PoExtractor.Razor/MetadataProviders/RazorMetadataProvider.cs index 01971a2..0ea7aaf 100644 --- a/src/OrchardCoreContrib.PoExtractor.Razor/MetadataProviders/RazorMetadataProvider.cs +++ b/src/OrchardCoreContrib.PoExtractor.Razor/MetadataProviders/RazorMetadataProvider.cs @@ -35,6 +35,7 @@ public string GetContext(SyntaxNode node) ArgumentNullException.ThrowIfNull(node); var path = node.SyntaxTree.FilePath.TrimStart(_basePath); + path = RemoveProjectNameFromPath(path); path = RemoveRazorFileExtension(path); return path.Replace(Path.DirectorySeparatorChar, '.'); @@ -108,4 +109,25 @@ private string GetSourceCodeLine(string path, int line) return null; } + + private static string RemoveProjectNameFromPath(string path) + { + // Remove leading directory separator if present + if (path.StartsWith(Path.DirectorySeparatorChar)) + { + path = path.Substring(1); + } + + // Find the first directory separator to locate the project name + var firstSeparatorIndex = path.IndexOf(Path.DirectorySeparatorChar); + + // If there's no separator, just return the path as is + if (firstSeparatorIndex == -1) + { + return path; + } + + // Remove the project name (everything before the first separator) + return path.Substring(firstSeparatorIndex + 1); + } } diff --git a/src/OrchardCoreContrib.PoExtractor/Program.cs b/src/OrchardCoreContrib.PoExtractor/Program.cs index a20204b..b46b938 100644 --- a/src/OrchardCoreContrib.PoExtractor/Program.cs +++ b/src/OrchardCoreContrib.PoExtractor/Program.cs @@ -110,7 +110,6 @@ public static void Main(string[] args) { var projectPath = Path.GetDirectoryName(projectFile); var projectBasePath = Path.GetDirectoryName(projectPath) + Path.DirectorySeparatorChar; - var projectRelativePath = projectPath[projectBasePath.Length..]; var rootedProject = projectPath == inputPath.Value ? projectPath : projectPath[(projectPath.IndexOf(inputPath.Value, StringComparison.Ordinal) + inputPath.Value.Length + 1)..]; diff --git a/test/OrchardCoreContrib.PoExtractor.Razor.Tests/OrchardCoreContrib.PoExtractor.Razor.Tests.csproj b/test/OrchardCoreContrib.PoExtractor.Razor.Tests/OrchardCoreContrib.PoExtractor.Razor.Tests.csproj new file mode 100644 index 0000000..c683456 --- /dev/null +++ b/test/OrchardCoreContrib.PoExtractor.Razor.Tests/OrchardCoreContrib.PoExtractor.Razor.Tests.csproj @@ -0,0 +1,16 @@ + + + + net8.0 + false + + + + + + + + + + + diff --git a/test/OrchardCoreContrib.PoExtractor.Razor.Tests/RazorMetadataProviderIntegrationTests.cs b/test/OrchardCoreContrib.PoExtractor.Razor.Tests/RazorMetadataProviderIntegrationTests.cs new file mode 100644 index 0000000..384c71a --- /dev/null +++ b/test/OrchardCoreContrib.PoExtractor.Razor.Tests/RazorMetadataProviderIntegrationTests.cs @@ -0,0 +1,34 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using OrchardCoreContrib.PoExtractor.Razor.MetadataProviders; +using System.IO; +using Xunit; + +namespace OrchardCoreContrib.PoExtractor.Razor.Tests +{ + public class RazorMetadataProviderIntegrationTests + { + [Fact] + public void GetContext_RemovesProjectNameFromPath() + { + // Arrange + // The test runs from the output directory, so use relative paths + var outputDir = Directory.GetCurrentDirectory(); + var basePath = Path.Combine(outputDir, "Sample"); + var filePath = Path.Combine(basePath, "MyProject", "Views", "Home", "Index.cshtml"); + var code = File.ReadAllText(filePath); + + // Create a dummy syntax tree for the file + var syntaxTree = CSharpSyntaxTree.ParseText(code, path: filePath); + var root = syntaxTree.GetRoot(); + + var provider = new RazorMetadataProvider(basePath); + + // Act + var context = provider.GetContext(root); + + // Assert + Assert.Equal("Views.Home.Index", context); + } + } +} diff --git a/test/OrchardCoreContrib.PoExtractor.Razor.Tests/RazorMetadataProviderTests.cs b/test/OrchardCoreContrib.PoExtractor.Razor.Tests/RazorMetadataProviderTests.cs new file mode 100644 index 0000000..691cfe1 --- /dev/null +++ b/test/OrchardCoreContrib.PoExtractor.Razor.Tests/RazorMetadataProviderTests.cs @@ -0,0 +1,30 @@ +using Xunit; +using System; +using System.IO; +using System.Reflection; + +namespace OrchardCoreContrib.PoExtractor.Razor.Tests +{ + public class RazorMetadataProviderTests + { + [Theory] + [InlineData("/MyProject/Views/Home/Index.cshtml", "Views/Home/Index.cshtml")] + [InlineData("\\MyProject\\Views\\Home\\Index.cshtml", "Views/Home/Index.cshtml")] + [InlineData("MyProject/Index.cshtml", "Index.cshtml")] + [InlineData("Index.cshtml", "Index.cshtml")] + public void RemoveProjectNameFromPath_RemovesProjectName(string input, string expected) + { + // Use reflection to access the private static method + var type = typeof(OrchardCoreContrib.PoExtractor.Razor.MetadataProviders.RazorMetadataProvider); + var method = type.GetMethod("RemoveProjectNameFromPath", BindingFlags.NonPublic | BindingFlags.Static); + Assert.NotNull(method); + + // Normalize input for current OS + var normalizedInput = input.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar); + var normalizedExpected = expected.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar); + + var result = (string)method.Invoke(null, new object[] { normalizedInput }); + Assert.Equal(normalizedExpected, result); + } + } +} diff --git a/test/OrchardCoreContrib.PoExtractor.Razor.Tests/Sample/MyProject/Views/Home/Index.cshtml b/test/OrchardCoreContrib.PoExtractor.Razor.Tests/Sample/MyProject/Views/Home/Index.cshtml new file mode 100644 index 0000000..2bdd285 --- /dev/null +++ b/test/OrchardCoreContrib.PoExtractor.Razor.Tests/Sample/MyProject/Views/Home/Index.cshtml @@ -0,0 +1,7 @@ +@* Sample Razor View *@ +@{ + ViewData["Title"] = "Home Page"; +} + +

Welcome

+

This is a sample view.

diff --git a/test/OrchardCoreContrib.PoExtractor.Razor.Tests/Sample/Views/Home/Index.cshtml b/test/OrchardCoreContrib.PoExtractor.Razor.Tests/Sample/Views/Home/Index.cshtml new file mode 100644 index 0000000..2bdd285 --- /dev/null +++ b/test/OrchardCoreContrib.PoExtractor.Razor.Tests/Sample/Views/Home/Index.cshtml @@ -0,0 +1,7 @@ +@* Sample Razor View *@ +@{ + ViewData["Title"] = "Home Page"; +} + +

Welcome

+

This is a sample view.