C#: Re-factor the hardcoded package names into a separate class.

This commit is contained in:
Michael Nebel
2023-11-10 08:58:58 +01:00
parent 98dbbe907e
commit e89fe8ddde
4 changed files with 34 additions and 21 deletions

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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 });
}
}

View File

@@ -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);
}