Skip to content

Msgctxt without project name #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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}"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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, '.');
Expand Down Expand Up @@ -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);
}
}
1 change: 0 additions & 1 deletion src/OrchardCoreContrib.PoExtractor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)..];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../../src/OrchardCoreContrib.PoExtractor.Razor/OrchardCoreContrib.PoExtractor.Razor.csproj" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<None Include="Sample/**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@* Sample Razor View *@
@{
ViewData["Title"] = "Home Page";
}

<h1>Welcome</h1>
<p>This is a sample view.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@* Sample Razor View *@
@{
ViewData["Title"] = "Home Page";
}

<h1>Welcome</h1>
<p>This is a sample view.</p>
Loading