Skip to content

Commit

Permalink
Merge pull request #83 from svick/version-comparison
Browse files Browse the repository at this point in the history
Use NuGet.Versioning.SemanticVersion to correctly compare package versions
  • Loading branch information
praeclarum committed Oct 14, 2019
2 parents 7fef80b + 86c5ed5 commit c9e7aec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 37 deletions.
46 changes: 9 additions & 37 deletions Data/PackageVersions.cs
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using NuGet.Versioning;

namespace FuGetGallery
{
Expand Down Expand Up @@ -120,11 +120,8 @@ public class PackageVersion : IComparable<PackageVersion>
{
string versionString = "";

public int Major { get; private set; }
public int Minor { get; private set; }
public int Patch { get; private set; }
public int Build { get; private set; }
public string Rest { get; private set; } = "";
public SemanticVersion SemanticVersion { get; private set; }

public DateTime? PublishTime { get; set; }

public bool IsPublished => PublishTime.HasValue && PublishTime.Value.Year > 1970;
Expand All @@ -135,38 +132,13 @@ public class PackageVersion : IComparable<PackageVersion>
if (versionString == value || string.IsNullOrEmpty (value))
return;
versionString = value;
var di = value.IndexOf ('-');
var vpart = di > 0 ? value.Substring (0, di) : value;
var rest = di > 0 ? value.Substring (di) : "";
var parts = vpart.Split ('.');
var maj = 0;
var min = 0;
var patch = 0;
var build = 0;
if (parts.Length > 0) int.TryParse(parts[0], out maj);
if (parts.Length > 1) int.TryParse(parts[1], out min);
if (parts.Length > 2) int.TryParse(parts[2], out patch);
if (parts.Length > 3) int.TryParse (parts[3], out build);
Major = maj;
Minor = min;
Patch = patch;
Build = build;
Rest = rest;

SemanticVersion.TryParse (value, out var semanticVersion);
SemanticVersion = semanticVersion;
}
}

public int CompareTo(PackageVersion other)
{
var c = Major.CompareTo(other.Major);
if (c != 0) return c;
c = Minor.CompareTo(other.Minor);
if (c != 0) return c;
c = Patch.CompareTo(other.Patch);
if (c != 0) return c;
c = Build.CompareTo (other.Build);
if (c != 0) return c;
return string.Compare(Rest, other.Rest, StringComparison.Ordinal);
}
public int CompareTo(PackageVersion other) => Comparer<SemanticVersion>.Default.Compare (SemanticVersion, other.SemanticVersion);

public override bool Equals(object obj)
{
Expand Down
1 change: 1 addition & 0 deletions FuGetGallery.csproj
Expand Up @@ -11,6 +11,7 @@
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.2.0" />
<PackageReference Include="NGraphics" Version="0.6.0-beta1" />
<PackageReference Include="NuGet.Versioning" Version="5.3.0" />
<PackageReference Include="sqlite-net-pcl" Version="1.6.258-beta" />
<PackageReference Include="System.CodeDom" Version="4.5.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
Expand Down

0 comments on commit c9e7aec

Please sign in to comment.