C#: Choose between .NET framework or core DLLs in standalone

This commit is contained in:
Tamas Vajk
2023-10-04 10:45:09 +02:00
parent a31f946d6f
commit 4887c697c9
5 changed files with 149 additions and 58 deletions

View File

@@ -31,21 +31,21 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
}
}
private bool useAspNetDlls = false;
private bool useAspNetCoreDlls = false;
/// <summary>
/// True if any file in the source directory indicates that ASP.NET is used.
/// The following heuristic is used to decide, if ASP.NET is used:
/// True if any file in the source directory indicates that ASP.NET Core is used.
/// The following heuristic is used to decide, if ASP.NET Core is used:
/// If any file in the source directory contains something like (this will most like be a .csproj file)
/// <Project Sdk="Microsoft.NET.Sdk.Web">
/// <FrameworkReference Include="Microsoft.AspNetCore.App"/>
/// </summary>
public bool UseAspNetDlls
public bool UseAspNetCoreDlls
{
get
{
initialize.Run();
return useAspNetDlls;
return useAspNetCoreDlls;
}
}
@@ -60,6 +60,27 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
}
}
private bool isLegacyProjectStructureUsed = false;
public bool IsLegacyProjectStructureUsed
{
get
{
initialize.Run();
return isLegacyProjectStructureUsed;
}
}
private bool isNewProjectStructureUsed = false;
public bool IsNewProjectStructureUsed
{
get
{
initialize.Run();
return isNewProjectStructureUsed;
}
}
public HashSet<string> CustomImplicitUsings
{
get
@@ -141,9 +162,9 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
}
// Determine if ASP.NET is used.
if (!useAspNetDlls)
if (!useAspNetCoreDlls)
{
useAspNetDlls =
useAspNetCoreDlls =
IsGroupMatch(line, ProjectSdk(), "Sdk", "Microsoft.NET.Sdk.Web") ||
IsGroupMatch(line, FrameworkReference(), "Include", "Microsoft.AspNetCore.App");
}
@@ -164,6 +185,12 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
implicitUsingNamespaces.Add(ns);
}
}
// Determine project structure:
isLegacyProjectStructureUsed |= MicrosoftCSharpTargets().IsMatch(line);
isNewProjectStructureUsed |= ProjectSdk().IsMatch(line);
isNewProjectStructureUsed |= FrameworkReference().IsMatch(line);
// TODO: we could also check `<Sdk Name="Microsoft.NET.Sdk" />`
}
}
catch (Exception ex)
@@ -184,6 +211,9 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
[GeneratedRegex("<Using.*\\sInclude=\"(.*?)\".*/?>", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)]
private static partial Regex CustomImplicitUsingDeclarations();
[GeneratedRegex("<Import.*\\sProject=\".*Microsoft\\.CSharp\\.targets\".*/?>", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)]
private static partial Regex MicrosoftCSharpTargets();
}
internal interface IUnsafeFileReader