aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.WindowsInstaller/Bind/CreatePatchTransformsCommand.cs
blob: 854d973e61ae0394c28dedbdf4cf2a268618fbcc (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
// 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.Bind
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using WixToolset.Core.WindowsInstaller.Msi;
    using WixToolset.Core.WindowsInstaller.Unbind;
    using WixToolset.Data;
    using WixToolset.Data.Tuples;
    using WixToolset.Data.WindowsInstaller;
    using WixToolset.Extensibility.Services;

    internal class CreatePatchTransformsCommand
    {
        public CreatePatchTransformsCommand(IMessaging messaging, Intermediate intermediate, string intermediateFolder)
        {
            this.Messaging = messaging;
            this.Intermediate = intermediate;
            this.IntermediateFolder = intermediateFolder;
        }

        private IMessaging Messaging { get; }

        private Intermediate Intermediate { get; }

        private string IntermediateFolder { get; }

        public IEnumerable<PatchTransform> PatchTransforms { get; private set; }

        public IEnumerable<PatchTransform> Execute()
        {
            var patchTransforms = new List<PatchTransform>();

            var tuples = this.Intermediate.Sections.SelectMany(s => s.Tuples).OfType<WixPatchBaselineTuple>();

            foreach (var tuple in tuples)
            {
                WindowsInstallerData transform;

                if (tuple.TransformFile is null)
                {
                    var baselineData = this.GetData(tuple.BaselineFile.Path);
                    var updateData = this.GetData(tuple.UpdateFile.Path);

                    var command = new GenerateTransformCommand(this.Messaging, baselineData, updateData, false);
                    transform = command.Execute();
                }
                else
                {
                    var exportBasePath = Path.Combine(this.IntermediateFolder, "_trans"); // TODO: come up with a better path.

                    var command = new UnbindTransformCommand(this.Messaging, tuple.TransformFile.Path, exportBasePath, this.IntermediateFolder);
                    transform = command.Execute();
                }

                patchTransforms.Add(new PatchTransform(tuple.Id.Id, transform));
            }

            this.PatchTransforms = patchTransforms;

            return this.PatchTransforms;
        }

        private WindowsInstallerData GetData(string path)
        {
            var ext = Path.GetExtension(path);

            if (".msi".Equals(ext, StringComparison.OrdinalIgnoreCase))
            {
                using (var database = new Database(path, OpenDatabase.ReadOnly))
                {
                    var exportBasePath = Path.Combine(this.IntermediateFolder, "_msi"); // TODO: come up with a better path.

                    var isAdminImage = false; // TODO: need a better way to set this

                    var command = new UnbindDatabaseCommand(this.Messaging, database, path, OutputType.Product, exportBasePath, this.IntermediateFolder, isAdminImage, suppressDemodularization: true, skipSummaryInfo: true);
                    return command.Execute();
                }
            }
            else // assume .wixpdb (or .wixout)
            {
                var data = WindowsInstallerData.Load(path, true);
                return data;
            }
        }
    }
}