Skip to content

Commit

Permalink
#20 - Added ability to whitelist base templates
Browse files Browse the repository at this point in the history
  • Loading branch information
blipson89 committed Oct 18, 2019
1 parent c97685f commit 629626a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/Leprechaun.Console/Leprechaun.config
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@
</fieldFilter>

<typeNameGenerator type="Leprechaun.MetadataGeneration.StandardTypeNameGenerator, Leprechaun" singleInstance="true" namespaceRootPath="/sitecore/templates/$(layer)/$(module)" keepLeadingUnderscores="false" />
<templateReader type="Leprechaun.TemplateReaders.DataStoreTemplateReader, Leprechaun" singleInstance="true" />
<templateReader type="Leprechaun.TemplateReaders.DataStoreTemplateReader, Leprechaun" singleInstance="true">
<excludedBaseTemplate id="{8CA06D6A-B353-44E8-BC31-B528C7306971}" name="Rendering Parameters Template" />
</templateReader>

<!-- This should match up with the Unicorn/Rainbow configuration -->
<serializationFormatter type="Rainbow.Storage.Yaml.YamlSerializationFormatter, Rainbow.Storage.Yaml" singleInstance="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void GetAllFields_Includes_AllFields(TestableDataSourceTemplateReader sut

public class TestableDataSourceTemplateReader : DataStoreTemplateReader
{
public TestableDataSourceTemplateReader(IDataStore dataStore) : base(dataStore)
public TestableDataSourceTemplateReader(IDataStore dataStore) : base(null, dataStore)
{
}

Expand Down
21 changes: 21 additions & 0 deletions src/Leprechaun/Extensions/XmlNodeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Xml;

namespace Leprechaun.Extensions
{
public static class XmlNodeExtensions
{
public static string GetExpectedAttribute(this XmlNode node, string attributeName)
{
if(node == null)
throw new ArgumentNullException(nameof(node));

// ReSharper disable once PossibleNullReferenceException
XmlAttribute attribute = node.Attributes[attributeName];

if (attribute == null) throw new InvalidOperationException($"Missing expected '{attributeName}' attribute on '{node.Name}' node while processing: {node.OuterXml}");

return attribute.Value;
}
}
}
18 changes: 5 additions & 13 deletions src/Leprechaun/Filters/StandardTemplatePredicate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Xml;
using Configy.Containers;
using Leprechaun.Extensions;
using Leprechaun.Filters.Exclusions;
using Leprechaun.Model;
using Rainbow.Storage;
Expand Down Expand Up @@ -114,7 +115,7 @@ protected virtual void EnsureEntriesExist(string configurationName)

protected virtual TemplateTreeRoot CreateIncludeEntry(XmlNode configuration)
{
string path = GetExpectedAttribute(configuration, "path");
string path = configuration.GetExpectedAttribute("path");

// ReSharper disable once PossibleNullReferenceException
var name = configuration.Attributes["name"];
Expand All @@ -135,13 +136,13 @@ protected virtual IPresetTreeExclusion CreateExcludeEntry(XmlElement excludeNode
{
if (excludeNode.HasAttribute("path"))
{
return new PathBasedPresetTreeExclusion(GetExpectedAttribute(excludeNode, "path"), root);
return new PathBasedPresetTreeExclusion(excludeNode.GetExpectedAttribute("path"), root);
}

var exclusions = excludeNode.ChildNodes
.OfType<XmlElement>()
.Where(element => element.Name.Equals("except") && element.HasAttribute("name"))
.Select(element => GetExpectedAttribute(element, "name"))
.Select(element => element.GetExpectedAttribute("name"))
.ToArray();

if (excludeNode.HasAttribute("children"))
Expand All @@ -151,21 +152,12 @@ protected virtual IPresetTreeExclusion CreateExcludeEntry(XmlElement excludeNode

if (excludeNode.HasAttribute("childrenOfPath"))
{
return new ChildrenOfPathBasedPresetTreeExclusion(GetExpectedAttribute(excludeNode, "childrenOfPath"), exclusions, root);
return new ChildrenOfPathBasedPresetTreeExclusion(excludeNode.GetExpectedAttribute("childrenOfPath"), exclusions, root);
}

throw new InvalidOperationException($"Unable to parse invalid exclusion value: {excludeNode.OuterXml}");
}

protected static string GetExpectedAttribute(XmlNode node, string attributeName)
{
// ReSharper disable once PossibleNullReferenceException
var attribute = node.Attributes[attributeName];

if (attribute == null) throw new InvalidOperationException($"Missing expected '{attributeName}' attribute on '{node.Name}' node while processing predicate: {node.OuterXml}");

return attribute.Value;
}

IEnumerable<TreeRoot> ITreeRootFactory.CreateTreeRoots()
{
Expand Down
32 changes: 30 additions & 2 deletions src/Leprechaun/TemplateReaders/DataStoreTemplateReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Leprechaun.Extensions;
using Leprechaun.Model;
using Rainbow.Model;
using Rainbow.Storage;
Expand All @@ -27,9 +29,32 @@ public class DataStoreTemplateReader : ITemplateReader

private readonly IDataStore _dataStore;

public DataStoreTemplateReader(IDataStore dataStore)
public DataStoreTemplateReader(XmlNode configNode, IDataStore dataStore)
{
_dataStore = dataStore;
ParseExcludedTemplates(configNode);
}

protected void ParseExcludedTemplates(XmlNode configNode)
{
IEnumerable<XmlNode> nodes = configNode.ChildNodes
.Cast<XmlNode>()
.Where(node => node.Name == "excludedBaseTemplate")
.ToList();

foreach (XmlNode excludedTemplate in nodes)
{
var id = excludedTemplate.GetExpectedAttribute("id");
if (Guid.TryParse(id, out Guid templateId))
{
_ignoredBaseTemplateIds.Add(templateId);
}
else
{
throw new InvalidOperationException($"'{id}' is not a valid Guid in '{excludedTemplate.OuterXml}'");
}
}

}

public TemplateInfo[] GetTemplates(params TreeRoot[] rootPaths)
Expand Down Expand Up @@ -169,12 +194,15 @@ protected virtual Guid[] ParseMultilistValue(string value)
.ToArray();
}

protected virtual ICollection<Guid> IgnoredBaseTemplateIds => new HashSet<Guid>
private readonly HashSet<Guid> _ignoredBaseTemplateIds = new HashSet<Guid>
{
StandardTemplateId,
FolderId
};

protected virtual ICollection<Guid> IgnoredBaseTemplateIds => _ignoredBaseTemplateIds;


protected virtual IDictionary<Guid, string> GetAllFields(IItemData item)
{
var allFields = new Dictionary<Guid, string>();
Expand Down

0 comments on commit 629626a

Please sign in to comment.