From 311dbab658184e603953791a075c776456226b95 Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Fri, 29 May 2020 11:44:46 +1000 Subject: Add overloads to WindowsInstallerData.Load for table definitions. --- src/WixToolset.Data/WindowsInstaller/SubStorage.cs | 5 ++- .../WindowsInstaller/TableDefinition.cs | 15 +++++++- .../WindowsInstaller/TableDefinitionCollection.cs | 5 ++- .../WindowsInstaller/WindowsInstallerData.cs | 43 ++++++++++++++++++---- src/test/WixToolsetTest.Data/SerializeFixture.cs | 40 ++++++++++++++++++++ 5 files changed, 94 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/WixToolset.Data/WindowsInstaller/SubStorage.cs b/src/WixToolset.Data/WindowsInstaller/SubStorage.cs index e24839c0..76b1b795 100644 --- a/src/WixToolset.Data/WindowsInstaller/SubStorage.cs +++ b/src/WixToolset.Data/WindowsInstaller/SubStorage.cs @@ -36,8 +36,9 @@ namespace WixToolset.Data.WindowsInstaller /// Creates a SubStorage from the XmlReader. /// /// Reader to get data from. + /// Table definitions to use for strongly-typed rows. /// New SubStorage object. - internal static SubStorage Read(XmlReader reader) + internal static SubStorage Read(XmlReader reader, TableDefinitionCollection tableDefinitions) { if (reader.LocalName != "subStorage") { @@ -71,7 +72,7 @@ namespace WixToolset.Data.WindowsInstaller switch (reader.LocalName) { case WindowsInstallerData.XmlElementName: - data = WindowsInstallerData.Read(reader, true); + data = WindowsInstallerData.Read(reader, tableDefinitions, true); break; default: throw new XmlException(); diff --git a/src/WixToolset.Data/WindowsInstaller/TableDefinition.cs b/src/WixToolset.Data/WindowsInstaller/TableDefinition.cs index 9ec37895..214544ca 100644 --- a/src/WixToolset.Data/WindowsInstaller/TableDefinition.cs +++ b/src/WixToolset.Data/WindowsInstaller/TableDefinition.cs @@ -156,12 +156,16 @@ namespace WixToolset.Data.WindowsInstaller /// Parses table definition from xml reader. /// /// Reader to get data from. + /// Table definitions to use for strongly-typed rows. /// The TableDefintion represented by the Xml. - internal static TableDefinition Read(XmlReader reader) + internal static TableDefinition Read(XmlReader reader, TableDefinitionCollection tableDefinitions) { var empty = reader.IsEmptyElement; string name = null; + IntermediateTupleDefinition tupleDefinition = null; var unreal = false; + var tupleIdIsPrimaryKey = false; + Type strongRowType = null; while (reader.MoveToNextAttribute()) { @@ -181,6 +185,13 @@ namespace WixToolset.Data.WindowsInstaller throw new XmlException(); } + if (tableDefinitions.TryGet(name, out var tableDefinition)) + { + tupleDefinition = tableDefinition.TupleDefinition; + tupleIdIsPrimaryKey = tableDefinition.TupleIdIsPrimaryKey; + strongRowType = tableDefinition.StrongRowType; + } + var columns = new List(); var hasPrimaryKeyColumn = false; @@ -226,7 +237,7 @@ namespace WixToolset.Data.WindowsInstaller } } - return new TableDefinition(name, null, columns.ToArray(), unreal); + return new TableDefinition(name, tupleDefinition, columns.ToArray(), unreal, tupleIdIsPrimaryKey, strongRowType); } /// diff --git a/src/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs b/src/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs index 91385d74..fcc2b1f6 100644 --- a/src/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs +++ b/src/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs @@ -132,8 +132,9 @@ namespace WixToolset.Data.WindowsInstaller /// Loads a collection of table definitions from a XmlReader in memory. /// /// Reader to get data from. + /// Table definitions to use for strongly-typed rows. /// The TableDefinitionCollection represented by the xml. - internal static TableDefinitionCollection Read(XmlReader reader) + internal static TableDefinitionCollection Read(XmlReader reader, TableDefinitionCollection tableDefinitions) { if ("tableDefinitions" != reader.LocalName) { @@ -160,7 +161,7 @@ namespace WixToolset.Data.WindowsInstaller switch (reader.LocalName) { case "tableDefinition": - tableDefinitionCollection.Add(TableDefinition.Read(reader)); + tableDefinitionCollection.Add(TableDefinition.Read(reader, tableDefinitions)); break; default: throw new XmlException(); diff --git a/src/WixToolset.Data/WindowsInstaller/WindowsInstallerData.cs b/src/WixToolset.Data/WindowsInstaller/WindowsInstallerData.cs index e30be598..67a074c6 100644 --- a/src/WixToolset.Data/WindowsInstaller/WindowsInstallerData.cs +++ b/src/WixToolset.Data/WindowsInstaller/WindowsInstallerData.cs @@ -112,10 +112,23 @@ namespace WixToolset.Data.WindowsInstaller /// Suppresses wix.dll version mismatch check. /// Output object. public static WindowsInstallerData Load(string path, bool suppressVersionCheck = false) + { + var tableDefinitions = new TableDefinitionCollection(WindowsInstallerTableDefinitions.All); + return WindowsInstallerData.Load(path, tableDefinitions, suppressVersionCheck); + } + + /// + /// Loads an output from a path on disk. + /// + /// Path to output file saved on disk. + /// Table definitions to use for creating strongly-typed rows. + /// Suppresses wix.dll version mismatch check. + /// Output object. + public static WindowsInstallerData Load(string path, TableDefinitionCollection tableDefinitions, bool suppressVersionCheck = false) { using (var wixOutput = WixOutput.Read(path)) { - return WindowsInstallerData.Load(wixOutput, suppressVersionCheck); + return WindowsInstallerData.Load(wixOutput, tableDefinitions, suppressVersionCheck); } } @@ -126,6 +139,19 @@ namespace WixToolset.Data.WindowsInstaller /// Suppresses wix.dll version mismatch check. /// Output object. public static WindowsInstallerData Load(WixOutput wixOutput, bool suppressVersionCheck = false) + { + var tableDefinitions = new TableDefinitionCollection(WindowsInstallerTableDefinitions.All); + return WindowsInstallerData.Load(wixOutput, tableDefinitions, suppressVersionCheck); + } + + /// + /// Loads an output from a WixOutput object. + /// + /// WixOutput object. + /// Table definitions to use for creating strongly-typed rows. + /// Suppresses wix.dll version mismatch check. + /// Output object. + public static WindowsInstallerData Load(WixOutput wixOutput, TableDefinitionCollection tableDefinitions, bool suppressVersionCheck = false) { using (var stream = wixOutput.GetDataStream(WixOutputStreamName)) using (var reader = XmlReader.Create(stream, null, wixOutput.Uri.AbsoluteUri)) @@ -133,7 +159,7 @@ namespace WixToolset.Data.WindowsInstaller try { reader.MoveToContent(); - return WindowsInstallerData.Read(reader, suppressVersionCheck); + return WindowsInstallerData.Read(reader, tableDefinitions, suppressVersionCheck); } catch (XmlException xe) { @@ -146,9 +172,10 @@ namespace WixToolset.Data.WindowsInstaller /// Processes an XmlReader and builds up the output object. /// /// Reader to get data from. + /// Table definitions to use for creating strongly-typed rows. /// Suppresses wix.dll version mismatch check. /// The Output represented by the Xml. - internal static WindowsInstallerData Read(XmlReader reader, bool suppressVersionCheck) + internal static WindowsInstallerData Read(XmlReader reader, TableDefinitionCollection tableDefinitions, bool suppressVersionCheck) { if (!reader.LocalName.Equals(WindowsInstallerData.XmlElementName)) { @@ -203,7 +230,7 @@ namespace WixToolset.Data.WindowsInstaller } // loop through the rest of the xml building up the Output object - TableDefinitionCollection tableDefinitions = null; + TableDefinitionCollection xmlTableDefinitions = null; var tables = new List(); if (!empty) { @@ -218,17 +245,17 @@ namespace WixToolset.Data.WindowsInstaller switch (reader.LocalName) { case "subStorage": - output.SubStorages.Add(SubStorage.Read(reader)); + output.SubStorages.Add(SubStorage.Read(reader, tableDefinitions)); break; case "table": - if (null == tableDefinitions) + if (null == xmlTableDefinitions) { throw new XmlException(); } - tables.Add(Table.Read(reader, tableDefinitions)); + tables.Add(Table.Read(reader, xmlTableDefinitions)); break; case "tableDefinitions": - tableDefinitions = TableDefinitionCollection.Read(reader); + xmlTableDefinitions = TableDefinitionCollection.Read(reader, tableDefinitions); break; default: throw new XmlException(); diff --git a/src/test/WixToolsetTest.Data/SerializeFixture.cs b/src/test/WixToolsetTest.Data/SerializeFixture.cs index 6e224438..198b2571 100644 --- a/src/test/WixToolsetTest.Data/SerializeFixture.cs +++ b/src/test/WixToolsetTest.Data/SerializeFixture.cs @@ -8,8 +8,11 @@ namespace WixToolsetTest.Data using WixToolset.Data; using WixToolset.Data.Bind; using WixToolset.Data.Tuples; + using WixToolset.Data.WindowsInstaller.Rows; using Xunit; + using Wid = WixToolset.Data.WindowsInstaller; + public class SerializeFixture { [Fact] @@ -383,5 +386,42 @@ namespace WixToolsetTest.Data File.Delete(path); } } + + [Fact] + public void CanSaveAndLoadWindowsInstallerData() + { + var sln = new SourceLineNumber("test.wxs", 1); + var windowsInstallerData = new Wid.WindowsInstallerData(sln) + { + Type = OutputType.Product, + }; + + var fileTable = windowsInstallerData.EnsureTable(Wid.WindowsInstallerTableDefinitions.File); + var fileRow = (FileRow)fileTable.CreateRow(sln); + fileRow.File = "TestFile"; + + var path = Path.GetTempFileName(); + try + { + using (var wixout = WixOutput.Create(path)) + { + windowsInstallerData.Save(wixout); + } + + var loaded = Wid.WindowsInstallerData.Load(path); + + var loadedTable = Assert.Single(loaded.Tables); + Assert.Equal(Wid.WindowsInstallerTableDefinitions.File.Name, loadedTable.Name); + + var loadedRow = Assert.Single(loadedTable.Rows); + var loadedFileRow = Assert.IsType(loadedRow); + + Assert.Equal("TestFile", loadedFileRow.File); + } + finally + { + File.Delete(path); + } + } } } -- cgit v1.2.3-55-g6feb