Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named slots #43

Merged
merged 5 commits into from
Dec 10, 2023
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
34 changes: 34 additions & 0 deletions src/ManiaTemplates/Components/MtComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class MtComponent
public required Dictionary<string, MtComponentProperty> Properties { get; init; }
public required List<string> Namespaces { get; init; }
public required List<MtComponentScript> Scripts { get; init; }
public HashSet<string> Slots { get; init; } = new();

/// <summary>
/// Creates a MtComponent instance from template contents.
Expand All @@ -24,6 +25,7 @@ public static MtComponent FromTemplate(ManiaTemplateEngine engine, string templa
var namespaces = new List<string>();
var foundProperties = new Dictionary<string, MtComponentProperty>();
var maniaScripts = new List<MtComponentScript>();
var slots = new HashSet<string>();
var componentTemplate = "";
var hasSlot = false;
string? layer = null;
Expand Down Expand Up @@ -57,6 +59,7 @@ public static MtComponent FromTemplate(ManiaTemplateEngine engine, string templa
componentTemplate = node.InnerXml;
layer = ParseDisplayLayer(node);
hasSlot = NodeHasSlot(node);
slots = GetSlotNamesInTemplate(node);
break;

case "script":
Expand All @@ -70,6 +73,7 @@ public static MtComponent FromTemplate(ManiaTemplateEngine engine, string templa
{
TemplateContent = componentTemplate,
HasSlot = hasSlot,
Slots = slots,
ImportedComponents = foundComponents,
Properties = foundProperties,
Namespaces = namespaces,
Expand Down Expand Up @@ -222,6 +226,36 @@ private static bool NodeHasSlot(XmlNode node)
.Any(NodeHasSlot);
}

/// <summary>
/// Gets all slot names recursively.
/// </summary>
private static HashSet<string> GetSlotNamesInTemplate(XmlNode node)
{
var slotNames = new HashSet<string>();

if (node.Name == "slot")
{
slotNames.Add(node.Attributes?["name"]?.Value.ToLower() ?? "default");
}
else if (node.HasChildNodes)
{
foreach (XmlNode childNode in node.ChildNodes)
{
foreach (var slotName in GetSlotNamesInTemplate(childNode))
{
if (slotNames.Contains(slotName))
{
throw new DuplicateSlotException($"""A slot with the name "{slotName}" already exists.""");
}

slotNames.Add(slotName);
}
}
}

return slotNames;
}

public string Id()
{
return "MtContext" + GetHashCode().ToString().Replace("-", "N");
Expand Down
4 changes: 3 additions & 1 deletion src/ManiaTemplates/Components/MtComponentSlot.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using ManiaTemplates.ControlElements;
using ManiaTemplates.Lib;

namespace ManiaTemplates.Components;

public class MtComponentSlot
{
public required int Scope { get; init; }
public required string RenderMethod { get; init; }
public required string RenderMethodT4 { get; init; }
public required MtDataContext Context { get; init; }
public string Name { get; init; } = "default";
}
18 changes: 18 additions & 0 deletions src/ManiaTemplates/Exceptions/DuplicateSlotException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace ManiaTemplates.Exceptions;

public class DuplicateSlotException : Exception
{
public DuplicateSlotException()
{
}

public DuplicateSlotException(string message)
: base(message)
{
}

public DuplicateSlotException(string message, Exception inner)
: base(message, inner)
{
}
}
Loading