Skip to content

Commit

Permalink
V13/clean root (#664)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
KevinJump and henryjump authored Aug 28, 2024
1 parent e06547c commit ad37275
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Bin/
# Config/
Archive/
uSync.Site/
uSync.Examples/

[Dd]ist/

Expand Down
22 changes: 20 additions & 2 deletions uSync.BackOffice/SyncHandlers/Handlers/TemplateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +25,7 @@ namespace uSync.BackOffice.SyncHandlers.Handlers
/// </summary>
[SyncHandler(uSyncConstants.Handlers.TemplateHandler, "Templates", "Templates", uSyncConstants.Priorites.Templates,
Icon = "icon-layout", EntityType = UdiEntityType.Template, IsTwoPass = true)]
public class TemplateHandler : SyncHandlerLevelBase<ITemplate, IFileService>, ISyncHandler,
public class TemplateHandler : SyncHandlerLevelBase<ITemplate, IFileService>, ISyncHandler, ISyncPostImportHandler,
INotificationHandler<SavedNotification<ITemplate>>,
INotificationHandler<DeletedNotification<ITemplate>>,
INotificationHandler<MovedNotification<ITemplate>>,
Expand All @@ -51,6 +51,24 @@ public TemplateHandler(
this.fileService = fileService;
}

/// <inheritdoc/>
public IEnumerable<uSyncAction> ProcessPostImport(string folder, IEnumerable<uSyncAction> actions, HandlerSettings config)
{
if (actions == null || !actions.Any())
return Enumerable.Empty<uSyncAction>();

var results = new List<uSyncAction>();

// 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;
}

/// <inheritdoc/>
protected override string GetItemName(ITemplate item) => item.Name;

Expand Down
5 changes: 4 additions & 1 deletion uSync.BackOffice/SyncHandlers/SyncHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

/// <summary>
Expand Down
13 changes: 12 additions & 1 deletion uSync.BackOffice/SyncHandlers/SyncHandlerContainerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,18 @@ public virtual IEnumerable<uSyncAction> ProcessPostImport(IEnumerable<uSyncActio
if (actions == null || !actions.Any())
return Enumerable.Empty<uSyncAction>();

return CleanFolders(-1);
var results = new List<uSyncAction>();

// 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;
}

/// <summary>
Expand Down
12 changes: 10 additions & 2 deletions uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ protected IList<Guid> 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);
}
Expand Down Expand Up @@ -1022,6 +1022,14 @@ public IEnumerable<uSyncAction> 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();
Expand Down Expand Up @@ -1172,7 +1180,7 @@ protected virtual bool HasChildren(TObject item)
/// </summary>
protected void CreateCleanFile(Guid key, string filename)
{
if (string.IsNullOrWhiteSpace(filename) || key == Guid.Empty)
if (string.IsNullOrWhiteSpace(filename))
return;

var folder = Path.GetDirectoryName(filename);
Expand Down
2 changes: 1 addition & 1 deletion uSync.Core/Serialization/Serializers/DataTypeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public DataTypeSerializer(IEntityService entityService, ILogger<DataTypeSerializ
/// this only works because we are keeping the track of
/// all the deletes and renames when they happen
/// and we can only reliably do that for items
/// that have ContainerTree's because they are not
/// that have ContainerTrees because they are not
/// real trees - but flat (each alias is unique)
/// </remarks>
protected override SyncAttempt<IDataType> ProcessDelete(Guid key, string alias, SerializerFlags flags)
Expand Down
12 changes: 12 additions & 0 deletions uSync.Core/Serialization/Serializers/TemplateSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ public TemplateSerializer(
_capabilityChecker = capabilityChecker;
}

protected override SyncAttempt<ITemplate> 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<ITemplate>.Succeed(alias, ChangeType.Hidden);
}

protected override SyncAttempt<ITemplate> DeserializeCore(XElement node, SyncSerializerOptions options)
{
var key = node.GetKey();
Expand Down
13 changes: 13 additions & 0 deletions uSync.Core/Serialization/SyncContainerSerializerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -26,6 +27,18 @@ public SyncContainerSerializerBase(IEntityService entityService, ILogger<SyncCon
this.containerType = containerType;
}

protected override SyncAttempt<TObject> 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<TObject>.Succeed(alias, ChangeType.Hidden);
}

protected override Attempt<TObject> FindOrCreate(XElement node)
{

Expand Down

0 comments on commit ad37275

Please sign in to comment.