Files
gregExtractor/GregExtractorCli.cs

141 lines
4.9 KiB
C#

using System.Text;
namespace gregExtractor;
internal static class GregExtractorCli
{
public static int Run(string[] args)
{
try
{
if (args.Length == 0)
return PrintHelp();
string command = args[0].Trim().ToLowerInvariant();
Dictionary<string, string> options = ParseOptions(args.Skip(1).ToArray());
return command switch
{
"scan" => RunScan(options),
"template" => RunTemplate(options),
"help" or "--help" or "-h" => PrintHelp(),
_ => PrintHelp($"Unknown command: {command}"),
};
}
catch (Exception ex)
{
Console.Error.WriteLine($"[gregExtractor] Fehler: {ex}");
return 1;
}
}
private static int RunScan(IReadOnlyDictionary<string, string> options)
{
string source = GetRequired(options, "source");
string state = GetOptional(options, "state", Path.Combine(AppContext.BaseDirectory, "state"));
var store = new SnapshotStore(state);
var service = new HookAutomationService(new SourceScanner(), store);
SourceSnapshot snapshot = service.ScanCurrent(source);
ChangeReport report = service.CompareWithPrevious(snapshot);
service.Persist(snapshot, report);
Console.WriteLine("[gregExtractor] Scan abgeschlossen");
Console.WriteLine($"- Source: {snapshot.SourceRoot}");
Console.WriteLine($"- Files: {snapshot.FileCount}");
Console.WriteLine($"- Methods: {snapshot.Methods.Count}");
Console.WriteLine($"- Added: {report.Added} | Removed: {report.Removed} | SigChanged: {report.SignatureChanged} | BodyChanged: {report.BodyChanged}");
return 0;
}
private static int RunTemplate(IReadOnlyDictionary<string, string> options)
{
string repoRoot = GetRequired(options, "repo");
string output = GetRequired(options, "output");
string plugin = GetOptional(options, "name", "greg.Plugin.HookTemplate");
string packageVersion = GetOptional(options, "gregcore-version", "0.0.0-local");
string @namespace = plugin;
string className = plugin.Split('.', StringSplitOptions.RemoveEmptyEntries).LastOrDefault() ?? "GregPluginHookTemplate";
var store = new SnapshotStore(Path.Combine(repoRoot, "gregExtractor", "state"));
var service = new HookAutomationService(new SourceScanner(), store);
IReadOnlyList<HookCatalogRow> rows = service.LoadHookCatalogRows(repoRoot);
var paths = service.GeneratePluginTemplate(
repoRoot,
output,
plugin,
@namespace,
className,
author: "gregExtractor",
rows,
packageVersion);
Console.WriteLine("[gregExtractor] Template erzeugt");
Console.WriteLine($"- Project: {paths.CsprojPath}");
Console.WriteLine($"- Main: {paths.MainCsPath}");
Console.WriteLine($"- Readme: {paths.ReadmePath}");
return 0;
}
private static string GetRequired(IReadOnlyDictionary<string, string> options, string name)
{
if (!options.TryGetValue(name, out string? value) || string.IsNullOrWhiteSpace(value))
throw new ArgumentException($"Missing required option --{name}");
return value;
}
private static string GetOptional(IReadOnlyDictionary<string, string> options, string name, string fallback)
{
if (options.TryGetValue(name, out string? value) && !string.IsNullOrWhiteSpace(value))
return value;
return fallback;
}
private static Dictionary<string, string> ParseOptions(string[] args)
{
var map = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
for (int index = 0; index < args.Length; index++)
{
string token = args[index];
if (!token.StartsWith("--", StringComparison.Ordinal))
continue;
string key = token[2..].Trim();
string value = "true";
if (index + 1 < args.Length && !args[index + 1].StartsWith("--", StringComparison.Ordinal))
{
value = args[index + 1];
index++;
}
map[key] = value;
}
return map;
}
private static int PrintHelp(string? error = null)
{
if (!string.IsNullOrWhiteSpace(error))
Console.Error.WriteLine($"[gregExtractor] {error}");
var sb = new StringBuilder();
sb.AppendLine("gregExtractor CLI");
sb.AppendLine();
sb.AppendLine("Commands:");
sb.AppendLine(" scan --source <path> [--state <path>]");
sb.AppendLine(" template --repo <path> --output <path> [--name <plugin>] [--gregcore-version <version>]");
sb.AppendLine(" --gui (startet die bestehende GUI)");
Console.WriteLine(sb.ToString());
return string.IsNullOrWhiteSpace(error) ? 0 : 1;
}
}