From 9c714a8f1baa6e0130e5cd00cbdca649cebaf6a5 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Fri, 25 Oct 2019 00:48:35 -0700 Subject: Update to WixOutput file structure to fix embedded file handling --- src/WixToolset.Core/Bind/ExpectedExtractFile.cs | 4 +-- src/WixToolset.Core/Bind/ExtractEmbeddedFiles.cs | 38 ++++++++++++---------- .../Bind/ExtractEmbeddedFilesCommand.cs | 36 ++++---------------- src/WixToolset.Core/Bind/ResolveFieldsCommand.cs | 4 +-- 4 files changed, 31 insertions(+), 51 deletions(-) (limited to 'src/WixToolset.Core/Bind') diff --git a/src/WixToolset.Core/Bind/ExpectedExtractFile.cs b/src/WixToolset.Core/Bind/ExpectedExtractFile.cs index afad12fc..b27cdfee 100644 --- a/src/WixToolset.Core/Bind/ExpectedExtractFile.cs +++ b/src/WixToolset.Core/Bind/ExpectedExtractFile.cs @@ -1,4 +1,4 @@ -// 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. +// 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.Bind { @@ -9,7 +9,7 @@ namespace WixToolset.Core.Bind { public Uri Uri { get; set; } - public int EmbeddedFileIndex { get; set; } + public string EmbeddedFileId { get; set; } public string OutputPath { get; set; } } diff --git a/src/WixToolset.Core/Bind/ExtractEmbeddedFiles.cs b/src/WixToolset.Core/Bind/ExtractEmbeddedFiles.cs index 644e5c63..35c8a2f0 100644 --- a/src/WixToolset.Core/Bind/ExtractEmbeddedFiles.cs +++ b/src/WixToolset.Core/Bind/ExtractEmbeddedFiles.cs @@ -15,7 +15,7 @@ namespace WixToolset.Core.Bind /// internal class ExtractEmbeddedFiles { - private Dictionary> filesWithEmbeddedFiles = new Dictionary>(); + private readonly Dictionary> filesWithEmbeddedFiles = new Dictionary>(); public IEnumerable Uris => this.filesWithEmbeddedFiles.Keys; @@ -23,28 +23,28 @@ namespace WixToolset.Core.Bind /// Adds an embedded file index to track and returns the path where the embedded file will be extracted. Duplicates will return the same extract path. /// /// Uri to file containing the embedded files. - /// Index of the embedded file to extract. - /// Path where temporary files should be placed. + /// Id of the embedded file to extract. + /// Folder where extracted files should be placed. /// The extract path for the embedded file. - public string AddEmbeddedFileIndex(Uri uri, int embeddedFileIndex, string tempPath) + public string AddEmbeddedFileToExtract(Uri uri, string embeddedFileId, string extractFolder) { // If the uri to the file that contains the embedded file does not already have embedded files // being extracted, create the dictionary to track that. if (!this.filesWithEmbeddedFiles.TryGetValue(uri, out var extracts)) { - extracts = new SortedList(); + extracts = new SortedList(StringComparer.OrdinalIgnoreCase); this.filesWithEmbeddedFiles.Add(uri, extracts); } // If the embedded file is not already tracked in the dictionary of extracts, add it. - if (!extracts.TryGetValue(embeddedFileIndex, out var extractPath)) + if (!extracts.TryGetValue(embeddedFileId, out var extractPath)) { - string localFileNameWithoutExtension = Path.GetFileNameWithoutExtension(uri.LocalPath); - string unique = this.HashUri(uri.AbsoluteUri); - string extractedName = String.Format(CultureInfo.InvariantCulture, @"{0}_{1}\{2}", localFileNameWithoutExtension, unique, embeddedFileIndex); + var localFileNameWithoutExtension = Path.GetFileNameWithoutExtension(uri.LocalPath); + var unique = this.HashUri(uri.AbsoluteUri); + var extractedName = String.Format(CultureInfo.InvariantCulture, @"{0}_{1}\{2}", localFileNameWithoutExtension, unique, embeddedFileId); - extractPath = Path.Combine(tempPath, extractedName); - extracts.Add(embeddedFileIndex, extractPath); + extractPath = Path.GetFullPath(Path.Combine(extractFolder, extractedName)); + extracts.Add(embeddedFileId, extractPath); } return extractPath; @@ -52,35 +52,39 @@ namespace WixToolset.Core.Bind public IEnumerable GetExpectedEmbeddedFiles() { + var files = new List(); + foreach (var uriWithExtracts in this.filesWithEmbeddedFiles) { foreach (var extracts in uriWithExtracts.Value) { - yield return new ExpectedExtractFile + files.Add(new ExpectedExtractFile { Uri = uriWithExtracts.Key, - EmbeddedFileIndex = extracts.Key, + EmbeddedFileId = extracts.Key, OutputPath = extracts.Value, - }; + }); } } + + return files; } public IEnumerable GetExtractFilesForUri(Uri uri) { if (!this.filesWithEmbeddedFiles.TryGetValue(uri, out var extracts)) { - extracts = new SortedList(); + extracts = new SortedList(StringComparer.OrdinalIgnoreCase); } - return extracts.Select(e => new ExpectedExtractFile() { Uri = uri, EmbeddedFileIndex = e.Key, OutputPath = e.Value }); + return extracts.Select(e => new ExpectedExtractFile { Uri = uri, EmbeddedFileId = e.Key, OutputPath = e.Value }); } private string HashUri(string uri) { using (SHA1 sha1 = new SHA1CryptoServiceProvider()) { - byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(uri)); + var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(uri)); return Convert.ToBase64String(hash).TrimEnd('=').Replace('+', '-').Replace('/', '_'); } } diff --git a/src/WixToolset.Core/Bind/ExtractEmbeddedFilesCommand.cs b/src/WixToolset.Core/Bind/ExtractEmbeddedFilesCommand.cs index d82609db..683c3c50 100644 --- a/src/WixToolset.Core/Bind/ExtractEmbeddedFilesCommand.cs +++ b/src/WixToolset.Core/Bind/ExtractEmbeddedFilesCommand.cs @@ -2,10 +2,9 @@ namespace WixToolset.Core.Bind { + using System; using System.Collections.Generic; - using System.IO; using System.Linq; - using System.Reflection; using WixToolset.Data; using WixToolset.Extensibility.Data; @@ -26,41 +25,18 @@ namespace WixToolset.Core.Bind { var baseUri = expectedEmbeddedFileByUri.Key; - Stream stream = null; - try + using (var wixout = WixOutput.Read(baseUri)) { - // If the embedded files are stored in an assembly resource stream (usually - // a .wixlib embedded in a WixExtension). - if ("embeddedresource" == baseUri.Scheme) - { - var assemblyPath = Path.GetFullPath(baseUri.LocalPath); - var resourceName = baseUri.Fragment.TrimStart('#'); - - var assembly = Assembly.LoadFile(assemblyPath); - stream = assembly.GetManifestResourceStream(resourceName); - } - else // normal file (usually a binary .wixlib on disk). - { - stream = File.OpenRead(baseUri.LocalPath); - } + var uniqueIds = new SortedSet(StringComparer.OrdinalIgnoreCase); - using (var fs = FileStructure.Read(stream)) + foreach (var embeddedFile in expectedEmbeddedFileByUri) { - var uniqueIndicies = new SortedSet(); - - foreach (var embeddedFile in expectedEmbeddedFileByUri) + if (uniqueIds.Add(embeddedFile.EmbeddedFileId)) { - if (uniqueIndicies.Add(embeddedFile.EmbeddedFileIndex)) - { - fs.ExtractEmbeddedFile(embeddedFile.EmbeddedFileIndex, embeddedFile.OutputPath); - } + wixout.ExtractEmbeddedFile(embeddedFile.EmbeddedFileId, embeddedFile.OutputPath); } } } - finally - { - stream?.Close(); - } } } } diff --git a/src/WixToolset.Core/Bind/ResolveFieldsCommand.cs b/src/WixToolset.Core/Bind/ResolveFieldsCommand.cs index 2c213402..19a26915 100644 --- a/src/WixToolset.Core/Bind/ResolveFieldsCommand.cs +++ b/src/WixToolset.Core/Bind/ResolveFieldsCommand.cs @@ -98,9 +98,9 @@ namespace WixToolset.Core.Bind #endif // File is embedded and path to it was not modified above. - if (objectField.EmbeddedFileIndex.HasValue && isDefault) + if (isDefault && objectField.Embed) { - var extractPath = this.FilesWithEmbeddedFiles.AddEmbeddedFileIndex(objectField.BaseUri, objectField.EmbeddedFileIndex.Value, this.IntermediateFolder); + var extractPath = this.FilesWithEmbeddedFiles.AddEmbeddedFileToExtract(objectField.BaseUri, objectField.Path, this.IntermediateFolder); // Set the path to the embedded file once where it will be extracted. field.Set(extractPath); -- cgit v1.2.3-55-g6feb