isn/src/isnd/Data/Catalog/CatalogPage.cs

100 lines
3.1 KiB
C#

3 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using isnd.Data.Packages;
using Newtonsoft.Json;
using NuGet.Versioning;
namespace isnd.Data.Catalog
{
3 years ago
public class CatalogPage : Permalink
3 years ago
{
private readonly string pkgid;
3 years ago
private readonly List<Package> items;
3 years ago
3 years ago
private readonly string apiBase;
3 years ago
3 years ago
public CatalogPage (string pkgid, string apiBase) : base(apiBase + $"/registration/{pkgid}/index.json")
3 years ago
{
3 years ago
Parent = apiBase + $"/registration/{pkgid}/index.json";
this.items = new List<Package>();
3 years ago
this.pkgid = pkgid;
3 years ago
this.apiBase = apiBase;
3 years ago
}
2 years ago
public CatalogPage(string pkgid, string apiBase, List<PackageVersion> versions) : this(pkgid, apiBase)
3 years ago
{
AddVersionRange(versions);
}
public string GetPackageId()
{
return pkgid;
}
/// <summary>
/// The array of registration leaves and their associate metadata
/// </summary>
/// <value></value>
[JsonProperty("items")]
3 years ago
public Package[] Items { get => items.ToArray(); }
3 years ago
public void AddVersionRange(IEnumerable<PackageVersion> vitems)
{
if (vitems.Count() == 0) return;
NuGetVersion upper = null;
NuGetVersion lower = null;
3 years ago
PackageVersion latest = null;
3 years ago
if (Lower!=null) lower = new NuGetVersion(Lower);
if (Upper!=null) upper = new NuGetVersion(Upper);
// Assert.True(items.All(p=>p.Id == id));
long commitMax = 0;
foreach (var p in vitems)
{
if (p.LatestCommit==null) continue;
3 years ago
var pkg = p.ToPackage(apiBase);
if (items.Contains(pkg)) continue;
3 years ago
if (upper == null) upper = p.NugetVersion;
else if ( upper < p.NugetVersion) upper = p.NugetVersion;
if (lower == null) lower = p.NugetVersion;
else if (lower > p.NugetVersion) lower = p.NugetVersion;
3 years ago
if (p.CommitNId > commitMax)
{
latest = p;
}
items.Add(pkg);
3 years ago
}
Upper = upper?.ToFullString() ?? lower?.ToFullString();
Lower = lower?.ToFullString() ?? upper?.ToFullString();
3 years ago
}
/// <summary>
/// The highest SemVer 2.0.0 version in the page (inclusive)
/// </summary>
/// <value></value>
[JsonProperty("upper"), JsonRequired]
public string Upper { get; private set; }
/// <summary>
/// The lowest SemVer 2.0.0 version in the page (inclusive)
/// </summary>
/// <value></value>
[JsonProperty("lower"), JsonRequired]
public string Lower { get; private set; }
/// <summary>
/// The URL to the registration index
/// </summary>
/// <value></value>
[JsonProperty("parent")]
public string Parent { get; set; }
[JsonProperty("count")]
public int Count { get => items.Count; }
}
}