Implement diff-reporter PoC (#5)

This commit is contained in:
minsung
2026-04-07 14:12:15 +09:00
parent 3c5294a4cb
commit 7920de15b3
11 changed files with 536 additions and 0 deletions

View 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");
}