| 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.Burn.Bundles |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.Linq; |
| 8 | using WixToolset.Data; |
| 9 | using WixToolset.Data.Symbols; |
| 10 | using WixToolset.Extensibility.Services; |
| 11 | |
| 12 | internal class GetPackageFacadesCommand |
| 13 | { |
| 14 | public GetPackageFacadesCommand(IMessaging messaging, IEnumerable<WixBundlePackageSymbol> chainPackageSymbols, IntermediateSection section) |
| 15 | { |
| 16 | this.Messaging = messaging; |
| 17 | this.ChainPackageSymbols = chainPackageSymbols; |
| 18 | this.Section = section; |
| 19 | } |
| 20 | |
| 21 | private IEnumerable<WixBundlePackageSymbol> ChainPackageSymbols { get; } |
| 22 | |
| 23 | private IMessaging Messaging { get; } |
| 24 | |
| 25 | private IntermediateSection Section { get; } |
| 26 | |
| 27 | public PackageFacades PackageFacades { get; private set; } |
| 28 | |
| 29 | public void Execute() |
| 30 | { |
| 31 | var wixGroupPackagesGroupedById = this.Section.Symbols.OfType<WixGroupSymbol>().Where(g => g.ParentType == ComplexReferenceParentType.Package).ToLookup(g => g.ParentId); |
| 32 | var bundlePackages = this.Section.Symbols.OfType<WixBundleBundlePackageSymbol>().ToDictionary(t => t.Id.Id); |
| 33 | var exePackages = this.Section.Symbols.OfType<WixBundleExePackageSymbol>().ToDictionary(t => t.Id.Id); |
| 34 | var msiPackages = this.Section.Symbols.OfType<WixBundleMsiPackageSymbol>().ToDictionary(t => t.Id.Id); |
| 35 | var mspPackages = this.Section.Symbols.OfType<WixBundleMspPackageSymbol>().ToDictionary(t => t.Id.Id); |
| 36 | var msuPackages = this.Section.Symbols.OfType<WixBundleMsuPackageSymbol>().ToDictionary(t => t.Id.Id); |
| 37 | var bundlePackagePayloads = this.Section.Symbols.OfType<WixBundleBundlePackagePayloadSymbol>().ToDictionary(t => t.Id.Id); |
| 38 | var exePackagePayloads = this.Section.Symbols.OfType<WixBundleExePackagePayloadSymbol>().ToDictionary(t => t.Id.Id); |
| 39 | var msiPackagePayloads = this.Section.Symbols.OfType<WixBundleMsiPackagePayloadSymbol>().ToDictionary(t => t.Id.Id); |
| 40 | var mspPackagePayloads = this.Section.Symbols.OfType<WixBundleMspPackagePayloadSymbol>().ToDictionary(t => t.Id.Id); |
| 41 | var msuPackagePayloads = this.Section.Symbols.OfType<WixBundleMsuPackagePayloadSymbol>().ToDictionary(t => t.Id.Id); |
| 42 | |
| 43 | var facades = new PackageFacades(); |
| 44 | |
| 45 | foreach (var package in this.ChainPackageSymbols) |
| 46 | { |
| 47 | var id = package.Id.Id; |
| 48 | |
| 49 | IntermediateSymbol packagePayload = null; |
| 50 | foreach (var wixGroup in wixGroupPackagesGroupedById[id]) |
| 51 | { |
| 52 | if (wixGroup.ChildType == ComplexReferenceChildType.PackagePayload) |
| 53 | { |
| 54 | IntermediateSymbol tempPackagePayload = null; |
| 55 | if (bundlePackagePayloads.TryGetValue(wixGroup.ChildId, out var bundlePackagePayload)) |
| 56 | { |
| 57 | if (package.Type == WixBundlePackageType.Bundle) |
| 58 | { |
| 59 | tempPackagePayload = bundlePackagePayload; |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | this.Messaging.Write(ErrorMessages.PackagePayloadUnsupported(bundlePackagePayload.SourceLineNumbers, "Bundle")); |
| 64 | this.Messaging.Write(ErrorMessages.PackagePayloadUnsupported2(package.SourceLineNumbers)); |
| 65 | } |
| 66 | } |
| 67 | else if (exePackagePayloads.TryGetValue(wixGroup.ChildId, out var exePackagePayload)) |
| 68 | { |
| 69 | if (package.Type == WixBundlePackageType.Exe) |
| 70 | { |
| 71 | tempPackagePayload = exePackagePayload; |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | this.Messaging.Write(ErrorMessages.PackagePayloadUnsupported(exePackagePayload.SourceLineNumbers, "Exe")); |
| 76 | this.Messaging.Write(ErrorMessages.PackagePayloadUnsupported2(package.SourceLineNumbers)); |
| 77 | } |
| 78 | } |
| 79 | else if (msiPackagePayloads.TryGetValue(wixGroup.ChildId, out var msiPackagePayload)) |
| 80 | { |
| 81 | if (package.Type == WixBundlePackageType.Msi) |
| 82 | { |
| 83 | tempPackagePayload = msiPackagePayload; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | this.Messaging.Write(ErrorMessages.PackagePayloadUnsupported(msiPackagePayload.SourceLineNumbers, "Msi")); |
| 88 | this.Messaging.Write(ErrorMessages.PackagePayloadUnsupported2(package.SourceLineNumbers)); |
| 89 | } |
| 90 | } |
| 91 | else if (mspPackagePayloads.TryGetValue(wixGroup.ChildId, out var mspPackagePayload)) |
| 92 | { |
| 93 | if (package.Type == WixBundlePackageType.Msp) |
| 94 | { |
| 95 | tempPackagePayload = mspPackagePayload; |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | this.Messaging.Write(ErrorMessages.PackagePayloadUnsupported(mspPackagePayload.SourceLineNumbers, "Msp")); |
| 100 | this.Messaging.Write(ErrorMessages.PackagePayloadUnsupported2(package.SourceLineNumbers)); |
| 101 | } |
| 102 | } |
| 103 | else if (msuPackagePayloads.TryGetValue(wixGroup.ChildId, out var msuPackagePayload)) |
| 104 | { |
| 105 | if (package.Type == WixBundlePackageType.Msu) |
| 106 | { |
| 107 | tempPackagePayload = msuPackagePayload; |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | this.Messaging.Write(ErrorMessages.PackagePayloadUnsupported(msuPackagePayload.SourceLineNumbers, "Msu")); |
| 112 | this.Messaging.Write(ErrorMessages.PackagePayloadUnsupported2(package.SourceLineNumbers)); |
| 113 | } |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | this.Messaging.Write(ErrorMessages.IdentifierNotFound(package.Type + "PackagePayload", wixGroup.ChildId)); |
| 118 | } |
| 119 | |
| 120 | if (tempPackagePayload != null) |
| 121 | { |
| 122 | if (packagePayload == null) |
| 123 | { |
| 124 | packagePayload = tempPackagePayload; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | this.Messaging.Write(ErrorMessages.MultiplePackagePayloads(tempPackagePayload.SourceLineNumbers, id, packagePayload.Id.Id, tempPackagePayload.Id.Id)); |
| 129 | this.Messaging.Write(ErrorMessages.MultiplePackagePayloads2(packagePayload.SourceLineNumbers)); |
| 130 | this.Messaging.Write(ErrorMessages.MultiplePackagePayloads3(package.SourceLineNumbers)); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (packagePayload == null) |
| 137 | { |
| 138 | this.Messaging.Write(ErrorMessages.MissingPackagePayload(package.SourceLineNumbers, id, package.Type.ToString())); |
| 139 | continue; |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | package.PayloadRef = packagePayload.Id.Id; |
| 144 | } |
| 145 | |
| 146 | switch (package.Type) |
| 147 | { |
| 148 | case WixBundlePackageType.Bundle: |
| 149 | if (bundlePackages.TryGetValue(id, out var bundlePackage)) |
| 150 | { |
| 151 | facades.Add(new PackageFacade(package, bundlePackage, packagePayload)); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | this.Messaging.Write(ErrorMessages.IdentifierNotFound("WixBundleBundlePackage", id)); |
| 156 | } |
| 157 | break; |
| 158 | |
| 159 | case WixBundlePackageType.Exe: |
| 160 | if (exePackages.TryGetValue(id, out var exePackage)) |
| 161 | { |
| 162 | facades.Add(new PackageFacade(package, exePackage, packagePayload)); |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | this.Messaging.Write(ErrorMessages.IdentifierNotFound("WixBundleExePackage", id)); |
| 167 | } |
| 168 | break; |
| 169 | |
| 170 | case WixBundlePackageType.Msi: |
| 171 | if (msiPackages.TryGetValue(id, out var msiPackage)) |
| 172 | { |
| 173 | facades.Add(new PackageFacade(package, msiPackage, packagePayload)); |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | this.Messaging.Write(ErrorMessages.IdentifierNotFound("WixBundleMsiPackage", id)); |
| 178 | } |
| 179 | break; |
| 180 | |
| 181 | case WixBundlePackageType.Msp: |
| 182 | if (mspPackages.TryGetValue(id, out var mspPackage)) |
| 183 | { |
| 184 | facades.Add(new PackageFacade(package, mspPackage, packagePayload)); |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | this.Messaging.Write(ErrorMessages.IdentifierNotFound("WixBundleMspPackage", id)); |
| 189 | } |
| 190 | break; |
| 191 | |
| 192 | case WixBundlePackageType.Msu: |
| 193 | if (msuPackages.TryGetValue(id, out var msuPackage)) |
| 194 | { |
| 195 | facades.Add(new PackageFacade(package, msuPackage, packagePayload)); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | this.Messaging.Write(ErrorMessages.IdentifierNotFound("WixBundleMsuPackage", id)); |
| 200 | } |
| 201 | break; |
| 202 | |
| 203 | default: |
| 204 | throw new NotImplementedException(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | this.PackageFacades = facades; |
| 209 | } |
| 210 | } |
| 211 | } |