| 1 | // 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. |
| 2 | |
| 3 | namespace WixToolset.Core.Link |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.Linq; |
| 8 | using WixToolset.Data; |
| 9 | using WixToolset.Data.Burn; |
| 10 | using WixToolset.Data.Symbols; |
| 11 | using WixToolset.Extensibility.Services; |
| 12 | |
| 13 | internal class FlattenAndProcessBundleTablesCommand |
| 14 | { |
| 15 | public FlattenAndProcessBundleTablesCommand(IntermediateSection entrySection, IMessaging messaging) |
| 16 | { |
| 17 | this.EntrySection = entrySection; |
| 18 | this.Messaging = messaging; |
| 19 | } |
| 20 | |
| 21 | private IntermediateSection EntrySection { get; } |
| 22 | |
| 23 | private IMessaging Messaging { get; } |
| 24 | |
| 25 | public void Execute() |
| 26 | { |
| 27 | this.FlattenBundleTables(); |
| 28 | |
| 29 | if (this.Messaging.EncounteredError) |
| 30 | { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | this.ProcessBundleComplexReferences(); |
| 35 | } |
| 36 | |
| 37 | /// <summary> |
| 38 | /// Flattens the tables used in a Bundle. |
| 39 | /// </summary> |
| 40 | private void FlattenBundleTables() |
| 41 | { |
| 42 | // We need to flatten the nested PayloadGroups and PackageGroups under |
| 43 | // UX, Chain, and any Containers. When we're done, the WixGroups table |
| 44 | // will hold Payloads under UX, ChainPackages (references?) under Chain, |
| 45 | // and ContainerPackages/Payloads under any authored Containers. |
| 46 | var groups = new WixGroupingOrdering(this.EntrySection, this.Messaging); |
| 47 | |
| 48 | // Create UX payloads and Package payloads and Container packages |
| 49 | groups.UseTypes(new[] { ComplexReferenceParentType.Container, ComplexReferenceParentType.Layout, ComplexReferenceParentType.PackageGroup, ComplexReferenceParentType.PayloadGroup, ComplexReferenceParentType.Package }, |
| 50 | new[] { ComplexReferenceChildType.ContainerPackage, ComplexReferenceChildType.PackageGroup, ComplexReferenceChildType.Package, ComplexReferenceChildType.PackagePayload, ComplexReferenceChildType.PayloadGroup, ComplexReferenceChildType.Payload }); |
| 51 | groups.FlattenAndRewriteGroups(ComplexReferenceParentType.Package, false); |
| 52 | groups.FlattenAndRewriteGroups(ComplexReferenceParentType.Container, false); |
| 53 | groups.FlattenAndRewriteGroups(ComplexReferenceParentType.Layout, false); |
| 54 | |
| 55 | // Create Chain packages... |
| 56 | groups.UseTypes(new[] { ComplexReferenceParentType.PackageGroup }, new[] { ComplexReferenceChildType.Package, ComplexReferenceChildType.PackageGroup }); |
| 57 | groups.FlattenAndRewriteRows(ComplexReferenceParentType.PackageGroup, BurnConstants.BundleChainPackageGroupId, false); |
| 58 | |
| 59 | groups.RemoveUsedGroupRows(); |
| 60 | } |
| 61 | |
| 62 | private void ProcessBundleComplexReferences() |
| 63 | { |
| 64 | var containersById = this.EntrySection.Symbols.OfType<WixBundleContainerSymbol>().ToDictionary(c => c.Id.Id); |
| 65 | var groups = this.EntrySection.Symbols.OfType<WixGroupSymbol>().ToList(); |
| 66 | var payloadsById = this.EntrySection.Symbols.OfType<WixBundlePayloadSymbol>().ToDictionary(c => c.Id.Id); |
| 67 | |
| 68 | var containerByPackage = new Dictionary<string, WixBundleContainerSymbol>(); |
| 69 | var referencedPackages = new HashSet<string>(); |
| 70 | var payloadsInBA = new HashSet<string>(); |
| 71 | var payloadsInPackageOrLayout = new HashSet<string>(); |
| 72 | |
| 73 | foreach (var groupSymbol in groups) |
| 74 | { |
| 75 | switch (groupSymbol.ChildType) |
| 76 | { |
| 77 | case ComplexReferenceChildType.ContainerPackage: |
| 78 | switch (groupSymbol.ParentType) |
| 79 | { |
| 80 | case ComplexReferenceParentType.Container: |
| 81 | if (containerByPackage.TryGetValue(groupSymbol.ChildId, out var collisionContainer)) |
| 82 | { |
| 83 | this.Messaging.Write(LinkerErrors.PackageInMultipleContainers(groupSymbol.SourceLineNumbers, groupSymbol.ChildId, groupSymbol.ParentId, collisionContainer.Id.Id)); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | containerByPackage.Add(groupSymbol.ChildId, containersById[groupSymbol.ParentId]); |
| 88 | } |
| 89 | break; |
| 90 | } |
| 91 | break; |
| 92 | case ComplexReferenceChildType.Package: |
| 93 | switch (groupSymbol.ParentType) |
| 94 | { |
| 95 | case ComplexReferenceParentType.PackageGroup: |
| 96 | if (groupSymbol.ParentId == BurnConstants.BundleChainPackageGroupId) |
| 97 | { |
| 98 | referencedPackages.Add(groupSymbol.ChildId); |
| 99 | } |
| 100 | break; |
| 101 | } |
| 102 | break; |
| 103 | case ComplexReferenceChildType.Payload: |
| 104 | switch (groupSymbol.ParentType) |
| 105 | { |
| 106 | case ComplexReferenceParentType.Container: |
| 107 | if (groupSymbol.ParentId == BurnConstants.BurnUXContainerName) |
| 108 | { |
| 109 | payloadsInBA.Add(groupSymbol.ChildId); |
| 110 | } |
| 111 | break; |
| 112 | case ComplexReferenceParentType.Layout: |
| 113 | payloadsById[groupSymbol.ChildId].LayoutOnly = true; |
| 114 | payloadsInPackageOrLayout.Add(groupSymbol.ChildId); |
| 115 | break; |
| 116 | case ComplexReferenceParentType.Package: |
| 117 | payloadsInPackageOrLayout.Add(groupSymbol.ChildId); |
| 118 | break; |
| 119 | } |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | foreach (var package in this.EntrySection.Symbols.OfType<WixBundlePackageSymbol>()) |
| 125 | { |
| 126 | if (!referencedPackages.Contains(package.Id.Id)) |
| 127 | { |
| 128 | this.Messaging.Write(LinkerErrors.UnscheduledChainPackage(package.SourceLineNumbers, package.Id.Id)); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | foreach (var rollbackBoundary in this.EntrySection.Symbols.OfType<WixBundleRollbackBoundarySymbol>()) |
| 133 | { |
| 134 | if (!referencedPackages.Contains(rollbackBoundary.Id.Id)) |
| 135 | { |
| 136 | this.Messaging.Write(LinkerErrors.UnscheduledRollbackBoundary(rollbackBoundary.SourceLineNumbers, rollbackBoundary.Id.Id)); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | foreach (var payload in payloadsById.Values) |
| 141 | { |
| 142 | var payloadId = payload.Id.Id; |
| 143 | if (payloadsInBA.Contains(payloadId)) |
| 144 | { |
| 145 | if (payloadsInPackageOrLayout.Contains(payloadId)) |
| 146 | { |
| 147 | this.Messaging.Write(LinkerErrors.PayloadSharedWithBA(payload.SourceLineNumbers, payloadId)); |
| 148 | } |
| 149 | } |
| 150 | else if (!payloadsInPackageOrLayout.Contains(payloadId)) |
| 151 | { |
| 152 | this.Messaging.Write(LinkerErrors.OrphanedPayload(payload.SourceLineNumbers, payloadId)); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (this.Messaging.EncounteredError) |
| 157 | { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | // Assign authored payloads to authored containers. |
| 162 | // Compressed Payloads not assigned to a container here will get assigned to the default attached container during binding. |
| 163 | foreach (var groupSymbol in groups) |
| 164 | { |
| 165 | if (groupSymbol.ChildType == ComplexReferenceChildType.Payload && groupSymbol.ParentType == ComplexReferenceParentType.Container) |
| 166 | { |
| 167 | var payloadSymbol = payloadsById[groupSymbol.ChildId]; |
| 168 | var containerId = groupSymbol.ParentId; |
| 169 | |
| 170 | if (String.IsNullOrEmpty(payloadSymbol.ContainerRef)) |
| 171 | { |
| 172 | if (payloadSymbol.Compressed == false) |
| 173 | { |
| 174 | if (containerId == BurnConstants.BurnUXContainerName) |
| 175 | { |
| 176 | // In theory, UX payloads could be embedded in the UX CAB, external to the bundle EXE, or even |
| 177 | // downloaded. The current engine requires the UX to be fully present before any downloading starts, |
| 178 | // so that rules out downloading. Also, the burn engine does not currently copy external UX payloads |
| 179 | // into the temporary UX directory correctly, so we don't allow external either. |
| 180 | if (payloadSymbol.SourceFile is null) |
| 181 | { |
| 182 | this.Messaging.Write(LinkerErrors.BAContainerCannotContainRemotePayload(payloadSymbol.SourceLineNumbers, payloadSymbol.Name)); |
| 183 | } |
| 184 | else if (PackagingType.Embedded != payloadSymbol.Packaging) |
| 185 | { |
| 186 | this.Messaging.Write(WarningMessages.UxPayloadsOnlySupportEmbedding(payloadSymbol.SourceLineNumbers, payloadSymbol.SourceFile.Path)); |
| 187 | payloadSymbol.Packaging = PackagingType.Embedded; |
| 188 | } |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | this.Messaging.Write(LinkerErrors.UncompressedPayloadInContainer(payloadSymbol.SourceLineNumbers, groupSymbol.ChildId, containerId)); |
| 193 | } |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | payloadSymbol.Compressed = true; |
| 198 | payloadSymbol.ContainerRef = containerId; |
| 199 | } |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | this.Messaging.Write(LinkerWarnings.PayloadInMultipleContainers(groupSymbol.SourceLineNumbers, groupSymbol.ChildId, containerId, payloadSymbol.ContainerRef)); |
| 204 | } |
| 205 | |
| 206 | if (payloadSymbol.LayoutOnly) |
| 207 | { |
| 208 | this.Messaging.Write(LinkerWarnings.LayoutPayloadInContainer(groupSymbol.SourceLineNumbers, groupSymbol.ChildId, containerId)); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |