aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.Burn/Bundles/CreateBundleExtensionManifestCommand.cs
blob: 74ccd89583666b0d4d408e001c280feff9827048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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.Burn.Bundles
{
    using System;
    using System.Globalization;
    using System.IO;
    using System.Text;
    using System.Xml;
    using WixToolset.Data;
    using WixToolset.Data.Burn;
    using WixToolset.Data.Symbols;

    internal class CreateBundleExtensionManifestCommand
    {
        public CreateBundleExtensionManifestCommand(IntermediateSection section, WixBundleSymbol bundleSymbol, int lastUXPayloadIndex, string intermediateFolder, IInternalBurnBackendHelper internalBurnBackendHelper)
        {
            this.Section = section;
            this.BundleSymbol = bundleSymbol;
            this.LastUXPayloadIndex = lastUXPayloadIndex;
            this.IntermediateFolder = intermediateFolder;
            this.InternalBurnBackendHelper = internalBurnBackendHelper;
        }

        private IntermediateSection Section { get; }

        private WixBundleSymbol BundleSymbol { get; }

        private IInternalBurnBackendHelper InternalBurnBackendHelper { get; }

        private int LastUXPayloadIndex { get; }

        private string IntermediateFolder { get; }

        public WixBundlePayloadSymbol BundleExtensionManifestPayloadRow { get; private set; }

        public string OutputPath { get; private set; }

        public void Execute()
        {
            this.OutputPath = this.CreateBundleExtensionManifest();

            this.BundleExtensionManifestPayloadRow = this.CreateBundleExtensionManifestPayloadRow(this.OutputPath);
        }

        private string CreateBundleExtensionManifest()
        {
            var path = Path.Combine(this.IntermediateFolder, "wix-bextdata.xml");

            Directory.CreateDirectory(Path.GetDirectoryName(path));

            using (var writer = new XmlTextWriter(path, Encoding.Unicode))
            {
                writer.Formatting = Formatting.Indented;
                writer.WriteStartDocument();
                writer.WriteStartElement("BundleExtensionData", BurnCommon.BundleExtensionDataNamespace);

                this.InternalBurnBackendHelper.WriteBundleExtensionData(writer);

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }

            return path;
        }

        private WixBundlePayloadSymbol CreateBundleExtensionManifestPayloadRow(string bextManifestPath)
        {
            var generatedId = Common.GenerateIdentifier("ux", BurnCommon.BundleExtensionDataFileName);

            var symbol = this.Section.AddSymbol(new WixBundlePayloadSymbol(this.BundleSymbol.SourceLineNumbers, new Identifier(AccessModifier.Private, generatedId))
            {
                Name = BurnCommon.BundleExtensionDataFileName,
                SourceFile = new IntermediateFieldPathValue { Path = bextManifestPath },
                Compressed = true,
                UnresolvedSourceFile = bextManifestPath,
                ContainerRef = BurnConstants.BurnUXContainerName,
                EmbeddedId = String.Format(CultureInfo.InvariantCulture, BurnCommon.BurnUXContainerEmbeddedIdFormat, this.LastUXPayloadIndex),
                Packaging = PackagingType.Embedded,
            });

            var fileInfo = new FileInfo(bextManifestPath);

            symbol.FileSize = (int)fileInfo.Length;

            symbol.Hash = BundleHashAlgorithm.Hash(fileInfo);

            return symbol;
        }
    }
}