From 31a4ee0e646c8a2dd66948d7cbe3b37973eb160c Mon Sep 17 00:00:00 2001 From: Kevin Jump Date: Mon, 6 Jan 2020 10:56:31 +0000 Subject: [PATCH] Include / Exclude Paths. --- .../Configuration/BackOfficeConfig.cs | 10 +-- .../SyncHandlers/SyncHandlerBase.cs | 32 +++++++-- .../Handlers/ContentHandler.cs | 66 ++++++++++++++++++- .../Handlers/MediaHandler.cs | 60 +++++++++++++++++ uSync8.Site/config/uSync8.config | 6 +- 5 files changed, 163 insertions(+), 11 deletions(-) diff --git a/uSync8.BackOffice/Configuration/BackOfficeConfig.cs b/uSync8.BackOffice/Configuration/BackOfficeConfig.cs index f86d457d..b970b57a 100644 --- a/uSync8.BackOffice/Configuration/BackOfficeConfig.cs +++ b/uSync8.BackOffice/Configuration/BackOfficeConfig.cs @@ -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(); - 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); @@ -222,7 +222,7 @@ public HandlerSettings LoadHandlerConfig(XElement node, uSyncSettings defaultSet } settings.Settings = perHandlerSettings; - } + // } return settings; } diff --git a/uSync8.BackOffice/SyncHandlers/SyncHandlerBase.cs b/uSync8.BackOffice/SyncHandlers/SyncHandlerBase.cs index 90ed0a51..3032e10e 100644 --- a/uSync8.BackOffice/SyncHandlers/SyncHandlerBase.cs +++ b/uSync8.BackOffice/SyncHandlers/SyncHandlerBase.cs @@ -321,12 +321,18 @@ public virtual SyncAttempt 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.Succeed(Path.GetFileName(filePath), default(TObject) ,ChangeType.NoChange, "Not Imported (Based on config)"); + } } } catch (FileNotFoundException notFoundException) @@ -339,6 +345,16 @@ public virtual SyncAttempt Import(string filePath, HandlerSettings conf } } + /// + /// check to see if this element should be imported as part of the process. + /// + virtual protected bool ShouldImport(XElement node, HandlerSettings config) => true; + + /// + /// Check to see if this elment should be exported. + /// + virtual protected bool ShouldExport(XElement node, HandlerSettings config) => true; + virtual public SyncAttempt ImportSecondPass(string file, TObject item, HandlerSettings config, SyncUpdateCallback callback) { if (IsTwoPass) @@ -444,7 +460,15 @@ virtual public IEnumerable 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.SetAction(attempt, filename).AsEnumerableOfOne(); diff --git a/uSync8.ContentEdition/Handlers/ContentHandler.cs b/uSync8.ContentEdition/Handlers/ContentHandler.cs index b8e43e20..6e4aba70 100644 --- a/uSync8.ContentEdition/Handlers/ContentHandler.cs +++ b/uSync8.ContentEdition/Handlers/ContentHandler.cs @@ -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; @@ -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; @@ -42,7 +47,6 @@ public ContentHandler( } - protected override void DeleteViaService(IContent item) => contentService.Delete(item); @@ -82,5 +86,65 @@ public uSyncAction Import(string file) var attempt = this.Import(file, DefaultConfig, SerializerFlags.OnePass); return uSyncActionHelper.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("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("Not processing item, {0} path {1} is excluded", node.Attribute("Alias").ValueOrDefault("unknown"), path); + return false; + } + } + } + + return true; + } + + /// + /// Should we save this value to disk? + /// + /// + /// 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 + /// + + protected override bool ShouldExport(XElement node, HandlerSettings config) + { + if (config.Settings.ContainsKey("RulesOnExport") && config.Settings["RulesOnExport"].InvariantEquals("true")) + { + return ShouldImport(node, config); + } + + return true; + } + } } diff --git a/uSync8.ContentEdition/Handlers/MediaHandler.cs b/uSync8.ContentEdition/Handlers/MediaHandler.cs index c4f8e789..1841e8d9 100644 --- a/uSync8.ContentEdition/Handlers/MediaHandler.cs +++ b/uSync8.ContentEdition/Handlers/MediaHandler.cs @@ -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("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("Not processing item, {0} path {1} is excluded", node.Attribute("Alias").ValueOrDefault("unknown"), path); + return false; + } + } + } + + return true; + } + + /// + /// Should we save this value to disk? + /// + /// + /// 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 + /// + + protected override bool ShouldExport(XElement node, HandlerSettings config) + { + if (config.Settings.ContainsKey("RulesOnExport") && config.Settings["RulesOnExport"].InvariantEquals("true")) + { + return ShouldImport(node, config); + } + + return true; + } + } } diff --git a/uSync8.Site/config/uSync8.config b/uSync8.Site/config/uSync8.config index 3fbbd015..dc12e1c5 100644 --- a/uSync8.Site/config/uSync8.config +++ b/uSync8.Site/config/uSync8.config @@ -22,7 +22,11 @@ - + + + + +