@joebigelow / wix / commits / 311dbab6

Add overloads to WindowsInstallerData.Load for table definitions.

Sean Hall committed May 29, 2020 at 11:44 UTC 311dbab658184e603953791a075c776456226b95
5 files changed +94 -14
src/WixToolset.Data/WindowsInstaller/SubStorage.cs
+3 -2
@@ -36,8 +36,9 @@ namespace WixToolset.Data.WindowsInstaller
36 /// Creates a SubStorage from the XmlReader.
37 /// </summary>
38 /// <param name="reader">Reader to get data from.</param>
39 + /// <param name="tableDefinitions">Table definitions to use for strongly-typed rows.</param>
40 /// <returns>New SubStorage object.</returns>
40 - internal static SubStorage Read(XmlReader reader)
41 + internal static SubStorage Read(XmlReader reader, TableDefinitionCollection tableDefinitions)
42 {
43 if (reader.LocalName != "subStorage")
44 {
@@ -71,7 +72,7 @@ namespace WixToolset.Data.WindowsInstaller
72 switch (reader.LocalName)
73 {
74 case WindowsInstallerData.XmlElementName:
74 - data = WindowsInstallerData.Read(reader, true);
75 + data = WindowsInstallerData.Read(reader, tableDefinitions, true);
76 break;
77 default:
78 throw new XmlException();
src/WixToolset.Data/WindowsInstaller/TableDefinition.cs
+13 -2
@@ -156,12 +156,16 @@ namespace WixToolset.Data.WindowsInstaller
156 /// Parses table definition from xml reader.
157 /// </summary>
158 /// <param name="reader">Reader to get data from.</param>
159 + /// <param name="tableDefinitions">Table definitions to use for strongly-typed rows.</param>
160 /// <returns>The TableDefintion represented by the Xml.</returns>
160 - internal static TableDefinition Read(XmlReader reader)
161 + internal static TableDefinition Read(XmlReader reader, TableDefinitionCollection tableDefinitions)
162 {
163 var empty = reader.IsEmptyElement;
164 string name = null;
165 + IntermediateTupleDefinition tupleDefinition = null;
166 var unreal = false;
167 + var tupleIdIsPrimaryKey = false;
168 + Type strongRowType = null;
169
170 while (reader.MoveToNextAttribute())
171 {
@@ -181,6 +185,13 @@ namespace WixToolset.Data.WindowsInstaller
185 throw new XmlException();
186 }
187
188 + if (tableDefinitions.TryGet(name, out var tableDefinition))
189 + {
190 + tupleDefinition = tableDefinition.TupleDefinition;
191 + tupleIdIsPrimaryKey = tableDefinition.TupleIdIsPrimaryKey;
192 + strongRowType = tableDefinition.StrongRowType;
193 + }
194 +
195 var columns = new List<ColumnDefinition>();
196 var hasPrimaryKeyColumn = false;
197
@@ -226,7 +237,7 @@ namespace WixToolset.Data.WindowsInstaller
237 }
238 }
239
229 - return new TableDefinition(name, null, columns.ToArray(), unreal);
240 + return new TableDefinition(name, tupleDefinition, columns.ToArray(), unreal, tupleIdIsPrimaryKey, strongRowType);
241 }
242
243 /// <summary>
src/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs
+3 -2
@@ -132,8 +132,9 @@ namespace WixToolset.Data.WindowsInstaller
132 /// Loads a collection of table definitions from a XmlReader in memory.
133 /// </summary>
134 /// <param name="reader">Reader to get data from.</param>
135 + /// <param name="tableDefinitions">Table definitions to use for strongly-typed rows.</param>
136 /// <returns>The TableDefinitionCollection represented by the xml.</returns>
136 - internal static TableDefinitionCollection Read(XmlReader reader)
137 + internal static TableDefinitionCollection Read(XmlReader reader, TableDefinitionCollection tableDefinitions)
138 {
139 if ("tableDefinitions" != reader.LocalName)
140 {
@@ -160,7 +161,7 @@ namespace WixToolset.Data.WindowsInstaller
161 switch (reader.LocalName)
162 {
163 case "tableDefinition":
163 - tableDefinitionCollection.Add(TableDefinition.Read(reader));
164 + tableDefinitionCollection.Add(TableDefinition.Read(reader, tableDefinitions));
165 break;
166 default:
167 throw new XmlException();
src/WixToolset.Data/WindowsInstaller/WindowsInstallerData.cs
+35 -8
@@ -112,10 +112,23 @@ namespace WixToolset.Data.WindowsInstaller
112 /// <param name="suppressVersionCheck">Suppresses wix.dll version mismatch check.</param>
113 /// <returns>Output object.</returns>
114 public static WindowsInstallerData Load(string path, bool suppressVersionCheck = false)
115 + {
116 + var tableDefinitions = new TableDefinitionCollection(WindowsInstallerTableDefinitions.All);
117 + return WindowsInstallerData.Load(path, tableDefinitions, suppressVersionCheck);
118 + }
119 +
120 + /// <summary>
121 + /// Loads an output from a path on disk.
122 + /// </summary>
123 + /// <param name="path">Path to output file saved on disk.</param>
124 + /// <param name="tableDefinitions">Table definitions to use for creating strongly-typed rows.</param>
125 + /// <param name="suppressVersionCheck">Suppresses wix.dll version mismatch check.</param>
126 + /// <returns>Output object.</returns>
127 + public static WindowsInstallerData Load(string path, TableDefinitionCollection tableDefinitions, bool suppressVersionCheck = false)
128 {
129 using (var wixOutput = WixOutput.Read(path))
130 {
118 - return WindowsInstallerData.Load(wixOutput, suppressVersionCheck);
131 + return WindowsInstallerData.Load(wixOutput, tableDefinitions, suppressVersionCheck);
132 }
133 }
134
@@ -126,6 +139,19 @@ namespace WixToolset.Data.WindowsInstaller
139 /// <param name="suppressVersionCheck">Suppresses wix.dll version mismatch check.</param>
140 /// <returns>Output object.</returns>
141 public static WindowsInstallerData Load(WixOutput wixOutput, bool suppressVersionCheck = false)
142 + {
143 + var tableDefinitions = new TableDefinitionCollection(WindowsInstallerTableDefinitions.All);
144 + return WindowsInstallerData.Load(wixOutput, tableDefinitions, suppressVersionCheck);
145 + }
146 +
147 + /// <summary>
148 + /// Loads an output from a WixOutput object.
149 + /// </summary>
150 + /// <param name="wixOutput">WixOutput object.</param>
151 + /// <param name="tableDefinitions">Table definitions to use for creating strongly-typed rows.</param>
152 + /// <param name="suppressVersionCheck">Suppresses wix.dll version mismatch check.</param>
153 + /// <returns>Output object.</returns>
154 + public static WindowsInstallerData Load(WixOutput wixOutput, TableDefinitionCollection tableDefinitions, bool suppressVersionCheck = false)
155 {
156 using (var stream = wixOutput.GetDataStream(WixOutputStreamName))
157 using (var reader = XmlReader.Create(stream, null, wixOutput.Uri.AbsoluteUri))
@@ -133,7 +159,7 @@ namespace WixToolset.Data.WindowsInstaller
159 try
160 {
161 reader.MoveToContent();
136 - return WindowsInstallerData.Read(reader, suppressVersionCheck);
162 + return WindowsInstallerData.Read(reader, tableDefinitions, suppressVersionCheck);
163 }
164 catch (XmlException xe)
165 {
@@ -146,9 +172,10 @@ namespace WixToolset.Data.WindowsInstaller
172 /// Processes an XmlReader and builds up the output object.
173 /// </summary>
174 /// <param name="reader">Reader to get data from.</param>
175 + /// <param name="tableDefinitions">Table definitions to use for creating strongly-typed rows.</param>
176 /// <param name="suppressVersionCheck">Suppresses wix.dll version mismatch check.</param>
177 /// <returns>The Output represented by the Xml.</returns>
151 - internal static WindowsInstallerData Read(XmlReader reader, bool suppressVersionCheck)
178 + internal static WindowsInstallerData Read(XmlReader reader, TableDefinitionCollection tableDefinitions, bool suppressVersionCheck)
179 {
180 if (!reader.LocalName.Equals(WindowsInstallerData.XmlElementName))
181 {
@@ -203,7 +230,7 @@ namespace WixToolset.Data.WindowsInstaller
230 }
231
232 // loop through the rest of the xml building up the Output object
206 - TableDefinitionCollection tableDefinitions = null;
233 + TableDefinitionCollection xmlTableDefinitions = null;
234 var tables = new List<Table>();
235 if (!empty)
236 {
@@ -218,17 +245,17 @@ namespace WixToolset.Data.WindowsInstaller
245 switch (reader.LocalName)
246 {
247 case "subStorage":
221 - output.SubStorages.Add(SubStorage.Read(reader));
248 + output.SubStorages.Add(SubStorage.Read(reader, tableDefinitions));
249 break;
250 case "table":
224 - if (null == tableDefinitions)
251 + if (null == xmlTableDefinitions)
252 {
253 throw new XmlException();
254 }
228 - tables.Add(Table.Read(reader, tableDefinitions));
255 + tables.Add(Table.Read(reader, xmlTableDefinitions));
256 break;
257 case "tableDefinitions":
231 - tableDefinitions = TableDefinitionCollection.Read(reader);
258 + xmlTableDefinitions = TableDefinitionCollection.Read(reader, tableDefinitions);
259 break;
260 default:
261 throw new XmlException();
src/test/WixToolsetTest.Data/SerializeFixture.cs
+40
@@ -8,8 +8,11 @@ namespace WixToolsetTest.Data
8 using WixToolset.Data;
9 using WixToolset.Data.Bind;
10 using WixToolset.Data.Tuples;
11 + using WixToolset.Data.WindowsInstaller.Rows;
12 using Xunit;
13
14 + using Wid = WixToolset.Data.WindowsInstaller;
15 +
16 public class SerializeFixture
17 {
18 [Fact]
@@ -383,5 +386,42 @@ namespace WixToolsetTest.Data
386 File.Delete(path);
387 }
388 }
389 +
390 + [Fact]
391 + public void CanSaveAndLoadWindowsInstallerData()
392 + {
393 + var sln = new SourceLineNumber("test.wxs", 1);
394 + var windowsInstallerData = new Wid.WindowsInstallerData(sln)
395 + {
396 + Type = OutputType.Product,
397 + };
398 +
399 + var fileTable = windowsInstallerData.EnsureTable(Wid.WindowsInstallerTableDefinitions.File);
400 + var fileRow = (FileRow)fileTable.CreateRow(sln);
401 + fileRow.File = "TestFile";
402 +
403 + var path = Path.GetTempFileName();
404 + try
405 + {
406 + using (var wixout = WixOutput.Create(path))
407 + {
408 + windowsInstallerData.Save(wixout);
409 + }
410 +
411 + var loaded = Wid.WindowsInstallerData.Load(path);
412 +
413 + var loadedTable = Assert.Single(loaded.Tables);
414 + Assert.Equal(Wid.WindowsInstallerTableDefinitions.File.Name, loadedTable.Name);
415 +
416 + var loadedRow = Assert.Single(loadedTable.Rows);
417 + var loadedFileRow = Assert.IsType<FileRow>(loadedRow);
418 +
419 + Assert.Equal("TestFile", loadedFileRow.File);
420 + }
421 + finally
422 + {
423 + File.Delete(path);
424 + }
425 + }
426 }
427 }