Skip to content

Commit 5bc8804

Browse files
committed
C#: Introduce method to decide whether we need to include ASP.NET dlls in standalone compilation.
1 parent 88b51e6 commit 5bc8804

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,52 @@ private static string GetGroup(ReadOnlySpan<char> input, ValueMatch valueMatch,
340340
return match.Slice(quoteIndex1 + 1, quoteIndex2).ToString().ToLowerInvariant();
341341
}
342342

343+
private static bool IsGroupMatch(ReadOnlySpan<char> line, Regex regex, string groupPrefix, string value)
344+
{
345+
foreach (var valueMatch in regex.EnumerateMatches(line))
346+
{
347+
// We can't get the group from the ValueMatch, so doing it manually:
348+
if (GetGroup(line, valueMatch, groupPrefix) == value)
349+
{
350+
return true;
351+
}
352+
}
353+
return false;
354+
}
355+
356+
/// <summary>
357+
/// Returns true if the given project uses the ASP.NET components.
358+
/// The following heuristic is used to decide, if ASP.NET is used:
359+
/// If any file in the project contains either of (this will most like be a .csproj file)
360+
/// <Project Sdk="Microsoft.NET.Sdk.Web" ...
361+
/// <FrameworkReference Include="Microsoft.AspNetCore.App" ...
362+
/// </summary>
363+
private bool UseAspNetDlls()
364+
{
365+
var allFiles = GetFiles("*.*");
366+
foreach (var file in allFiles)
367+
{
368+
try
369+
{
370+
using var sr = new StreamReader(file);
371+
ReadOnlySpan<char> line;
372+
while ((line = sr.ReadLine()) != null)
373+
{
374+
if (IsGroupMatch(line, ProjectSdk(), "Sdk", "Microsoft.NET.Sdk.Web") ||
375+
IsGroupMatch(line, FrameworkReference(), "Include", "Microsoft.AspNetCore.App"))
376+
{
377+
return true;
378+
}
379+
}
380+
}
381+
catch (Exception ex)
382+
{
383+
progressMonitor.FailedToReadFile(file, ex);
384+
}
385+
}
386+
return false;
387+
}
388+
343389
private void DownloadMissingPackages(IEnumerable<string> restoreTargets)
344390
{
345391
var alreadyDownloadedPackages = Directory.GetDirectories(packageDirectory.DirInfo.FullName).Select(d => Path.GetFileName(d).ToLowerInvariant()).ToHashSet();

0 commit comments

Comments
 (0)