2022-02-15 18:38:47 +00:00
|
|
|
namespace QuestReader.Services;
|
|
|
|
|
|
|
|
using System.Reflection;
|
2022-02-19 16:42:47 +00:00
|
|
|
using QuestReader.Models;
|
2022-02-15 18:38:47 +00:00
|
|
|
|
|
|
|
public class Generator
|
|
|
|
{
|
2022-02-16 12:38:08 +00:00
|
|
|
public StandaloneTemplate<TemplateModel> RazorTemplate { get; set; }
|
2022-02-15 18:38:47 +00:00
|
|
|
|
|
|
|
public string QuestName { get; set; }
|
|
|
|
|
|
|
|
public PostsSource PostsSource { get; set; }
|
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
public string AssetsPath { get; set; }
|
|
|
|
|
|
|
|
public string OutputPath { get; set; }
|
2022-02-15 18:38:47 +00:00
|
|
|
|
|
|
|
public Generator(string questName)
|
|
|
|
{
|
|
|
|
QuestName = questName;
|
2022-02-20 19:27:27 +00:00
|
|
|
AssetsPath = $"/static/{questName}";
|
|
|
|
PostsSource = new PostsSource(questName);
|
2022-02-15 18:38:47 +00:00
|
|
|
|
2022-02-16 12:38:08 +00:00
|
|
|
var razorEngine = new RazorStandalone<StandaloneTemplate<TemplateModel>>("QuestReader");
|
2022-02-15 18:38:47 +00:00
|
|
|
var templateFile = "page_template.cshtml";
|
2022-02-16 12:38:08 +00:00
|
|
|
RazorTemplate = razorEngine.Compile(
|
|
|
|
"page_template.cshtml"
|
|
|
|
) ?? throw new Exception("No template");
|
2022-02-15 18:38:47 +00:00
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
Console.WriteLine($"Using \"{templateFile}\" with base URL {AssetsPath}");
|
2022-02-15 18:38:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public string Run()
|
|
|
|
{
|
2022-02-19 16:42:47 +00:00
|
|
|
RazorTemplate.Model = new TemplateModel
|
2022-02-15 18:38:47 +00:00
|
|
|
{
|
2022-02-16 12:38:08 +00:00
|
|
|
Metadata = PostsSource.Metadata,
|
2022-02-20 19:27:27 +00:00
|
|
|
Posts = PostsSource.Accepted.ToList(),
|
2022-02-16 12:38:08 +00:00
|
|
|
AllPosts = PostsSource.Posts,
|
|
|
|
Now = @DateTime.UtcNow,
|
2022-02-20 19:27:27 +00:00
|
|
|
AssetsPath = AssetsPath.TrimEnd('/'), // Strip trailing slash
|
2022-02-16 12:38:08 +00:00
|
|
|
ToolVersion = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "unknown"
|
|
|
|
};
|
|
|
|
|
|
|
|
var outputStream = new MemoryStream();
|
|
|
|
RazorTemplate.ExecuteAsync(outputStream).Wait();
|
2022-02-15 18:38:47 +00:00
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
var outputPath = Path.Join(OutputPath ?? PostsSource.BasePath, "output.html");
|
2022-02-16 12:38:08 +00:00
|
|
|
Console.WriteLine($"Template output {outputStream.Length} bytes");
|
|
|
|
File.WriteAllBytes(outputPath, outputStream.ToArray());
|
2022-02-15 18:38:47 +00:00
|
|
|
Console.WriteLine($"Wrote output to {outputPath}");
|
|
|
|
return outputPath;
|
|
|
|
}
|
|
|
|
}
|