diff --git a/.gitignore b/.gitignore index 7f24a60..46ed030 100644 --- a/.gitignore +++ b/.gitignore @@ -342,4 +342,4 @@ healthchecksdb *.DotSettings *.DS_Store **/*.Production.json -Logs/ \ No newline at end of file +**/*.log \ No newline at end of file diff --git a/src/TUI.Engine/Extensions.cs b/src/TUI.Engine/Extensions.cs index 9ef3c11..3f7ed4f 100644 --- a/src/TUI.Engine/Extensions.cs +++ b/src/TUI.Engine/Extensions.cs @@ -42,10 +42,10 @@ public static class Extensions public static Version ToVersion(this string textVersion) { - var version = textVersion.Replace("^", "").Replace("~", "").Split("."); + var version = textVersion.Replace("^", "").Replace("v", "").Replace("~", "").Split("."); var major = Convert.ToInt32(version[0]); var minor = Convert.ToInt32(version[1]); var patch = Convert.ToInt32(version[2].Split('-')[0]); return new Version(major, minor, patch); } -} \ No newline at end of file +} diff --git a/src/TUI/Controls/Components/PanelComponent.cs b/src/TUI/Controls/Components/PanelComponent.cs index 9bb9d5c..80661c9 100644 --- a/src/TUI/Controls/Components/PanelComponent.cs +++ b/src/TUI/Controls/Components/PanelComponent.cs @@ -3,6 +3,7 @@ using TUI.Engine; using TUI.Engine.Attributes; using TUI.Engine.Components; using TUI.Engine.Theme; +using TUI.Logs; using static TUI.Engine.Symbols; namespace TUI.Controls.Components; @@ -19,12 +20,13 @@ public class PanelComponent : ComponentBase, IComponent private static void RenderTopLine(StringBuilder builder, Size size, string title) { - var halfWidth = (size.Width - title.GetWidth() - (int)Indentation.BorderWidth * 2 - - (int)Indentation.Default * 2) / 2; + var availableWidth = (size.Width - title.GetWidth() - (int)Indentation.BorderWidth * 2 - + (int)Indentation.Default * 2); + var halfWidth = availableWidth/ 2; builder.Append(Angles.LeftTop); builder.Append(Lines.Horizontal.Repeat(halfWidth)); builder.AppendFormat("{0}{1}{0}", Space.Repeat(Convert.ToInt32(Indentation.Default)), title); - builder.Append(Lines.Horizontal.Repeat(halfWidth + halfWidth % 2)); + builder.Append(Lines.Horizontal.Repeat(halfWidth + availableWidth % 2)); builder.Append(Angles.RightTop); builder.Append(LineBreak); } diff --git a/src/TUI/Logs/Log.cs b/src/TUI/Logs/Log.cs new file mode 100644 index 0000000..2b416d0 --- /dev/null +++ b/src/TUI/Logs/Log.cs @@ -0,0 +1,20 @@ +namespace TUI.Logs; + +public static class Log +{ + private static string Now => DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + public static void Trace(string message) => Write("🌚", message); + public static void Debug(string message) => Write("🦎", message); + public static void Info(string message) => Write("🦋", message); + public static void Warning(string message) => Write("🍋", message); + public static void Error(string message) => Write("🐞", message); + public static void Fatal(string message) => Write("💀", message); + + public static void Write(string icon, string message) + { + // /tld/src/TUI/file.log + var file = "file.log"; + var line = string.Join('|', Now, icon, message); + File.AppendAllText(file, line + Environment.NewLine); + } +} \ No newline at end of file diff --git a/src/TUI/Pages/PageBase.cs b/src/TUI/Pages/PageBase.cs index 91aa0e7..9511f78 100644 --- a/src/TUI/Pages/PageBase.cs +++ b/src/TUI/Pages/PageBase.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using TUI.Logs; namespace TUI.Pages; @@ -6,17 +7,18 @@ public abstract class PageBase : IPage { public void Open() { - Debugger.Log(0, "Event", $"Open page ${GetType().UnderlyingSystemType.Name}\n"); + Debugger.Log(0, "Event", $"Open page {GetType().UnderlyingSystemType.Name}\n"); + Log.Trace($"Open page {GetType().UnderlyingSystemType.Name}."); Bind(); Initial(); Render(); } - + public abstract void Load(); - + public abstract void Initial(); - + public abstract void Render(); - + public abstract void Bind(); } \ No newline at end of file diff --git a/src/TUI/Program.cs b/src/TUI/Program.cs index c5120cb..dd9213b 100644 --- a/src/TUI/Program.cs +++ b/src/TUI/Program.cs @@ -1,9 +1,12 @@ -using TUI.Pages; +using TUI.Logs; +using TUI.Pages; Console.Clear(); Console.CursorVisible = false; +Log.Info("Run application."); + var welcomePage = WelcomePage.Instance; welcomePage.Open(); Thread.Sleep(500); @@ -21,9 +24,11 @@ do { case ConsoleKey.Q: waitCommand = false; + Log.Trace("Run command quit."); break; case ConsoleKey.R: key = null; + Log.Trace("Run command load deps."); currentPage.Load(); break; case ConsoleKey.D1: @@ -44,5 +49,6 @@ do } } while (waitCommand); +Log.Info("Quit application."); Console.Clear(); Console.CursorVisible = true; \ No newline at end of file diff --git a/src/TUI/Providers/Dependencies/DependencyRepository.cs b/src/TUI/Providers/Dependencies/DependencyRepository.cs index 45ac9d9..56f54c6 100644 --- a/src/TUI/Providers/Dependencies/DependencyRepository.cs +++ b/src/TUI/Providers/Dependencies/DependencyRepository.cs @@ -1,6 +1,7 @@ using System.Text.Json; using System.Text.Json.Nodes; using TUI.Domain; +using TUI.Logs; using YamlDotNet.Serialization; using YamlDotNet.Serialization.NamingConventions; @@ -9,7 +10,7 @@ namespace TUI.Providers.Dependencies; public class DependencyRepository { private DependenciesDto? _dependenciesDto; - + private DependenciesDto DependenciesDto { get @@ -18,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 @@ -41,70 +42,69 @@ 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, new Hub(hub.Origin, hub.Type)))); } - + return projects; } - - + + 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); - + Log.Debug($"Fetch dependencies for project {project.Name}."); 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); } } @@ -116,10 +116,10 @@ public class DependencyRepository // // 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; // } diff --git a/src/TUI/Store/DependenciesStore.cs b/src/TUI/Store/DependenciesStore.cs index 9d8c9d4..7441dbd 100644 --- a/src/TUI/Store/DependenciesStore.cs +++ b/src/TUI/Store/DependenciesStore.cs @@ -3,6 +3,7 @@ using TUI.Controls.Components; using TUI.Domain; using TUI.Engine; using TUI.Engine.Theme; +using TUI.Logs; using TUI.Providers.Dependencies; namespace TUI.Store; @@ -26,6 +27,7 @@ public class DependenciesStore } catch(Exception ex) { + Log.Error("Fail load actual deps for project " +project.Name + ". " + ex.Message); Debugger.Log(0, "error", ex.Message); SpeakerComponent.Instance.Shout(Symbols.Error.Error(), $"Fetch failed for project{project.Name}"); return new List(); @@ -34,10 +36,18 @@ public class DependenciesStore public void Bind() { - SpeakerComponent.Instance.Shout("🤔", "Prepare javascript conventions"); - ConventionDependencies = Repository.ReadConventions("javascript"); + try + { + SpeakerComponent.Instance.Shout("🤔", "Prepare javascript conventions"); + ConventionDependencies = Repository.ReadConventions("javascript"); - SpeakerComponent.Instance.Shout("🤩", "Prepare javascript projects"); - Projects = Repository.ReadProjects("javascript"); + SpeakerComponent.Instance.Shout("🤩", "Prepare javascript projects"); + Projects = Repository.ReadProjects("javascript"); + Log.Info("Loading settings."); + } + catch (Exception ex) + { + Log.Error("Fail Loading settings. " + ex.Message); + } } } \ No newline at end of file