Skip to content

Commit

Permalink
refactor: Update access modifiers and improve argument parsing in Pre…
Browse files Browse the repository at this point in the history
…viewProcess
  • Loading branch information
github-actions[bot] committed Dec 2, 2024
1 parent b16aa4f commit 279b724
Showing 7 changed files with 76 additions and 61 deletions.
111 changes: 61 additions & 50 deletions AzureLiquid.Preview/PreviewProcess.cs
Original file line number Diff line number Diff line change
@@ -74,15 +74,15 @@ public PreviewProcess()
/// <c>true</c> if should watch; otherwise, <c>false</c>.
/// </value>
[ExcludeFromCodeCoverage]
internal bool ShouldWatch { get; private set; }
private bool ShouldWatch { get; set; }

/// <summary>
/// Gets a value indicating whether this instance can parse the inputs and render the output.
/// </summary>
/// <returns>
/// <c>true</c> if this instance can parse; otherwise, <c>false</c>.
/// </returns>
public bool CanRender => File.Exists(Template) && File.Exists(Content) && !string.IsNullOrEmpty(Output);
private bool CanRender => File.Exists(Template) && File.Exists(Content) && !string.IsNullOrEmpty(Output);

/// <summary>
/// Start a new instance of the <see cref="PreviewProcess" /> 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".
/// </param>
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)
/// <param name="path">The target path.</param>
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
/// <param name="path">The target path.</param>
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)
/// <param name="path">The target path.</param>
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();
}

/// <summary>
@@ -229,12 +236,14 @@ private static void RenderAndWatch(PreviewProcess preview)
/// <param name="preview">The current preview process.</param>
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();
}

/// <summary>
@@ -244,7 +253,7 @@ private static void LogMissingFiles(PreviewProcess preview)
/// <param name="preview">The instance of <see cref="PreviewProcess" /> to handle the output.</param>
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()
/// </summary>
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;
}

/// <summary>
2 changes: 1 addition & 1 deletion AzureLiquid.Tests/Arrangement.cs
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ public Arrangement()
Expected = "<p>Jane Doe</p>"
};

var simple = "Simple Template";
const string simple = "Simple Template";
SimpleTemplate = new TemplateFact<BasicObject>
{
Content = new BasicObject(simple),
3 changes: 2 additions & 1 deletion AzureLiquid.Tests/LiquidParserTests.cs
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@ public void EnsureDeepParsing()
public void EnsureTemplateParsing()
{
Arrange(new Arrangement().SimpleTemplate);
Arrange(new Arrangement().Albums);
}

/// <summary>
@@ -84,7 +85,7 @@ private static LiquidParser CreateParser<T>(TemplateFact<T> fact, bool forceCame

if (fact.Content is string content)
{
if (content!.Contains("<?xml"))
if (content.Contains("<?xml"))
{
parser.SetContentXml(content);
}
5 changes: 2 additions & 3 deletions AzureLiquid.Tests/PreviewProcessTests.cs
Original file line number Diff line number Diff line change
@@ -140,11 +140,10 @@ public void EnsureObjectCreation()
public void EnsureWatcher()
{
// Arrange
var instance = PreviewProcess.Create(new[]
{
var instance = PreviewProcess.Create([
"--template", "./Resources/event.liquid", "--content", "./Resources/event.json", "--output",
"./Resources/preview.txt"
});
]);

// Act
instance.StartWatch();
12 changes: 6 additions & 6 deletions AzureLiquid.Tests/TemplateFact.cs
Original file line number Diff line number Diff line change
@@ -11,26 +11,26 @@ namespace AzureLiquid.Tests;
public class TemplateFact<T>
{
/// <summary>
/// Gets or sets the template.
/// Gets the template.
/// </summary>
/// <value>
/// The template.
/// </value>
public string? Template { get; set; }
public string? Template { get; init; }

/// <summary>
/// Gets or sets the expected result.
/// Gets the expected result.
/// </summary>
/// <value>
/// The expected result.
/// </value>
public string? Expected { get; set; }
public string? Expected { get; init; }

/// <summary>
/// Gets or sets the content.
/// Gets the content.
/// </summary>
/// <value>
/// The content.
/// </value>
public T? Content { get; set; }
public T? Content { get; init; }
}
1 change: 1 addition & 0 deletions AzureLiquid.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -348,4 +348,5 @@ Licensed under the open source Apache License, Version 2.0.&#xD;
&lt;/copyright&gt;&#xD;
</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECodeCleanup_002EFileHeader_002EFileHeaderSettingsMigrate/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002EMemberReordering_002EMigrations_002ECSharpFileLayoutPatternRemoveIsAttributeUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Rpic/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
3 changes: 3 additions & 0 deletions qodana.yaml
Original file line number Diff line number Diff line change
@@ -8,3 +8,6 @@ exclude:
- name: All
paths:
- AzureLiquid.Tests/Resources
- AzureLiquid.Preview/Properties/launchSettings.json
- name: UnusedMember.Global
- name: UnusedType.Global

0 comments on commit 279b724

Please sign in to comment.