mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
C#: Re-factor the hardcoded package names into a separate class.
This commit is contained in:
@@ -14,14 +14,6 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
||||
{
|
||||
private readonly ProgressMonitor progressMonitor;
|
||||
|
||||
private static readonly string[] netFrameworks = new[] {
|
||||
"microsoft.aspnetcore.app.ref",
|
||||
"microsoft.netcore.app.ref",
|
||||
"microsoft.netframework.referenceassemblies",
|
||||
"microsoft.windowsdesktop.app.ref",
|
||||
"netstandard.library.ref"
|
||||
};
|
||||
|
||||
internal Assets(ProgressMonitor progressMonitor)
|
||||
{
|
||||
this.progressMonitor = progressMonitor;
|
||||
@@ -109,7 +101,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
||||
}
|
||||
|
||||
// If this is a .NET framework reference then include everything.
|
||||
if (netFrameworks.Any(framework => name.StartsWith(framework)))
|
||||
if (FrameworkPackageNames.AllFrameworks.Any(framework => name.StartsWith(framework)))
|
||||
{
|
||||
dependencies.AddFramework(name);
|
||||
}
|
||||
|
||||
@@ -232,13 +232,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
||||
{
|
||||
// Multiple dotnet framework packages could be present.
|
||||
// The order of the packages is important, we're adding the first one that is present in the nuget cache.
|
||||
var packagesInPrioOrder = new string[]
|
||||
{
|
||||
"microsoft.netcore.app.ref", // net7.0, ... net5.0, netcoreapp3.1, netcoreapp3.0
|
||||
"microsoft.netframework.referenceassemblies.", // net48, ..., net20
|
||||
"netstandard.library.ref", // netstandard2.1
|
||||
"netstandard.library" // netstandard2.0
|
||||
};
|
||||
var packagesInPrioOrder = FrameworkPackageNames.NetFrameworks;
|
||||
|
||||
var frameworkPath = packagesInPrioOrder
|
||||
.Select((s, index) => (Index: index, Path: GetPackageDirectory(s)))
|
||||
@@ -308,7 +302,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
||||
}
|
||||
|
||||
// First try to find ASP.NET Core assemblies in the NuGet packages
|
||||
if (GetPackageDirectory("microsoft.aspnetcore.app.ref") is string aspNetCorePackage)
|
||||
if (GetPackageDirectory(FrameworkPackageNames.AspNetCoreFramework) is string aspNetCorePackage)
|
||||
{
|
||||
progressMonitor.LogInfo($"Found ASP.NET Core in NuGet packages. Not adding installation directory.");
|
||||
dllPaths.Add(aspNetCorePackage);
|
||||
@@ -322,7 +316,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
||||
|
||||
private void AddMicrosoftWindowsDesktopDlls(ISet<string> dllPaths)
|
||||
{
|
||||
if (GetPackageDirectory("microsoft.windowsdesktop.app.ref") is string windowsDesktopApp)
|
||||
if (GetPackageDirectory(FrameworkPackageNames.WindowsDesktopFramework) is string windowsDesktopApp)
|
||||
{
|
||||
progressMonitor.LogInfo($"Found Windows Desktop App in NuGet packages.");
|
||||
dllPaths.Add(windowsDesktopApp);
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Semmle.Extraction.CSharp.DependencyFetching
|
||||
{
|
||||
internal static class FrameworkPackageNames
|
||||
{
|
||||
// The order of the packages is important.
|
||||
public static readonly string[] NetFrameworks = new string[]
|
||||
{
|
||||
"microsoft.netcore.app.ref", // net7.0, ... net5.0, netcoreapp3.1, netcoreapp3.0
|
||||
"microsoft.netframework.referenceassemblies.", // net48, ..., net20
|
||||
"netstandard.library.ref", // netstandard2.1
|
||||
"netstandard.library" // netstandard2.0
|
||||
};
|
||||
|
||||
public static string AspNetCoreFramework =>
|
||||
"microsoft.aspnetcore.app.ref";
|
||||
|
||||
public static string WindowsDesktopFramework =>
|
||||
"microsoft.windowsdesktop.app.ref";
|
||||
|
||||
public static readonly IEnumerable<string> AllFrameworks =
|
||||
NetFrameworks
|
||||
.Union(new string[] { AspNetCoreFramework, WindowsDesktopFramework });
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ namespace Semmle.Extraction.Tests
|
||||
Assert.Equal(6, dependencies.Packages.Count());
|
||||
|
||||
var normalizedPaths = dependencies.Paths.Select(FixExpectedPathOnWindows);
|
||||
// Required references
|
||||
// Used references
|
||||
Assert.Contains("castle.core/4.4.1/lib/netstandard1.5/Castle.Core.dll", normalizedPaths);
|
||||
Assert.Contains("castle.core/4.4.1/lib/netstandard1.5/Castle.Core2.dll", normalizedPaths);
|
||||
Assert.Contains("json.net/1.0.33/lib/netstandard2.0/Json.Net.dll", normalizedPaths);
|
||||
@@ -57,10 +57,10 @@ namespace Semmle.Extraction.Tests
|
||||
Assert.Equal(2, dependencies.Paths.Count());
|
||||
|
||||
var normalizedPaths = dependencies.Paths.Select(FixExpectedPathOnWindows);
|
||||
// Required references
|
||||
// Used references
|
||||
Assert.Contains("microsoft.netframework.referenceassemblies/1.0.3", normalizedPaths);
|
||||
Assert.Contains("microsoft.netframework.referenceassemblies.net48/1.0.3", normalizedPaths);
|
||||
// Used packages
|
||||
// Used frameworks
|
||||
Assert.Contains("microsoft.netframework.referenceassemblies", dependencies.Packages);
|
||||
Assert.Contains("microsoft.netframework.referenceassemblies.net48", dependencies.Packages);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user