Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#239 ability to create custom migrations for widget properties #263

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# * text=auto
* text eol=crlf

# Ensure binary files are not treated as text https://stackoverflow.com/a/32278635/939634
*.png binary
Expand Down
1,438 changes: 719 additions & 719 deletions KVA/Migration.Tool.Source/Handlers/MigratePagesCommandHandler.cs

Large diffs are not rendered by default.

157 changes: 79 additions & 78 deletions KVA/Migration.Tool.Source/Helpers/PageBuilderWidgetsPatcher.cs
Original file line number Diff line number Diff line change
@@ -1,78 +1,79 @@
using Migration.Tool.Common.Helpers;
using Migration.Tool.Source.Services.Model;

using Newtonsoft.Json.Linq;

namespace Migration.Tool.Source.Helpers;

public static class PageBuilderWidgetsPatcher
{
public static EditableAreasConfiguration DeferredPatchConfiguration(EditableAreasConfiguration configuration, TreePathConvertor convertor, out bool anythingChanged)
{
anythingChanged = false;
foreach (var configurationEditableArea in configuration.EditableAreas ?? [])
{
foreach (var sectionConfiguration in configurationEditableArea.Sections ?? [])
{
foreach (var sectionConfigurationZone in sectionConfiguration.Zones ?? [])
{
foreach (var configurationZoneWidget in sectionConfigurationZone.Widgets ?? [])
{
DeferredPatchWidget(configurationZoneWidget, convertor, out bool anythingChangedTmp);
anythingChanged = anythingChanged || anythingChangedTmp;
}
}
}
}

return configuration;
}

private static void DeferredPatchWidget(WidgetConfiguration configurationZoneWidget, TreePathConvertor convertor, out bool anythingChanged)
{
anythingChanged = false;
if (configurationZoneWidget == null)
{
return;
}

var list = configurationZoneWidget.Variants ?? [];
for (int i = 0; i < list.Count; i++)
{
var variant = JObject.FromObject(list[i]);
DeferredPatchProperties(variant, convertor, out bool anythingChangedTmp);

list[i] = variant.ToObject<WidgetVariantConfiguration>();
anythingChanged = anythingChanged || anythingChangedTmp;
}
}

public static void DeferredPatchProperties(JObject propertyContainer, TreePathConvertor convertor, out bool anythingChanged)
{
anythingChanged = false;
if (propertyContainer?["properties"] is JObject { Count: 1 } properties)
{
foreach ((string key, var value) in properties)
{
switch (key)
{
case "TreePath":
{
string? nodeAliasPath = value?.Value<string>();
string treePath = convertor.GetConvertedOrUnchangedAssumingChannel(nodeAliasPath);
if (!TreePathConvertor.TreePathComparer.Equals(nodeAliasPath, treePath))
{
properties["TreePath"] = JToken.FromObject(treePath);
anythingChanged = true;
}

break;
}

default:
break;
}
}
}
}
}
using Migration.Tool.Common.Helpers;
using Migration.Tool.Common.Model;
using Newtonsoft.Json.Linq;

namespace Migration.Tool.Source.Helpers;

public static class PageBuilderWidgetsPatcher
{
public static EditableAreasConfiguration DeferredPatchConfiguration(EditableAreasConfiguration configuration, TreePathConvertor convertor, out bool anythingChanged)
{
anythingChanged = false;
foreach (var configurationEditableArea in configuration.EditableAreas ?? [])
{
foreach (var sectionConfiguration in configurationEditableArea.Sections ?? [])
{
foreach (var sectionConfigurationZone in sectionConfiguration.Zones ?? [])
{
foreach (var configurationZoneWidget in sectionConfigurationZone.Widgets ?? [])
{
DeferredPatchWidget(configurationZoneWidget, convertor, out bool anythingChangedTmp);
anythingChanged = anythingChanged || anythingChangedTmp;
}
}
}
}

return configuration;
}

private static void DeferredPatchWidget(WidgetConfiguration? configurationZoneWidget, TreePathConvertor convertor, out bool anythingChanged)
{
anythingChanged = false;
if (configurationZoneWidget == null)
{
return;
}

var list = configurationZoneWidget.Variants ?? [];
for (int i = 0; i < list.Count; i++)
{
if (list[i] is { } variantJson)
{
var variant = JObject.FromObject(variantJson);
DeferredPatchProperties(variant, convertor, out bool anythingChangedTmp);

list[i] = variant.ToObject<WidgetVariantConfiguration>();
anythingChanged = anythingChanged || anythingChangedTmp;
}
}
}

public static void DeferredPatchProperties(JObject propertyContainer, TreePathConvertor convertor, out bool anythingChanged)
{
anythingChanged = false;
if (propertyContainer?["properties"] is JObject { Count: 1 } properties)
{
foreach ((string key, var value) in properties)
{
switch (key)
{
case "TreePath" when value?.Value<string>() is { } nodeAliasPath:
{
string treePath = convertor.GetConvertedOrUnchangedAssumingChannel(nodeAliasPath);
if (!TreePathConvertor.TreePathComparer.Equals(nodeAliasPath, treePath))
{
properties["TreePath"] = JToken.FromObject(treePath);
anythingChanged = true;
}

break;
}

default:
break;
}
}
}
}
}
217 changes: 109 additions & 108 deletions KVA/Migration.Tool.Source/KsCoreDiExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,108 +1,109 @@
using CMS.DataEngine;
using CMS.FormEngine;
using CMS.MediaLibrary;
using CMS.Membership;
using CMS.Modules;
using CMS.OnlineForms;
using CMS.Websites;

using Kentico.Xperience.UMT;

using MediatR;

using Microsoft.Extensions.DependencyInjection;

using Migration.Tool.Common;
using Migration.Tool.Common.Abstractions;
using Migration.Tool.Common.MigrationProtocol;
using Migration.Tool.Common.Services;
using Migration.Tool.Common.Services.BulkCopy;
using Migration.Tool.Common.Services.Ipc;
using Migration.Tool.KXP.Models;
using Migration.Tool.Source.Auxiliary;
using Migration.Tool.Source.Behaviors;
using Migration.Tool.Source.Contexts;
using Migration.Tool.Source.Helpers;
using Migration.Tool.Source.Mappers;
using Migration.Tool.Source.Model;
using Migration.Tool.Source.Providers;
using Migration.Tool.Source.Services;

namespace Migration.Tool.Source;

public static class KsCoreDiExtensions
{
public static IServiceProvider ServiceProvider { get; private set; } = null!;
public static void InitServiceProvider(IServiceProvider serviceProvider) => ServiceProvider = serviceProvider;

public static IServiceCollection UseKsToolCore(this IServiceCollection services, bool? migrateMediaToMediaLibrary = false)
{
var printService = new PrintService();
services.AddSingleton<IPrintService>(printService);
HandbookReference.PrintService = printService;
LogExtensions.PrintService = printService;

services.AddTransient<IModuleLoader, ModuleLoader>();

services.AddSingleton<ModelFacade>();
services.AddSingleton<ISpoiledGuidContext, SpoiledGuidContext>();
services.AddSingleton(s => s.GetRequiredService<ISpoiledGuidContext>() as SpoiledGuidContext ?? throw new InvalidOperationException());

services.AddSingleton<ISourceGuidContext, SourceGuidContext>();
services.AddSingleton<EntityIdentityFacade>();
services.AddSingleton<IdentityLocator>();
services.AddSingleton<IAssetFacade, AssetFacade>();
services.AddSingleton<MediaLinkServiceFactory>();
services.AddSingleton<ClassMappingProvider>();

services.AddTransient<BulkDataCopyService>();
services.AddTransient<CmsRelationshipService>();
services.AddTransient<CoupledDataService>();
if (migrateMediaToMediaLibrary ?? false)
{
services.AddScoped<IAttachmentMigrator, AttachmentMigratorToMediaLibrary>();
services.AddScoped<IMediaFileMigrator, MediaFileMigrator>();
}
else
{
services.AddScoped<IAttachmentMigrator, AttachmentMigratorToContentItem>();
services.AddScoped<IMediaFileMigrator, MediaFileMigratorToContentItem>();
}
services.AddScoped<PageTemplateMigrator>();
services.AddScoped<ClassService>();

services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(KsCoreDiExtensions).Assembly));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestHandlingBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(CommandConstraintBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(XbKApiContextBehavior<,>));

services.AddSingleton<SourceInstanceContext>();
services.AddSingleton<DeferredPathService>();
services.AddTransient<IpcService>();
services.AddTransient<ReusableSchemaService>();

services.AddScoped<PrimaryKeyMappingContext>();
services.AddScoped<IPrimaryKeyMappingContext, PrimaryKeyMappingContext>(s => s.GetRequiredService<PrimaryKeyMappingContext>());
services.AddScoped<IPrimaryKeyLocatorService, PrimaryKeyLocatorService>();

// umt mappers
services.AddTransient<IUmtMapper<CmsTreeMapperSource>, ContentItemMapper>();
services.AddTransient<IUmtMapper<TagModelSource>, TagMapper>();

// mappers
services.AddTransient<IEntityMapper<CmsAttachmentMapperSource, MediaFileInfo>, CmsAttachmentMapper>();
services.AddTransient<IEntityMapper<ICmsClass, DataClassInfo>, CmsClassMapper>();
services.AddTransient<IEntityMapper<ICmsForm, BizFormInfo>, CmsFormMapper>();
services.AddTransient<IEntityMapper<ICmsForm, CmsForm>, CmsFormMapperEf>();
services.AddTransient<IEntityMapper<ICmsResource, ResourceInfo>, ResourceMapper>();
services.AddTransient<IEntityMapper<AlternativeFormMapperSource, AlternativeFormInfo>, AlternativeFormMapper>();
services.AddTransient<IEntityMapper<MemberInfoMapperSource, MemberInfo>, MemberInfoMapper>();
services.AddTransient<IEntityMapper<ICmsPageTemplateConfiguration, PageTemplateConfigurationInfo>, PageTemplateConfigurationMapper>();
services.AddTransient<IEntityMapper<MediaLibraryInfoMapperSource, MediaLibraryInfo>, MediaLibraryInfoMapper>();
services.AddTransient<IEntityMapper<MediaFileInfoMapperSource, MediaFileInfo>, MediaFileInfoMapper>();

services.AddUniversalMigrationToolkit();

return services;
}
}
using CMS.DataEngine;
using CMS.FormEngine;
using CMS.MediaLibrary;
using CMS.Membership;
using CMS.Modules;
using CMS.OnlineForms;
using CMS.Websites;

using Kentico.Xperience.UMT;

using MediatR;

using Microsoft.Extensions.DependencyInjection;

using Migration.Tool.Common;
using Migration.Tool.Common.Abstractions;
using Migration.Tool.Common.MigrationProtocol;
using Migration.Tool.Common.Services;
using Migration.Tool.Common.Services.BulkCopy;
using Migration.Tool.Common.Services.Ipc;
using Migration.Tool.KXP.Models;
using Migration.Tool.Source.Auxiliary;
using Migration.Tool.Source.Behaviors;
using Migration.Tool.Source.Contexts;
using Migration.Tool.Source.Helpers;
using Migration.Tool.Source.Mappers;
using Migration.Tool.Source.Model;
using Migration.Tool.Source.Providers;
using Migration.Tool.Source.Services;

namespace Migration.Tool.Source;

public static class KsCoreDiExtensions
{
public static IServiceProvider ServiceProvider { get; private set; } = null!;
public static void InitServiceProvider(IServiceProvider serviceProvider) => ServiceProvider = serviceProvider;

public static IServiceCollection UseKsToolCore(this IServiceCollection services, bool? migrateMediaToMediaLibrary = false)
{
var printService = new PrintService();
services.AddSingleton<IPrintService>(printService);
HandbookReference.PrintService = printService;
LogExtensions.PrintService = printService;

services.AddTransient<IModuleLoader, ModuleLoader>();

services.AddSingleton<ModelFacade>();
services.AddSingleton<ISpoiledGuidContext, SpoiledGuidContext>();
services.AddSingleton(s => s.GetRequiredService<ISpoiledGuidContext>() as SpoiledGuidContext ?? throw new InvalidOperationException());

services.AddSingleton<ISourceGuidContext, SourceGuidContext>();
services.AddSingleton<EntityIdentityFacade>();
services.AddSingleton<IdentityLocator>();
services.AddSingleton<IAssetFacade, AssetFacade>();
services.AddSingleton<MediaLinkServiceFactory>();
services.AddSingleton<ClassMappingProvider>();
services.AddTransient<PageBuilderPatcher>();

services.AddTransient<BulkDataCopyService>();
services.AddTransient<CmsRelationshipService>();
services.AddTransient<CoupledDataService>();
if (migrateMediaToMediaLibrary ?? false)
{
services.AddScoped<IAttachmentMigrator, AttachmentMigratorToMediaLibrary>();
services.AddScoped<IMediaFileMigrator, MediaFileMigrator>();
}
else
{
services.AddScoped<IAttachmentMigrator, AttachmentMigratorToContentItem>();
services.AddScoped<IMediaFileMigrator, MediaFileMigratorToContentItem>();
}
services.AddScoped<PageTemplateMigrator>();
services.AddScoped<ClassService>();

services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(KsCoreDiExtensions).Assembly));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestHandlingBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(CommandConstraintBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(XbKApiContextBehavior<,>));

services.AddSingleton<SourceInstanceContext>();
services.AddSingleton<DeferredPathService>();
services.AddTransient<IpcService>();
services.AddTransient<ReusableSchemaService>();

services.AddScoped<PrimaryKeyMappingContext>();
services.AddScoped<IPrimaryKeyMappingContext, PrimaryKeyMappingContext>(s => s.GetRequiredService<PrimaryKeyMappingContext>());
services.AddScoped<IPrimaryKeyLocatorService, PrimaryKeyLocatorService>();

// umt mappers
services.AddTransient<IUmtMapper<CmsTreeMapperSource>, ContentItemMapper>();
services.AddTransient<IUmtMapper<TagModelSource>, TagMapper>();

// mappers
services.AddTransient<IEntityMapper<CmsAttachmentMapperSource, MediaFileInfo>, CmsAttachmentMapper>();
services.AddTransient<IEntityMapper<ICmsClass, DataClassInfo>, CmsClassMapper>();
services.AddTransient<IEntityMapper<ICmsForm, BizFormInfo>, CmsFormMapper>();
services.AddTransient<IEntityMapper<ICmsForm, CmsForm>, CmsFormMapperEf>();
services.AddTransient<IEntityMapper<ICmsResource, ResourceInfo>, ResourceMapper>();
services.AddTransient<IEntityMapper<AlternativeFormMapperSource, AlternativeFormInfo>, AlternativeFormMapper>();
services.AddTransient<IEntityMapper<MemberInfoMapperSource, MemberInfo>, MemberInfoMapper>();
services.AddTransient<IEntityMapper<ICmsPageTemplateConfiguration, PageTemplateConfigurationInfo>, PageTemplateConfigurationMapper>();
services.AddTransient<IEntityMapper<MediaLibraryInfoMapperSource, MediaLibraryInfo>, MediaLibraryInfoMapper>();
services.AddTransient<IEntityMapper<MediaFileInfoMapperSource, MediaFileInfo>, MediaFileInfoMapper>();

services.AddUniversalMigrationToolkit();

return services;
}
}
Loading
Loading