2023-08-06 20:12:22 +00:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Net;
|
2023-08-03 19:21:06 +00:00
|
|
|
using System.Text;
|
|
|
|
using System.Text.Json;
|
|
|
|
using Pastel;
|
|
|
|
using TUI.Controls;
|
|
|
|
using TUI.Domain;
|
2023-08-06 20:12:22 +00:00
|
|
|
using TUI.Settings;
|
2023-08-03 19:21:06 +00:00
|
|
|
using TUI.UserInterface;
|
|
|
|
|
|
|
|
|
|
|
|
namespace TUI.Dashboards;
|
|
|
|
|
|
|
|
public class DependencyDashboard : IControl<Project>
|
|
|
|
{
|
2023-08-06 20:12:22 +00:00
|
|
|
private const int TitleWidth = 25;
|
2023-08-03 19:21:06 +00:00
|
|
|
private const int ColumnWidth = 10;
|
|
|
|
|
2023-08-06 20:12:22 +00:00
|
|
|
private readonly Table _table = new();
|
2023-08-03 19:21:06 +00:00
|
|
|
|
|
|
|
public void Render(Project project, Position position)
|
|
|
|
{
|
|
|
|
var dashboard = new Dashboard();
|
|
|
|
dashboard.Render(project.Icon, position);
|
|
|
|
|
|
|
|
var header = project.Dependencies.Select(GetConventionVersion).ToArray();
|
|
|
|
var rows = project.Sources.Select(GetTitle).ToArray();
|
|
|
|
|
|
|
|
var tablePosition = new Position(
|
|
|
|
position.Left + Theme.BorderWidth,
|
|
|
|
position.Top + Theme.BorderWidth);
|
|
|
|
|
|
|
|
var tableProps = new TableProps(header, rows, TitleWidth, ColumnWidth);
|
|
|
|
|
|
|
|
_table.Render(tableProps, tablePosition);
|
|
|
|
|
|
|
|
for (var rowId = 0; rowId < rows.Length; rowId++)
|
|
|
|
{
|
|
|
|
var actualDependencies = GetDependencies(project.Sources[rowId], project.Dependencies);
|
|
|
|
_table.RenderRow(rowId + 1, rows[rowId] + actualDependencies);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-06 20:12:22 +00:00
|
|
|
private static string GetDependencies(SourceDto sourceDto, IEnumerable<DependencyDto> conventionDependencies)
|
2023-08-03 19:21:06 +00:00
|
|
|
{
|
2023-08-04 04:09:49 +00:00
|
|
|
try
|
|
|
|
{
|
2023-08-06 20:12:22 +00:00
|
|
|
var package = DownloadPackage(sourceDto);
|
2023-08-03 19:21:06 +00:00
|
|
|
|
2023-08-04 04:09:49 +00:00
|
|
|
return string.Join("",
|
|
|
|
conventionDependencies
|
|
|
|
.Select(package.Dependencies.GetVersion)
|
2023-08-06 20:12:22 +00:00
|
|
|
.Select(RenderCurrentVersion));
|
|
|
|
}
|
|
|
|
catch (HttpRequestException exception)
|
|
|
|
{
|
|
|
|
switch (exception.StatusCode)
|
|
|
|
{
|
|
|
|
case HttpStatusCode.BadRequest:
|
|
|
|
return " Request have errors.".Pastel(Palette.ErrorColor);
|
|
|
|
case HttpStatusCode.Forbidden:
|
|
|
|
return " Not enough rights.".Pastel(Palette.ErrorColor);
|
|
|
|
case HttpStatusCode.NotFound:
|
|
|
|
return " Repository not found.".Pastel(Palette.ErrorColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw;
|
2023-08-04 04:09:49 +00:00
|
|
|
}
|
2023-08-06 20:12:22 +00:00
|
|
|
catch (Exception exception)
|
2023-08-04 04:09:49 +00:00
|
|
|
{
|
2023-08-06 20:12:22 +00:00
|
|
|
Debugger.Break();
|
2023-08-04 04:09:49 +00:00
|
|
|
return " We tried to send a request but couldn't. Check your configuration.".Pastel(Palette.ErrorColor);
|
|
|
|
}
|
2023-08-03 19:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private readonly static Dictionary<string, Package> Packages = new();
|
|
|
|
|
2023-08-06 20:12:22 +00:00
|
|
|
private static Package DownloadPackage(SourceDto sourceDto)
|
2023-08-03 19:21:06 +00:00
|
|
|
{
|
2023-08-06 20:12:22 +00:00
|
|
|
if (Packages.TryGetValue(sourceDto.Repo, out var downloadPackage))
|
2023-08-03 19:21:06 +00:00
|
|
|
{
|
|
|
|
return downloadPackage;
|
|
|
|
}
|
|
|
|
|
|
|
|
using HttpClient client = new();
|
2023-08-06 20:12:22 +00:00
|
|
|
var endpoint = sourceDto.Tags.Have("gitlab") ? GetGitlabEndpoint(sourceDto) : sourceDto.Repo;
|
2023-08-04 04:09:49 +00:00
|
|
|
var json = client.GetStringAsync(endpoint).GetAwaiter().GetResult();
|
2023-08-03 19:21:06 +00:00
|
|
|
var package = JsonSerializer.Deserialize<Package>(json);
|
2023-08-06 20:12:22 +00:00
|
|
|
Packages.Add(sourceDto.Repo, package);
|
2023-08-03 19:21:06 +00:00
|
|
|
return package;
|
|
|
|
}
|
|
|
|
|
2023-08-06 20:12:22 +00:00
|
|
|
private static string GetGitlabEndpoint(SourceDto sourceDto)
|
2023-08-04 04:09:49 +00:00
|
|
|
{
|
|
|
|
var token = Environment.GetEnvironmentVariable("TLD_GITLAB_PAT");
|
2023-08-06 20:12:22 +00:00
|
|
|
return $"{sourceDto.Repo}/api/v4/projects/{sourceDto.ProjectId}/repository/files/package.json/raw?" +
|
2023-08-04 04:09:49 +00:00
|
|
|
$"private_token={token}&ref=master";
|
|
|
|
}
|
2023-08-06 20:12:22 +00:00
|
|
|
|
|
|
|
private static string GetConventionVersion(DependencyDto dependencyDto)
|
2023-08-03 19:21:06 +00:00
|
|
|
{
|
2023-08-06 20:12:22 +00:00
|
|
|
return dependencyDto.Icon.Pastel(dependencyDto.Color) + dependencyDto.Version.Primary();
|
2023-08-03 19:21:06 +00:00
|
|
|
}
|
|
|
|
|
2023-08-06 20:12:22 +00:00
|
|
|
private static string RenderCurrentVersion(string version)
|
2023-08-03 19:21:06 +00:00
|
|
|
{
|
2023-08-06 20:12:22 +00:00
|
|
|
var versionWidth = version.Width();
|
|
|
|
if (versionWidth == 0)
|
|
|
|
{
|
|
|
|
return ' '.Repeat(ColumnWidth - 1) + "".Hint();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ' '.Repeat(ColumnWidth - versionWidth) + version;
|
2023-08-03 19:21:06 +00:00
|
|
|
}
|
|
|
|
|
2023-08-06 20:12:22 +00:00
|
|
|
private static string GetTitle(SourceDto sourceDto)
|
2023-08-03 19:21:06 +00:00
|
|
|
{
|
|
|
|
var rowText = new StringBuilder();
|
|
|
|
|
|
|
|
RenderPadding(rowText);
|
2023-08-06 20:12:22 +00:00
|
|
|
RenderTags(rowText, sourceDto);
|
|
|
|
rowText.Append(sourceDto.Name);
|
2023-08-03 19:21:06 +00:00
|
|
|
RenderPadding(rowText);
|
|
|
|
var text = rowText.ToString();
|
|
|
|
return $"{text}{' '.Repeat(TitleWidth - text.Width())}";
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void RenderPadding(StringBuilder rowText)
|
|
|
|
{
|
|
|
|
rowText.Append(new string(' ', Theme.Padding));
|
|
|
|
}
|
|
|
|
|
2023-08-06 20:12:22 +00:00
|
|
|
private static void RenderTags(StringBuilder rowText, SourceDto sourceDto)
|
2023-08-03 19:21:06 +00:00
|
|
|
{
|
2023-08-06 20:12:22 +00:00
|
|
|
rowText.Append(GetGitApplication(sourceDto));
|
|
|
|
rowText.Append(sourceDto.Tags.Have("public")
|
2023-08-03 19:21:06 +00:00
|
|
|
? GetIcon("", "00FFFF")
|
|
|
|
: GetIcon("", "AFE1AF"));
|
2023-08-06 20:12:22 +00:00
|
|
|
rowText.Append(GetIcon("", "4285F4", sourceDto.Tags.Have("seo")));
|
|
|
|
rowText.Append(GetIcon("", "FFD700", sourceDto.Tags.Have("auth")));
|
|
|
|
rowText.Append(GetApplicationType(sourceDto));
|
2023-08-03 19:21:06 +00:00
|
|
|
}
|
|
|
|
|
2023-08-06 20:12:22 +00:00
|
|
|
private static string GetApplicationType(SourceDto sourceDto)
|
2023-08-03 19:21:06 +00:00
|
|
|
{
|
2023-08-06 20:12:22 +00:00
|
|
|
if (sourceDto.Tags.Have("site"))
|
2023-08-03 19:21:06 +00:00
|
|
|
return GetIcon("", "BF40BF");
|
2023-08-06 20:12:22 +00:00
|
|
|
if (sourceDto.Tags.Have("api"))
|
2023-08-03 19:21:06 +00:00
|
|
|
return GetIcon("", "7F52FF");
|
2023-08-06 20:12:22 +00:00
|
|
|
if (sourceDto.Tags.Have("package"))
|
2023-08-03 19:21:06 +00:00
|
|
|
return GetIcon("", "CB0000");
|
2023-08-06 20:12:22 +00:00
|
|
|
if (sourceDto.Tags.Have("image"))
|
2023-08-03 19:21:06 +00:00
|
|
|
return GetIcon("", "086DD7");
|
|
|
|
|
|
|
|
return GetIcon("", "CB0000");
|
|
|
|
}
|
|
|
|
|
2023-08-06 20:12:22 +00:00
|
|
|
private static string GetGitApplication(SourceDto sourceDto) => sourceDto.Repo switch
|
2023-08-03 19:21:06 +00:00
|
|
|
{
|
|
|
|
{ } url when url.Contains("gitlab") => GetIcon("", "E24329"),
|
|
|
|
{ } url when url.Contains("github") => GetIcon("", "ADBAC7"),
|
|
|
|
_ => GetIcon("", "F14E32")
|
|
|
|
};
|
|
|
|
|
|
|
|
private static string GetIcon(string icon, string activeColor, bool enabled = true) =>
|
|
|
|
(icon.Pastel(enabled ? activeColor : "71797E") + " ").PadLeft(2);
|
|
|
|
|
|
|
|
public void Next()
|
|
|
|
{
|
|
|
|
_table.Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Previous()
|
|
|
|
{
|
|
|
|
_table.Previous();
|
|
|
|
}
|
|
|
|
}
|