-
Notifications
You must be signed in to change notification settings - Fork 753
Expand file tree
/
Copy pathPackageArgument.cs
More file actions
110 lines (92 loc) · 3.52 KB
/
PackageArgument.cs
File metadata and controls
110 lines (92 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.Collections.Generic;
using System.CommandLine.Parsing;
using System.Diagnostics.CodeAnalysis;
namespace NuGet.CommandLine.XPlat.Commands.Package
{
internal record PackageArgument<TVersion> : IEqualityComparer<PackageArgument<TVersion>>
{
public required string Id { get; init; }
public required TVersion? Version { get; init; }
internal delegate bool TryParseVersion(string value, out TVersion? version);
private readonly IEqualityComparer<TVersion?> _versionComparer;
public PackageArgument(IEqualityComparer<TVersion?> versionComparer)
{
_versionComparer = versionComparer;
}
public static IReadOnlyList<PackageArgument<TVersion>> Parse(
ArgumentResult result,
TryParseVersion parseVersion,
Func<string, string> getInvalidVersionMessage,
IEqualityComparer<TVersion?> versionComparer)
{
ArgumentNullException.ThrowIfNull(result);
ArgumentNullException.ThrowIfNull(parseVersion);
ArgumentNullException.ThrowIfNull(getInvalidVersionMessage);
if (result.Tokens.Count == 0)
{
return [];
}
List<PackageArgument<TVersion>> packages = new List<PackageArgument<TVersion>>(result.Tokens.Count);
foreach (var token in result.Tokens)
{
string? packageId;
TVersion? newVersion;
int separatorIndex = token.Value.IndexOf('@');
if (separatorIndex < 0)
{
packageId = token.Value;
newVersion = default;
}
else
{
packageId = token.Value[..separatorIndex];
string versionString = token.Value[(separatorIndex + 1)..];
if (string.IsNullOrEmpty(versionString))
{
result.AddError(Messages.Error_MissingVersion(token.Value));
return [];
}
if (!parseVersion(versionString, out newVersion))
{
result.AddError(getInvalidVersionMessage(versionString));
return [];
}
}
var package = new PackageArgument<TVersion>(versionComparer)
{
Id = packageId,
Version = newVersion
};
packages.Add(package);
}
return packages;
}
public bool Equals(PackageArgument<TVersion>? x, PackageArgument<TVersion>? y)
{
if (ReferenceEquals(x, y))
{
return true;
}
if (x is null || y is null)
{
return false;
}
if (!x.Id!.Equals(y.Id, StringComparison.OrdinalIgnoreCase))
{
return false;
}
return _versionComparer.Equals(x.Version, y.Version);
}
public int GetHashCode([DisallowNull] PackageArgument<TVersion> obj)
{
HashCode hash = new HashCode();
hash.Add(obj.Id);
hash.Add(obj.Version);
return hash.ToHashCode();
}
}
}