Skip to content

Commit

Permalink
update: Command_Build logic
Browse files Browse the repository at this point in the history
  • Loading branch information
netpyoung committed Nov 22, 2024
1 parent 982764a commit 2273c66
Showing 1 changed file with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace NF.Tool.PatchNoteMaker.CLI.Commands
Expand Down Expand Up @@ -106,13 +107,16 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
return 1;
}
Fragment fragment = FragmentFinder.SplitFragments(fragmentResult.FragmentContents, config.Types, isAllBullets: true);
bool isRenderTitle = !string.IsNullOrEmpty(config.Maker.TitleFormat);
bool isRenderTitle = string.IsNullOrEmpty(config.Maker.TitleFormat);

// TODO(pyoung): impl fill model
TemplateModel model = TemplateModel.Create(
versionData,
isRenderTitle,
fragment
);


using (ScopedFileDeleter deleter = new ScopedFileDeleter())
{
AnsiConsole.MarkupLine("[green]*[/] Loading template...");
Expand All @@ -130,23 +134,79 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
AnsiConsole.MarkupLine("[green]*[/] Rendering news fragments...");
string renderOutputPath = deleter.Register(Path.GetTempFileName());
await TemplateRenderer.Render(templatePath, config, model, renderOutputPath);
string output = await File.ReadAllTextAsync(renderOutputPath);
string rendered = await File.ReadAllTextAsync(renderOutputPath);

string topLine;
if (!string.IsNullOrEmpty(config.Maker.TitleFormat))
{
topLine = string.Format($"{config.Maker.TitleFormat}\n", projectName, projectVersion, projectDate);
}
else
{
topLine = string.Empty;
}
string content = $"{topLine}{rendered}";

if (setting.IsDraft)
{
AnsiConsole.MarkupLine("[green]*[/] show draft...");
AnsiConsole.WriteLine(output);
AnsiConsole.WriteLine(content);
return 0;
}

AnsiConsole.MarkupLine("[green]*[/] Writing to newsfile...");
// TODO(pyoung): impl append_to_newsfile
// append_to_newsfile(
//base_directory,
//news_file,
//config.start_string,
//top_line,
//content,
File.Move(renderOutputPath, config.Maker.OutputFileName, overwrite: true);
}

string newsFile = string.Empty;
AnsiConsole.MarkupLine("[green]*[/] Staging newsfile...");
// TODO(pyoung): impl
GitHelper.StageNewsfile(baseDirectory, newsFile);

AnsiConsole.MarkupLine("[green]*[/] Removing news fragments...");
// TODO(pyoung): impl
string[] fragmentFpaths = fragmentResult.FragmentFiles.Select(x => x.FileName).ToArray();
if (fragmentFpaths.Length == 0)
{
AnsiConsole.MarkupLine("No news fragments to remove. Skipping!");
}
else if (setting.IsAnswerKeep)
{
AnsiConsole.MarkupLine("Keeping the following files:");
foreach (string x in fragmentFpaths)
{
AnsiConsole.WriteLine(x);
}
}
else if (setting.IsAnswerYes)
{
AnsiConsole.MarkupLine("Removing the following files:");
foreach (string x in fragmentFpaths)
{
AnsiConsole.WriteLine(x);
}

AnsiConsole.MarkupLine("[green]*[/] Removing news fragments...");
GitHelper.RemoveFiles(fragmentFpaths);
}
else
{
AnsiConsole.MarkupLine("I want to remove the following files:");
foreach (string x in fragmentFpaths)
{
AnsiConsole.WriteLine(x);
}

if (AnsiConsole.Confirm("Is it okay if I remove those files?"))
{
AnsiConsole.MarkupLine("[green]*[/] Removing news fragments...");
GitHelper.RemoveFiles(fragmentFpaths);
}
}

AnsiConsole.MarkupLine("[green]*[/] Done!");
return 0;
Expand Down

0 comments on commit 2273c66

Please sign in to comment.