| 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.Diagnostics; |
| 8 | using System.IO; |
| 9 | using System.Linq; |
| 10 | using WixToolset.Data; |
| 11 | using WixToolset.Data.Burn; |
| 12 | using WixToolset.Data.Symbols; |
| 13 | using WixToolset.Extensibility.Data; |
| 14 | using WixToolset.Extensibility.Services; |
| 15 | |
| 16 | internal class CreateNonUXContainers |
| 17 | { |
| 18 | public CreateNonUXContainers(IBackendHelper backendHelper, IMessaging messaging, IEnumerable<WixBundleContainerSymbol> containerSymbols, Dictionary<string, WixBundlePayloadSymbol> payloadSymbols, string intermediateFolder, string layoutFolder, CompressionLevel? defaultCompressionLevel) |
| 19 | { |
| 20 | this.BackendHelper = backendHelper; |
| 21 | this.Messaging = messaging; |
| 22 | this.Containers = containerSymbols; |
| 23 | this.PayloadSymbols = payloadSymbols; |
| 24 | this.IntermediateFolder = intermediateFolder; |
| 25 | this.LayoutFolder = layoutFolder; |
| 26 | this.DefaultCompressionLevel = defaultCompressionLevel; |
| 27 | } |
| 28 | |
| 29 | public IEnumerable<IFileTransfer> FileTransfers { get; private set; } |
| 30 | |
| 31 | public IEnumerable<ITrackedFile> TrackedFiles { get; private set; } |
| 32 | |
| 33 | public WixBundleContainerSymbol UXContainer { get; set; } |
| 34 | |
| 35 | public IEnumerable<WixBundlePayloadSymbol> UXContainerPayloads { get; private set; } |
| 36 | |
| 37 | private IEnumerable<WixBundleContainerSymbol> Containers { get; } |
| 38 | |
| 39 | private IBackendHelper BackendHelper { get; } |
| 40 | |
| 41 | private IMessaging Messaging { get; } |
| 42 | |
| 43 | private Dictionary<string, WixBundlePayloadSymbol> PayloadSymbols { get; } |
| 44 | |
| 45 | private string IntermediateFolder { get; } |
| 46 | |
| 47 | private string LayoutFolder { get; } |
| 48 | |
| 49 | private CompressionLevel? DefaultCompressionLevel { get; } |
| 50 | |
| 51 | public void Execute() |
| 52 | { |
| 53 | var fileTransfers = new List<IFileTransfer>(); |
| 54 | var trackedFiles = new List<ITrackedFile>(); |
| 55 | var uxPayloadSymbols = new List<WixBundlePayloadSymbol>(); |
| 56 | |
| 57 | var attachedContainerIndex = 1; // count starts at one because UX container is "0". |
| 58 | |
| 59 | var payloadsByContainer = this.PayloadSymbols.Values.ToLookup(p => p.ContainerRef); |
| 60 | |
| 61 | foreach (var container in this.Containers) |
| 62 | { |
| 63 | var containerId = container.Id.Id; |
| 64 | |
| 65 | var containerPayloads = payloadsByContainer[containerId]; |
| 66 | |
| 67 | if (!containerPayloads.Any()) |
| 68 | { |
| 69 | if (containerId != BurnConstants.BurnDefaultAttachedContainerName) |
| 70 | { |
| 71 | this.Messaging.Write(BurnBackendWarnings.EmptyContainer(container.SourceLineNumbers, containerId)); |
| 72 | } |
| 73 | } |
| 74 | else if (BurnConstants.BurnUXContainerName == containerId) |
| 75 | { |
| 76 | this.UXContainer = container; |
| 77 | |
| 78 | container.AttachedContainerIndex = 0; |
| 79 | container.WorkingPath = Path.Combine(this.IntermediateFolder, container.Name); |
| 80 | |
| 81 | uxPayloadSymbols.AddRange(containerPayloads); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | container.WorkingPath = Path.Combine(this.IntermediateFolder, container.Name); |
| 86 | |
| 87 | if (ContainerType.Detached == container.Type) |
| 88 | { |
| 89 | // Add file transfer to move the detached containers from intermediate build location to the correct output location. |
| 90 | var outputPath = Path.Combine(this.LayoutFolder, container.Name); |
| 91 | var transfer = this.BackendHelper.CreateFileTransfer(container.WorkingPath, outputPath, true, container.SourceLineNumbers); |
| 92 | fileTransfers.Add(transfer); |
| 93 | |
| 94 | trackedFiles.Add(this.BackendHelper.TrackFile(outputPath, TrackedFileType.BuiltContentOutput, container.SourceLineNumbers)); |
| 95 | } |
| 96 | else // update the attached container index. |
| 97 | { |
| 98 | Debug.Assert(ContainerType.Attached == container.Type); |
| 99 | |
| 100 | container.AttachedContainerIndex = attachedContainerIndex; |
| 101 | ++attachedContainerIndex; |
| 102 | |
| 103 | trackedFiles.Add(this.BackendHelper.TrackFile(container.WorkingPath, TrackedFileType.Temporary, container.SourceLineNumbers)); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if (!this.Messaging.EncounteredError) |
| 109 | { |
| 110 | foreach (var container in this.Containers.Where(c => !String.IsNullOrEmpty(c.WorkingPath) && c.Id.Id != BurnConstants.BurnUXContainerName)) |
| 111 | { |
| 112 | this.CreateContainer(container, payloadsByContainer[container.Id.Id]); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | this.UXContainerPayloads = uxPayloadSymbols; |
| 117 | this.FileTransfers = fileTransfers; |
| 118 | this.TrackedFiles = trackedFiles; |
| 119 | } |
| 120 | |
| 121 | private void CreateContainer(WixBundleContainerSymbol container, IEnumerable<WixBundlePayloadSymbol> containerPayloads) |
| 122 | { |
| 123 | var command = new CreateContainerCommand(containerPayloads, container.WorkingPath, this.DefaultCompressionLevel); |
| 124 | command.Execute(); |
| 125 | |
| 126 | container.Hash = command.Hash; |
| 127 | container.Size = command.Size; |
| 128 | } |
| 129 | } |
| 130 | } |