From 9f8cb5374481b6c8a06eb2739858332350f72666 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sat, 11 Nov 2017 01:45:59 -0800 Subject: Additional IR updates --- .../Bind/AssignMediaCommand.cs | 187 ++++++++++----------- 1 file changed, 92 insertions(+), 95 deletions(-) (limited to 'src/WixToolset.Core.WindowsInstaller/Bind/AssignMediaCommand.cs') diff --git a/src/WixToolset.Core.WindowsInstaller/Bind/AssignMediaCommand.cs b/src/WixToolset.Core.WindowsInstaller/Bind/AssignMediaCommand.cs index 74e2cdb5..f426b96d 100644 --- a/src/WixToolset.Core.WindowsInstaller/Bind/AssignMediaCommand.cs +++ b/src/WixToolset.Core.WindowsInstaller/Bind/AssignMediaCommand.cs @@ -1,43 +1,44 @@ // 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.WindowsInstaller.Databases +namespace WixToolset.Core.WindowsInstaller.Bind { using System; using System.Collections.Generic; using System.Globalization; + using System.Linq; using WixToolset.Core.Bind; using WixToolset.Data; using WixToolset.Data.Rows; + using WixToolset.Data.Tuples; /// /// AssignMediaCommand assigns files to cabs based on Media or MediaTemplate rows. /// - public class AssignMediaCommand + internal class AssignMediaCommand { - public AssignMediaCommand() + public AssignMediaCommand(IntermediateSection section) { this.CabinetNameTemplate = "Cab{0}.cab"; + this.Section = section; } - public Output Output { private get; set; } + private IntermediateSection Section { get; } + + public IEnumerable FileFacades { private get; set; } public bool FilesCompressed { private get; set; } public string CabinetNameTemplate { private get; set; } - public IEnumerable FileFacades { private get; set; } - - public TableDefinitionCollection TableDefinitions { private get; set; } - /// /// Gets cabinets with their file rows. /// - public Dictionary> FileFacadesByCabinetMedia { get; private set; } + public Dictionary> FileFacadesByCabinetMedia { get; private set; } /// /// Get media rows. /// - public RowDictionary MediaRows { get; private set; } + public Dictionary MediaRows { get; private set; } /// /// Get uncompressed file rows. This will contain file rows of File elements that are marked with compression=no. @@ -47,42 +48,41 @@ namespace WixToolset.Core.WindowsInstaller.Databases public void Execute() { - Dictionary> filesByCabinetMedia = new Dictionary>(); + var filesByCabinetMedia = new Dictionary>(); - RowDictionary mediaRows = new RowDictionary(); + var mediaRows = new Dictionary(); List uncompressedFiles = new List(); - MediaRow mergeModuleMediaRow = null; - Table mediaTable = this.Output.Tables["Media"]; - Table mediaTemplateTable = this.Output.Tables["WixMediaTemplate"]; + var mediaTable = this.Section.Tuples.OfType().ToList(); + var mediaTemplateTable = this.Section.Tuples.OfType().ToList(); // If both tables are authored, it is an error. - if ((mediaTemplateTable != null && mediaTemplateTable.Rows.Count > 0) && (mediaTable != null && mediaTable.Rows.Count > 1)) + if ((mediaTemplateTable != null && mediaTemplateTable.Count > 0) && (mediaTable != null && mediaTable.Count > 1)) { throw new WixException(WixErrors.MediaTableCollision(null)); } // When building merge module, all the files go to "#MergeModule.CABinet". - if (OutputType.Module == this.Output.Type) + if (SectionType.Module == this.Section.Type) { - Table mergeModuleMediaTable = new Table(this.TableDefinitions["Media"]); - mergeModuleMediaRow = (MediaRow)mergeModuleMediaTable.CreateRow(null); + var mergeModuleMediaRow = new MediaTuple(); mergeModuleMediaRow.Cabinet = "#MergeModule.CABinet"; - filesByCabinetMedia.Add(mergeModuleMediaRow, new List()); - } + this.Section.Tuples.Add(mergeModuleMediaRow); - if (OutputType.Module == this.Output.Type || null == mediaTemplateTable) + filesByCabinetMedia.Add(mergeModuleMediaRow, new List(this.FileFacades)); + } + else if (null == mediaTemplateTable) { - this.ManuallyAssignFiles(mediaTable, mergeModuleMediaRow, this.FileFacades, filesByCabinetMedia, mediaRows, uncompressedFiles); + this.ManuallyAssignFiles(mediaTable, this.FileFacades, filesByCabinetMedia, mediaRows, uncompressedFiles); } else { this.AutoAssignFiles(mediaTable, this.FileFacades, filesByCabinetMedia, mediaRows, uncompressedFiles); } - this.FileFacadesByCabinetMedia = new Dictionary>(); + this.FileFacadesByCabinetMedia = new Dictionary>(); foreach (var mediaRowWithFiles in filesByCabinetMedia) { @@ -98,7 +98,7 @@ namespace WixToolset.Core.WindowsInstaller.Databases /// Assign files to cabinets based on MediaTemplate authoring. /// /// FileRowCollection - private void AutoAssignFiles(Table mediaTable, IEnumerable fileFacades, Dictionary> filesByCabinetMedia, RowDictionary mediaRows, List uncompressedFiles) + private void AutoAssignFiles(List mediaTable, IEnumerable fileFacades, Dictionary> filesByCabinetMedia, Dictionary mediaRows, List uncompressedFiles) { const int MaxCabIndex = 999; @@ -107,13 +107,19 @@ namespace WixToolset.Core.WindowsInstaller.Databases int maxPreCabSizeInMB = 0; int currentCabIndex = 0; - MediaRow currentMediaRow = null; + MediaTuple currentMediaRow = null; - Table mediaTemplateTable = this.Output.Tables["WixMediaTemplate"]; + var mediaTemplateTable = this.Section.Tuples.OfType(); + + // Remove all previous media tuples since they will be replaced with + // media template. + foreach (var mediaTuple in mediaTable) + { + this.Section.Tuples.Remove(mediaTuple); + } // Auto assign files to cabinets based on maximum uncompressed media size - mediaTable.Rows.Clear(); - WixMediaTemplateRow mediaTemplateRow = (WixMediaTemplateRow)mediaTemplateTable.Rows[0]; + var mediaTemplateRow = mediaTemplateTable.Single(); if (!String.IsNullOrEmpty(mediaTemplateRow.CabinetTemplate)) { @@ -149,9 +155,9 @@ namespace WixToolset.Core.WindowsInstaller.Databases { // When building a product, if the current file is not to be compressed or if // the package set not to be compressed, don't cab it. - if (OutputType.Product == this.Output.Type && - (!facade.File.Compressed.Value || - (!facade.File.Compressed.HasValue && !this.FilesCompressed))) + if (SectionType.Product == this.Section.Type && + ((facade.File.Compressed.HasValue && !facade.File.Compressed.Value) || + (!facade.File.Compressed.HasValue && !this.FilesCompressed))) { uncompressedFiles.Add(facade); continue; @@ -172,8 +178,8 @@ namespace WixToolset.Core.WindowsInstaller.Databases if (currentPreCabSize > maxPreCabSizeInBytes) { // Overflow due to current file - currentMediaRow = this.AddMediaRow(mediaTemplateRow, mediaTable, ++currentCabIndex); - mediaRows.Add(currentMediaRow); + currentMediaRow = this.AddMediaRow(mediaTemplateRow, ++currentCabIndex); + mediaRows.Add(currentMediaRow.DiskId, currentMediaRow); filesByCabinetMedia.Add(currentMediaRow, new List()); List cabinetFileRows = filesByCabinetMedia[currentMediaRow]; @@ -188,8 +194,8 @@ namespace WixToolset.Core.WindowsInstaller.Databases if (currentMediaRow == null) { // Create new cab and MediaRow - currentMediaRow = this.AddMediaRow(mediaTemplateRow, mediaTable, ++currentCabIndex); - mediaRows.Add(currentMediaRow); + currentMediaRow = this.AddMediaRow(mediaTemplateRow, ++currentCabIndex); + mediaRows.Add(currentMediaRow.DiskId, currentMediaRow); filesByCabinetMedia.Add(currentMediaRow, new List()); } @@ -201,11 +207,13 @@ namespace WixToolset.Core.WindowsInstaller.Databases } // If there are uncompressed files and no MediaRow, create a default one. - if (uncompressedFiles.Count > 0 && mediaTable.Rows.Count == 0) + if (uncompressedFiles.Count > 0 && !this.Section.Tuples.OfType().Any()) { - MediaRow defaultMediaRow = (MediaRow)mediaTable.CreateRow(null); + var defaultMediaRow = new MediaTuple(null, new Identifier(1, AccessModifier.Private)); defaultMediaRow.DiskId = 1; - mediaRows.Add(defaultMediaRow); + + mediaRows.Add(1, defaultMediaRow); + this.Section.Tuples.Add(defaultMediaRow); } } @@ -213,79 +221,65 @@ namespace WixToolset.Core.WindowsInstaller.Databases /// Assign files to cabinets based on Media authoring. /// /// - /// /// - private void ManuallyAssignFiles(Table mediaTable, MediaRow mergeModuleMediaRow, IEnumerable fileFacades, Dictionary> filesByCabinetMedia, RowDictionary mediaRows, List uncompressedFiles) + private void ManuallyAssignFiles(List mediaTable, IEnumerable fileFacades, Dictionary> filesByCabinetMedia, Dictionary mediaRows, List uncompressedFiles) { - if (OutputType.Module != this.Output.Type) + if (mediaTable.Any()) { - if (null != mediaTable) + var cabinetMediaRows = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (var mediaRow in mediaTable) { - Dictionary cabinetMediaRows = new Dictionary(StringComparer.InvariantCultureIgnoreCase); - foreach (MediaRow mediaRow in mediaTable.Rows) + // If the Media row has a cabinet, make sure it is unique across all Media rows. + if (!String.IsNullOrEmpty(mediaRow.Cabinet)) { - // If the Media row has a cabinet, make sure it is unique across all Media rows. - if (!String.IsNullOrEmpty(mediaRow.Cabinet)) + if (cabinetMediaRows.TryGetValue(mediaRow.Cabinet, out var existingRow)) { - MediaRow existingRow; - if (cabinetMediaRows.TryGetValue(mediaRow.Cabinet, out existingRow)) - { - Messaging.Instance.OnMessage(WixErrors.DuplicateCabinetName(mediaRow.SourceLineNumbers, mediaRow.Cabinet)); - Messaging.Instance.OnMessage(WixErrors.DuplicateCabinetName2(existingRow.SourceLineNumbers, existingRow.Cabinet)); - } - else - { - cabinetMediaRows.Add(mediaRow.Cabinet, mediaRow); - } + Messaging.Instance.OnMessage(WixErrors.DuplicateCabinetName(mediaRow.SourceLineNumbers, mediaRow.Cabinet)); + Messaging.Instance.OnMessage(WixErrors.DuplicateCabinetName2(existingRow.SourceLineNumbers, existingRow.Cabinet)); + } + else + { + cabinetMediaRows.Add(mediaRow.Cabinet, mediaRow); } - - mediaRows.Add(mediaRow); } + + mediaRows.Add(mediaRow.DiskId, mediaRow); } + } - foreach (MediaRow mediaRow in mediaRows.Values) + foreach (var mediaRow in mediaRows.Values) + { + if (null != mediaRow.Cabinet) { - if (null != mediaRow.Cabinet) - { - filesByCabinetMedia.Add(mediaRow, new List()); - } + filesByCabinetMedia.Add(mediaRow, new List()); } } foreach (FileFacade facade in fileFacades) { - if (OutputType.Module == this.Output.Type) + if (!mediaRows.TryGetValue(facade.WixFile.DiskId, out var mediaRow)) { - filesByCabinetMedia[mergeModuleMediaRow].Add(facade); + Messaging.Instance.OnMessage(WixErrors.MissingMedia(facade.File.SourceLineNumbers, facade.WixFile.DiskId)); + continue; } - else - { - MediaRow mediaRow; - if (!mediaRows.TryGetValue(facade.WixFile.DiskId.ToString(CultureInfo.InvariantCulture), out mediaRow)) - { - Messaging.Instance.OnMessage(WixErrors.MissingMedia(facade.File.SourceLineNumbers, facade.WixFile.DiskId)); - continue; - } - // When building a product, if the current file is not to be compressed or if - // the package set not to be compressed, don't cab it. - if (OutputType.Product == this.Output.Type && - (!facade.File.Compressed.Value || - (!facade.File.Compressed.HasValue && !this.FilesCompressed))) + // When building a product, if the current file is not to be compressed or if + // the package set not to be compressed, don't cab it. + if (SectionType.Product == this.Section.Type && + (!facade.File.Compressed.Value || + (!facade.File.Compressed.HasValue && !this.FilesCompressed))) + { + uncompressedFiles.Add(facade); + } + else // file is marked compressed. + { + if (filesByCabinetMedia.TryGetValue(mediaRow, out var cabinetFiles)) { - uncompressedFiles.Add(facade); + cabinetFiles.Add(facade); } - else // file is marked compressed. + else { - List cabinetFiles; - if (filesByCabinetMedia.TryGetValue(mediaRow, out cabinetFiles)) - { - cabinetFiles.Add(facade); - } - else - { - Messaging.Instance.OnMessage(WixErrors.ExpectedMediaCabinet(facade.File.SourceLineNumbers, facade.File.File, facade.WixFile.DiskId)); - } + Messaging.Instance.OnMessage(WixErrors.ExpectedMediaCabinet(facade.File.SourceLineNumbers, facade.File.File, facade.WixFile.DiskId)); } } } @@ -297,17 +291,20 @@ namespace WixToolset.Core.WindowsInstaller.Databases /// /// /// - private MediaRow AddMediaRow(WixMediaTemplateRow mediaTemplateRow, Table mediaTable, int cabIndex) + private MediaTuple AddMediaRow(WixMediaTemplateTuple mediaTemplateRow, int cabIndex) { - MediaRow currentMediaRow = (MediaRow)mediaTable.CreateRow(mediaTemplateRow.SourceLineNumbers); + var currentMediaRow = new MediaTuple(mediaTemplateRow.SourceLineNumbers, new Identifier(cabIndex, AccessModifier.Private)); currentMediaRow.DiskId = cabIndex; currentMediaRow.Cabinet = String.Format(CultureInfo.InvariantCulture, this.CabinetNameTemplate, cabIndex); - Table wixMediaTable = this.Output.EnsureTable(this.TableDefinitions["WixMedia"]); - WixMediaRow row = (WixMediaRow)wixMediaTable.CreateRow(mediaTemplateRow.SourceLineNumbers); - row.DiskId = cabIndex; + this.Section.Tuples.Add(currentMediaRow); + + var row = new WixMediaTuple(mediaTemplateRow.SourceLineNumbers, new Identifier(cabIndex, AccessModifier.Private)); + row.DiskId_ = cabIndex; row.CompressionLevel = mediaTemplateRow.CompressionLevel; + this.Section.Tuples.Add(row); + return currentMediaRow; } } -- cgit v1.2.3-55-g6feb