diff --git a/src/TUI.Engine/Nodes/INode.cs b/src/TUI.Engine/Nodes/INode.cs index 97e3966..02e87ed 100644 --- a/src/TUI.Engine/Nodes/INode.cs +++ b/src/TUI.Engine/Nodes/INode.cs @@ -7,6 +7,9 @@ namespace TUI.Engine.Nodes; public interface INode : IResizable { DrawContext DrawContext { get; set; } + StyleContext StyleContext { get; set; } } -public record DrawContext(ICanvas Canvas, Position Pencil, Size MaxSize); \ No newline at end of file +public record DrawContext(ICanvas Canvas, Position Pencil, Size MaxSize); + +public record StyleContext(string? Foreground, string? Background = null); \ No newline at end of file diff --git a/src/TUI.Engine/Nodes/NodeBase.cs b/src/TUI.Engine/Nodes/NodeBase.cs index 79a4182..30e88cd 100644 --- a/src/TUI.Engine/Nodes/NodeBase.cs +++ b/src/TUI.Engine/Nodes/NodeBase.cs @@ -57,4 +57,6 @@ public abstract class NodeBase : INode #endregion Resizing public DrawContext? DrawContext { get; set; } + + public StyleContext? StyleContext { get; set; } } \ No newline at end of file diff --git a/src/TUI.Engine/Rendering/ComponentCraftsman.cs b/src/TUI.Engine/Rendering/ComponentCraftsman.cs index cfb7e4a..a24cba9 100644 --- a/src/TUI.Engine/Rendering/ComponentCraftsman.cs +++ b/src/TUI.Engine/Rendering/ComponentCraftsman.cs @@ -1,3 +1,4 @@ +using Pastel; using TUI.Engine.Attributes; using TUI.Engine.Components; using TUI.Engine.Nodes; @@ -8,31 +9,34 @@ namespace TUI.Engine.Rendering; internal sealed class ComponentCraftsman : CraftsmanBase, IDrawable { private readonly ICanvas _canvas; - + public ComponentCraftsman(ICanvas canvas) { _canvas = canvas; } - + public Size Draw(IComponent component, Position pencil, Size maxSize) { var sketch = component.MakeSketch(maxSize); var sketchSize = sketch.GetSize(); - + var correctedPencil = component.CorrectContentPosition(pencil, maxSize, sketchSize); - + Debug(pencil, maxSize); - + foreach (var line in sketch.Crop(maxSize)) { component.DrawContext = new DrawContext(_canvas, pencil, maxSize); - + _canvas.SetPencil(correctedPencil); - _canvas.Paint(line); - + _canvas.Paint(component.StyleContext is not null + ? line.RemoveColors().Pastel(component.StyleContext.Foreground) + : line); + + correctedPencil = correctedPencil with { Top = correctedPencil.Top + 1 }; } - + return sketchSize; } } \ No newline at end of file diff --git a/src/TUI.Engine/Theme/Palette.cs b/src/TUI.Engine/Theme/Palette.cs index 5ab1765..8f9b7ea 100644 --- a/src/TUI.Engine/Theme/Palette.cs +++ b/src/TUI.Engine/Theme/Palette.cs @@ -4,31 +4,32 @@ namespace TUI.Engine.Theme; public static class Palette { - private const string HoverColor = "292928"; - private const string PrimaryColor = "84BA64"; - private const string HintColor = "71797E"; - private const string ErrorColor = "CA3433"; - private const string WarningColor = "EC9706"; - private const string InfoColor = "25799F"; - + public const string HoverColor = "292928"; + public const string PrimaryColor = "84BA64"; + public const string HintColor = "71797E"; + public const string DisableColor = "303030"; + public const string ErrorColor = "CA3433"; + public const string WarningColor = "EC9706"; + public const string InfoColor = "25799F"; + public static string Main(this string currentText, bool isFocused = true) => isFocused ? currentText.Pastel(PrimaryColor) : Hint(currentText); - + public static string Hint(this string currentText) => currentText.Pastel(HintColor); - + public static string Hint(this char currentText) => currentText.ToString().Pastel(HintColor); - + public static string Disable(this string currentText) => currentText.RemoveColors().Pastel(HintColor); - + public static string Warning(this string currentText) => currentText.Pastel(WarningColor); - + public static string Error(this string currentText) => currentText.Pastel(ErrorColor); - + public static string Info(this string currentText) => currentText.Pastel(InfoColor); - + public static string Info(this char currentText) => currentText.ToString().Pastel(InfoColor); - + public static string Info(this int currentText) => currentText.ToString().Pastel(InfoColor); } \ No newline at end of file diff --git a/src/TUI/Controls/Common/StubComponent.cs b/src/TUI/Controls/Common/StubComponent.cs index 8a498ee..a3c39e9 100644 --- a/src/TUI/Controls/Common/StubComponent.cs +++ b/src/TUI/Controls/Common/StubComponent.cs @@ -9,25 +9,32 @@ namespace TUI.Controls.Common; public class StubComponent : ComponentBase { private readonly Size _size; - - public StubComponent(Size size) + private readonly string? _text; + + public StubComponent(Size size, string? text = null) { _size = size; + _text = text; SetFixed(Orientation.Horizontal, size.Width); SetFixed(Orientation.Vertical, size.Height); } - + protected override Sketch DrawComponent(Size minSize) { var builder = new StringBuilder(); var height = 0; - + + while (_size.Height > height) { - builder.Append(Symbols.Space.Repeat(_size.Width)); + builder.Append(Symbols.Space.Repeat(_size.Width - (_text?.GetWidth() ?? 0))); height++; } - + + if (_text is not null) + { + builder.Append(_text); + } return new Sketch(builder.ToString()); } } \ No newline at end of file diff --git a/src/TUI/Controls/Components/ProjectTitle.cs b/src/TUI/Controls/Components/ProjectTitle.cs index 2a75355..a5f9559 100644 --- a/src/TUI/Controls/Components/ProjectTitle.cs +++ b/src/TUI/Controls/Components/ProjectTitle.cs @@ -12,12 +12,12 @@ namespace TUI.Controls.Components; public class ProjectTitle : ComponentBase { private readonly Project _project; - + public ProjectTitle(Project project) { _project = project; } - + protected override Sketch DrawComponent(Size minSize) { var builder = new StringBuilder(); @@ -34,14 +34,14 @@ public class ProjectTitle : ComponentBase builder.Append(_project.Name.Disable()); return new Sketch(builder); } - - private string GetHub() => _project.Hub == "gitlab" ? GitLab : GitHub; - + + private string GetHub() => _project.Hub.Type == "gitlab" ? GitLab : GitHub; + private string GetApplicationType() { foreach (var application in Icons.Applications.Where(application => _project.Tags.Have(application.Value))) return application.Key; - + return Undefined.Hint(); } } \ No newline at end of file diff --git a/src/TUI/Controls/Components/VersionComponent.cs b/src/TUI/Controls/Components/VersionComponent.cs index 23d6e70..0f4408b 100644 --- a/src/TUI/Controls/Components/VersionComponent.cs +++ b/src/TUI/Controls/Components/VersionComponent.cs @@ -1,4 +1,5 @@ using System.Text; +using TUI.Domain; using TUI.Engine.Attributes; using TUI.Engine.Components; @@ -9,26 +10,31 @@ public class VersionComponent : ComponentBase private readonly VersionType _type; private readonly string _version; private readonly Brand? _brand; - - public VersionComponent(VersionType type, string version, Brand? brand) + private readonly VersionStatus _status; + + public VersionComponent(string version, Brand brand, VersionStatus status = VersionStatus.SoGood, + VersionType type = VersionType.Release) { - _type = type; _version = version; _brand = brand; + _status = status; + _type = type; } - + protected override Sketch DrawComponent(Size minSize) { var builder = new StringBuilder(); - + + // builder.Append(_type.ToString()); + if (_brand is not null) { builder.Append(_brand.ColorLogo()); } - + builder.Append(_version); var sketch = builder.ToString(); - - return new Sketch(_type.Colorize(sketch)); + + return new Sketch(_status.Colorize(sketch)); } } \ No newline at end of file diff --git a/src/TUI/Controls/Components/VersionStatus.cs b/src/TUI/Controls/Components/VersionStatus.cs new file mode 100644 index 0000000..0974abe --- /dev/null +++ b/src/TUI/Controls/Components/VersionStatus.cs @@ -0,0 +1,10 @@ +namespace TUI.Controls.Components; + +public enum VersionStatus +{ + NotFound, + ToNew, + SoGood, + BeNice, + TooOld, +} \ No newline at end of file diff --git a/src/TUI/Controls/Components/VersionType.cs b/src/TUI/Controls/Components/VersionType.cs index 108dc0a..786c668 100644 --- a/src/TUI/Controls/Components/VersionType.cs +++ b/src/TUI/Controls/Components/VersionType.cs @@ -3,8 +3,10 @@ namespace TUI.Controls.Components; public enum VersionType { Convention, - ToNew, - SoGood, - BeNice, - TooOld, + Release, + Candidate, + Canary, + Alpha, + Beta, + Next, } \ No newline at end of file diff --git a/src/TUI/Controls/Components/VersionTypeExtensions.cs b/src/TUI/Controls/Components/VersionTypeExtensions.cs index 3178ca7..519815b 100644 --- a/src/TUI/Controls/Components/VersionTypeExtensions.cs +++ b/src/TUI/Controls/Components/VersionTypeExtensions.cs @@ -4,14 +4,13 @@ namespace TUI.Controls.Components; public static class VersionTypeExtensions { - public static string Colorize(this VersionType versionType, string value) => - versionType switch + public static string Colorize(this VersionStatus versionStatus, string value) => + versionStatus switch { - VersionType.TooOld => value.Warning(), - VersionType.ToNew => value.Info(), - VersionType.SoGood => value.Hint(), - VersionType.BeNice => value.Main(), - VersionType.Convention => value.Main(), + VersionStatus.TooOld => value.Warning(), + VersionStatus.ToNew => value.Info(), + VersionStatus.SoGood => value.Hint(), + VersionStatus.BeNice => value.Main(), _ => value }; } \ No newline at end of file diff --git a/src/TUI/Controls/Containers/DashboardContainer.cs b/src/TUI/Controls/Containers/DashboardContainer.cs index a556a5c..d775396 100644 --- a/src/TUI/Controls/Containers/DashboardContainer.cs +++ b/src/TUI/Controls/Containers/DashboardContainer.cs @@ -29,4 +29,6 @@ public class DashboardContainer : ContainerBase } public override Nodes GetNodes() => _children; + + public Nodes GetContent() => _content.GetNodes(); } \ No newline at end of file diff --git a/src/TUI/Controls/Containers/DependenciesContainer.cs b/src/TUI/Controls/Containers/DependenciesContainer.cs index a6be9db..23393c9 100644 --- a/src/TUI/Controls/Containers/DependenciesContainer.cs +++ b/src/TUI/Controls/Containers/DependenciesContainer.cs @@ -1,6 +1,7 @@ using TUI.Controls.Common; using TUI.Controls.Components; using TUI.Domain; +using TUI.Engine; using TUI.Engine.Attributes; using TUI.Engine.Attributes.Alignments; using TUI.Engine.Attributes.Orientations; @@ -13,38 +14,76 @@ namespace TUI.Controls.Containers; public class DependenciesContainer : ContainerBase { + public readonly Project? Project; + private const int VersionColumnWidth = 10; + private const int TitleColumnWidth = 25; - + private readonly Nodes _dependencies = new(); - + + public DependenciesContainer() + { + } + + public DependenciesContainer(Project project) + { + Project = project; + } + public void AddTitleStub() { var size = new Size(TitleColumnWidth, 1); var title = new StubComponent(size); title.SetPadding(Level.Normal); - + _dependencies.Add(title); } - + public void AddTitle(IComponent title) { title.SetPadding(Level.Normal); title.SetFixed(Orientation.Horizontal, TitleColumnWidth); title.SetAlignment(Horizontal.Left); - + + if (Project is not null && Project.Legacy) + { + title.StyleContext = new StyleContext(Palette.DisableColor); + } + _dependencies.Add(title); } - - public void AddDependency(Dependency dependency) + + public void AddDependencyStub() { - var version = new VersionComponent(VersionType.Convention, dependency.Version, dependency.Brand); + var size = new Size(VersionColumnWidth, 1); + var stub = new StubComponent(size, Symbols.NotFound.Hint()); + stub.SetPadding(Level.Normal); + stub.SetAlignment(Horizontal.Right); + stub.SetFixed(Orientation.Horizontal, VersionColumnWidth); + + if (Project is not null && Project.Legacy) + { + stub.StyleContext = new StyleContext(Palette.DisableColor); + } + + _dependencies.Add(stub); + } + + public void AddDependency(Dependency dependency, VersionStatus status = VersionStatus.BeNice) + { + var version = new VersionComponent(dependency.Version, dependency.Brand, status, dependency.Type); version.SetPadding(Level.Normal); version.SetAlignment(Horizontal.Right); version.SetFixed(Orientation.Horizontal, VersionColumnWidth); - + + if (Project is not null && Project.Legacy) + { + version.StyleContext = new StyleContext(Palette.DisableColor); + } + _dependencies.Add(version); } - + public override Nodes GetNodes() => _dependencies; } \ No newline at end of file diff --git a/src/TUI/Controls/Statics/Hints/VersionHints.cs b/src/TUI/Controls/Statics/Hints/VersionHints.cs index 1a41614..b271bb2 100644 --- a/src/TUI/Controls/Statics/Hints/VersionHints.cs +++ b/src/TUI/Controls/Statics/Hints/VersionHints.cs @@ -10,10 +10,10 @@ public class VersionHints : StaticComponentBase { private readonly Dictionary _hints = new() { - { "󰎔", VersionType.ToNew.Colorize("too new") }, - { "", VersionType.SoGood.Colorize("so good") }, - { "", VersionType.BeNice.Colorize("be nice") }, - { "󰬟", VersionType.TooOld.Colorize("too old") } + { "󰎔", VersionStatus.ToNew.Colorize("too new") }, + { "", VersionStatus.SoGood.Colorize("so good") }, + { "", VersionStatus.BeNice.Colorize("be nice") }, + { "󰬟", VersionStatus.TooOld.Colorize("too old") } }; protected override void RenderWithCache(StringBuilder builder) diff --git a/src/TUI/Domain/Brand.cs b/src/TUI/Domain/Brand.cs index 0530a0b..f3422a6 100644 --- a/src/TUI/Domain/Brand.cs +++ b/src/TUI/Domain/Brand.cs @@ -1,8 +1,8 @@ using Pastel; -namespace TUI.Controls.Components; +namespace TUI.Domain; -public record Brand(string Name, string Logo, string Color) +public record Brand(string Name, string? Logo = null, string? Color = null) { - public string ColorLogo() => Logo.Pastel(Color); + public string ColorLogo() => Logo?.Pastel(Color) ?? string.Empty; }; \ No newline at end of file diff --git a/src/TUI/Domain/Dependency.cs b/src/TUI/Domain/Dependency.cs index 38ff60d..b29419a 100644 --- a/src/TUI/Domain/Dependency.cs +++ b/src/TUI/Domain/Dependency.cs @@ -2,4 +2,66 @@ using TUI.Controls.Components; namespace TUI.Domain; -public record Dependency(string Version, Brand Brand); \ No newline at end of file +public record Dependency() +{ + private readonly Version _current; + public readonly Brand Brand; + public VersionType Type { get; private set; } + + public string Version => $"{_current.Major}.{_current.Minor}.{_current.Patch}"; + + public Dependency(string version, Brand brand) : this() + { + _current = new Version(version); + Brand = brand; + } + + public VersionStatus Comparison(Dependency outerDependency) + { + // if (string.IsNullOrEmpty(Version) || string.IsNullOrEmpty(outerDependency.Version)) + // { + // return VersionStatus.NotFound; + // } + + var outer = outerDependency._current; + + Type = _current.Type; + + if (_current.Major < outer.Major) + { + return VersionStatus.TooOld; + } + + if (_current.Major > outer.Major) + { + return VersionStatus.ToNew; + } + + if (_current.Minor < outer.Minor) + { + return VersionStatus.BeNice; + } + + if (_current.Minor > outer.Minor) + { + return VersionStatus.ToNew; + } + + if (_current.Patch < outer.Patch) + { + return VersionStatus.BeNice; + } + + if (_current.Patch > outer.Patch) + { + return VersionStatus.ToNew; + } + + if (outer.Type != VersionType.Release) + { + return VersionStatus.ToNew; + } + + return VersionStatus.SoGood; + } +}; \ No newline at end of file diff --git a/src/TUI/Domain/Hub.cs b/src/TUI/Domain/Hub.cs index ff0a924..403d320 100644 --- a/src/TUI/Domain/Hub.cs +++ b/src/TUI/Domain/Hub.cs @@ -1,6 +1,3 @@ namespace TUI.Domain; -public record Hub -{ - -} \ No newline at end of file +public record Hub(string Origin, string Type); \ No newline at end of file diff --git a/src/TUI/Domain/Project.cs b/src/TUI/Domain/Project.cs index 1764c0b..c75be9f 100644 --- a/src/TUI/Domain/Project.cs +++ b/src/TUI/Domain/Project.cs @@ -1,12 +1,14 @@ namespace TUI.Domain; -public record Project(int Id, string Name, IEnumerable Tags, string Hub) +public record Project(int Id, string Name, IEnumerable Tags, Hub Hub) { private IEnumerable Dependencies => new List(); - + public bool IsPublicNetwork => Tags.Contains("public"); - + public bool HasAuth => Tags.Contains("auth"); - + public bool SeoDependent => Tags.Contains("seo"); + + public bool Legacy => Tags.Contains("legacy"); } \ No newline at end of file diff --git a/src/TUI/Domain/Version.cs b/src/TUI/Domain/Version.cs new file mode 100644 index 0000000..ed61736 --- /dev/null +++ b/src/TUI/Domain/Version.cs @@ -0,0 +1,32 @@ +using TUI.Controls.Components; + +namespace TUI.Domain; + +public class Version +{ + public readonly int Major; + public readonly int Minor; + public readonly int Patch; + public readonly VersionType Type; + + public Version(string version) + { + var parts = version.Split('.'); + + Major = Convert.ToInt32(parts[0].Replace("^", "").Replace("~", "")); + Minor = Convert.ToInt32(parts[1]); + Patch = Convert.ToInt32(string.Join("", parts[2].TakeWhile(char.IsDigit))); + + var extension = parts[2].Replace(Patch.ToString(), ""); + + Type = extension switch + { + not null when extension.Contains("rc") => VersionType.Candidate, + not null when extension.Contains("beta") => VersionType.Beta, + not null when extension.Contains("alpha") => VersionType.Alpha, + not null when extension.Contains("canary") => VersionType.Canary, + not null when extension.Contains("next") => VersionType.Next, + _ => VersionType.Release + }; + } +} \ No newline at end of file diff --git a/src/TUI/Pages/DependenciesPage.cs b/src/TUI/Pages/DependenciesPage.cs index 4820dc8..f2b7f5e 100644 --- a/src/TUI/Pages/DependenciesPage.cs +++ b/src/TUI/Pages/DependenciesPage.cs @@ -6,57 +6,97 @@ using TUI.Store; namespace TUI.Pages; +public record DependenciesState(HeaderContainer Header, DashboardContainer Dashboard, FooterContainer Footer); + public class DependenciesPage : PageBase { private DependenciesStore _store; - - public override void Render() + + private DependenciesState _state; + + public override void Initial() { - ICanvas canvas = new ConsoleCanvas(); - var header = new HeaderContainer(); var dashboard = new DashboardContainer(); var dependenciesHeader = new DependenciesContainer(); dependenciesHeader.AddTitleStub(); - + foreach (var conventionDependency in _store.ConventionDependencies) { dependenciesHeader.AddDependency(conventionDependency); } - + dashboard.AddChildren(dependenciesHeader); - + foreach (var project in _store.Projects) { - var projectDependencies = new DependenciesContainer(); + var projectDependencies = new DependenciesContainer(project); projectDependencies.AddTitle(new ProjectTitle(project)); dashboard.AddChildren(projectDependencies); } - + var breadCrumbs = new BreadCrumbsComponent(" Dependencies", "JavaScript"); var footer = new FooterContainer(breadCrumbs); - var layout = new DashboardLayout(header, dashboard, footer); - canvas.Draw(layout); - - // CommandLine = new CommandLine(); - // DependenciesView = new DependenciesView(); + + _state = new DependenciesState(header, dashboard, footer); } - + + public override void Render() + { + ICanvas canvas = new ConsoleCanvas(); + var layout = new DashboardLayout(_state.Header, _state.Dashboard, _state.Footer); + canvas.Draw(layout); + } + + public void LoadDependencies() + { + Initial(); + var projects = _state.Dashboard.GetContent(); + foreach (var projectDependencies in projects.Cast().Skip(1)) + { + if (projectDependencies is null) + { + continue; + } + + var project = projectDependencies.Project; + var actualDependencies = _store.ActualDependencies(project).ToArray(); + + foreach (var conventionDependency in _store.ConventionDependencies) + { + var actualDependency = actualDependencies.SingleOrDefault( + dependency => string.Equals(dependency.Brand.Name, conventionDependency.Brand.Name, + StringComparison.CurrentCultureIgnoreCase)); + + if (actualDependency is null) + { + projectDependencies.AddDependencyStub(); + continue; + } + + var versionType = actualDependency.Comparison(conventionDependency); + projectDependencies.AddDependency(actualDependency, versionType); + } + + Render(); + } + } + public override void Bind() { _store = new DependenciesStore(); _store.Bind(); } - + // private bool _commandLineInDisplay; - + // private ProjectDto _currentProjectDto; - + // private bool _headerInDisplay = true; - - + + // public string FocusedElement { get; set; } = ""; - + // public void OpenDeps(ProjectDto projectDto) // { // _currentProjectDto = projectDto; diff --git a/src/TUI/Pages/IPage.cs b/src/TUI/Pages/IPage.cs index 3a9b32a..13894e8 100644 --- a/src/TUI/Pages/IPage.cs +++ b/src/TUI/Pages/IPage.cs @@ -3,6 +3,7 @@ namespace TUI.Pages; interface IPage { void Open(); + void Initial(); void Render(); void Bind(); } \ No newline at end of file diff --git a/src/TUI/Pages/PageBase.cs b/src/TUI/Pages/PageBase.cs index 2dbce48..2c9e357 100644 --- a/src/TUI/Pages/PageBase.cs +++ b/src/TUI/Pages/PageBase.cs @@ -8,10 +8,13 @@ public abstract class PageBase : IPage { Debugger.Log(0, "Event", $"Open page ${GetType().UnderlyingSystemType.Name}\n"); Bind(); + Initial(); Render(); } - + + public abstract void Initial(); + public abstract void Render(); - + public abstract void Bind(); } \ No newline at end of file diff --git a/src/TUI/Pages/WelcomePage.cs b/src/TUI/Pages/WelcomePage.cs index 169c276..0c651d2 100644 --- a/src/TUI/Pages/WelcomePage.cs +++ b/src/TUI/Pages/WelcomePage.cs @@ -10,6 +10,10 @@ namespace TUI.Pages; public class WelcomePage : PageBase { + public override void Initial() + { + } + public override void Render() { ICanvas canvas = new ConsoleCanvas(); diff --git a/src/TUI/Program.cs b/src/TUI/Program.cs index 14e60b8..0f91ded 100644 --- a/src/TUI/Program.cs +++ b/src/TUI/Program.cs @@ -13,15 +13,24 @@ Thread.Sleep(500); var dependenciesPage = new DependenciesPage(); dependenciesPage.Open(); -var key = new ConsoleKeyInfo('1', ConsoleKey.NoName, false, false, false); + +ConsoleKeyInfo? key = null; + var waitCommand = true; do { - // if (key.Key == ConsoleKey.Q && !display.CommandLine.IsFocused) - // { - // waitCommand = false; - // continue; - // } + if (key?.Key == ConsoleKey.Q) + { + waitCommand = false; + continue; + } + + if (key?.Key == ConsoleKey.R) + { + dependenciesPage.LoadDependencies(); + key = null; + continue; + } // // if (display.CommandLine.IsFocused) // { @@ -58,7 +67,7 @@ do // break; // } // } - + key = Console.ReadKey(true); } while (waitCommand); diff --git a/src/TUI/Providers/Dependencies/DependencyRepository.cs b/src/TUI/Providers/Dependencies/DependencyRepository.cs index 80d5c91..d13627e 100644 --- a/src/TUI/Providers/Dependencies/DependencyRepository.cs +++ b/src/TUI/Providers/Dependencies/DependencyRepository.cs @@ -1,3 +1,5 @@ +using System.Text.Json; +using System.Text.Json.Nodes; using TUI.Controls.Components; using TUI.Domain; using YamlDotNet.Serialization; @@ -8,7 +10,7 @@ namespace TUI.Providers.Dependencies; public class DependencyRepository { private DependenciesDto? _dependenciesDto; - + private DependenciesDto DependenciesDto { get @@ -17,18 +19,18 @@ public class DependencyRepository { return _dependenciesDto; } - + var deserializer = new DeserializerBuilder() .WithNamingConvention(UnderscoredNamingConvention.Instance) .Build(); - + using var sr = new StreamReader("dependencies.yaml"); _dependenciesDto = deserializer.Deserialize(sr.ReadToEnd()); - + return _dependenciesDto; } } - + public IEnumerable ReadConventions(string stackName) { return DependenciesDto.Stacks @@ -40,22 +42,86 @@ public class DependencyRepository return new Dependency(convention.Version, brand); }); } - + public IEnumerable ReadProjects(string stackName) { var projects = new List(); - + var hubs = DependenciesDto.Stacks .Single(stack => stack.Name == stackName) .Hubs; - + foreach (var hub in hubs) { projects.AddRange(hub .Projects - .Select(proj => new Project(proj.Id, proj.Name, proj.Tags, hub.Type))); + .Select(proj => new Project(proj.Id, proj.Name, proj.Tags, new Hub(hub.Origin, hub.Type)))); } - + return projects; } -} \ No newline at end of file + + + public IEnumerable ReadActual(Project project) + { + var dependencies = new List(); + + if (project.Hub.Type == "gitlab") + { + var endpoint = GetGitlabEndpoint(project.Hub.Origin, project.Id); + using HttpClient client = new(); + var json = client.GetStringAsync(endpoint).GetAwaiter().GetResult(); + + var packageJson = JsonSerializer.Deserialize(json); + + dependencies.AddRange(Map(packageJson?.Dependencies)); + dependencies.AddRange(Map(packageJson?.DevDependencies)); + dependencies.AddRange(Map(packageJson?.Engines)); + } + + return dependencies; + } + + private static string GetGitlabEndpoint(string origin, int projectId) + { + var token = Environment.GetEnvironmentVariable("TLD_GITLAB_PAT"); + return $"{origin}/api/v4/projects/{projectId}/repository/files/package.json/raw?" + + $"private_token={token}&ref=dev"; + } + + private static IEnumerable Map(JsonObject? dependencies) + { + if (dependencies is null) + { + yield break; + } + + foreach (var dependency in dependencies) + { + var actualVersion = dependency.Value?.ToString(); + var brand = new Brand(dependency.Key); + + if (actualVersion is null) + { + continue; + } + + yield return new Dependency(actualVersion, brand); + } + } +} + + +// private static Package DownloadPackage(ProjectDto project) +// { +// // var endpoint = projectDto.Tags.Have("gitlab") ? GetGitlabEndpoint(projectDto) : projectDto.Repo; +// var endpoint = ""; +// if (Packages.TryGetValue(endpoint, out var downloadPackage)) return downloadPackage; +// +// using HttpClient client = new(); +// var json = client.GetStringAsync(endpoint).GetAwaiter().GetResult(); +// +// Packages.Add(endpoint, package); +// return package; +// } +// \ No newline at end of file diff --git a/src/TUI/Providers/Dependencies/Package.cs b/src/TUI/Providers/Dependencies/PackageJson.cs similarity index 92% rename from src/TUI/Providers/Dependencies/Package.cs rename to src/TUI/Providers/Dependencies/PackageJson.cs index 2fb82a9..30387aa 100644 --- a/src/TUI/Providers/Dependencies/Package.cs +++ b/src/TUI/Providers/Dependencies/PackageJson.cs @@ -4,7 +4,7 @@ using TUI.Engine; namespace TUI.Providers.Dependencies; -public class Package +public class PackageJson { [JsonPropertyName("dependencies")] public JsonObject? Dependencies { get; set; } @@ -14,20 +14,20 @@ public class Package [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().ToVersion(); } } \ No newline at end of file diff --git a/src/TUI/Store/DependenciesStore.cs b/src/TUI/Store/DependenciesStore.cs index 166f638..6c0c431 100644 --- a/src/TUI/Store/DependenciesStore.cs +++ b/src/TUI/Store/DependenciesStore.cs @@ -1,5 +1,6 @@ using TUI.Controls.Components; using TUI.Domain; +using TUI.Engine.Theme; using TUI.Providers.Dependencies; namespace TUI.Store; @@ -7,40 +8,31 @@ namespace TUI.Store; public class DependenciesStore { public IEnumerable ConventionDependencies; - + public IEnumerable Projects; - + + private DependencyRepository Repository = new(); + + public IEnumerable ActualDependencies(Project project) + { + SpeakerComponent.Instance.Shout("", $"Fetch actual dependencies for project {project.Name.Main()}"); + try + { + return Repository.ReadActual(project); + } + catch + { + SpeakerComponent.Instance.Shout("", $"Fetch failed for project{project.Name}"); + return new List(); + } + } + public void Bind() { - var repo = new DependencyRepository(); - - ConventionDependencies = repo.ReadConventions("javascript"); SpeakerComponent.Instance.Shout("🤔", "Prepare javascript conventions"); - - Projects = repo.ReadProjects("javascript"); + ConventionDependencies = Repository.ReadConventions("javascript"); + SpeakerComponent.Instance.Shout("🤩", "Prepare javascript projects"); + Projects = Repository.ReadProjects("javascript"); } - - // private readonly static Dictionary Packages = new(); - // - // private static Package DownloadPackage(ProjectDto projectDto) - // { - // // var endpoint = projectDto.Tags.Have("gitlab") ? GetGitlabEndpoint(projectDto) : projectDto.Repo; - // var endpoint = ""; - // if (Packages.TryGetValue(endpoint, out var downloadPackage)) return downloadPackage; - // - // using HttpClient client = new(); - // var json = client.GetStringAsync(endpoint).GetAwaiter().GetResult(); - // var package = JsonSerializer.Deserialize(json); - // Packages.Add(endpoint, package); - // return package; - // } - // - // private static string GetGitlabEndpoint(ProjectDto projectDto) - // { - // var token = Environment.GetEnvironmentVariable("TLD_GITLAB_PAT"); - // // return $"{projectDto.Repo}/api/v4/projects/{projectDto.ProjectId}/repository/files/package.json/raw?" + - // // $"private_token={token}&ref=dev"; - // return ""; - // } } \ No newline at end of file diff --git a/tests/TUI.Controls.Tests/VersionComponentTests.cs b/tests/TUI.Controls.Tests/VersionComponentTests.cs index 66111a5..e07d621 100644 --- a/tests/TUI.Controls.Tests/VersionComponentTests.cs +++ b/tests/TUI.Controls.Tests/VersionComponentTests.cs @@ -1,5 +1,6 @@ using FluentAssertions; using TUI.Controls.Components; +using TUI.Domain; using TUI.Engine.Attributes; using TUI.Engine.Components; @@ -9,19 +10,45 @@ namespace TUI.Engine.Tests { [Theory] [Trait("Category", nameof(Sketch))] - [InlineData(VersionType.Convention, "\u001b[38;2;132;186;100m10.12.33\u001b[0m")] - [InlineData(VersionType.BeNice, "\u001b[38;2;132;186;100m10.12.33\u001b[0m")] - [InlineData(VersionType.SoGood, "\u001b[38;2;113;121;126m10.12.33\u001b[0m")] - [InlineData(VersionType.ToNew, "\u001b[38;2;37;121;159m10.12.33\u001b[0m")] - [InlineData(VersionType.TooOld, "\u001b[38;2;236;151;6m10.12.33\u001b[0m")] - public void DrawSketchVersionTypes(VersionType versionType, string expected) + [InlineData(VersionStatus.BeNice, "\u001b[38;2;132;186;100m10.12.33\u001b[0m")] + [InlineData(VersionStatus.SoGood, "\u001b[38;2;113;121;126m10.12.33\u001b[0m")] + [InlineData(VersionStatus.ToNew, "\u001b[38;2;37;121;159m10.12.33\u001b[0m")] + [InlineData(VersionStatus.TooOld, "\u001b[38;2;236;151;6m10.12.33\u001b[0m")] + public void DrawSketchVersionTypes(VersionStatus versionStatus, string expected) { var brand = new Brand("Docker", "󰡨", "#1d63ed"); - var version = new VersionComponent(versionType, "10.12.33", brand); - + var version = new VersionComponent("10.12.33", brand, versionStatus); + var sketch = (version as IComponent).MakeSketch(new Size(10, 2)); - + sketch.ToString().Should().Be(expected); } + + [Theory] + [Trait("Category", nameof(Dependency))] + [InlineData("1.0.0", "0.0.1", VersionStatus.ToNew)] + [InlineData("1.0.0", "0.1.1", VersionStatus.ToNew)] + [InlineData("1.0.0", "0.1.0", VersionStatus.ToNew)] + [InlineData("1.2.0", "1.0.0", VersionStatus.ToNew)] + [InlineData("1.2.0", "1.0.1", VersionStatus.ToNew)] + [InlineData("1.2.0", "1.1.0", VersionStatus.ToNew)] + [InlineData("1.0.0", "1.0.0-rc", VersionStatus.ToNew)] + [InlineData("1.0.0", "1.0.0", VersionStatus.SoGood)] + [InlineData("^1.0.0", "1.0.0", VersionStatus.SoGood)] + [InlineData("1.2.0", "1.3.0", VersionStatus.BeNice)] + [InlineData("1.3.1", "1.3.3", VersionStatus.BeNice)] + [InlineData("1.2.0", "2.1.0", VersionStatus.TooOld)] + [InlineData("1.2.0", "2.0.1", VersionStatus.TooOld)] + [InlineData("1.2.0", "2.3.1", VersionStatus.TooOld)] + public void ComparisonDependencies(string actual, string convention, VersionStatus expectedType) + { + var brand = new Brand("Poker", "󱢢", "#1d63ed"); + var actualDependency = new Dependency(actual, brand); + var conventionDependency = new Dependency(convention, brand); + + var status = actualDependency.Comparison(conventionDependency); + + status.Should().Be(expectedType); + } } } \ No newline at end of file