-
Notifications
You must be signed in to change notification settings - Fork 105
Add TargetFramework awareness to NuGet detector #1266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3df6338
dc6278e
ae22db9
ef3e082
cd28ca9
407a41b
7b38156
dac5d58
6e9d01c
0a378c6
46e066e
fed423b
903af51
f97e17d
5b5ac57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
namespace Microsoft.ComponentDetection.Detectors.NuGet; | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using global::NuGet.Frameworks; | ||
using global::NuGet.Versioning; | ||
|
||
/// <summary> | ||
/// Represents a set of packages that are provided by a specific framework. | ||
/// At the moment this only represents the packages that are provided by the Microsoft.NETCore.App framework. | ||
/// We could extend this to represent the packages provided by other frameworks like Microsoft.AspNetCore.App and Microsoft.WindowsDesktop.App. | ||
/// </summary> | ||
internal sealed partial class FrameworkPackages : IEnumerable<KeyValuePair<string, NuGetVersion>>, IEnumerable | ||
{ | ||
private static readonly Dictionary<NuGetFramework, FrameworkPackages> FrameworkPackagesByFramework = []; | ||
|
||
static FrameworkPackages() | ||
{ | ||
AddPackages(NETStandard20.Instance); | ||
AddPackages(NETStandard21.Instance); | ||
AddPackages(NETCoreApp20.Instance); | ||
AddPackages(NETCoreApp21.Instance); | ||
AddPackages(NETCoreApp22.Instance); | ||
AddPackages(NETCoreApp30.Instance); | ||
AddPackages(NETCoreApp31.Instance); | ||
AddPackages(NETCoreApp50.Instance); | ||
AddPackages(NETCoreApp60.Instance); | ||
AddPackages(NETCoreApp70.Instance); | ||
AddPackages(NETCoreApp80.Instance); | ||
AddPackages(NETCoreApp90.Instance); | ||
|
||
static void AddPackages(FrameworkPackages packages) => FrameworkPackagesByFramework[packages.Framework] = packages; | ||
} | ||
|
||
public FrameworkPackages(NuGetFramework framework) => this.Framework = framework; | ||
|
||
public FrameworkPackages(NuGetFramework framework, FrameworkPackages frameworkPackages) | ||
: this(framework) => this.Packages = new(frameworkPackages.Packages); | ||
|
||
public NuGetFramework Framework { get; } | ||
|
||
public Dictionary<string, NuGetVersion> Packages { get; } = new Dictionary<string, NuGetVersion>(StringComparer.OrdinalIgnoreCase); | ||
|
||
public static FrameworkPackages GetFrameworkPackages(NuGetFramework framework) | ||
{ | ||
if (FrameworkPackagesByFramework.TryGetValue(framework, out var frameworkPackages)) | ||
{ | ||
return frameworkPackages; | ||
} | ||
|
||
// if we didn't predefine the package overrides, load them from the targeting pack | ||
// we might just leave this out since in future frameworks we'll have this functionality built into NuGet. | ||
var frameworkPackagesFromPack = LoadFrameworkPackagesFromPack(framework); | ||
Check warning on line 56 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
|
||
return FrameworkPackagesByFramework[framework] = frameworkPackagesFromPack ?? new FrameworkPackages(framework); | ||
} | ||
|
||
private static FrameworkPackages LoadFrameworkPackagesFromPack(NuGetFramework framework) | ||
{ | ||
Check warning on line 62 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
if (framework is null || framework.Framework != FrameworkConstants.FrameworkIdentifiers.NetCoreApp) | ||
{ | ||
return null; | ||
Check warning on line 65 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
} | ||
|
||
// packs location : %ProgramFiles%\dotnet\packs | ||
var packsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "dotnet", "packs", "Microsoft.NETCore.App.Ref"); | ||
Check warning on line 69 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
if (!Directory.Exists(packsFolder)) | ||
{ | ||
return null; | ||
Check warning on line 72 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
} | ||
|
||
var packVersionPattern = $"{framework.Version.Major}.{framework.Version.Minor}.*"; | ||
var packDirectories = Directory.GetDirectories(packsFolder, packVersionPattern); | ||
Check warning on line 76 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
var packageOverridesFile = packDirectories | ||
.Select(d => (Overrides: Path.Combine(d, "data", "PackageOverrides.txt"), Version: ParseVersion(Path.GetFileName(d)))) | ||
.Where(d => File.Exists(d.Overrides)) | ||
.OrderByDescending(d => d.Version) | ||
.FirstOrDefault().Overrides; | ||
Check warning on line 81 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
|
||
if (packageOverridesFile == null) | ||
{ | ||
Check warning on line 84 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
// we should also try to grab them from the user's package folder - they'll be in one location or the other. | ||
return null; | ||
Check warning on line 86 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
} | ||
|
||
// Adapted from https://github.com/dotnet/sdk/blob/c3a8f72c3a5491c693ff8e49e7406136a12c3040/src/Tasks/Common/ConflictResolution/PackageOverride.cs#L52-L68 | ||
var frameworkPackages = new FrameworkPackages(framework); | ||
var packageOverrides = File.ReadAllLines(packageOverridesFile); | ||
Check warning on line 91 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
|
||
foreach (var packageOverride in packageOverrides) | ||
{ | ||
var packageOverrideParts = packageOverride.Trim().Split('|'); | ||
Check warning on line 95 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
|
||
if (packageOverrideParts.Length == 2) | ||
{ | ||
var packageId = packageOverrideParts[0]; | ||
var packageVersion = ParseVersion(packageOverrideParts[1]); | ||
Check warning on line 100 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
|
||
frameworkPackages.Packages[packageId] = packageVersion; | ||
} | ||
} | ||
Check warning on line 104 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
|
||
return frameworkPackages; | ||
Check warning on line 106 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
|
||
static NuGetVersion ParseVersion(string versionString) => NuGetVersion.TryParse(versionString, out var version) ? version : null; | ||
} | ||
Check warning on line 109 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
|
||
private void Add(string id, string version) | ||
{ | ||
// intentionally redirect to indexer to allow for overwrite | ||
this.Packages[id] = NuGetVersion.Parse(version); | ||
} | ||
|
||
public bool IsAFrameworkComponent(string id, NuGetVersion version) => this.Packages.TryGetValue(id, out var frameworkPackageVersion) && frameworkPackageVersion >= version; | ||
|
||
IEnumerator<KeyValuePair<string, NuGetVersion>> IEnumerable<KeyValuePair<string, NuGetVersion>>.GetEnumerator() => this.Packages.GetEnumerator(); | ||
Check warning on line 119 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
|
||
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException(); | ||
Check warning on line 121 in src/Microsoft.ComponentDetection.Detectors/nuget/FrameworkPackages/FrameworkPackages.cs
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
namespace Microsoft.ComponentDetection.Detectors.NuGet; | ||
|
||
using global::NuGet.Frameworks; | ||
|
||
/// <summary> | ||
/// Framework packages for net5.0. | ||
/// </summary> | ||
internal partial class FrameworkPackages | ||
{ | ||
internal static class NETCoreApp50 | ||
{ | ||
internal static FrameworkPackages Instance { get; } = new(NuGetFramework.Parse("net5.0"), NETCoreApp31.Instance) | ||
{ | ||
{ "Microsoft.CSharp", "4.7.0" }, | ||
{ "runtime.debian.8-x64.runtime.native.System", "4.3.0" }, | ||
{ "runtime.debian.8-x64.runtime.native.System.IO.Compression", "4.3.0" }, | ||
{ "runtime.debian.8-x64.runtime.native.System.Net.Http", "4.3.0" }, | ||
{ "runtime.debian.8-x64.runtime.native.System.Net.Security", "4.3.0" }, | ||
{ "runtime.debian.8-x64.runtime.native.System.Security.Cryptography", "4.3.0" }, | ||
{ "runtime.fedora.23-x64.runtime.native.System", "4.3.0" }, | ||
{ "runtime.fedora.23-x64.runtime.native.System.IO.Compression", "4.3.0" }, | ||
{ "runtime.fedora.23-x64.runtime.native.System.Net.Http", "4.3.0" }, | ||
{ "runtime.fedora.23-x64.runtime.native.System.Net.Security", "4.3.0" }, | ||
{ "runtime.fedora.23-x64.runtime.native.System.Security.Cryptography", "4.3.0" }, | ||
{ "runtime.fedora.24-x64.runtime.native.System", "4.3.0" }, | ||
{ "runtime.fedora.24-x64.runtime.native.System.IO.Compression", "4.3.0" }, | ||
{ "runtime.fedora.24-x64.runtime.native.System.Net.Http", "4.3.0" }, | ||
{ "runtime.fedora.24-x64.runtime.native.System.Net.Security", "4.3.0" }, | ||
{ "runtime.fedora.24-x64.runtime.native.System.Security.Cryptography", "4.3.0" }, | ||
{ "runtime.opensuse.13.2-x64.runtime.native.System", "4.3.0" }, | ||
{ "runtime.opensuse.13.2-x64.runtime.native.System.IO.Compression", "4.3.0" }, | ||
{ "runtime.opensuse.13.2-x64.runtime.native.System.Net.Http", "4.3.0" }, | ||
{ "runtime.opensuse.13.2-x64.runtime.native.System.Net.Security", "4.3.0" }, | ||
{ "runtime.opensuse.13.2-x64.runtime.native.System.Security.Cryptography", "4.3.0" }, | ||
{ "runtime.opensuse.42.1-x64.runtime.native.System", "4.3.0" }, | ||
{ "runtime.opensuse.42.1-x64.runtime.native.System.IO.Compression", "4.3.0" }, | ||
{ "runtime.opensuse.42.1-x64.runtime.native.System.Net.Http", "4.3.0" }, | ||
{ "runtime.opensuse.42.1-x64.runtime.native.System.Net.Security", "4.3.0" }, | ||
{ "runtime.opensuse.42.1-x64.runtime.native.System.Security.Cryptography", "4.3.0" }, | ||
{ "runtime.osx.10.10-x64.runtime.native.System", "4.3.0" }, | ||
{ "runtime.osx.10.10-x64.runtime.native.System.IO.Compression", "4.3.0" }, | ||
{ "runtime.osx.10.10-x64.runtime.native.System.Net.Http", "4.3.0" }, | ||
{ "runtime.osx.10.10-x64.runtime.native.System.Net.Security", "4.3.0" }, | ||
{ "runtime.osx.10.10-x64.runtime.native.System.Security.Cryptography", "4.3.0" }, | ||
{ "runtime.rhel.7-x64.runtime.native.System", "4.3.0" }, | ||
{ "runtime.rhel.7-x64.runtime.native.System.IO.Compression", "4.3.0" }, | ||
{ "runtime.rhel.7-x64.runtime.native.System.Net.Http", "4.3.0" }, | ||
{ "runtime.rhel.7-x64.runtime.native.System.Net.Security", "4.3.0" }, | ||
{ "runtime.rhel.7-x64.runtime.native.System.Security.Cryptography", "4.3.0" }, | ||
{ "runtime.ubuntu.14.04-x64.runtime.native.System", "4.3.0" }, | ||
{ "runtime.ubuntu.14.04-x64.runtime.native.System.IO.Compression", "4.3.0" }, | ||
{ "runtime.ubuntu.14.04-x64.runtime.native.System.Net.Http", "4.3.0" }, | ||
{ "runtime.ubuntu.14.04-x64.runtime.native.System.Net.Security", "4.3.0" }, | ||
{ "runtime.ubuntu.14.04-x64.runtime.native.System.Security.Cryptography", "4.3.0" }, | ||
{ "runtime.ubuntu.16.04-x64.runtime.native.System", "4.3.0" }, | ||
{ "runtime.ubuntu.16.04-x64.runtime.native.System.IO.Compression", "4.3.0" }, | ||
{ "runtime.ubuntu.16.04-x64.runtime.native.System.Net.Http", "4.3.0" }, | ||
{ "runtime.ubuntu.16.04-x64.runtime.native.System.Net.Security", "4.3.0" }, | ||
{ "runtime.ubuntu.16.04-x64.runtime.native.System.Security.Cryptography", "4.3.0" }, | ||
{ "runtime.ubuntu.16.10-x64.runtime.native.System", "4.3.0" }, | ||
{ "runtime.ubuntu.16.10-x64.runtime.native.System.IO.Compression", "4.3.0" }, | ||
{ "runtime.ubuntu.16.10-x64.runtime.native.System.Net.Http", "4.3.0" }, | ||
{ "runtime.ubuntu.16.10-x64.runtime.native.System.Net.Security", "4.3.0" }, | ||
{ "runtime.ubuntu.16.10-x64.runtime.native.System.Security.Cryptography", "4.3.0" }, | ||
{ "System.Buffers", "4.5.1" }, | ||
{ "System.Collections.Immutable", "5.0.0" }, | ||
{ "System.ComponentModel.Annotations", "5.0.0" }, | ||
{ "System.Diagnostics.DiagnosticSource", "5.0.0" }, | ||
{ "System.Formats.Asn1", "5.0.0" }, | ||
{ "System.Net.Http.Json", "5.0.0" }, | ||
{ "System.Reflection.DispatchProxy", "4.7.1" }, | ||
{ "System.Reflection.Metadata", "5.0.0" }, | ||
{ "System.Runtime.CompilerServices.Unsafe", "5.0.0" }, | ||
{ "System.Text.Encoding.CodePages", "5.0.0" }, | ||
{ "System.Text.Encodings.Web", "5.0.0" }, | ||
{ "System.Text.Json", "5.0.0" }, | ||
{ "System.Threading.Channels", "5.0.0" }, | ||
{ "System.Threading.Tasks.Dataflow", "5.0.0" }, | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace Microsoft.ComponentDetection.Detectors.NuGet; | ||
|
||
using global::NuGet.Frameworks; | ||
|
||
/// <summary> | ||
/// Framework packages for net6.0. | ||
/// </summary> | ||
internal partial class FrameworkPackages | ||
{ | ||
internal static class NETCoreApp60 | ||
{ | ||
internal static FrameworkPackages Instance { get; } = new(NuGetFramework.Parse("net6.0"), NETCoreApp50.Instance) | ||
{ | ||
{ "Microsoft.Win32.Registry", "5.0.0" }, | ||
{ "System.Collections.Immutable", "6.0.0" }, | ||
{ "System.Diagnostics.DiagnosticSource", "6.0.1" }, | ||
{ "System.Formats.Asn1", "6.0.1" }, | ||
{ "System.IO.FileSystem.AccessControl", "5.0.0" }, | ||
{ "System.IO.Pipes.AccessControl", "5.0.0" }, | ||
{ "System.Net.Http.Json", "6.0.1" }, | ||
{ "System.Reflection.Metadata", "6.0.1" }, | ||
{ "System.Runtime.CompilerServices.Unsafe", "6.0.0" }, | ||
{ "System.Security.AccessControl", "6.0.1" }, | ||
{ "System.Security.Cryptography.Cng", "5.0.0" }, | ||
{ "System.Security.Cryptography.OpenSsl", "5.0.0" }, | ||
{ "System.Security.Principal.Windows", "5.0.0" }, | ||
{ "System.Text.Encoding.CodePages", "6.0.0" }, | ||
{ "System.Text.Encodings.Web", "6.0.0" }, | ||
{ "System.Text.Json", "6.0.9" }, | ||
{ "System.Threading.Channels", "6.0.0" }, | ||
{ "System.Threading.Tasks.Dataflow", "6.0.0" }, | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace Microsoft.ComponentDetection.Detectors.NuGet; | ||
|
||
using global::NuGet.Frameworks; | ||
|
||
/// <summary> | ||
/// Framework packages for net7.0. | ||
/// </summary> | ||
internal partial class FrameworkPackages | ||
{ | ||
internal static class NETCoreApp70 | ||
{ | ||
internal static FrameworkPackages Instance { get; } = new(NuGetFramework.Parse("net7.0"), NETCoreApp60.Instance) | ||
{ | ||
{ "System.Collections.Immutable", "7.0.0" }, | ||
{ "System.Diagnostics.DiagnosticSource", "7.0.2" }, | ||
{ "System.Formats.Asn1", "7.0.0" }, | ||
{ "System.Net.Http.Json", "7.0.1" }, | ||
{ "System.Reflection.Metadata", "7.0.2" }, | ||
{ "System.Text.Encoding.CodePages", "7.0.0" }, | ||
{ "System.Text.Encodings.Web", "7.0.0" }, | ||
{ "System.Text.Json", "7.0.4" }, | ||
{ "System.Threading.Channels", "7.0.0" }, | ||
{ "System.Threading.Tasks.Dataflow", "7.0.0" }, | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace Microsoft.ComponentDetection.Detectors.NuGet; | ||
|
||
using global::NuGet.Frameworks; | ||
|
||
/// <summary> | ||
/// Framework packages for net8.0. | ||
/// </summary> | ||
internal partial class FrameworkPackages | ||
{ | ||
internal static class NETCoreApp80 | ||
{ | ||
internal static FrameworkPackages Instance { get; } = new(NuGetFramework.Parse("net8.0"), NETCoreApp70.Instance) | ||
{ | ||
{ "System.Collections.Immutable", "8.0.0" }, | ||
{ "System.Diagnostics.DiagnosticSource", "8.0.1" }, | ||
{ "System.Formats.Asn1", "8.0.1" }, | ||
{ "System.Net.Http.Json", "8.0.0" }, | ||
{ "System.Reflection.Metadata", "8.0.0" }, | ||
{ "System.Text.Encoding.CodePages", "8.0.0" }, | ||
{ "System.Text.Encodings.Web", "8.0.0" }, | ||
{ "System.Text.Json", "8.0.4" }, | ||
{ "System.Threading.Channels", "8.0.0" }, | ||
{ "System.Threading.Tasks.Dataflow", "8.0.1" }, | ||
}; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.