Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ bld/

# Visual Studio 2015 cache/options directory
.vs/

# VS Code user settings
.vscode/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using DocumentFormat.OpenXml.Packaging;

namespace TriasDev.Templify.DocumentGenerator.Generators;

/// <summary>
/// Generates the warning report template used by Templify to render processing warnings.
/// This template is embedded in the main library as a resource.
/// </summary>
public class WarningReportTemplateGenerator : BaseExampleGenerator
{
public override string Name => "warning-report";

public override string Description => "Warning report template for Templify processing warnings";

public override string GenerateTemplate(string outputDirectory)
{
var templatePath = Path.Combine(outputDirectory, $"{Name}-template.docx");

using (var doc = CreateDocument(templatePath))
{
var body = doc.MainDocumentPart!.Document.Body!;

Check warning on line 21 in TriasDev.Templify.DocumentGenerator/Generators/WarningReportTemplateGenerator.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest, 8.0.x)

Dereference of a possibly null reference.

Check warning on line 21 in TriasDev.Templify.DocumentGenerator/Generators/WarningReportTemplateGenerator.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest, 9.0.x)

Dereference of a possibly null reference.

Check warning on line 21 in TriasDev.Templify.DocumentGenerator/Generators/WarningReportTemplateGenerator.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 6.0.x)

Dereference of a possibly null reference.

Check warning on line 21 in TriasDev.Templify.DocumentGenerator/Generators/WarningReportTemplateGenerator.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 10.0.x)

Dereference of a possibly null reference.

Check warning on line 21 in TriasDev.Templify.DocumentGenerator/Generators/WarningReportTemplateGenerator.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 9.0.x)

Dereference of a possibly null reference.

Check warning on line 21 in TriasDev.Templify.DocumentGenerator/Generators/WarningReportTemplateGenerator.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest, 10.0.x)

Dereference of a possibly null reference.

Check warning on line 21 in TriasDev.Templify.DocumentGenerator/Generators/WarningReportTemplateGenerator.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 8.0.x)

Dereference of a possibly null reference.

Check warning on line 21 in TriasDev.Templify.DocumentGenerator/Generators/WarningReportTemplateGenerator.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows-latest, 8.0.x)

Dereference of a possibly null reference.

Check warning on line 21 in TriasDev.Templify.DocumentGenerator/Generators/WarningReportTemplateGenerator.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows-latest, 10.0.x)

Dereference of a possibly null reference.

Check warning on line 21 in TriasDev.Templify.DocumentGenerator/Generators/WarningReportTemplateGenerator.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows-latest, 6.0.x)

Dereference of a possibly null reference.

Check warning on line 21 in TriasDev.Templify.DocumentGenerator/Generators/WarningReportTemplateGenerator.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows-latest, 9.0.x)

Dereference of a possibly null reference.

Check warning on line 21 in TriasDev.Templify.DocumentGenerator/Generators/WarningReportTemplateGenerator.cs

View workflow job for this annotation

GitHub Actions / Pack NuGet

Dereference of a possibly null reference.

// Title
AddParagraph(body, "Template Processing Warning Report", isBold: true);
AddEmptyParagraph(body);

// Metadata
AddParagraph(body, "Generated: {{GeneratedAt}}");
AddEmptyParagraph(body);

// Summary section
AddParagraph(body, "Summary", isBold: true);
AddParagraph(body, "Total Warnings: {{TotalWarnings}}");
AddEmptyParagraph(body);

// Summary table
var summaryTable = CreateTable(2);
AddTableHeaderRow(summaryTable, "Warning Type", "Count");
AddTableRow(summaryTable, "Missing Variables", "{{MissingVariableCount}}");
AddTableRow(summaryTable, "Missing Collections", "{{MissingCollectionCount}}");
AddTableRow(summaryTable, "Null Collections", "{{NullCollectionCount}}");
AddTableRow(summaryTable, "Failed Expressions", "{{FailedExpressionCount}}");
body.AppendChild(summaryTable);
AddEmptyParagraph(body);

// Missing Variables section (conditional)
AddParagraph(body, "{{#if HasMissingVariables}}");
AddParagraph(body, "Missing Variables", isBold: true);
AddParagraph(body, "The following variables were referenced in the template but not found in the data:");
AddEmptyParagraph(body);

var missingVarsTable = CreateTable(2);
AddTableHeaderRow(missingVarsTable, "Variable Name", "Context");
AddTableRow(missingVarsTable, "{{#foreach MissingVariables}}", "");
AddTableRow(missingVarsTable, "{{VariableName}}", "{{Context}}");
AddTableRow(missingVarsTable, "{{/foreach}}", "");
body.AppendChild(missingVarsTable);
AddEmptyParagraph(body);
AddParagraph(body, "{{/if}}");

// Missing Collections section (conditional)
AddParagraph(body, "{{#if HasMissingCollections}}");
AddParagraph(body, "Missing Loop Collections", isBold: true);
AddParagraph(body, "The following collections were referenced in loops but not found in the data:");
AddEmptyParagraph(body);

var missingCollTable = CreateTable(2);
AddTableHeaderRow(missingCollTable, "Collection Name", "Context");
AddTableRow(missingCollTable, "{{#foreach MissingCollections}}", "");
AddTableRow(missingCollTable, "{{VariableName}}", "{{Context}}");
AddTableRow(missingCollTable, "{{/foreach}}", "");
body.AppendChild(missingCollTable);
AddEmptyParagraph(body);
AddParagraph(body, "{{/if}}");

// Null Collections section (conditional)
AddParagraph(body, "{{#if HasNullCollections}}");
AddParagraph(body, "Null Loop Collections", isBold: true);
AddParagraph(body, "The following collections were found but had null values:");
AddEmptyParagraph(body);

var nullCollTable = CreateTable(2);
AddTableHeaderRow(nullCollTable, "Collection Name", "Context");
AddTableRow(nullCollTable, "{{#foreach NullCollections}}", "");
AddTableRow(nullCollTable, "{{VariableName}}", "{{Context}}");
AddTableRow(nullCollTable, "{{/foreach}}", "");
body.AppendChild(nullCollTable);
AddEmptyParagraph(body);
AddParagraph(body, "{{/if}}");

// Failed Expressions section (conditional)
AddParagraph(body, "{{#if HasFailedExpressions}}");
AddParagraph(body, "Failed Expressions", isBold: true);
AddParagraph(body, "The following expressions could not be evaluated:");
AddEmptyParagraph(body);

var failedExprTable = CreateTable(2);
AddTableHeaderRow(failedExprTable, "Expression", "Error");
AddTableRow(failedExprTable, "{{#foreach FailedExpressions}}", "");
AddTableRow(failedExprTable, "{{VariableName}}", "{{Message}}");
AddTableRow(failedExprTable, "{{/foreach}}", "");
body.AppendChild(failedExprTable);
AddEmptyParagraph(body);
AddParagraph(body, "{{/if}}");

// Footer
AddEmptyParagraph(body);
AddParagraph(body, "End of Warning Report");

doc.Save();
}

return templatePath;
}

public override Dictionary<string, object> GetSampleData()
{
// Sample data for testing the template
return new Dictionary<string, object>
{
["GeneratedAt"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
["TotalWarnings"] = 7,
["MissingVariableCount"] = 2,
["MissingCollectionCount"] = 2,
["NullCollectionCount"] = 1,
["FailedExpressionCount"] = 2,
["HasMissingVariables"] = true,
["HasMissingCollections"] = true,
["HasNullCollections"] = true,
["HasFailedExpressions"] = true,
["MissingVariables"] = new List<Dictionary<string, object>>
{
new() { ["VariableName"] = "CustomerName", ["Context"] = "placeholder" },
new() { ["VariableName"] = "Customer.Email", ["Context"] = "placeholder" }
},
["MissingCollections"] = new List<Dictionary<string, object>>
{
new() { ["VariableName"] = "OrderItems", ["Context"] = "loop: OrderItems" },
new() { ["VariableName"] = "Categories", ["Context"] = "loop: Categories" }
},
["NullCollections"] = new List<Dictionary<string, object>>
{
new() { ["VariableName"] = "Products", ["Context"] = "loop: Products" }
},
["FailedExpressions"] = new List<Dictionary<string, object>>
{
new() { ["VariableName"] = "Price > 100", ["Message"] = "Variable 'Price' was not found in the data." },
new() { ["VariableName"] = "Status = \"Active\"", ["Message"] = "Variable 'Status' was not found in the data." }
}
};
}
}
1 change: 1 addition & 0 deletions TriasDev.Templify.DocumentGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
new HelloWorldGenerator(),
new InvoiceGenerator(),
new ConditionalGenerator(),
new WarningReportTemplateGenerator(),
};

// Parse command line arguments
Expand Down
85 changes: 79 additions & 6 deletions TriasDev.Templify.Gui/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public partial class MainWindowViewModel : ViewModelBase
[ObservableProperty]
private bool _enableHtmlEntityReplacement;

/// <summary>
/// Stores the last processing result to enable warning report generation.
/// </summary>
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(GenerateWarningReportCommand))]
private UiProcessingResult? _lastProcessingResult;

public MainWindowViewModel(
ITemplifyService templifyService,
IFileDialogService fileDialogService)
Expand Down Expand Up @@ -191,23 +198,28 @@ private async Task ProcessTemplateAsync()
EnableHtmlEntityReplacement,
progressReporter);

// Store for warning report generation
LastProcessingResult = result;

if (result.Success)
{
Results.Add("✓ Template processed successfully!");
Results.Add($"✓ Made {result.Processing.ReplacementCount} replacements");
Results.Add($"✓ Output saved to: {result.OutputPath}");

if (result.Validation.MissingVariables.Count > 0)
if (result.Processing.HasWarnings)
{
Results.Add($"⚠ {result.Validation.MissingVariables.Count} missing variables:");
foreach (string missing in result.Validation.MissingVariables.Take(5))
Results.Add($"⚠ {result.Processing.Warnings.Count} processing warnings:");
foreach (ProcessingWarning warning in result.Processing.Warnings.Take(5))
{
Results.Add($" - {missing}");
string truncatedMessage = TruncateMessage(warning.Message, 60);
Results.Add($" - {warning.Type}: {warning.VariableName} - {truncatedMessage}");
}
if (result.Validation.MissingVariables.Count > 5)
if (result.Processing.Warnings.Count > 5)
{
Results.Add($" ... and {result.Validation.MissingVariables.Count - 5} more");
Results.Add($" ... and {result.Processing.Warnings.Count - 5} more");
}
Results.Add(" (Use 'Generate Warning Report' for full details)");
}

StatusMessage = "Processing complete";
Expand Down Expand Up @@ -269,8 +281,59 @@ private void Clear()
Results.Clear();
StatusMessage = "Ready";
Progress = 0;
LastProcessingResult = null;
}

[RelayCommand(CanExecute = nameof(CanGenerateWarningReport))]
private async Task GenerateWarningReportAsync()
{
if (LastProcessingResult == null || !LastProcessingResult.Processing.HasWarnings)
{
return;
}

try
{
// Generate default filename based on template name
string defaultName = "warning-report.docx";
if (!string.IsNullOrEmpty(TemplatePath))
{
string templateName = Path.GetFileNameWithoutExtension(TemplatePath);
defaultName = $"{templateName}-warnings.docx";
}

// Ask user where to save
string? savePath = await _fileDialogService.SaveOutputFileAsync(defaultName);
if (string.IsNullOrEmpty(savePath))
{
return; // User cancelled
}

// Generate and save the report
byte[] reportBytes = LastProcessingResult.Processing.GetWarningReportBytes();
await File.WriteAllBytesAsync(savePath, reportBytes);

Results.Add($"✓ Warning report saved to: {savePath}");
StatusMessage = "Warning report generated";

// Offer to open the report
Process.Start(new ProcessStartInfo
{
FileName = savePath,
UseShellExecute = true
});
}
catch (Exception ex)
{
Results.Add($"✗ Failed to generate warning report: {ex.Message}");
}
}

private bool CanGenerateWarningReport() =>
LastProcessingResult != null &&
LastProcessingResult.Success &&
LastProcessingResult.Processing.HasWarnings;

private void UpdateOutputPath()
{
if (string.IsNullOrEmpty(TemplatePath))
Expand All @@ -293,4 +356,14 @@ private string GenerateOutputFileName()

return "output.docx";
}

private static string TruncateMessage(string message, int maxLength)
{
if (string.IsNullOrEmpty(message) || message.Length <= maxLength)
{
return message;
}

return message[..(maxLength - 3)] + "...";
}
}
4 changes: 4 additions & 0 deletions TriasDev.Templify.Gui/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@

<!-- Bottom Action Buttons -->
<StackPanel Grid.Row="8" Orientation="Horizontal" Spacing="10" HorizontalAlignment="Right">
<Button Content="Generate Warning Report"
Command="{Binding GenerateWarningReportCommand}"
ToolTip.Tip="Generate a Word document containing all processing warnings"
Width="170"/>
<Button Content="Open Output File"
Command="{Binding OpenOutputFileCommand}"
Width="130"/>
Expand Down
Loading
Loading