From 279b72472ae5b5c7a4fbf84e9cdc7e55a508cb65 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 2 Dec 2024 09:41:17 +0000 Subject: [PATCH] refactor: Update access modifiers and improve argument parsing in PreviewProcess --- AzureLiquid.Preview/PreviewProcess.cs | 111 +++++++++++++---------- AzureLiquid.Tests/Arrangement.cs | 2 +- AzureLiquid.Tests/LiquidParserTests.cs | 3 +- AzureLiquid.Tests/PreviewProcessTests.cs | 5 +- AzureLiquid.Tests/TemplateFact.cs | 12 +-- AzureLiquid.sln.DotSettings | 1 + qodana.yaml | 3 + 7 files changed, 76 insertions(+), 61 deletions(-) diff --git a/AzureLiquid.Preview/PreviewProcess.cs b/AzureLiquid.Preview/PreviewProcess.cs index cf082f4..8cbd8f3 100644 --- a/AzureLiquid.Preview/PreviewProcess.cs +++ b/AzureLiquid.Preview/PreviewProcess.cs @@ -74,7 +74,7 @@ public PreviewProcess() /// true if should watch; otherwise, false. /// [ExcludeFromCodeCoverage] - internal bool ShouldWatch { get; private set; } + private bool ShouldWatch { get; set; } /// /// Gets a value indicating whether this instance can parse the inputs and render the output. @@ -82,7 +82,7 @@ public PreviewProcess() /// /// true if this instance can parse; otherwise, false. /// - public bool CanRender => File.Exists(Template) && File.Exists(Content) && !string.IsNullOrEmpty(Output); + private bool CanRender => File.Exists(Template) && File.Exists(Content) && !string.IsNullOrEmpty(Output); /// /// Start a new instance of the class using the incoming arguments. @@ -114,7 +114,7 @@ public static PreviewProcess Create(string[] args) /// The arguments. Values are expected to be "--template", "--help", "--content", "--output" or /// "--watch". /// - internal void ParseArguments(string[] args) + private void ParseArguments(string[] args) { for (var index = 0; index < args.Length; index++) { @@ -147,16 +147,18 @@ internal void ParseArguments(string[] args) /// The target path. private void ParseOutputResults(string[] args, int index, string arg, string path) { - if (IsArgMatch(arg, "output") && index - 1 < args.Length) + if (!IsArgMatch(arg, "output") || index - 1 >= args.Length) { - try - { - Output = Path.GetFullPath(args[index + 1], path); - } - catch - { - WriteErrorLine($"Invalid output path: {args[index + 1]}"); - } + return; + } + + try + { + Output = Path.GetFullPath(args[index + 1], path); + } + catch + { + WriteErrorLine($"Invalid output path: {args[index + 1]}"); } } @@ -169,16 +171,18 @@ private void ParseOutputResults(string[] args, int index, string arg, string pat /// The target path. private void ParseContent(string[] args, int index, string arg, string path) { - if (IsArgMatch(arg, "content") && index - 1 < args.Length) + if (!IsArgMatch(arg, "content") || index - 1 >= args.Length) { - try - { - Content = Path.GetFullPath(args[index + 1], path); - } - catch - { - WriteErrorLine($"Invalid content path: {args[index + 1]}"); - } + return; + } + + try + { + Content = Path.GetFullPath(args[index + 1], path); + } + catch + { + WriteErrorLine($"Invalid content path: {args[index + 1]}"); } } @@ -191,17 +195,18 @@ private void ParseContent(string[] args, int index, string arg, string path) /// The target path. private void ParseTemplate(string[] args, int index, string arg, string path) { - // Parse incoming template file - if (IsArgMatch(arg, "template") && index - 1 < args.Length) + if (!IsArgMatch(arg, "template") || index - 1 >= args.Length) { - try - { - Template = Path.GetFullPath(args[index + 1], path); - } - catch - { - WriteErrorLine($"Invalid template path: {args[index + 1]}"); - } + return; + } + + try + { + Template = Path.GetFullPath(args[index + 1], path); + } + catch + { + WriteErrorLine($"Invalid template path: {args[index + 1]}"); } } @@ -213,14 +218,16 @@ private static void RenderAndWatch(PreviewProcess preview) { preview.Render(); - if (preview.ShouldWatch) + if (!preview.ShouldWatch) { - preview.StartWatch(); - preview.LogMessage("Press any key to exit file watch..."); - _ = Console.ReadKey(); - preview.StopWatch(); - preview.LogMessage(); + return; } + + preview.StartWatch(); + preview.LogMessage("Press any key to exit file watch..."); + _ = Console.ReadKey(); + preview.StopWatch(); + preview.LogMessage(); } /// @@ -229,12 +236,14 @@ private static void RenderAndWatch(PreviewProcess preview) /// The current preview process. private static void LogMissingFiles(PreviewProcess preview) { - if (!string.IsNullOrEmpty(preview.Content) && !string.IsNullOrEmpty(preview.Template)) + if (string.IsNullOrEmpty(preview.Content) || string.IsNullOrEmpty(preview.Template)) { - Console.ForegroundColor = ConsoleColor.Yellow; - preview.LogMessage(" Unable to render as input files are not found"); - preview.LogMessage(); + return; } + + Console.ForegroundColor = ConsoleColor.Yellow; + preview.LogMessage(" Unable to render as input files are not found"); + preview.LogMessage(); } /// @@ -244,7 +253,7 @@ private static void LogMissingFiles(PreviewProcess preview) /// The instance of to handle the output. private static void HandleNoArgumentsPassed(string[] args, PreviewProcess preview) { - if (args?.Length == 0) + if (args.Length == 0) { preview.WriteHelpOutput(); } @@ -376,17 +385,19 @@ public string Render() /// public void StartWatch() { - if (CanRender) + if (!CanRender) { - if (_contentWatcher == null) - { - _contentWatcher = StartWatch(Content); - _templateWatcher = StartWatch(Template); - } + return; + } - _contentWatcher!.EnableRaisingEvents = true; - _templateWatcher!.EnableRaisingEvents = true; + if (_contentWatcher == null) + { + _contentWatcher = StartWatch(Content); + _templateWatcher = StartWatch(Template); } + + _contentWatcher!.EnableRaisingEvents = true; + _templateWatcher!.EnableRaisingEvents = true; } /// diff --git a/AzureLiquid.Tests/Arrangement.cs b/AzureLiquid.Tests/Arrangement.cs index e94a5cd..71d37bc 100644 --- a/AzureLiquid.Tests/Arrangement.cs +++ b/AzureLiquid.Tests/Arrangement.cs @@ -24,7 +24,7 @@ public Arrangement() Expected = "

Jane Doe

" }; - var simple = "Simple Template"; + const string simple = "Simple Template"; SimpleTemplate = new TemplateFact { Content = new BasicObject(simple), diff --git a/AzureLiquid.Tests/LiquidParserTests.cs b/AzureLiquid.Tests/LiquidParserTests.cs index 8cf6b71..585e0e7 100644 --- a/AzureLiquid.Tests/LiquidParserTests.cs +++ b/AzureLiquid.Tests/LiquidParserTests.cs @@ -49,6 +49,7 @@ public void EnsureDeepParsing() public void EnsureTemplateParsing() { Arrange(new Arrangement().SimpleTemplate); + Arrange(new Arrangement().Albums); } /// @@ -84,7 +85,7 @@ private static LiquidParser CreateParser(TemplateFact fact, bool forceCame if (fact.Content is string content) { - if (content!.Contains(" { /// - /// Gets or sets the template. + /// Gets the template. /// /// /// The template. /// - public string? Template { get; set; } + public string? Template { get; init; } /// - /// Gets or sets the expected result. + /// Gets the expected result. /// /// /// The expected result. /// - public string? Expected { get; set; } + public string? Expected { get; init; } /// - /// Gets or sets the content. + /// Gets the content. /// /// /// The content. /// - public T? Content { get; set; } + public T? Content { get; init; } } \ No newline at end of file diff --git a/AzureLiquid.sln.DotSettings b/AzureLiquid.sln.DotSettings index a710983..ac10a75 100644 --- a/AzureLiquid.sln.DotSettings +++ b/AzureLiquid.sln.DotSettings @@ -348,4 +348,5 @@ Licensed under the open source Apache License, Version 2.0. </copyright> True + True True \ No newline at end of file diff --git a/qodana.yaml b/qodana.yaml index 7366556..1ffd0b5 100644 --- a/qodana.yaml +++ b/qodana.yaml @@ -8,3 +8,6 @@ exclude: - name: All paths: - AzureLiquid.Tests/Resources + - AzureLiquid.Preview/Properties/launchSettings.json + - name: UnusedMember.Global + - name: UnusedType.Global