From ad372754236927be0f670065d34c4c77c9b5e789 Mon Sep 17 00:00:00 2001 From: Kevin Jump Date: Wed, 28 Aug 2024 10:05:34 +0100 Subject: [PATCH] V13/clean root (#664) * Allow for the creation of clean files at the root level. * fixes #656 moved deletes to the end (#657) also corrected a typo * moved more deletes to end of process (#658) * ignore examples --------- Co-authored-by: Henry Jump <140804846+henryjump@users.noreply.github.com> --- .gitignore | 1 + .../SyncHandlers/Handlers/TemplateHandler.cs | 22 +++++++++++++++++-- .../SyncHandlers/SyncHandlerBase.cs | 5 ++++- .../SyncHandlers/SyncHandlerContainerBase.cs | 13 ++++++++++- .../SyncHandlers/SyncHandlerRoot.cs | 12 ++++++++-- .../Serializers/DataTypeSerializer.cs | 2 +- .../Serializers/TemplateSerializer.cs | 12 ++++++++++ .../SyncContainerSerializerBase.cs | 13 +++++++++++ 8 files changed, 73 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 2d40d443..2b663c1e 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ Bin/ # Config/ Archive/ uSync.Site/ +uSync.Examples/ [Dd]ist/ diff --git a/uSync.BackOffice/SyncHandlers/Handlers/TemplateHandler.cs b/uSync.BackOffice/SyncHandlers/Handlers/TemplateHandler.cs index b76f7aab..def71e1d 100644 --- a/uSync.BackOffice/SyncHandlers/Handlers/TemplateHandler.cs +++ b/uSync.BackOffice/SyncHandlers/Handlers/TemplateHandler.cs @@ -15,7 +15,7 @@ using uSync.BackOffice.Configuration; using uSync.BackOffice.Services; using uSync.Core; - +using uSync.Core.Serialization; using static Umbraco.Cms.Core.Constants; namespace uSync.BackOffice.SyncHandlers.Handlers @@ -25,7 +25,7 @@ namespace uSync.BackOffice.SyncHandlers.Handlers /// [SyncHandler(uSyncConstants.Handlers.TemplateHandler, "Templates", "Templates", uSyncConstants.Priorites.Templates, Icon = "icon-layout", EntityType = UdiEntityType.Template, IsTwoPass = true)] - public class TemplateHandler : SyncHandlerLevelBase, ISyncHandler, + public class TemplateHandler : SyncHandlerLevelBase, ISyncHandler, ISyncPostImportHandler, INotificationHandler>, INotificationHandler>, INotificationHandler>, @@ -51,6 +51,24 @@ public TemplateHandler( this.fileService = fileService; } + /// + public IEnumerable ProcessPostImport(string folder, IEnumerable actions, HandlerSettings config) + { + if (actions == null || !actions.Any()) + return Enumerable.Empty(); + + var results = new List(); + + // we only do deletes here. + foreach (var action in actions.Where(x => x.Change == ChangeType.Hidden)) + { + results.AddRange( + Import(action.FileName, config, SerializerFlags.LastPass)); + } + + return results; + } + /// protected override string GetItemName(ITemplate item) => item.Name; diff --git a/uSync.BackOffice/SyncHandlers/SyncHandlerBase.cs b/uSync.BackOffice/SyncHandlers/SyncHandlerBase.cs index ed57382c..c8fe6d54 100644 --- a/uSync.BackOffice/SyncHandlers/SyncHandlerBase.cs +++ b/uSync.BackOffice/SyncHandlers/SyncHandlerBase.cs @@ -101,7 +101,10 @@ private int GetCleanParentId(string cleanFile) var node = syncFileService.LoadXElement(cleanFile); var id = node.Attribute("Id").ValueOrDefault(0); if (id != 0) return id; - return GetCleanParent(cleanFile)?.Id ?? 0; + + return GetCleanParent(cleanFile)?.Id ?? + (node.GetKey() == Guid.Empty ? -1 : 0); + } /// diff --git a/uSync.BackOffice/SyncHandlers/SyncHandlerContainerBase.cs b/uSync.BackOffice/SyncHandlers/SyncHandlerContainerBase.cs index 3cf64595..c1e71172 100644 --- a/uSync.BackOffice/SyncHandlers/SyncHandlerContainerBase.cs +++ b/uSync.BackOffice/SyncHandlers/SyncHandlerContainerBase.cs @@ -120,7 +120,18 @@ public virtual IEnumerable ProcessPostImport(IEnumerable(); - return CleanFolders(-1); + var results = new List(); + + // we only do deletes here. + foreach (var action in actions.Where(x => x.Change == ChangeType.Hidden)) + { + results.AddRange( + Import(action.FileName, config, SerializerFlags.LastPass)); + } + + results.AddRange(CleanFolders(-1)); + + return results; } /// diff --git a/uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs b/uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs index ac67f358..6a9ec250 100644 --- a/uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs +++ b/uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs @@ -813,7 +813,7 @@ protected IList GetFolderKeys(string folder, bool flat) { var node = XElement.Load(file); var key = node.GetKey(); - if (key != Guid.Empty && !keys.Contains(key)) + if (!keys.Contains(key)) { keys.Add(key); } @@ -1022,6 +1022,14 @@ public IEnumerable Export(Udi udi, string[] folders, HandlerSetting if (item != null) return Export(item, folders, settings); + if (udi.IsRoot && settings.CreateClean) + { + // for roots we still can create a clean + var targetFolder = folders.Last(); + var filename = Path.Combine(targetFolder, $"{Guid.Empty}.{this.uSyncConfig.Settings.DefaultExtension}"); + CreateCleanFile(Guid.Empty, filename); + } + return uSyncAction.Fail(nameof(udi), this.handlerType, this.ItemType, ChangeType.Fail, $"Item not found {udi}", new KeyNotFoundException(nameof(udi))) .AsEnumerableOfOne(); @@ -1172,7 +1180,7 @@ protected virtual bool HasChildren(TObject item) /// protected void CreateCleanFile(Guid key, string filename) { - if (string.IsNullOrWhiteSpace(filename) || key == Guid.Empty) + if (string.IsNullOrWhiteSpace(filename)) return; var folder = Path.GetDirectoryName(filename); diff --git a/uSync.Core/Serialization/Serializers/DataTypeSerializer.cs b/uSync.Core/Serialization/Serializers/DataTypeSerializer.cs index bd6f4b2d..5a86e8de 100644 --- a/uSync.Core/Serialization/Serializers/DataTypeSerializer.cs +++ b/uSync.Core/Serialization/Serializers/DataTypeSerializer.cs @@ -75,7 +75,7 @@ public DataTypeSerializer(IEntityService entityService, ILogger protected override SyncAttempt ProcessDelete(Guid key, string alias, SerializerFlags flags) diff --git a/uSync.Core/Serialization/Serializers/TemplateSerializer.cs b/uSync.Core/Serialization/Serializers/TemplateSerializer.cs index f337e496..c2437c0b 100644 --- a/uSync.Core/Serialization/Serializers/TemplateSerializer.cs +++ b/uSync.Core/Serialization/Serializers/TemplateSerializer.cs @@ -57,6 +57,18 @@ public TemplateSerializer( _capabilityChecker = capabilityChecker; } + protected override SyncAttempt ProcessDelete(Guid key, string alias, SerializerFlags flags) + { + if (flags.HasFlag(SerializerFlags.LastPass)) + { + logger.LogDebug("Processing deletes as part of the last pass"); + return base.ProcessDelete(key, alias, flags); + } + + logger.LogDebug("Delete not processing as this is not the final pass"); + return SyncAttempt.Succeed(alias, ChangeType.Hidden); + } + protected override SyncAttempt DeserializeCore(XElement node, SyncSerializerOptions options) { var key = node.GetKey(); diff --git a/uSync.Core/Serialization/SyncContainerSerializerBase.cs b/uSync.Core/Serialization/SyncContainerSerializerBase.cs index 63bb143a..8515e643 100644 --- a/uSync.Core/Serialization/SyncContainerSerializerBase.cs +++ b/uSync.Core/Serialization/SyncContainerSerializerBase.cs @@ -11,6 +11,7 @@ using Umbraco.Cms.Core.Models.Entities; using Umbraco.Cms.Core.Services; using Umbraco.Extensions; +using uSync.Core.Models; namespace uSync.Core.Serialization { @@ -26,6 +27,18 @@ public SyncContainerSerializerBase(IEntityService entityService, ILogger ProcessDelete(Guid key, string alias, SerializerFlags flags) + { + if (flags.HasFlag(SerializerFlags.LastPass)) + { + logger.LogDebug("Processing deletes as part of the last pass"); + return base.ProcessDelete(key, alias, flags); + } + + logger.LogDebug("Delete not processing as this is not the final pass"); + return SyncAttempt.Succeed(alias, ChangeType.Hidden); + } + protected override Attempt FindOrCreate(XElement node) {