Switched Generation command to use lambda handler.

This commit is contained in:
Harrison Deng 2022-05-18 22:03:08 -05:00
parent be2cf466b4
commit 1cb6730bb1

View File

@ -7,37 +7,34 @@ using System.Resources.NetStandard;
namespace DotNetResxUtils.Commands namespace DotNetResxUtils.Commands
{ {
internal class GenerationCommand : Command internal class GenerationCommand : Command
{ {
const string NAME = "generate"; const string NAME = "generate";
const string DESC = "Generate a .resx file."; const string DESC = "Generate a .resx file.";
public GenerationCommand() : base(NAME, DESC) public GenerationCommand() : base(NAME, DESC)
{ {
Argument<FileInfo> destArg = new Argument<FileInfo>("destination", "The destination path to store this file. If no extension is given, .resx will automatically be concatenated.");
Add(destArg);
Option<FileInfo?> fromOpt = new Option<FileInfo?>("--from", "Generates a .resx file from the given file."); Option<FileInfo?> fromOpt = new Option<FileInfo?>("--from", "Generates a .resx file from the given file.");
Add(fromOpt);
Argument<FileInfo> destArg = new Argument<FileInfo>("destination", "The destination path to store this file. If no extension is given, .resx will automatically be concatenated."); this.SetHandler(async (FileInfo to, FileInfo? from) =>
this.AddOption(fromOpt);
this.AddArgument(destArg);
this.SetHandler<FileInfo, FileInfo?>(CommandHandler, destArg, fromOpt);
}
private async void CommandHandler(FileInfo to, FileInfo? from)
{
IDictionary<string, string> flattened = new Dictionary<string, string>();
if (from != null)
{ {
flattened = await FlattenJson(from); IDictionary<string, string> flattened = new Dictionary<string, string>();
} if (from != null)
using (ResXResourceWriter resxWriter = new ResXResourceWriter(to.FullName))
{
foreach (KeyValuePair<string, string> keyVal in flattened)
{ {
resxWriter.AddResource(keyVal.Key, keyVal.Value); flattened = await FlattenJson(from);
} }
} using (ResXResourceWriter resxWriter = new ResXResourceWriter(to.FullName))
{
foreach (KeyValuePair<string, string> keyVal in flattened)
{
resxWriter.AddResource(keyVal.Key, keyVal.Value);
}
}
}, destArg, fromOpt);
} }
private async Task<IDictionary<string, string>> FlattenJson(FileInfo jsonFile) private async Task<IDictionary<string, string>> FlattenJson(FileInfo jsonFile)