|
| 1 | +using System.Linq; |
| 2 | +using System; |
| 3 | +using Umbraco.Cms.Core.Deploy; |
| 4 | +using Umbraco.Cms.Core.Models; |
| 5 | +using Umbraco.Cms.Core.Semver; |
| 6 | +using Umbraco.Deploy.Core.Migrators; |
| 7 | +using Umbraco.Deploy.Infrastructure.Artifacts; |
| 8 | +using Umbraco.Deploy.Infrastructure.Artifacts.ContentType; |
| 9 | +using System.Collections.Generic; |
| 10 | +using Umbraco.Extensions; |
| 11 | + |
| 12 | +namespace Umbraco.Deploy.Contrib.Migrators; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Migrates the <see cref="DataTypeArtifact" /> to remove Matryoshka data types (using the <see cref="EditorAlias" /> editor) and |
| 16 | +/// <see cref="ContentTypeArtifactBase" /> to replace properties using the removed Matryoshka data types with native groups. |
| 17 | +/// </summary> |
| 18 | +public sealed class ReplaceMatryoshkaArtifactMigrator : ArtifactMigratorBase<IArtifact> |
| 19 | +{ |
| 20 | + private const string EditorAlias = "Our.Umbraco.Matryoshka.GroupSeparator"; |
| 21 | + |
| 22 | + private readonly HashSet<Guid> _removedDataTypeKeys = []; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Initializes a new instance of the <see cref="ReplaceMatryoshkaArtifactMigrator" /> class. |
| 26 | + /// </summary> |
| 27 | + public ReplaceMatryoshkaArtifactMigrator() |
| 28 | + // Matryoshka was only supported on v8 |
| 29 | + => MaxVersion = new SemVersion(9, 0, 0); |
| 30 | + |
| 31 | + /// <inheritdoc /> |
| 32 | + protected override bool CanMigrateType(Type type) |
| 33 | + => type == typeof(DataTypeArtifact) || type.Inherits<ContentTypeArtifactBase>(); |
| 34 | + |
| 35 | + /// <inheritdoc /> |
| 36 | + protected override IArtifact? Migrate(IArtifact artifact) |
| 37 | + => artifact switch |
| 38 | + { |
| 39 | + // Remove Matryoshka data types |
| 40 | + DataTypeArtifact dataTypeArtifact when dataTypeArtifact.EditorAlias == EditorAlias => Migrate(dataTypeArtifact), |
| 41 | + // Replace properties with removed Matryoshka data types with native groups |
| 42 | + ContentTypeArtifactBase contentTypeArtifactBase => Migrate(contentTypeArtifactBase), |
| 43 | + // Ignore other artifacts |
| 44 | + _ => artifact, |
| 45 | + }; |
| 46 | + |
| 47 | + private DataTypeArtifact? Migrate(DataTypeArtifact artifact) |
| 48 | + { |
| 49 | + // Track removed data types by key |
| 50 | + _removedDataTypeKeys.Add(artifact.Key); |
| 51 | + |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + private ContentTypeArtifactBase? Migrate(ContentTypeArtifactBase artifact) |
| 56 | + { |
| 57 | + // Remove property types using the removed data types |
| 58 | + artifact.PropertyTypes = artifact.PropertyTypes.Where(x => _removedDataTypeKeys.Contains(x.DataType.Guid) is false).ToArray(); |
| 59 | + |
| 60 | + // Convert property groups to tabs and create new groups when removed data types are found |
| 61 | + var propertyGroups = new List<ContentTypeArtifactBase.PropertyGroup>(); |
| 62 | + foreach (var propertyGroup in artifact.PropertyGroups.OrderBy(x => x.SortOrder).ToArray()) |
| 63 | + { |
| 64 | + // Convert groups to tabs |
| 65 | + propertyGroup.Type = (short)PropertyGroupType.Tab; |
| 66 | + |
| 67 | + var propertyTypes = new List<ContentTypeArtifactBase.PropertyType>(); |
| 68 | + foreach (var propertyType in propertyGroup.PropertyTypes.OrderByDescending(x => x.SortOrder).ToArray()) |
| 69 | + { |
| 70 | + if (_removedDataTypeKeys.Contains(propertyType.DataType.Guid)) |
| 71 | + { |
| 72 | + // Create new group below tab |
| 73 | + propertyGroups.Add(new ContentTypeArtifactBase.PropertyGroup() |
| 74 | + { |
| 75 | + Key = propertyType.Key, |
| 76 | + Name = propertyType.Name, |
| 77 | + Alias = propertyGroup.Alias + "/" + propertyType.Alias, |
| 78 | + PropertyTypes = propertyTypes.ToArray(), |
| 79 | + SortOrder = propertyType.SortOrder, |
| 80 | + Type = (short)PropertyGroupType.Group, |
| 81 | + }); |
| 82 | + |
| 83 | + propertyTypes.Clear(); |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + // Keep property type |
| 88 | + propertyTypes.Add(propertyType); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + propertyGroup.PropertyTypes = propertyTypes.ToArray(); |
| 93 | + propertyGroups.Add(propertyGroup); |
| 94 | + } |
| 95 | + |
| 96 | + artifact.PropertyGroups = propertyGroups.ToArray(); |
| 97 | + |
| 98 | + return artifact; |
| 99 | + } |
| 100 | +} |
0 commit comments