Files
gregExtractor/Commands/CreateCommand.cs
T

96 lines
3.3 KiB
C#

using gregExtractor.Models;
using gregExtractor.Services;
namespace gregExtractor.Commands;
public sealed class CreateCommand : AsyncCommand<CreateCommand.Settings>
{
public sealed class Settings : CommandSettings
{
[CommandArgument(0, "<MOD_NAME>")]
public string ModName { get; init; } = string.Empty;
[CommandOption("--type <TYPE>")]
[DefaultValue("harmonyPatch")]
public string Type { get; init; } = "harmonyPatch";
[CommandOption("--category <CATEGORY>")]
public string? Category { get; init; }
[CommandOption("--path <OUTPUT_PATH>")]
public string? Path { get; init; }
}
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{
if (string.IsNullOrWhiteSpace(settings.ModName))
{
AnsiConsole.MarkupLine("[red]Mod name is required.[/]");
return -1;
}
if (!TryParseTemplateType(settings.Type, out TemplateType templateType))
{
AnsiConsole.MarkupLine($"[red]Invalid --type value: {Markup.Escape(settings.Type)}[/]");
return -1;
}
string outputPath = string.IsNullOrWhiteSpace(settings.Path)
? Directory.GetCurrentDirectory()
: settings.Path;
string hooksPath = Path.Combine(Directory.GetCurrentDirectory(), "game_hooks.json");
var service = new TemplateService();
var progressMessages = new List<string>();
var progress = new Progress<string>(message => progressMessages.Add(message));
TemplateGenerationResult result = await service.GenerateModAsync(
settings.ModName,
settings.Category,
templateType,
outputPath,
hooksPath,
progress,
CancellationToken.None)
.ConfigureAwait(false);
foreach (string message in progressMessages)
AnsiConsole.MarkupLine($"[grey]{Markup.Escape(message)}[/]");
AnsiConsole.MarkupLine($"[green]Scaffold created:[/] {Markup.Escape(result.ModDirectory)}");
var tree = new Tree(Path.GetFileName(result.ModDirectory));
foreach (string file in result.GeneratedFiles.OrderBy(file => file, StringComparer.OrdinalIgnoreCase))
{
string relative = Path.GetRelativePath(result.ModDirectory, file).Replace('\\', '/');
tree.AddNode(relative);
}
AnsiConsole.Write(tree);
return 0;
}
private static bool TryParseTemplateType(string value, out TemplateType templateType)
{
templateType = TemplateType.HarmonyPatch;
return value.ToLowerInvariant() switch
{
"harmonypatch" => Assign(TemplateType.HarmonyPatch, out templateType),
"customserver" => Assign(TemplateType.CustomServer, out templateType),
"customui" => Assign(TemplateType.CustomUI, out templateType),
"customworld" => Assign(TemplateType.CustomWorld, out templateType),
"customfurniture" => Assign(TemplateType.CustomFurniture, out templateType),
"customnpc" => Assign(TemplateType.CustomNPC, out templateType),
_ => false,
};
}
private static bool Assign(TemplateType type, out TemplateType templateType)
{
templateType = type;
return true;
}
}