C#: Change source generated razor file paths to be relative to csproj

This commit is contained in:
Tamas Vajk
2025-01-06 09:27:00 +01:00
parent b0062fc727
commit c9fab0b071
5 changed files with 24 additions and 29 deletions

View File

@@ -34,10 +34,25 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
using var sw = new StreamWriter(analyzerConfigPath);
sw.WriteLine("is_global = true");
foreach (var f in cshtmls.Select(f => f.Replace('\\', '/')))
foreach (var cshtml in cshtmls)
{
sw.WriteLine($"\n[{f}]");
var base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(f)); // TODO: this should be the relative path of the file.
var adjustedPath = cshtml.Replace('\\', '/');
string? relativePath;
try
{
var csprojFolder = Path.GetDirectoryName(csprojFile);
relativePath = csprojFolder is not null ? Path.GetRelativePath(csprojFolder, cshtml) : cshtml;
relativePath = relativePath.Replace('\\', '/');
}
catch (Exception e)
{
logger.LogWarning($"Failed to get relative path for {cshtml}: {e.Message}");
relativePath = adjustedPath;
}
sw.WriteLine($"\n[{adjustedPath}]");
var base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(relativePath));
sw.WriteLine($"build_metadata.AdditionalFiles.TargetPath = {base64}");
}
}