Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/codescene-refactor #38

Merged
merged 13 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!--
Keep the pull request in draft mode until you are completely ready to receive reviews. That implies that the "checklist before requesting a review" below is completed and that all Github checks has passed.
-->

## Why is the change needed?

<!-- Add requirements -->

## What was changed?

<!-- Add changelog -->
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ obj
**/*.DotSettings.user

# JetBrains
.idea
.idea

# Temp file
temp.md
224 changes: 75 additions & 149 deletions AzureLiquid.Preview/PreviewProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
/// </summary>
public class PreviewProcess
{
/// <summary>
/// The argument parser.
/// </summary>
private readonly PreviewProcessArguments _args;

/// <summary>
/// Handles writing console output to private persisted log.
/// </summary>
Expand All @@ -32,6 +37,7 @@
/// </summary>
public PreviewProcess()
{
_args = new PreviewProcessArguments();
Template = string.Empty;
Content = string.Empty;
Output = "./preview.txt";
Expand Down Expand Up @@ -68,7 +74,7 @@
public string Output { get; set; }

/// <summary>
/// Gets a value indicating whether the process should watch for changes to template or content files.
/// Gets or sets a value indicating whether the process should watch for changes to template or content files.
/// </summary>
/// <value>
/// <c>true</c> if should watch; otherwise, <c>false</c>.
Expand All @@ -87,16 +93,20 @@
/// <summary>
/// Start a new instance of the <see cref="PreviewProcess" /> class using the incoming arguments.
/// </summary>
/// <param name="args">The process arguments</param>
/// <param name="args">The process arguments.</param>
/// <returns>A new instance of the <see cref="PreviewProcess" /> class.</returns>
[ExcludeFromCodeCoverage]
public static PreviewProcess Create(string[] args)
{
var preview = new PreviewProcess();

// deepcode ignore XmlInjection: XML is not used by this application, it is passed back to the user, deepcode ignore XXE: <please specify a reason of ignoring this>
preview.ParseArguments(args);
preview.Template = preview._args.ParsePath(args, "template");
preview.Content = preview._args.ParsePath(args, "content");
preview.Output = preview._args.ParsePath(args, "output");
preview.ShouldWatch = PreviewProcessArguments.HasArgument(args, "watch");

HandleNoArgumentsPassed(args, preview);

if (preview.CanRender)
{
RenderAndWatch(preview);
Expand All @@ -109,107 +119,6 @@
return preview;
}

/// <summary>Parses the arguments and sets process options.</summary>
/// <param name="args">
/// The arguments. Values are expected to be "--template", "--help", "--content", "--output" or
/// "--watch".
/// </param>
private void ParseArguments(string[] args)
{
for (var index = 0; index < args.Length; index++)
{
var arg = args[index];
var path = Directory.GetCurrentDirectory();
ParseTemplate(args, index, arg, path);
ParseContent(args, index, arg, path);
ParseOutputResults(args, index, arg, path);

// Switch watch param if needed
if (IsArgMatch(arg, "watch"))
{
ShouldWatch = true;
}

// Show help info
if (IsArgMatch(arg, "help"))
{
WriteHelpOutput();
}
}
}

/// <summary>
/// Parses the output results file path if specified.
/// </summary>
/// <param name="args">The passed command arguments.</param>
/// <param name="index">The parameter index.</param>
/// <param name="arg">The current argument.</param>
/// <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)
{
return;
}

try
{
Output = Path.GetFullPath(args[index + 1], path);
}
catch
{
WriteErrorLine($"Invalid output path: {args[index + 1]}");
}
}

/// <summary>
/// Parses the incoming content file path if specified.
/// </summary>
/// <param name="args">The passed command arguments.</param>
/// <param name="index">The parameter index.</param>
/// <param name="arg">The current argument.</param>
/// <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)
{
return;
}

try
{
Content = Path.GetFullPath(args[index + 1], path);
}
catch
{
WriteErrorLine($"Invalid content path: {args[index + 1]}");
}
}

/// <summary>
/// Parses the incoming template file path if specified.
/// </summary>
/// <param name="args">The passed command arguments.</param>
/// <param name="index">The parameter index.</param>
/// <param name="arg">The current argument.</param>
/// <param name="path">The target path.</param>
private void ParseTemplate(string[] args, int index, string arg, string path)
{
if (!IsArgMatch(arg, "template") || index - 1 >= args.Length)
{
return;
}

try
{
Template = Path.GetFullPath(args[index + 1], path);
}
catch
{
WriteErrorLine($"Invalid template path: {args[index + 1]}");
}
}

/// <summary>
/// Renders the output and watches for changes if specified.
/// </summary>
Expand Down Expand Up @@ -287,19 +196,6 @@
Console.ForegroundColor = ConsoleColor.White;
}

/// <summary>
/// Determines whether the argument matches the partial argument key name.
/// </summary>
/// <param name="arg">The argument.</param>
/// <param name="key">The key.</param>
/// <returns>
/// <c>true</c> if argument found; otherwise, <c>false</c>.
/// </returns>
private static bool IsArgMatch(string arg, string key)
{
return string.CompareOrdinal(arg, "--" + key) == 0;
}

/// <summary>
/// Renders the output using the specified properties of the instance.
/// </summary>
Expand All @@ -308,76 +204,106 @@
{
if (!CanRender)
{
WriteErrorLine("Unable to render as inputs our outputs not found or not specified");
WriteErrorLine("Unable to render as inputs or outputs not found or not specified");

Check warning on line 207 in AzureLiquid.Preview/PreviewProcess.cs

View check run for this annotation

Codecov / codecov/patch

AzureLiquid.Preview/PreviewProcess.cs#L207

Added line #L207 was not covered by tests
return string.Empty;
}

string content;
try
var content = ReadFileContent(Content);
if (string.IsNullOrEmpty(content))
{
content = File.ReadAllText(Content);
return string.Empty;

Check warning on line 214 in AzureLiquid.Preview/PreviewProcess.cs

View check run for this annotation

Codecov / codecov/patch

AzureLiquid.Preview/PreviewProcess.cs#L214

Added line #L214 was not covered by tests
}
catch (IOException)

var template = ReadFileContent(Template);
if (string.IsNullOrEmpty(template))
{
// Lock issue, wait and retry
Thread.Sleep(TimeSpan.FromSeconds(1));
return Render();
return string.Empty;

Check warning on line 220 in AzureLiquid.Preview/PreviewProcess.cs

View check run for this annotation

Codecov / codecov/patch

AzureLiquid.Preview/PreviewProcess.cs#L220

Added line #L220 was not covered by tests
}

string template;
var parser = new LiquidParser();
return !SetParserContent(parser, content) ?
string.Empty :
RenderTemplate(parser, template);
}

/// <summary>
/// Reads the file content.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <returns>The file content.</returns>
private string ReadFileContent(string filePath)
{
try
{
template = File.ReadAllText(Template);
return File.ReadAllText(filePath);
}
catch (IOException)
{
// Lock issue, wait and retry
Thread.Sleep(TimeSpan.FromSeconds(1));
return Render();
return ReadFileContent(filePath);

Check warning on line 244 in AzureLiquid.Preview/PreviewProcess.cs

View check run for this annotation

Codecov / codecov/patch

AzureLiquid.Preview/PreviewProcess.cs#L244

Added line #L244 was not covered by tests
}
catch
{
LogWarning($"Unable to read file: {filePath}");
return string.Empty;

Check warning on line 249 in AzureLiquid.Preview/PreviewProcess.cs

View check run for this annotation

Codecov / codecov/patch

AzureLiquid.Preview/PreviewProcess.cs#L246-L249

Added lines #L246 - L249 were not covered by tests
}
Fixed Show fixed Hide fixed
}

var parser = new LiquidParser();

if (Content.ToLowerInvariant().EndsWith(".json"))
/// <summary>
/// Sets the parser content.
/// </summary>
/// <param name="parser">The parser.</param>
/// <param name="content">The content.</param>
/// <returns>
/// <c>true</c> if the content was set; otherwise, <c>false</c>.
/// </returns>
private bool SetParserContent(LiquidParser parser, string content)
{
try
{
try
if (Content.ToLowerInvariant().EndsWith(".json"))
{
parser.SetContentJson(content);
}
catch (Exception e)
{
LogWarning(" Unable to read input JSON file", e);
return string.Empty;
}
}

if (Content.ToLowerInvariant().EndsWith(".xml"))
{
try
else if (Content.ToLowerInvariant().EndsWith(".xml"))
{
parser.SetContentXml(content);
}
catch (Exception ex)
else
{
LogWarning(" Unable to read input XML file", ex);
return string.Empty;
WriteErrorLine("Unsupported content type");
return false;

Check warning on line 276 in AzureLiquid.Preview/PreviewProcess.cs

View check run for this annotation

Codecov / codecov/patch

AzureLiquid.Preview/PreviewProcess.cs#L275-L276

Added lines #L275 - L276 were not covered by tests
}
}
catch (Exception e)
{
LogWarning("Unable to set parser content", e);
return false;

Check warning on line 282 in AzureLiquid.Preview/PreviewProcess.cs

View check run for this annotation

Codecov / codecov/patch

AzureLiquid.Preview/PreviewProcess.cs#L279-L282

Added lines #L279 - L282 were not covered by tests
}
Dismissed Show dismissed Hide dismissed

return true;
}

/// <summary>
/// Renders the template.
/// </summary>
/// <param name="parser">The parser.</param>
/// <param name="template">The template.</param>
/// <returns>The output from the template.</returns>
private string RenderTemplate(LiquidParser parser, string template)
{
try
{
var output = parser.Parse(template).Render();
File.WriteAllText(Output, output);

return output;
}
catch (Exception e)
{
WriteErrorLine($"Error: {e.Message}");
return string.Empty;
}

// TODO: Refactor this method
}

/// <summary>
Expand Down
Loading
Loading