🐛 Fix bug when engines not found.

This commit is contained in:
Kolosov Alexandr 2023-08-08 09:28:20 +05:00
parent 8e6ce6bd3e
commit 44eb08f5e8
4 changed files with 93 additions and 53 deletions

View File

@ -49,7 +49,7 @@ public class DependencyDashboard : IControl<Project>
return string.Join("",
conventionDependencies
.Select(package.Dependencies.GetVersion)
.Select(dependency => GetVersion(dependency, package))
.Select(RenderCurrentVersion));
}
catch (HttpRequestException exception)
@ -61,7 +61,7 @@ public class DependencyDashboard : IControl<Project>
case HttpStatusCode.Forbidden:
return " Not enough rights.".Pastel(Palette.ErrorColor);
case HttpStatusCode.NotFound:
return " Repository not found.".Pastel(Palette.ErrorColor);
return " Repository or branch master not found.".Pastel(Palette.ErrorColor);
}
throw;
@ -73,6 +73,37 @@ public class DependencyDashboard : IControl<Project>
}
}
private static string GetVersion(DependencyDto dependency, Package package)
{
var currentVersion = package.ParseVersion(dependency.Name);
if (currentVersion == null)
{
return "".Hint();
}
var conventionVersion = dependency.Version?.ToVersion();
return PaintingVersion(currentVersion, conventionVersion);
}
private static string PaintingVersion(Version current, Version? convention)
{
var textVersion = current.ToString();
if (current > convention)
{
return textVersion.Info();
}
if (current < convention)
{
return current.Major == convention.Major ? textVersion.Warning() : textVersion.Error();
}
return textVersion.Primary();
}
private readonly static Dictionary<string, Package> Packages = new();
private static Package DownloadPackage(SourceDto sourceDto)
@ -86,7 +117,7 @@ public class DependencyDashboard : IControl<Project>
var endpoint = sourceDto.Tags.Have("gitlab") ? GetGitlabEndpoint(sourceDto) : sourceDto.Repo;
var json = client.GetStringAsync(endpoint).GetAwaiter().GetResult();
var package = JsonSerializer.Deserialize<Package>(json);
Packages.Add(sourceDto.Repo, package);
Packages.Add(endpoint, package);
return package;
}
@ -115,30 +146,38 @@ public class DependencyDashboard : IControl<Project>
private static string GetTitle(SourceDto sourceDto)
{
var rowText = new StringBuilder();
var title = "";
RenderPadding(rowText);
RenderTags(rowText, sourceDto);
rowText.Append(sourceDto.Name);
RenderPadding(rowText);
var text = rowText.ToString();
return $"{text}{' '.Repeat(TitleWidth - text.Width())}";
title += RenderPadding();
title += RenderTags(sourceDto);
if (title.Width() + sourceDto.Name.Length + Theme.Padding <= TitleWidth)
{
title += sourceDto.Name;
}
else
{
var maxNameWidth = TitleWidth - title.Width() - Theme.Padding;
title += $"{sourceDto.Name[..(maxNameWidth - 1)]}{"#".Hint()}";
}
private static void RenderPadding(StringBuilder rowText)
{
rowText.Append(new string(' ', Theme.Padding));
title += RenderPadding();
return $"{title}{' '.Repeat(TitleWidth - title.Width())}";
}
private static void RenderTags(StringBuilder rowText, SourceDto sourceDto)
private static string RenderPadding()
{
rowText.Append(GetGitApplication(sourceDto));
rowText.Append(sourceDto.Tags.Have("public")
? GetIcon("󰞉", "00FFFF")
: GetIcon("󰕑", "AFE1AF"));
rowText.Append(GetIcon("󰚩", "4285F4", sourceDto.Tags.Have("seo")));
rowText.Append(GetIcon("", "FFD700", sourceDto.Tags.Have("auth")));
rowText.Append(GetApplicationType(sourceDto));
return new string(' ', Theme.Padding);
}
private static string RenderTags(SourceDto sourceDto)
{
var tags = "";
tags += GetGitApplication(sourceDto);
tags += sourceDto.Tags.Have("public") ? GetIcon("󰞉", "00FFFF") : GetIcon("󰕑", "AFE1AF");
tags += GetIcon("󰚩", "4285F4", sourceDto.Tags.Have("seo"));
tags += GetIcon("", "FFD700", sourceDto.Tags.Have("auth"));
tags += GetApplicationType(sourceDto);
return tags;
}
private static string GetApplicationType(SourceDto sourceDto)

View File

@ -7,5 +7,36 @@ namespace TUI.Domain;
public class Package
{
[JsonPropertyName("dependencies")]
public JsonObject Dependencies { get; set; }
public JsonObject? Dependencies { get; set; }
[JsonPropertyName("devDependencies")]
public JsonObject? DevDependencies { get; set; }
[JsonPropertyName("engines")]
public JsonObject? Engines { get; set; }
public Version? ParseVersion(string? dependencyName)
{
if (dependencyName == null)
{
return null;
}
JsonNode? version = null;
var lowerDependencyName = dependencyName.ToLower();
Dependencies?.TryGetPropertyValue(lowerDependencyName, out version);
if (version == null)
{
Engines?.TryGetPropertyValue(lowerDependencyName, out version);
}
if (version == null)
{
DevDependencies?.TryGetPropertyValue(lowerDependencyName, out version);
}
return version?.GetValue<string>().ToVersion();
}
}

View File

@ -16,7 +16,7 @@ public static class Extensions
public static string Repeat(this char symbol, int repeatCount)
{
return new string(symbol, repeatCount);
return repeatCount < 0 ? "" : new string(symbol, repeatCount);
}
public static int Width(this string text)
@ -31,36 +31,7 @@ public static class Extensions
return stringInfo.LengthInTextElements;
}
public static string GetVersion(this JsonObject dependencies, DependencyDto dependencyDto)
{
dependencies.TryGetPropertyValue(dependencyDto.Name.ToLower(), out var version);
var currentVersion = version?.GetValue<string>().ToVersion();
if (currentVersion == null)
{
return "".Hint();
}
var conventionVersion = dependencyDto.Version.ToVersion();
if (currentVersion > conventionVersion)
{
return currentVersion.ToString().Info();
}
if (currentVersion < conventionVersion)
{
if (currentVersion.Major == conventionVersion.Major)
{
return currentVersion.ToString().Warning();
}
return currentVersion.ToString().Error();
}
return currentVersion.ToString().Primary();
}
private static Version? ToVersion(this string textVersion)
public static Version? ToVersion(this string textVersion)
{
var version = textVersion.Replace("^", "").Replace("~", "").Split(".");
if (version.Length != 3)

View File

@ -1,4 +1,3 @@
using System.Drawing;
using Pastel;