Skip to content

Commit

Permalink
#44 - Fixed crash when scope excludes root; Fixed predicate filters
Browse files Browse the repository at this point in the history
  • Loading branch information
blipson89 committed Oct 3, 2021
1 parent 0b82df6 commit 3445eeb
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Leprechaun.Adapters;
using Leprechaun.InputProviders.Sitecore.Extensions;
using Leprechaun.TemplateReaders;
Expand Down Expand Up @@ -28,10 +29,11 @@ public SitecoreItemDataAdapter(IItemData itemData, FilesystemTreeDataStore dataS
public IEnumerable<IItemFieldValueAdapter> SharedFields => _itemData.SharedFields.Select(sf => new SitecoreItemFieldValueAdapter(sf));
public IEnumerable<IItemLanguageAdapter> UnversionedFields => _itemData.UnversionedFields.Select(id => new SitecoreItemLanguageAdapter(id));
public IEnumerable<IItemVersionAdapter> Versions => _itemData.Versions.Select(x => new SitecoreItemVersionAdapter(x));
public IEnumerable<IItemDataAdapter> GetChildren()
public IEnumerable<IItemDataAdapter> GetChildren() => GetChildrenAsync().GetAwaiter().GetResult();
public async Task<IEnumerable<IItemDataAdapter>> GetChildrenAsync()
{
IItemTreeNode templateRootNode = _dataStore.GetTreeNodeSync(_itemData.Path);
return templateRootNode.GetChildrenSync().Select(n => new SitecoreItemDataAdapter(_dataStore.GetItemDataSync(n), _dataStore)).ToArray();
IItemTreeNode templateRootNode = await _dataStore.GetTreeNode(_itemData.Path);
return (await templateRootNode.GetChildren()).Select(n => new SitecoreItemDataAdapter(_dataStore.GetItemDataSync(n), _dataStore)).ToArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Xml;
using System.Collections.Generic;
using System.Xml;
using Configy.Containers;
using Leprechaun.Filters;
using Leprechaun.InputProviders.Sitecore.Configuration;
using Sitecore.DevEx.Serialization.Client.Datasources.Filesystem.Configuration;

namespace Leprechaun.InputProviders.Sitecore.Filters
{
Expand Down Expand Up @@ -33,5 +35,16 @@ public LeprechaunModuleConfiguration GetModule()
{
return _leprechaunModule;
}

public IEnumerable<FilesystemTreeSpec> GetTreeSpecs()
{
foreach (var treeNode in _leprechaunModule.SerializationModule.Items.Includes)
{
if (_includeNames.Contains(treeNode.Name.ToLowerInvariant()))
{
yield return treeNode;
}
}
}
}
}
8 changes: 6 additions & 2 deletions src/Leprechaun.InputProviders.Sitecore/Leprechaun.config
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@
-->
<codeGenerator type="Leprechaun.CodeGen.Roslyn.CSharpScriptCodeGenerator, Leprechaun.CodeGen.Roslyn" singleInstance="true" />

<!-- The template filter defines what items are included as templates from the data store. Should match up with Unicorn in both paths and names. -->
<!--
The template filter defines what items are included as templates from the data store.
The name MUST match up with the include name found in the .module.json
case-insensitive
-->
<templatePredicate type="Leprechaun.InputProviders.Sitecore.Filters.SitecoreTemplatePredicate, Leprechaun.InputProviders.Sitecore" rootNamespace="$(layer).$(module)" singleInstance="true">
<include name="Templates" path="/sitecore/templates/$(layer)/$(module)" />
<include name="Templates" />
</templatePredicate>

<fieldFilter type="Leprechaun.Filters.StandardFieldFilter, Leprechaun" singleInstance="true">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using Leprechaun.Filters;
using Leprechaun.InputProviders.Sitecore.Adapters;
Expand All @@ -9,13 +10,14 @@
using Leprechaun.InputProviders.Sitecore.Filters;
using Leprechaun.Model;
using Leprechaun.TemplateReaders;
using Sitecore.DevEx.Serialization;
using Sitecore.DevEx.Serialization.Client.Query;


namespace Leprechaun.InputProviders.Sitecore.TemplateReaders
{
public class SitecoreTemplateReader : BaseTemplateReader, ITemplateReader
{

public SitecoreTemplateReader(XmlNode configNode) : base(configNode)
{
}
Expand All @@ -24,22 +26,39 @@ public override TemplateInfo[] GetTemplates(ITemplatePredicate predicate)
{
if (predicate is SitecoreTemplatePredicate scPredicate)
{
return GetTemplates(scPredicate.GetModule()).ToArray();
return GetTemplates(scPredicate).GetAwaiter().GetResult().ToArray();
}
return new TemplateInfo[0];
}

public IEnumerable<TemplateInfo> GetTemplates(LeprechaunModuleConfiguration module)
public async Task<IEnumerable<TemplateInfo>> GetTemplates(SitecoreTemplatePredicate predicate)
{
return module.SerializationModule.Items.Includes
.AsParallel()
.SelectMany(fsTreeSpec =>
var module = predicate.GetModule();
var tasks = new List<Task<IEnumerable<TemplateInfo>>>();
foreach (var fstree in predicate.GetTreeSpecs())
{
if (fstree.Scope == TreeScope.DescendantsOnly)
{
var templateItemData = module.DataStore.GetItemDataSync(module.DataStore.GetTreeNodeSync(fsTreeSpec.Path));
var itemAdapter = new SitecoreItemDataAdapter(templateItemData, module.DataStore);
return ParseTemplates(itemAdapter);
})
.ToArray();
var templates = (await module.DataStore
.GetChildren(fstree.Path))
.Select(child => ConvertTreeToTemplates(module, child));

tasks.AddRange(templates);
}
else
{
tasks.Add(ConvertTreeToTemplates(module, await module.DataStore.GetTreeNode(fstree.Path)));
}
}

return (await Task.WhenAll(tasks)).SelectMany(x => x);
}

private async Task<IEnumerable<TemplateInfo>> ConvertTreeToTemplates(LeprechaunModuleConfiguration module, IItemTreeNode tn)
{
var templateItemData = await module.DataStore.GetItemData(tn);
var itemAdapter = new SitecoreItemDataAdapter(templateItemData, module.DataStore);
return ParseTemplates(itemAdapter);
}

protected override Guid[] ParseMultilistValue(string value)
Expand Down
2 changes: 2 additions & 0 deletions src/Leprechaun/Filters/BaseTemplatePredicate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Leprechaun.Filters
public abstract class BaseTemplatePredicate : ITemplatePredicate
{
protected readonly IList<ITemplateTreeRoot> _includeEntries;
protected readonly ISet<string> _includeNames;
protected readonly IContainer _configuration;
protected readonly string _rootNamespace;

Expand All @@ -24,6 +25,7 @@ protected BaseTemplatePredicate(XmlNode configNode, IContainer configuration, st
_rootNamespace = rootNamespace;

_includeEntries = ParsePreset(configNode);
_includeNames = new HashSet<string>(_includeEntries.Select(x => x.Name.ToLowerInvariant()));
EnsureEntriesExist(configuration?.Name ?? "Unknown");
}
/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions src/Leprechaun/TemplateReaders/BaseTemplateReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ protected void ParseExcludedTemplates(XmlNode configNode)

public abstract TemplateInfo[] GetTemplates(ITemplatePredicate predicate);

//public abstract TemplateInfo[] GetTemplates(params ITreeRoot[] rootPaths);

protected virtual IEnumerable<TemplateInfo> ParseTemplates(IItemDataAdapter root)
{
var processQueue = new Queue<IItemDataAdapter>();
Expand Down

0 comments on commit 3445eeb

Please sign in to comment.