isn/src/isnd/Services/PackageManager.cs

222 lines
7.1 KiB
C#
Raw Normal View History

2021-08-23 03:21:08 +01:00
using System;
2021-07-05 12:55:52 +01:00
using System.Collections.Generic;
using System.Linq;
2021-08-15 19:09:01 +01:00
using isnd.Controllers;
using isnd.Data;
2021-08-23 03:21:08 +01:00
using isnd.Data.Catalog;
using isnd.Entities;
using isnd.Interfaces;
2021-08-11 23:25:15 +01:00
using isnd.ViewModels;
2021-07-05 12:55:52 +01:00
using Microsoft.EntityFrameworkCore;
2021-08-23 03:21:08 +01:00
using Microsoft.Extensions.Options;
2021-07-05 12:55:52 +01:00
using NuGet.Versioning;
using Unleash;
namespace isnd.Services
{
2021-08-23 03:21:08 +01:00
public class PackageManager : IPackageManager
2021-07-05 12:55:52 +01:00
{
ApplicationDbContext dbContext;
2021-08-23 03:21:08 +01:00
public PackageManager(ApplicationDbContext dbContext, IOptions<IsndSettings> pmConfigOptions)
2021-07-05 12:55:52 +01:00
{
this.dbContext = dbContext;
2021-08-23 03:21:08 +01:00
CurrentCatalogIndex = GetCatalogIndex();
this.pmConfigOptions = pmConfigOptions.Value;
2021-07-05 12:55:52 +01:00
}
2021-08-23 03:21:08 +01:00
public PackageIndexViewModel SearchByName(string query,
int skip, int take, bool prerelease = false,
2021-07-05 12:55:52 +01:00
string packageType = null)
{
2021-08-10 00:10:26 +01:00
2021-07-05 12:55:52 +01:00
var scope = dbContext.Packages
.Include(p => p.Versions)
.Where(
p => (CamelCaseMatch(p.Id, query) || SeparatedByMinusMatch(p.Id, query))
&& (prerelease || p.Versions.Any(v => !v.IsPrerelease))
&& (packageType == null || p.Versions.Any(v => v.Type == packageType))
);
2021-08-10 00:10:26 +01:00
var total = scope.Count();
var pkgs = scope.Skip(skip).Take(take).ToArray();
2021-08-11 23:25:15 +01:00
return new PackageIndexViewModel
2021-08-23 03:21:08 +01:00
{
query = query,
totalHits = total,
data = pkgs
};
}
const int maxPageLen = 512;
public CatalogIndex GenerateCatalogIndex(string commitId)
{
var root = "/index.json";
var catalog = new CatalogIndex
{
CommitId = commitId,
CommitTimeStamp = DateTime.Now
};
var scope = dbContext.Packages.Where(p => p.Public)
.OrderBy(p => p.CommitTimeStamp);
catalog.Items = new List<PageRef>();
int pagecount = (int)(scope.Count() / maxPageLen);
for (int pagelen = 0, pagenum = 0; pagelen < pagecount; pagenum++)
{
Page p = new Page
2021-07-05 12:55:52 +01:00
{
2021-08-23 03:21:08 +01:00
2021-07-05 12:55:52 +01:00
};
2021-08-23 03:21:08 +01:00
}
dbContext.Packages.Where(p => p.Public)
.OrderBy(p => p.CommitTimeStamp);
return catalog;
2021-07-05 12:55:52 +01:00
}
2021-08-23 03:21:08 +01:00
public Page CatalogPage()
{
var scope = dbContext.Packages
.Where(P => P.Versions.Count > 0)
.Select(
p => new PackageRef { Id = p.Id, Version = p.Versions.Max().FullString });
return new Page
{
Items = scope.ToList()
};
}
public void PublishCatalog()
{
}
public AutoCompleteResult AutoComplete(string id,
int skip, int take, bool prerelease = false,
2021-07-05 12:55:52 +01:00
string packageType = null)
{
var scope = dbContext.PackageVersions.Where(
v => v.PackageId == id
&& (prerelease || !v.IsPrerelease)
&& (packageType == null || v.Type == packageType)
)
.OrderBy(v => v.FullString);
return new AutoCompleteResult
2021-08-23 03:21:08 +01:00
{
totalHits = scope.Count(),
data = scope.Select(v => v.FullString)
2021-07-05 12:55:52 +01:00
.Skip(skip).Take(take).ToArray()
2021-08-23 03:21:08 +01:00
};
2021-07-05 12:55:52 +01:00
}
// TODO stocker MetaData plutôt que FullString en base,
// et en profiter pour corriger ce listing
public string[] GetVersions(
string id,
NuGetVersion parsedVersion,
bool prerelease = false,
string packageType = null,
int skip = 0,
int take = 25)
{
return dbContext.PackageVersions.Where(
v => v.PackageId == id
&& (prerelease || !v.IsPrerelease)
&& (packageType == null || v.Type == packageType)
&& (parsedVersion.CompareTo(new SemanticVersion(v.Major, v.Minor, v.Patch)) < 0)
)
.OrderBy(v => v.FullString)
.Select(v => v.FullString)
.Skip(skip).Take(take).ToArray();
}
2021-08-23 03:21:08 +01:00
public static CatalogIndex CurrentCatalogIndex { get; protected set; }
private IsndSettings pmConfigOptions;
public virtual CatalogIndex GetCatalogIndex()
{
if (CurrentCatalogIndex == null)
{
LoadCatalogFromDb();
}
return CurrentCatalogIndex;
}
void LoadCatalogFromDb()
{
dbContext.Commits.OrderBy(c => c.TimeStamp);
throw new NotImplementedException();
}
protected static bool CamelCaseMatch(string id, string query)
2021-07-05 12:55:52 +01:00
{
// Assert.False (q==null);
2021-08-23 03:21:08 +01:00
if (string.IsNullOrEmpty(query)) return true;
2021-07-05 12:55:52 +01:00
while (id.Length > 0)
{
int i = 0;
while (id.Length > i && char.IsLower(id[i])) i++;
if (i == 0) break;
id = id.Substring(i);
2021-08-23 03:21:08 +01:00
if (id.StartsWith(query, System.StringComparison.OrdinalIgnoreCase)) return true;
2021-07-05 12:55:52 +01:00
}
return false;
}
protected static bool SeparatedByMinusMatch(string id, string q)
{
foreach (var part in id.Split('-'))
{
if (part.StartsWith(q, System.StringComparison.OrdinalIgnoreCase)) return true;
}
return false;
}
2021-08-23 03:21:08 +01:00
public IEnumerable<Resource> GetResources(IUnleash unleashClient)
2021-07-05 12:55:52 +01:00
{
var res = new List<Resource>();
if (unleashClient.IsEnabled("pkg-push"))
res.Add(
new Resource
{
id = "package",
type = "PackagePublish/2.0.0",
comment = "Package Publish service"
});
if (unleashClient.IsEnabled("pkg-get"))
res.Add(
new Resource
{
id = "package",
type = "PackageBaseAddress/3.0.0",
comment = "Package Base Address service"
});
if (unleashClient.IsEnabled("pkg-autocomplete"))
res.Add(
new Resource
{
2021-08-23 03:21:08 +01:00
id = "package",
2021-07-05 12:55:52 +01:00
type = "SearchAutocompleteService/3.5.0",
comment = "Auto complete service"
});
if (unleashClient.IsEnabled("pkg-search"))
res.Add(
new Resource
{
2021-08-23 03:21:08 +01:00
id = "package",
2021-07-05 12:55:52 +01:00
type = "SearchQueryService/3.5.0",
comment = "Search Query service"
});
return res;
}
}
}