aboutsummaryrefslogtreecommitdiff
path: root/src/dtf/WixToolsetTests.Dtf.WindowsInstaller/WindowsInstallerUtils.cs
blob: 644f19888640c795f35e8430f812fd2c1b7d624b (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// 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.Dtf.Test
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using WixToolset.Dtf.WindowsInstaller;

    public class WindowsInstallerUtils
    {
        public static void InitializeProductDatabase(Database db)
        {
            InitializeProductDatabase(db, false);
        }

        public static void InitializeProductDatabase(Database db, bool sixtyFourBit)
        {
            db.SummaryInfo.CodePage = (short) Encoding.Default.CodePage;
            db.SummaryInfo.Title = "Windows Installer Test";
            db.SummaryInfo.Subject = db.SummaryInfo.Title;
            db.SummaryInfo.Author = typeof(WindowsInstallerUtils).Assembly.FullName;
            db.SummaryInfo.CreatingApp = db.SummaryInfo.Author;
            db.SummaryInfo.Comments = typeof(WindowsInstallerUtils).FullName + ".CreateBasicDatabase()";
            db.SummaryInfo.Keywords = "Installer,MSI,Database";
            db.SummaryInfo.PageCount = 300;
            db.SummaryInfo.WordCount = 0;
            db.SummaryInfo.RevisionNumber = Guid.NewGuid().ToString("B").ToUpper();
            db.SummaryInfo.Template = (sixtyFourBit ? "x64" : "Intel") + ";0";

            foreach (TableInfo tableInfo in Schema.Tables)
            {
                db.Execute(tableInfo.SqlCreateString);
            }

            db.Execute("INSERT INTO `Directory` (`Directory`, `DefaultDir`) VALUES ('TARGETDIR', 'SourceDir')");
            db.Execute("INSERT INTO `Directory` (`Directory`, `Directory_Parent`, `DefaultDir`) VALUES ('ProgramFilesFolder', 'TARGETDIR', '.')");

            foreach (Action action in Sequence.InstallExecute)
            {
                db.Execute("INSERT INTO `InstallExecuteSequence` (`Action`, `Sequence`) VALUES ('{0}', {1})",
                    action.Name, action.Sequence);
            }
        }

        public const string UpgradeCode = "{05955FE8-005F-4695-A81F-D559338065BB}";

        public static void CreateTestProduct(Database db)
        {
            Guid productGuid = Guid.NewGuid();

            string[] properties = new string[]
            {
                "ProductCode", productGuid.ToString("B").ToUpper(),
                "UpgradeCode", UpgradeCode,
                "ProductName", "Windows Installer Test Product " + productGuid.ToString("P").ToUpper(),
                "ProductVersion", "1.0.0.0000",
            };

            using (View view = db.OpenView("INSERT INTO `Property` (`Property`, `Value`) VALUES (?, ?)"))
            {
                using (Record rec = new Record(2))
                {
                    for (int i = 0; i < properties.Length; i += 2)
                    {
                        rec[1] = properties[i];
                        rec[2] = properties[i + 1];
                        view.Execute(rec);
                    }
                }
            }

            int randomId = new Random().Next(10000);
            string productDir = "TestDir" + randomId;
            db.Execute(
                "INSERT INTO `Directory` (`Directory`, `Directory_Parent`, `DefaultDir`) " +
                "VALUES ('TestDir', 'ProgramFilesFolder', 'TestDir|{0}:.')", productDir);

            string compId = Guid.NewGuid().ToString("B").ToUpper();
            db.Execute(
                "INSERT INTO `Component` " +
                    "(`Component`, `ComponentId`, `Directory_`, `Attributes`, `KeyPath`) " +
                    "VALUES ('{0}', '{1}', '{2}', {3}, '{4}')",
                "TestRegComp1",
                compId,
                "TestDir",
                (int) ComponentAttributes.RegistryKeyPath,
                "TestReg1");

            string productReg = "TestReg" + randomId;
            db.Execute(
                "INSERT INTO `Registry` (`Registry`, `Root`, `Key`, `Component_`) VALUES ('{0}', {1}, '{2}', '{3}')",
                "TestReg1",
                -1,
                @"Software\Microsoft\Windows Installer Test\" + productReg,
                "TestRegComp1");

            db.Execute(
                "INSERT INTO `Feature` (`Feature`, `Title`, `Level`, `Attributes`) VALUES ('{0}', '{1}', {2}, {3})",
                "TestFeature1",
                "Test Feature 1",
                1,
                (int) FeatureAttributes.None);

            db.Execute(
                "INSERT INTO `FeatureComponents` (`Feature_`, `Component_`) VALUES ('{0}', '{1}')",
                "TestFeature1",
                "TestRegComp1");
        }

        public static void AddFeature(Database db, string featureName)
        {
            db.Execute(
                "INSERT INTO `Feature` (`Feature`, `Title`, `Level`, `Attributes`) VALUES ('{0}', '{1}', {2}, {3})",
                featureName,
                featureName,
                1,
                (int) FeatureAttributes.None);
        }

        public static void AddRegistryComponent(Database db,
            string featureName, string compName, string compId,
            string keyName, string keyValueName, string value)
        {
            db.Execute(
                "INSERT INTO `Component` " +
                    "(`Component`, `ComponentId`, `Directory_`, `Attributes`, `KeyPath`) " +
                    "VALUES ('{0}', '{1}', '{2}', {3}, '{4}')",
                compName,
                compId,
                "TestDir",
                (int) ComponentAttributes.RegistryKeyPath,
                compName + "Reg1");
            db.Execute(
                "INSERT INTO `Registry` (`Registry`, `Root`, `Key`, `Name`, `Value`, `Component_`) VALUES ('{0}', {1}, '{2}', '{3}', '{4}', '{5}')",
                compName + "Reg1",
                -1,
                @"Software\Microsoft\Windows Installer Test\" + keyName,
                keyValueName,
                value,
                compName);
            db.Execute(
                "INSERT INTO `FeatureComponents` (`Feature_`, `Component_`) VALUES ('{0}', '{1}')",
                featureName,
                compName);
        }

        public static void AddFileComponent(Database db,
            string featureName, string compName, string compId,
            string fileKey, string fileName)
        {
            db.Execute(
                "INSERT INTO `Component` " +
                    "(`Component`, `ComponentId`, `Directory_`, `Attributes`, `KeyPath`) " +
                    "VALUES ('{0}', '{1}', '{2}', {3}, '{4}')",
                compName,
                compId,
                "TestDir",
                (int) ComponentAttributes.None,
                fileKey);
            db.Execute(
                "INSERT INTO `File` " +
                    "(`File`, `Component_`, `FileName`, `FileSize`, `Attributes`, `Sequence`) " +
                    "VALUES ('{0}', '{1}', '{2}', 1, 0, 1)",
                fileKey,
                compName,
                fileName);
            db.Execute(
                "INSERT INTO `FeatureComponents` (`Feature_`, `Component_`) VALUES ('{0}', '{1}')",
                featureName,
                compName);
        }
    }
}