Implement diff-reporter PoC (#5)
This commit is contained in:
99
src/Recordingtest.DiffReporter.Cli/Program.cs
Normal file
99
src/Recordingtest.DiffReporter.Cli/Program.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Recordingtest.DiffReporter;
|
||||
|
||||
namespace Recordingtest.DiffReporter.Cli;
|
||||
|
||||
public static class Program
|
||||
{
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
string? approved = null, received = null, outDir = null;
|
||||
for (int i = 0; i < args.Length; i++)
|
||||
{
|
||||
switch (args[i])
|
||||
{
|
||||
case "--approved": approved = args[++i]; break;
|
||||
case "--received": received = args[++i]; break;
|
||||
case "--out": outDir = args[++i]; break;
|
||||
}
|
||||
}
|
||||
if (approved is null || received is null || outDir is null)
|
||||
{
|
||||
Console.Error.WriteLine("Usage: --approved <path> --received <path> --out <dir>");
|
||||
return 2;
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(outDir);
|
||||
DiffResult result;
|
||||
try
|
||||
{
|
||||
result = Differ.Compare(approved, received);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
Console.Error.WriteLine("I/O error: " + ex.Message);
|
||||
return 2;
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
Console.Error.WriteLine("I/O error: " + ex.Message);
|
||||
return 2;
|
||||
}
|
||||
|
||||
var jsonOpts = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = true
|
||||
};
|
||||
File.WriteAllText(Path.Combine(outDir, "diff.json"),
|
||||
JsonSerializer.Serialize(result, jsonOpts), Encoding.UTF8);
|
||||
File.WriteAllText(Path.Combine(outDir, "diff.md"),
|
||||
RenderMarkdown(result), Encoding.UTF8);
|
||||
|
||||
if (result.Identical)
|
||||
{
|
||||
Console.Out.WriteLine("identical");
|
||||
return 0;
|
||||
}
|
||||
Console.Out.WriteLine($"diff: +{result.Summary.Added} -{result.Summary.Removed} ~{result.Summary.Changed} in {result.File}");
|
||||
return 1;
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
Console.Error.WriteLine("I/O error: " + ex.Message);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
private static string RenderMarkdown(DiffResult r)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine($"# Diff: {r.File}");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine($"Identical: **{r.Identical}**");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("| Added | Removed | Changed |");
|
||||
sb.AppendLine("|------:|--------:|--------:|");
|
||||
sb.AppendLine($"| {r.Summary.Added} | {r.Summary.Removed} | {r.Summary.Changed} |");
|
||||
sb.AppendLine();
|
||||
if (r.Hunks.Count > 0)
|
||||
{
|
||||
sb.AppendLine("## Hunks");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("| # | LineOrOffset | Kind | Before | After |");
|
||||
sb.AppendLine("|--:|-------------:|------|--------|-------|");
|
||||
for (int i = 0; i < r.Hunks.Count; i++)
|
||||
{
|
||||
var h = r.Hunks[i];
|
||||
sb.AppendLine($"| {i} | {h.LineOrOffset} | {h.Kind} | {Escape(h.Before)} | {Escape(h.After)} |");
|
||||
}
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private static string Escape(string s) =>
|
||||
s.Replace("|", "\\|").Replace("\r", "").Replace("\n", "\\n");
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AssemblyName>Recordingtest.DiffReporter.Cli</AssemblyName>
|
||||
<RootNamespace>Recordingtest.DiffReporter.Cli</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Recordingtest.DiffReporter\Recordingtest.DiffReporter.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user