| 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.ExtensibilityServices |
| 4 | { |
| 5 | using System; |
| 6 | using System.Diagnostics; |
| 7 | using System.IO; |
| 8 | using WixToolset.Core.Burn.Bundles; |
| 9 | using WixToolset.Core.Burn.Interfaces; |
| 10 | using WixToolset.Data.Symbols; |
| 11 | |
| 12 | internal class PayloadHarvester : IPayloadHarvester |
| 13 | { |
| 14 | private static readonly Version EmptyVersion = new Version(0, 0, 0, 0); |
| 15 | |
| 16 | /// <inheritdoc /> |
| 17 | public bool HarvestStandardInformation(WixBundlePayloadSymbol payload) |
| 18 | { |
| 19 | var filePath = payload.SourceFile?.Path; |
| 20 | |
| 21 | if (String.IsNullOrEmpty(filePath)) |
| 22 | { |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | this.UpdatePayloadFileInformation(payload, filePath); |
| 27 | |
| 28 | this.UpdatePayloadVersionInformation(payload, filePath); |
| 29 | |
| 30 | return true; |
| 31 | } |
| 32 | |
| 33 | private void UpdatePayloadFileInformation(WixBundlePayloadSymbol payload, string filePath) |
| 34 | { |
| 35 | var fileInfo = new FileInfo(filePath); |
| 36 | |
| 37 | if (null != fileInfo) |
| 38 | { |
| 39 | payload.FileSize = fileInfo.Length; |
| 40 | |
| 41 | payload.Hash = BundleHashAlgorithm.Hash(fileInfo); |
| 42 | } |
| 43 | else |
| 44 | { |
| 45 | payload.FileSize = 0; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | private void UpdatePayloadVersionInformation(WixBundlePayloadSymbol payload, string filePath) |
| 50 | { |
| 51 | var versionInfo = FileVersionInfo.GetVersionInfo(filePath); |
| 52 | |
| 53 | if (null != versionInfo) |
| 54 | { |
| 55 | var version = versionInfo.ProductVersion; |
| 56 | |
| 57 | if (String.IsNullOrEmpty(version)) |
| 58 | { |
| 59 | version = versionInfo.FileVersion; |
| 60 | } |
| 61 | |
| 62 | if (String.IsNullOrEmpty(version)) |
| 63 | { |
| 64 | // Fallback to fixed version info block for the file. |
| 65 | var fixedVersion = new Version(versionInfo.ProductMajorPart, versionInfo.ProductMinorPart, versionInfo.ProductBuildPart, versionInfo.ProductPrivatePart); |
| 66 | |
| 67 | if (PayloadHarvester.EmptyVersion != fixedVersion) |
| 68 | { |
| 69 | version = fixedVersion.ToString(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | payload.Description = versionInfo.FileDescription; |
| 74 | payload.DisplayName = versionInfo.ProductName; |
| 75 | payload.Version = version; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |