Skip to content

Instantly share code, notes, and snippets.

@kamsar
Last active January 4, 2016 20:09
Show Gist options
  • Save kamsar/8671657 to your computer and use it in GitHub Desktop.
Save kamsar/8671657 to your computer and use it in GitHub Desktop.
Unicorn 2 Multiple Serialization Roots and Presets
<serialization>
<default>
<include database="master" path="/sitecore/templates"/>
<include database="core" path="/sitecore">
<exclude path="/sitecore/content/Home" />
</include>
</default>
<templates>
<include database="master" path="/sitecore/templates"/>
</templates>
<core>
<include database="core" path="/sitecore">
<exclude path="/sitecore/content/Home" />
</include>
</core>
</serialization>
using System;
using Kamsar.WebConsole;
using Sitecore.SecurityModel;
using Unicorn.Data;
using Unicorn.Dependencies;
using Unicorn.Predicates;
using Unicorn.Serialization;
using Unicorn.Serialization.Sitecore.Fiat;
namespace Debut.Web
{
public partial class SerializeMultipleRoots : WebConsolePage
{
protected override string PageTitle
{
get { return "Multiple Serialization Roots Demo"; }
}
protected override void Process(IProgressStatus console)
{
try
{
using (new SecurityDisabler())
{
Serialize("templates", @"D:\Web\Debut\serialized-templates", console);
console.Report(50);
Serialize("core", @"D:\Web\Debut\serialized-core", console);
console.Report(100);
}
}
catch (Exception ex)
{
console.ReportException(ex);
}
}
private void Serialize(string preset, string root, IProgressStatus progress)
{
// tell the Unicorn DI container to wire to the console for its progress logging
var dependencies = Registry.CreateCopyOfDefault();
dependencies.Register(() => progress);
dependencies.Register<IPredicate>(() => new SerializationPresetPredicate(dependencies.Resolve<ISourceDataProvider>(), preset));
dependencies.Register<ISerializationProvider>(() => new FiatSitecoreSerializationProvider(dependencies.Resolve<IPredicate>(), dependencies.Resolve<IFiatDeserializerLogger>(), root));
var predicate = dependencies.Resolve<IPredicate>();
var serializationProvider = dependencies.Resolve<ISerializationProvider>();
var roots = predicate.GetRootItems();
foreach (var serializationRoot in roots)
{
var rootReference = serializationProvider.GetReference(serializationRoot);
if (rootReference != null)
{
progress.ReportStatus("[D] existing serialized items under {0}", MessageType.Warning, rootReference.DisplayIdentifier);
rootReference.Delete();
}
progress.ReportStatus("[U] Serializing included items under root {0}", MessageType.Info, serializationRoot.DisplayIdentifier);
Serialize(serializationRoot, predicate, serializationProvider, progress);
}
}
private void Serialize(ISourceItem root, IPredicate predicate, ISerializationProvider serializationProvider, IProgressStatus progress)
{
var predicateResult = predicate.Includes(root);
if (predicateResult.IsIncluded)
{
serializationProvider.SerializeItem(root);
foreach (var child in root.Children)
{
Serialize(child, predicate, serializationProvider, progress);
}
}
else
{
progress.ReportStatus("[S] {0} because {1}", MessageType.Warning, root.DisplayIdentifier, predicateResult.Justification);
}
}
}
}
using System;
using Kamsar.WebConsole;
using Sitecore.SecurityModel;
using Unicorn.Data;
using Unicorn.Dependencies;
using Unicorn.Loader;
using Unicorn.Predicates;
using Unicorn.Serialization;
using Unicorn.Serialization.Sitecore.Fiat;
namespace Debut.Web
{
public partial class SyncMultipleRoots : WebConsolePage
{
protected override string PageTitle
{
get { return "Multiple Serialization Roots Demo"; }
}
protected override void Process(IProgressStatus console)
{
try
{
using (new SecurityDisabler())
{
Sync("templates", @"D:\Web\Debut\serialized-templates", console);
console.Report(50);
Sync("core", @"D:\Web\Debut\serialized-core", console);
console.Report(100);
}
}
catch (Exception ex)
{
console.ReportException(ex);
}
}
private void Sync(string preset, string rootPath, IProgressStatus console)
{
var dependencies = Registry.CreateCopyOfDefault();
dependencies.Register(() => console);
dependencies.Register<IPredicate>(() => new SerializationPresetPredicate(dependencies.Resolve<ISourceDataProvider>(), preset));
dependencies.Register<ISerializationProvider>(() => new FiatSitecoreSerializationProvider(dependencies.Resolve<IPredicate>(), dependencies.Resolve<IFiatDeserializerLogger>(), rootPath));
var loader = dependencies.Resolve<SerializationLoader>();
loader.LoadAll(dependencies);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment