aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data.WindowsInstaller/Output.cs
blob: 26772872819fb649aef1d45c1779f30157d8d89d (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// 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.Data
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Xml;

    /// <summary>
    /// Output is generated by the linker.
    /// </summary>
    public sealed class Output
    {
        public const string XmlNamespaceUri = "http://wixtoolset.org/schemas/v4/wixout";
        private static readonly Version CurrentVersion = new Version("4.0.0.0");

        /// <summary>
        /// Creates a new empty output object.
        /// </summary>
        /// <param name="sourceLineNumbers">The source line information for the output.</param>
        public Output(SourceLineNumber sourceLineNumbers)
        {
            this.SourceLineNumbers = sourceLineNumbers;
            this.SubStorages = new List<SubStorage>();
            this.Tables = new TableIndexedCollection();
        }

        /// <summary>
        /// Gets the type of the output.
        /// </summary>
        /// <value>Type of the output.</value>
        public OutputType Type { get; set; }

        /// <summary>
        /// Gets or sets the codepage for this output.
        /// </summary>
        /// <value>Codepage of the output.</value>
        public int Codepage { get; set; }

        /// <summary>
        /// Gets the source line information for this output.
        /// </summary>
        /// <value>The source line information for this output.</value>
        public SourceLineNumber SourceLineNumbers { get; private set; }

        /// <summary>
        /// Gets the substorages in this output.
        /// </summary>
        /// <value>The substorages in this output.</value>
        public ICollection<SubStorage> SubStorages { get; private set; }

        /// <summary>
        /// Gets the tables contained in this output.
        /// </summary>
        /// <value>Collection of tables.</value>
        public TableIndexedCollection Tables { get; private set; }

        /// <summary>
        /// Gets the output type corresponding to a given output filename extension.
        /// </summary>
        /// <param name="extension">Case-insensitive output filename extension.</param>
        /// <returns>Output type for the extension.</returns>
        public static OutputType GetOutputType(string extension)
        {
            if (extension.Equals(".exe", StringComparison.OrdinalIgnoreCase))
            {
                return OutputType.Bundle;
            }
            if (extension.Equals(".msi", StringComparison.OrdinalIgnoreCase))
            {
                return OutputType.Product;
            }
            else if (extension.Equals(".msm", StringComparison.OrdinalIgnoreCase))
            {
                return OutputType.Module;
            }
            else if (extension.Equals(".msp", StringComparison.OrdinalIgnoreCase))
            {
                return OutputType.Patch;
            }
            else if (extension.Equals(".mst", StringComparison.OrdinalIgnoreCase))
            {
                return OutputType.Transform;
            }
            else if (extension.Equals(".pcp", StringComparison.OrdinalIgnoreCase))
            {
                return OutputType.PatchCreation;
            }
            else
            {
                return OutputType.Unknown;
            }
        }

        /// <summary>
        /// Gets the filename extension corresponding to a given output type.
        /// </summary>
        /// <param name="type">One of the WiX output types.</param>
        /// <returns>Filename extension for the output type, for example ".msi".</returns>
        public static string GetExtension(OutputType type)
        {
            switch (type)
            {
                case OutputType.Bundle:
                    return ".exe";
                case OutputType.Product:
                    return ".msi";
                case OutputType.Module:
                    return ".msm";
                case OutputType.Patch:
                    return ".msp";
                case OutputType.Transform:
                    return ".mst";
                case OutputType.PatchCreation:
                    return ".pcp";
                default:
                    return ".wix";
            }
        }

        /// <summary>
        /// Loads an output from a path on disk.
        /// </summary>
        /// <param name="path">Path to output file saved on disk.</param>
        /// <param name="suppressVersionCheck">Suppresses wix.dll version mismatch check.</param>
        /// <returns>Output object.</returns>
        public static Output Load(string path, bool suppressVersionCheck)
        {
            using (FileStream stream = File.OpenRead(path))
            using (FileStructure fs = FileStructure.Read(stream))
            {
                if (FileFormat.Wixout != fs.FileFormat)
                {
                    throw new WixUnexpectedFileFormatException(path, FileFormat.Wixout, fs.FileFormat);
                }

                Uri uri = new Uri(Path.GetFullPath(path));
                using (XmlReader reader = XmlReader.Create(fs.GetDataStream(), null, uri.AbsoluteUri))
                {
                    try
                    {
                        reader.MoveToContent();
                        return Output.Read(reader, suppressVersionCheck);
                    }
                    catch (XmlException xe)
                    {
                        throw new WixCorruptFileException(path, fs.FileFormat, xe);
                    }
                }
            }
        }

        /// <summary>
        /// Saves an output to a path on disk.
        /// </summary>
        /// <param name="path">Path to save output file to on disk.</param>
        public void Save(string path)
        {
            Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path)));

            using (FileStream stream = File.Create(path))
            using (FileStructure fs = FileStructure.Create(stream, FileFormat.Wixout, null))
            using (XmlWriter writer = XmlWriter.Create(fs.GetDataStream()))
            {
                writer.WriteStartDocument();
                this.Write(writer);
                writer.WriteEndDocument();
            }
        }

        /// <summary>
        /// Processes an XmlReader and builds up the output object.
        /// </summary>
        /// <param name="reader">Reader to get data from.</param>
        /// <param name="suppressVersionCheck">Suppresses wix.dll version mismatch check.</param>
        /// <returns>The Output represented by the Xml.</returns>
        internal static Output Read(XmlReader reader, bool suppressVersionCheck)
        {
            if (!reader.LocalName.Equals("wixOutput"))
            {
                throw new XmlException();
            }

            bool empty = reader.IsEmptyElement;
            Output output = new Output(SourceLineNumber.CreateFromUri(reader.BaseURI));
            Version version = null;

            while (reader.MoveToNextAttribute())
            {
                switch (reader.LocalName)
                {
                    case "codepage":
                        output.Codepage = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture.NumberFormat);
                        break;
                    case "type":
                        switch (reader.Value)
                        {
                            case "Bundle":
                                output.Type = OutputType.Bundle;
                                break;
                            case "Module":
                                output.Type = OutputType.Module;
                                break;
                            case "Patch":
                                output.Type = OutputType.Patch;
                                break;
                            case "PatchCreation":
                                output.Type = OutputType.PatchCreation;
                                break;
                            case "Product":
                                output.Type = OutputType.Product;
                                break;
                            case "Transform":
                                output.Type = OutputType.Transform;
                                break;
                            default:
                                throw new XmlException();
                        }
                        break;
                    case "version":
                        version = new Version(reader.Value);
                        break;
                }
            }

            if (!suppressVersionCheck && null != version && !Output.CurrentVersion.Equals(version))
            {
                throw new WixException(WixDataErrors.VersionMismatch(SourceLineNumber.CreateFromUri(reader.BaseURI), "wixOutput", version.ToString(), Output.CurrentVersion.ToString()));
            }

            // loop through the rest of the xml building up the Output object
            TableDefinitionCollection tableDefinitions = null;
            List<Table> tables = new List<Table>();
            if (!empty)
            {
                bool done = false;

                // loop through all the fields in a row
                while (!done && reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            switch (reader.LocalName)
                            {
                                case "subStorage":
                                    output.SubStorages.Add(SubStorage.Read(reader));
                                    break;
                                case "table":
                                    if (null == tableDefinitions)
                                    {
                                        throw new XmlException();
                                    }
                                    tables.Add(Table.Read(reader, tableDefinitions));
                                    break;
                                case "tableDefinitions":
                                    tableDefinitions = TableDefinitionCollection.Read(reader);
                                    break;
                                default:
                                    throw new XmlException();
                            }
                            break;
                        case XmlNodeType.EndElement:
                            done = true;
                            break;
                    }
                }

                if (!done)
                {
                    throw new XmlException();
                }
            }

            output.Tables = new TableIndexedCollection(tables);
            return output;
        }

        /// <summary>
        /// Ensure this output contains a particular table.
        /// </summary>
        /// <param name="tableDefinition">Definition of the table that should exist.</param>
        /// <param name="section">Optional section to use for the table. If one is not provided, the entry section will be used.</param>
        /// <returns>The table in this output.</returns>
        public Table EnsureTable(TableDefinition tableDefinition)
        {
            if (!this.Tables.TryGetTable(tableDefinition.Name, out Table table))
            {
                table = new Table(tableDefinition);
                this.Tables.Add(table);
            }

            return table;
        }

        /// <summary>
        /// Persists an output in an XML format.
        /// </summary>
        /// <param name="writer">XmlWriter where the Output should persist itself as XML.</param>
        internal void Write(XmlWriter writer)
        {
            writer.WriteStartElement("wixOutput", XmlNamespaceUri);

            writer.WriteAttributeString("type", this.Type.ToString());

            if (0 != this.Codepage)
            {
                writer.WriteAttributeString("codepage", this.Codepage.ToString(CultureInfo.InvariantCulture));
            }

            writer.WriteAttributeString("version", Output.CurrentVersion.ToString());

            // Collect all the table definitions and write them.
            TableDefinitionCollection tableDefinitions = new TableDefinitionCollection();
            foreach (Table table in this.Tables)
            {
                tableDefinitions.Add(table.Definition);
            }
            tableDefinitions.Write(writer);

            foreach (Table table in this.Tables.OrderBy(t => t.Name))
            {
                table.Write(writer);
            }

            foreach (SubStorage subStorage in this.SubStorages)
            {
                subStorage.Write(writer);
            }

            writer.WriteEndElement();
        }
    }
}