Skip to content

Commit

Permalink
Include / Exclude Paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Jump committed Jan 6, 2020
1 parent 52c78a0 commit 31a4ee0
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 11 deletions.
10 changes: 5 additions & 5 deletions uSync8.BackOffice/Configuration/BackOfficeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,12 @@ public HandlerSettings LoadHandlerConfig(XElement node, uSyncSettings defaultSet
settings.BatchSave = GetLocalValue(node.Attribute("BatchSave"), defaultSettings.BatchSave);
settings.Actions = node.Attribute("Actions").ValueOrDefault("All").ToDelimitedList().ToArray();

var settingNode = node.Element("Settings");
if (settingNode != null)
{
// var settingNode = node.Element("Settings");
// if (settingNode != null)
// {
var perHandlerSettings = new Dictionary<string, string>();

foreach (var settingItem in settingNode.Elements("Add"))
foreach (var settingItem in node.Elements("Add"))
{
var key = settingItem.Attribute("Key").ValueOrDefault(string.Empty);
var value = settingItem.Attribute("Value").ValueOrDefault(string.Empty);
Expand All @@ -222,7 +222,7 @@ public HandlerSettings LoadHandlerConfig(XElement node, uSyncSettings defaultSet
}

settings.Settings = perHandlerSettings;
}
// }

return settings;
}
Expand Down
32 changes: 28 additions & 4 deletions uSync8.BackOffice/SyncHandlers/SyncHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,18 @@ public virtual SyncAttempt<TObject> Import(string filePath, HandlerSettings conf
try
{
syncFileService.EnsureFileExists(filePath);

using (var stream = syncFileService.OpenRead(filePath))
{
var node = XElement.Load(stream);
var attempt = serializer.Deserialize(node, flags);
return attempt;
if (ShouldImport(node, config))
{
var attempt = serializer.Deserialize(node, flags);
return attempt;
}
else
{
return SyncAttempt<TObject>.Succeed(Path.GetFileName(filePath), default(TObject) ,ChangeType.NoChange, "Not Imported (Based on config)");
}
}
}
catch (FileNotFoundException notFoundException)
Expand All @@ -339,6 +345,16 @@ public virtual SyncAttempt<TObject> Import(string filePath, HandlerSettings conf
}
}

/// <summary>
/// check to see if this element should be imported as part of the process.
/// </summary>
virtual protected bool ShouldImport(XElement node, HandlerSettings config) => true;

/// <summary>
/// Check to see if this elment should be exported.
/// </summary>
virtual protected bool ShouldExport(XElement node, HandlerSettings config) => true;

virtual public SyncAttempt<TObject> ImportSecondPass(string file, TObject item, HandlerSettings config, SyncUpdateCallback callback)
{
if (IsTwoPass)
Expand Down Expand Up @@ -444,7 +460,15 @@ virtual public IEnumerable<uSyncAction> Export(TObject item, string folder, Hand
var attempt = serializer.Serialize(item);
if (attempt.Success)
{
syncFileService.SaveXElement(attempt.Item, filename);
if (ShouldExport(attempt.Item, config))
{
// only write the file to disk if it should be exported.
syncFileService.SaveXElement(attempt.Item, filename);
}
else
{
return uSyncAction.SetAction(true, filename, type: typeof(TObject), change: ChangeType.NoChange, message: "Not Exported (Based on config)").AsEnumerableOfOne();
}
}

return uSyncActionHelper<XElement>.SetAction(attempt, filename).AsEnumerableOfOne();
Expand Down
66 changes: 65 additions & 1 deletion uSync8.ContentEdition/Handlers/ContentHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Linq;
using System.Xml.Linq;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
Expand All @@ -10,6 +13,8 @@
using uSync8.BackOffice.Services;
using uSync8.BackOffice.SyncHandlers;
using uSync8.Core.Dependency;
using uSync8.Core.Extensions;
using uSync8.Core.Models;
using uSync8.Core.Serialization;
using uSync8.Core.Tracking;

Expand Down Expand Up @@ -42,7 +47,6 @@ public ContentHandler(

}


protected override void DeleteViaService(IContent item)
=> contentService.Delete(item);

Expand Down Expand Up @@ -82,5 +86,65 @@ public uSyncAction Import(string file)
var attempt = this.Import(file, DefaultConfig, SerializerFlags.OnePass);
return uSyncActionHelper<IContent>.SetAction(attempt, file, this.Alias, IsTwoPass);
}

/*
* Config options.
* Include = Paths (comma seperated) (only include if path starts with one of these)
* Exclude = Paths (comma seperated) (exclude if path starts with one of these)
*
* RulesOnExport = bool (do we apply the rules on export as well as import?)
*/

protected override bool ShouldImport(XElement node, HandlerSettings config)
{
if (config.Settings.ContainsKey("Include"))
{
var include = config.Settings["Include"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (include.Length > 0)
{
var path = node.Element("Info")?.Element("Path").ValueOrDefault(string.Empty);
if (!string.IsNullOrWhiteSpace(path) && !include.Any(x => path.InvariantStartsWith(x)))
{
logger.Debug<ContentHandler>("Not processing item, {0} path {1} not in include path", node.Attribute("Alias").ValueOrDefault("unknown"), path);
return false;
}
}
}

if (config.Settings.ContainsKey("Exclude"))
{
var exclude = config.Settings["Exclude"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (exclude.Length > 0)
{
var path = node.Element("Info")?.Element("Path").ValueOrDefault(string.Empty);
if (!string.IsNullOrWhiteSpace(path) && exclude.Any(x => path.InvariantStartsWith(x)))
{
logger.Debug<ContentHandler>("Not processing item, {0} path {1} is excluded", node.Attribute("Alias").ValueOrDefault("unknown"), path);
return false;
}
}
}

return true;
}

/// <summary>
/// Should we save this value to disk?
/// </summary>
/// <remarks>
/// In general we save everything to disk, even if we are not going to remimport it later
/// but you can stop this with RulesOnExport = true in the settings
/// </remarks>

protected override bool ShouldExport(XElement node, HandlerSettings config)
{
if (config.Settings.ContainsKey("RulesOnExport") && config.Settings["RulesOnExport"].InvariantEquals("true"))
{
return ShouldImport(node, config);
}

return true;
}

}
}
60 changes: 60 additions & 0 deletions uSync8.ContentEdition/Handlers/MediaHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,65 @@ protected override void InitializeEvents(HandlerSettings settings)
MediaService.Moved += EventMovedItem;
MediaService.Trashed += EventMovedItem;
}

/*
* Config options.
* Include = Paths (comma seperated) (only include if path starts with one of these)
* Exclude = Paths (comma seperated) (exclude if path starts with one of these)
*
* RulesOnExport = bool (do we apply the rules on export as well as import?)
*/

protected override bool ShouldImport(XElement node, HandlerSettings config)
{
if (config.Settings.ContainsKey("Include"))
{
var include = config.Settings["Include"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (include.Length > 0)
{
var path = node.Element("Info")?.Element("Path").ValueOrDefault(string.Empty);
if (!string.IsNullOrWhiteSpace(path) && !include.Any(x => path.InvariantStartsWith(x)))
{
logger.Debug<ContentHandler>("Not processing item, {0} path {1} not in include path", node.Attribute("Alias").ValueOrDefault("unknown"), path);
return false;
}
}
}

if (config.Settings.ContainsKey("Exclude"))
{
var exclude = config.Settings["Exclude"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (exclude.Length > 0)
{
var path = node.Element("Info")?.Element("Path").ValueOrDefault(string.Empty);
if (!string.IsNullOrWhiteSpace(path) && exclude.Any(x => path.InvariantStartsWith(x)))
{
logger.Debug<ContentHandler>("Not processing item, {0} path {1} is excluded", node.Attribute("Alias").ValueOrDefault("unknown"), path);
return false;
}
}
}

return true;
}

/// <summary>
/// Should we save this value to disk?
/// </summary>
/// <remarks>
/// In general we save everything to disk, even if we are not going to remimport it later
/// but you can stop this with RulesOnExport = true in the settings
/// </remarks>

protected override bool ShouldExport(XElement node, HandlerSettings config)
{
if (config.Settings.ContainsKey("RulesOnExport") && config.Settings["RulesOnExport"].InvariantEquals("true"))
{
return ShouldImport(node, config);
}

return true;
}

}
}
6 changes: 5 additions & 1 deletion uSync8.Site/config/uSync8.config
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
<Handler Alias="dictionaryHandler" Enabled="true" Actions="All" />
<Handler Alias="mediaTypeHandler" Enabled="true" Actions="All" />
<Handler Alias="domainHandler" Enabled="true" Actions="All" />
<Handler Alias="contentHandler" Enabled="true" Actions="All" />
<Handler Alias="contentHandler" Enabled="true" Actions="All">
<Add Key="Include" Value="/HomepageNameUpdate" />
<Add Key="Exclude" Value="/HomepageNameUpdate/ContentTemplate" />
<Add Key="RulesOnExport" Value="true" />
</Handler>
<Handler Alias="mediaHandler" Enabled="true" Actions="All" />
</Handlers>
<Handlers Name="publish">
Expand Down

0 comments on commit 31a4ee0

Please sign in to comment.