Skip to content
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

support multi-part requirements #10255

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class RequirementTests
[InlineData("2.0", "~> 1.0", false)]
[InlineData("1", "~> 1", true)]
[InlineData("2", "~> 1", false)]
[InlineData("5.3.8", "< 6, > 5.2.4", true)]
public void IsSatisfiedBy(string versionString, string requirementString, bool expected)
{
var version = NuGetVersion.Parse(versionString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,59 @@ namespace NuGetUpdater.Core.Analyze;
/// See Gem::Version for a description on how versions and requirements work
/// together in RubyGems.
/// </remarks>
public class Requirement
public abstract class Requirement
{
public abstract bool IsSatisfiedBy(NuGetVersion version);

private static readonly Dictionary<string, Version> BumpMap = [];
/// <summary>
/// Return a new version object where the next to the last revision
/// number is one greater (e.g., 5.3.1 => 5.4).
/// </summary>
/// <remarks>
/// This logic intended to be similar to RubyGems Gem::Version#bump
/// </remarks>
public static Version Bump(NuGetVersion version)
{
if (BumpMap.TryGetValue(version.OriginalVersion!, out var bumpedVersion))
{
return bumpedVersion;
}

var versionParts = version.OriginalVersion! // Get the original string this version was created from
.Split('-')[0] // Get the version part without pre-release
.Split('.') // Split into Major.Minor.Patch.Revision if present
.Select(int.Parse)
.ToArray();

if (versionParts.Length > 1)
{
versionParts = versionParts[..^1]; // Remove the last part
}

versionParts[^1]++; // Increment the new last part

bumpedVersion = NuGetVersion.Parse(string.Join('.', versionParts)).Version;
BumpMap[version.OriginalVersion!] = bumpedVersion;

return bumpedVersion;
}

public static Requirement Parse(string requirement)
{
var specificParts = requirement.Split(',');
if (specificParts.Length == 1)
{
return IndividualRequirement.ParseIndividual(requirement);
}

var specificRequirements = specificParts.Select(IndividualRequirement.ParseIndividual).ToArray();
return new MultiPartRequirement(specificRequirements);
}
}


public class IndividualRequirement : Requirement
{
private static readonly ImmutableDictionary<string, Func<NuGetVersion, NuGetVersion, bool>> Operators = new Dictionary<string, Func<NuGetVersion, NuGetVersion, bool>>()
{
Expand All @@ -25,30 +77,10 @@ public class Requirement
["~>"] = (v, r) => v >= r && v.Version < Bump(r),
}.ToImmutableDictionary();

public static Requirement Parse(string requirement)
{
var splitIndex = requirement.LastIndexOfAny(['=', '>', '<']);

// Throw if the requirement is all operator and no version.
if (splitIndex == requirement.Length - 1)
{
throw new ArgumentException($"`{requirement}` is a invalid requirement string", nameof(requirement));
}

string[] parts = splitIndex == -1
? [requirement.Trim()]
: [requirement[..(splitIndex + 1)].Trim(), requirement[(splitIndex + 1)..].Trim()];

var op = parts.Length == 1 ? "=" : parts[0];
var version = NuGetVersion.Parse(parts[^1]);

return new Requirement(op, version);
}

public string Operator { get; }
public NuGetVersion Version { get; }

public Requirement(string op, NuGetVersion version)
public IndividualRequirement(string op, NuGetVersion version)
{
if (!Operators.ContainsKey(op))
{
Expand All @@ -64,42 +96,53 @@ public override string ToString()
return $"{Operator} {Version}";
}

public bool IsSatisfiedBy(NuGetVersion version)
public override bool IsSatisfiedBy(NuGetVersion version)
{
return Operators[Operator](version, Version);
}

private static readonly Dictionary<string, Version> BumpMap = [];
/// <summary>
/// Return a new version object where the next to the last revision
/// number is one greater (e.g., 5.3.1 => 5.4).
/// </summary>
/// <remarks>
/// This logic intended to be similar to RubyGems Gem::Version#bump
/// </remarks>
public static Version Bump(NuGetVersion version)
public static IndividualRequirement ParseIndividual(string requirement)
{
if (BumpMap.TryGetValue(version.OriginalVersion!, out var bumpedVersion))
var splitIndex = requirement.LastIndexOfAny(['=', '>', '<']);

// Throw if the requirement is all operator and no version.
if (splitIndex == requirement.Length - 1)
{
return bumpedVersion;
throw new ArgumentException($"`{requirement}` is a invalid requirement string", nameof(requirement));
}

var versionParts = version.OriginalVersion! // Get the original string this version was created from
.Split('-')[0] // Get the version part without pre-release
.Split('.') // Split into Major.Minor.Patch.Revision if present
.Select(int.Parse)
.ToArray();
string[] parts = splitIndex == -1
? [requirement.Trim()]
: [requirement[..(splitIndex + 1)].Trim(), requirement[(splitIndex + 1)..].Trim()];

if (versionParts.Length > 1)
var op = parts.Length == 1 ? "=" : parts[0];
var version = NuGetVersion.Parse(parts[^1]);

return new IndividualRequirement(op, version);
}
}

public class MultiPartRequirement : Requirement
{
public ImmutableArray<IndividualRequirement> Parts { get; }

public MultiPartRequirement(IndividualRequirement[] parts)
{
if (parts.Length <= 1)
{
versionParts = versionParts[..^1]; // Remove the last part
throw new ArgumentException("At least two parts are required", nameof(parts));
}

versionParts[^1]++; // Increment the new last part
Parts = parts.ToImmutableArray();
}

bumpedVersion = NuGetVersion.Parse(string.Join('.', versionParts)).Version;
BumpMap[version.OriginalVersion!] = bumpedVersion;
public override string ToString()
{
return string.Join(", ", Parts);
}

return bumpedVersion;
public override bool IsSatisfiedBy(NuGetVersion version)
{
return Parts.All(part => part.IsSatisfiedBy(version));
}
}
Loading