Skip to content

Commit d4425db

Browse files
authored
Enabling nullability in NuGet.Packaging - phase 1, models and simple classes (#7176)
1 parent 1158132 commit d4425db

40 files changed

Lines changed: 437 additions & 483 deletions

src/NuGet.Clients/NuGet.VisualStudio.Internal.Contracts/Formatters/PackageIdentityFormatter.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ private PackageIdentityFormatter()
4040
break;
4141
}
4242
}
43-
44-
return new PackageIdentity(id, version);
43+
if (id != null)
44+
{
45+
return new PackageIdentity(id, version);
46+
}
47+
return null;
4548
}
4649

4750
protected override void SerializeCore(ref MessagePackWriter writer, PackageIdentity value, MessagePackSerializerOptions options)

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Package/Update/PackageUpdateCommandRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ internal static async Task<int> Run(PackageUpdateArgs args, ILoggerWithColor log
180180
.SelectMany(tf => tf.Libraries.Select(library => (tf.TargetFramework, library)))
181181
.Where(tuple => tuple.library.Type == "package" && packageIdsWithVulnerabilities.Contains(tuple.library.Name!) && PackageHasVulnerability(tuple.library.Name!, tuple.library.Version!, knownVulnerabilities))
182182
.GroupBy(
183-
pair => new PackageIdentity(pair.library.Name, pair.library.Version),
183+
pair => new PackageIdentity(pair.library.Name!, pair.library.Version),
184184
pair => assetsFile.PackageSpec.TargetFrameworks.Single(tfm => tfm.FrameworkName == pair.TargetFramework).TargetAlias,
185185
(key, g) => (key, g.Distinct().ToList()))
186186
.ToList();

src/NuGet.Core/NuGet.Commands/PackagesLockFileBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public PackagesLockFile CreateNuGetLockFile(LockFile assetsFile)
4545

4646
foreach (var library in libraries.Where(e => e.Type == LibraryType.Package))
4747
{
48-
var identity = new PackageIdentity(library.Name, library.Version);
48+
var identity = new PackageIdentity(library.Name!, library.Version);
4949

5050
var dependency = new LockFileDependency()
5151
{
@@ -92,7 +92,7 @@ public PackagesLockFile CreateNuGetLockFile(LockFile assetsFile)
9292

9393
foreach (var projectReference in libraries.Where(e => e.Type == LibraryType.Project || e.Type == LibraryType.ExternalProject))
9494
{
95-
var projectIdentity = new PackageIdentity(projectReference.Name, projectReference.Version);
95+
var projectIdentity = new PackageIdentity(projectReference.Name!, projectReference.Version);
9696
var projectFullPath = projectFullPaths[projectIdentity];
9797
var id = PathUtility.GetStringComparerBasedOnOS().Equals(Path.GetFileNameWithoutExtension(projectFullPath), projectReference.Name)
9898
? projectReference.Name.ToLowerInvariant()

src/NuGet.Core/NuGet.Packaging/Core/ContentFilesEntry.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75

86
namespace NuGet.Packaging.Core
@@ -21,12 +19,12 @@ public class ContentFilesEntry
2119
/// <summary>
2220
/// Excluded files
2321
/// </summary>
24-
public string Exclude { get; }
22+
public string? Exclude { get; }
2523

2624
/// <summary>
2725
/// Build action
2826
/// </summary>
29-
public string BuildAction { get; }
27+
public string? BuildAction { get; }
3028

3129
/// <summary>
3230
/// If true the item will be copied to the output folder.
@@ -41,8 +39,8 @@ public class ContentFilesEntry
4139

4240
public ContentFilesEntry(
4341
string include,
44-
string exclude,
45-
string buildAction,
42+
string? exclude,
43+
string? buildAction,
4644
bool? copyToOutput,
4745
bool? flatten)
4846
{

src/NuGet.Core/NuGet.Packaging/Core/FrameworkReference.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Collections.Generic;
86
using NuGet.Common;
@@ -19,12 +17,27 @@ public FrameworkReference(string name)
1917
Name = name ?? throw new ArgumentNullException(nameof(name));
2018
}
2119

22-
public int Compare(FrameworkReference x, FrameworkReference y)
20+
public int Compare(FrameworkReference? x, FrameworkReference? y)
2321
{
22+
if (ReferenceEquals(x, y))
23+
{
24+
return 0;
25+
}
26+
27+
if (x is null)
28+
{
29+
return -1;
30+
}
31+
32+
if (y is null)
33+
{
34+
return 1;
35+
}
36+
2437
return ComparisonUtility.FrameworkReferenceNameComparer.Compare(x.Name, y.Name);
2538
}
2639

27-
public bool Equals(FrameworkReference other)
40+
public bool Equals(FrameworkReference? other)
2841
{
2942
if (other == null)
3043
{
@@ -39,7 +52,7 @@ public bool Equals(FrameworkReference other)
3952
return ComparisonUtility.FrameworkReferenceNameComparer.Equals(Name, other.Name);
4053
}
4154

42-
public override bool Equals(object obj)
55+
public override bool Equals(object? obj)
4356
{
4457
return Equals(obj as FrameworkReference);
4558
}
@@ -51,7 +64,7 @@ public override int GetHashCode()
5164
return combiner.CombinedHash;
5265
}
5366

54-
public int CompareTo(FrameworkReference other)
67+
public int CompareTo(FrameworkReference? other)
5568
{
5669
return Compare(this, other);
5770
}

src/NuGet.Core/NuGet.Packaging/Core/FrameworkReferenceGroup.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Collections.Generic;
86
using NuGet.Frameworks;
@@ -36,7 +34,7 @@ public FrameworkReferenceGroup(NuGetFramework targetFramework, IEnumerable<Frame
3634
/// </summary>
3735
public IEnumerable<FrameworkReference> FrameworkReferences { get; }
3836

39-
public bool Equals(FrameworkReferenceGroup other)
37+
public bool Equals(FrameworkReferenceGroup? other)
4038
{
4139
if (ReferenceEquals(other, null))
4240
{
@@ -52,7 +50,7 @@ public bool Equals(FrameworkReferenceGroup other)
5250
FrameworkReferences.OrderedEquals(other.FrameworkReferences, dependency => dependency);
5351
}
5452

55-
public override bool Equals(object obj)
53+
public override bool Equals(object? obj)
5654
{
5755
return Equals(obj as FrameworkReferenceGroup);
5856
}

src/NuGet.Core/NuGet.Packaging/Core/FrameworkSpecificGroup.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Collections.Generic;
86
using System.Linq;
@@ -66,7 +64,7 @@ public IEnumerable<string> Items
6664

6765
public bool HasEmptyFolder { get; }
6866

69-
public bool Equals(FrameworkSpecificGroup other)
67+
public bool Equals(FrameworkSpecificGroup? other)
7068
{
7169
if (ReferenceEquals(this, other))
7270
{
@@ -81,7 +79,7 @@ public bool Equals(FrameworkSpecificGroup other)
8179
return GetHashCode() == other.GetHashCode();
8280
}
8381

84-
public override bool Equals(object obj)
82+
public override bool Equals(object? obj)
8583
{
8684
var other = obj as FrameworkSpecificGroup;
8785

src/NuGet.Core/NuGet.Packaging/Core/PackageDependency.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Collections.Generic;
86
using System.Globalization;
@@ -49,16 +47,16 @@ public PackageDependency(string id)
4947
}
5048

5149
[JsonConstructor]
52-
public PackageDependency(string id, VersionRange versionRange)
50+
public PackageDependency(string id, VersionRange? versionRange)
5351
: this(id, versionRange, include: null, exclude: null)
5452
{
5553
}
5654

5755
public PackageDependency(
5856
string id,
59-
VersionRange versionRange,
60-
IReadOnlyList<string> include,
61-
IReadOnlyList<string> exclude)
57+
VersionRange? versionRange,
58+
IReadOnlyList<string>? include,
59+
IReadOnlyList<string>? exclude)
6260
{
6361
if (string.IsNullOrEmpty(id))
6462
{
@@ -71,12 +69,12 @@ public PackageDependency(
7169
Exclude = exclude ?? EmptyList;
7270
}
7371

74-
public bool Equals(PackageDependency other)
72+
public bool Equals(PackageDependency? other)
7573
{
7674
return PackageDependencyComparer.Default.Equals(this, other);
7775
}
7876

79-
public override bool Equals(object obj)
77+
public override bool Equals(object? obj)
8078
{
8179
var dependency = obj as PackageDependency;
8280

src/NuGet.Core/NuGet.Packaging/Core/PackageDependencyGroup.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Collections.Generic;
86
using System.Globalization;
@@ -22,7 +20,7 @@ public class PackageDependencyGroup : IEquatable<PackageDependencyGroup>, IFrame
2220
private readonly IEnumerable<PackageDependency> _packages;
2321

2422
[JsonConstructor]
25-
private PackageDependencyGroup(NuGetFramework targetFramework)
23+
private PackageDependencyGroup(NuGetFramework? targetFramework)
2624
{
2725
if (targetFramework == null)
2826
{
@@ -75,12 +73,12 @@ public IEnumerable<PackageDependency> Packages
7573
get { return _packages; }
7674
}
7775

78-
public override bool Equals(object obj)
76+
public override bool Equals(object? obj)
7977
{
8078
return Equals(obj as PackageDependencyGroup);
8179
}
8280

83-
public bool Equals(PackageDependencyGroup other)
81+
public bool Equals(PackageDependencyGroup? other)
8482
{
8583
if (other == null)
8684
{

src/NuGet.Core/NuGet.Packaging/Core/PackageDependencyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class PackageDependencyInfo : PackageIdentity, IEquatable<PackageDependen
2323
/// </summary>
2424
public IEnumerable<PackageDependency> Dependencies { get; }
2525

26-
public PackageDependencyInfo(string id, NuGetVersion version)
26+
public PackageDependencyInfo(string id, NuGetVersion? version)
2727
: this(id, version, null)
2828
{
2929
}
@@ -39,7 +39,7 @@ public PackageDependencyInfo(PackageIdentity identity, IEnumerable<PackageDepend
3939
/// <param name="id">package name</param>
4040
/// <param name="version">package version</param>
4141
/// <param name="dependencies">package dependencies</param>
42-
public PackageDependencyInfo(string id, NuGetVersion version, IEnumerable<PackageDependency>? dependencies)
42+
public PackageDependencyInfo(string id, NuGetVersion? version, IEnumerable<PackageDependency>? dependencies)
4343
: base(id, version)
4444
{
4545
Dependencies = dependencies?.ToArray() ?? Array.Empty<PackageDependency>();

0 commit comments

Comments
 (0)