aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/Link/FlattenAndProcessBundleTablesCommand.cs
blob: e8df25ed06b2f9360ac7e20edce7692a97d01ec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.Core.Link
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using WixToolset.Data;
    using WixToolset.Data.Burn;
    using WixToolset.Data.Symbols;
    using WixToolset.Extensibility.Services;

    internal class FlattenAndProcessBundleTablesCommand
    {
        public FlattenAndProcessBundleTablesCommand(IntermediateSection entrySection, IMessaging messaging)
        {
            this.EntrySection = entrySection;
            this.Messaging = messaging;
        }

        private IntermediateSection EntrySection { get; }

        private IMessaging Messaging { get; }

        public void Execute()
        {
            this.FlattenBundleTables();

            if (this.Messaging.EncounteredError)
            {
                return;
            }

            this.ProcessBundleComplexReferences();
        }

        /// <summary>
        /// Flattens the tables used in a Bundle.
        /// </summary>
        private void FlattenBundleTables()
        {
            // We need to flatten the nested PayloadGroups and PackageGroups under
            // UX, Chain, and any Containers. When we're done, the WixGroups table
            // will hold Payloads under UX, ChainPackages (references?) under Chain,
            // and ContainerPackages/Payloads under any authored Containers.
            var groups = new WixGroupingOrdering(this.EntrySection, this.Messaging);

            // Create UX payloads and Package payloads and Container packages
            groups.UseTypes(new[] { ComplexReferenceParentType.Container, ComplexReferenceParentType.Layout, ComplexReferenceParentType.PackageGroup, ComplexReferenceParentType.PayloadGroup, ComplexReferenceParentType.Package },
                            new[] { ComplexReferenceChildType.ContainerPackage, ComplexReferenceChildType.PackageGroup, ComplexReferenceChildType.Package, ComplexReferenceChildType.PackagePayload, ComplexReferenceChildType.PayloadGroup, ComplexReferenceChildType.Payload });
            groups.FlattenAndRewriteGroups(ComplexReferenceParentType.Package, false);
            groups.FlattenAndRewriteGroups(ComplexReferenceParentType.Container, false);
            groups.FlattenAndRewriteGroups(ComplexReferenceParentType.Layout, false);

            // Create Chain packages...
            groups.UseTypes(new[] { ComplexReferenceParentType.PackageGroup }, new[] { ComplexReferenceChildType.Package, ComplexReferenceChildType.PackageGroup });
            groups.FlattenAndRewriteRows(ComplexReferenceParentType.PackageGroup, "WixChain", false);

            groups.RemoveUsedGroupRows();
        }

        private void ProcessBundleComplexReferences()
        {
            var containersById = this.EntrySection.Symbols.OfType<WixBundleContainerSymbol>().ToDictionary(c => c.Id.Id);
            var groups = this.EntrySection.Symbols.OfType<WixGroupSymbol>().ToList();
            var payloadsById = this.EntrySection.Symbols.OfType<WixBundlePayloadSymbol>().ToDictionary(c => c.Id.Id);

            var containerByPackage = new Dictionary<string, WixBundleContainerSymbol>();
            var referencedPackages = new HashSet<string>();
            var payloadsInBA = new HashSet<string>();
            var payloadsInPackageOrLayout = new HashSet<string>();

            foreach (var groupSymbol in groups)
            {
                switch (groupSymbol.ChildType)
                {
                    case ComplexReferenceChildType.ContainerPackage:
                        switch (groupSymbol.ParentType)
                        {
                            case ComplexReferenceParentType.Container:
                                if (containerByPackage.TryGetValue(groupSymbol.ChildId, out var collisionContainer))
                                {
                                    this.Messaging.Write(LinkerErrors.PackageInMultipleContainers(groupSymbol.SourceLineNumbers, groupSymbol.ChildId, groupSymbol.ParentId, collisionContainer.Id.Id));
                                }
                                else
                                {
                                    containerByPackage.Add(groupSymbol.ChildId, containersById[groupSymbol.ParentId]);
                                }
                                break;
                        }
                        break;
                    case ComplexReferenceChildType.Package:
                        switch (groupSymbol.ParentType)
                        {
                            case ComplexReferenceParentType.PackageGroup:
                                if (groupSymbol.ParentId == "WixChain")
                                {
                                    referencedPackages.Add(groupSymbol.ChildId);
                                }
                                break;
                        }
                        break;
                    case ComplexReferenceChildType.Payload:
                        switch (groupSymbol.ParentType)
                        {
                            case ComplexReferenceParentType.Container:
                                if (groupSymbol.ParentId == BurnConstants.BurnUXContainerName)
                                {
                                    payloadsInBA.Add(groupSymbol.ChildId);
                                }
                                break;
                            case ComplexReferenceParentType.Layout:
                                payloadsById[groupSymbol.ChildId].LayoutOnly = true;
                                payloadsInPackageOrLayout.Add(groupSymbol.ChildId);
                                break;
                            case ComplexReferenceParentType.Package:
                                payloadsInPackageOrLayout.Add(groupSymbol.ChildId);
                                break;
                        }
                        break;
                }
            }

            foreach (var package in this.EntrySection.Symbols.OfType<WixBundlePackageSymbol>())
            {
                if (!referencedPackages.Contains(package.Id.Id))
                {
                    this.Messaging.Write(LinkerErrors.UnscheduledChainPackage(package.SourceLineNumbers, package.Id.Id));
                }
            }

            foreach (var rollbackBoundary in this.EntrySection.Symbols.OfType<WixBundleRollbackBoundarySymbol>())
            {
                if (!referencedPackages.Contains(rollbackBoundary.Id.Id))
                {
                    this.Messaging.Write(LinkerErrors.UnscheduledRollbackBoundary(rollbackBoundary.SourceLineNumbers, rollbackBoundary.Id.Id));
                }
            }

            foreach (var payload in payloadsById.Values)
            {
                var payloadId = payload.Id.Id;
                if (payloadsInBA.Contains(payloadId))
                {
                    if (payloadsInPackageOrLayout.Contains(payloadId))
                    {
                        this.Messaging.Write(LinkerErrors.PayloadSharedWithBA(payload.SourceLineNumbers, payloadId));
                    }
                }
                else if (!payloadsInPackageOrLayout.Contains(payloadId))
                {
                    this.Messaging.Write(LinkerErrors.OrphanedPayload(payload.SourceLineNumbers, payloadId));
                }
            }

            if (this.Messaging.EncounteredError)
            {
                return;
            }

            // Assign authored payloads to authored containers.
            // Compressed Payloads not assigned to a container here will get assigned to the default attached container during binding.
            foreach (var groupSymbol in groups)
            {
                if (groupSymbol.ChildType == ComplexReferenceChildType.Payload && groupSymbol.ParentType == ComplexReferenceParentType.Container)
                {
                    var payloadSymbol = payloadsById[groupSymbol.ChildId];
                    var containerId = groupSymbol.ParentId;

                    if (String.IsNullOrEmpty(payloadSymbol.ContainerRef))
                    {
                        payloadSymbol.ContainerRef = containerId;
                    }
                    else
                    {
                        this.Messaging.Write(LinkerWarnings.PayloadInMultipleContainers(groupSymbol.SourceLineNumbers, groupSymbol.ChildId, containerId, payloadSymbol.ContainerRef));
                    }

                    if (payloadSymbol.LayoutOnly)
                    {
                        this.Messaging.Write(LinkerWarnings.LayoutPayloadInContainer(groupSymbol.SourceLineNumbers, groupSymbol.ChildId, containerId));
                    }
                }
            }

        }
    }
}