From 53e877183abe0dbbb623c39380101bc369e9f265 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sat, 2 Dec 2017 00:44:45 -0800 Subject: Support tuples from extensions and make SourcePath a path instead of string --- src/WixToolset.Data/Intermediate.cs | 170 +++++++++++++++++++++++++++--------- 1 file changed, 127 insertions(+), 43 deletions(-) (limited to 'src/WixToolset.Data/Intermediate.cs') diff --git a/src/WixToolset.Data/Intermediate.cs b/src/WixToolset.Data/Intermediate.cs index b03492ce..4d4e17cc 100644 --- a/src/WixToolset.Data/Intermediate.cs +++ b/src/WixToolset.Data/Intermediate.cs @@ -4,6 +4,7 @@ namespace WixToolset.Data { using System; using System.Collections.Generic; + using System.Linq; using System.IO; using SimpleJson; @@ -95,9 +96,24 @@ namespace WixToolset.Data /// Suppress checking for wix.dll version mismatches. /// Returns the loaded intermediate. public static Intermediate Load(string path, bool suppressVersionCheck = false) + { + using (var stream = File.OpenRead(path)) + { + var creator = new SimpleTupleDefinitionCreator(); + return Intermediate.Load(stream, path, creator, suppressVersionCheck); + } + } + + /// + /// Loads an intermediate from a stream. + /// + /// Stream to intermediate file. + /// Suppress checking for wix.dll version mismatches. + /// Returns the loaded intermediate. + public static Intermediate Load(Stream stream, bool suppressVersionCheck = false) { var creator = new SimpleTupleDefinitionCreator(); - return Intermediate.Load(path, creator, suppressVersionCheck); + return Intermediate.Load(stream, creator, suppressVersionCheck); } /// @@ -109,51 +125,22 @@ namespace WixToolset.Data /// Returns the loaded intermediate. public static Intermediate Load(string path, ITupleDefinitionCreator creator, bool suppressVersionCheck = false) { - JsonObject jsonObject; - - using (FileStream stream = File.OpenRead(path)) - using (FileStructure fs = FileStructure.Read(stream)) - { - if (FileFormat.WixIR != fs.FileFormat) - { - throw new WixUnexpectedFileFormatException(path, FileFormat.WixIR, fs.FileFormat); - } - - var json = fs.GetData(); - jsonObject = SimpleJson.DeserializeObject(json) as JsonObject; - } - - if (!suppressVersionCheck) + using (var stream = File.OpenRead(path)) { - var versionJson = jsonObject.GetValueOrDefault("version"); - - if (!Version.TryParse(versionJson, out var version) || !Intermediate.CurrentVersion.Equals(version)) - { - throw new WixException(WixDataErrors.VersionMismatch(SourceLineNumber.CreateFromUri(path), "intermediate", versionJson, Intermediate.CurrentVersion.ToString())); - } + return Intermediate.Load(stream, path, creator, suppressVersionCheck); } + } - var id = jsonObject.GetValueOrDefault("id"); - - var sections = new List(); - - var sectionsJson = jsonObject.GetValueOrDefault("sections"); - foreach (JsonObject sectionJson in sectionsJson) - { - var section = IntermediateSection.Deserialize(creator, sectionJson); - sections.Add(section); - } - - var localizations = new Dictionary(StringComparer.OrdinalIgnoreCase); - - //var localizationsJson = jsonObject.GetValueOrDefault("localizations") ?? new JsonArray(); - //foreach (JsonObject localizationJson in localizationsJson) - //{ - // var localization = Localization.Deserialize(localizationJson); - // localizations.Add(localization.Culture, localization); - //} - - return new Intermediate(id, sections, localizations, null); + /// + /// Loads an intermediate from a path on disk. + /// + /// Stream to intermediate file. + /// ITupleDefinitionCreator to use when reconstituting the intermediate. + /// Suppress checking for wix.dll version mismatches. + /// Returns the loaded intermediate. + public static Intermediate Load(Stream stream, ITupleDefinitionCreator creator, bool suppressVersionCheck = false) + { + return Load(stream, "", creator, suppressVersionCheck); } /// @@ -183,6 +170,21 @@ namespace WixToolset.Data jsonObject.Add("sections", sectionsJson); + var customDefinitions = GetCustomDefinitionsInSections(); + + if (customDefinitions.Count > 0) + { + var customDefinitionsJson = new JsonArray(customDefinitions.Count); + + foreach (var kvp in customDefinitions.OrderBy(d => d.Key)) + { + var customDefinitionJson = kvp.Value.Serialize(); + customDefinitionsJson.Add(customDefinitionJson); + } + + jsonObject.Add("definitions", customDefinitionsJson); + } + //if (this.Localizations.Any()) //{ // var localizationsJson = new JsonArray(); @@ -200,6 +202,73 @@ namespace WixToolset.Data } } + /// + /// Loads an intermediate from a path on disk. + /// + /// Stream to intermediate file. + /// Path name of intermediate file. + /// ITupleDefinitionCreator to use when reconstituting the intermediate. + /// Suppress checking for wix.dll version mismatches. + /// Returns the loaded intermediate. + internal static Intermediate Load(Stream stream, string path, ITupleDefinitionCreator creator, bool suppressVersionCheck = false) + { + JsonObject jsonObject; + + using (var fs = FileStructure.Read(stream)) + { + if (FileFormat.WixIR != fs.FileFormat) + { + throw new WixUnexpectedFileFormatException(path, FileFormat.WixIR, fs.FileFormat); + } + + var json = fs.GetData(); + jsonObject = SimpleJson.DeserializeObject(json) as JsonObject; + } + + if (!suppressVersionCheck) + { + var versionJson = jsonObject.GetValueOrDefault("version"); + + if (!Version.TryParse(versionJson, out var version) || !Intermediate.CurrentVersion.Equals(version)) + { + throw new WixException(WixDataErrors.VersionMismatch(SourceLineNumber.CreateFromUri(path), "intermediate", versionJson, Intermediate.CurrentVersion.ToString())); + } + } + + var definitionsJson = jsonObject.GetValueOrDefault("definitions"); + + if (definitionsJson != null) + { + foreach (JsonObject definitionJson in definitionsJson) + { + var definition = IntermediateTupleDefinition.Deserialize(definitionJson); + creator.AddCustomTupleDefinition(definition); + } + } + + var id = jsonObject.GetValueOrDefault("id"); + + var sections = new List(); + + var sectionsJson = jsonObject.GetValueOrDefault("sections"); + foreach (JsonObject sectionJson in sectionsJson) + { + var section = IntermediateSection.Deserialize(creator, sectionJson); + sections.Add(section); + } + + var localizations = new Dictionary(StringComparer.OrdinalIgnoreCase); + + //var localizationsJson = jsonObject.GetValueOrDefault("localizations") ?? new JsonArray(); + //foreach (JsonObject localizationJson in localizationsJson) + //{ + // var localization = Localization.Deserialize(localizationJson); + // localizations.Add(localization.Culture, localization); + //} + + return new Intermediate(id, sections, localizations, null); + } + #if false /// /// Loads an intermediate from a path on disk. @@ -344,5 +413,20 @@ namespace WixToolset.Data writer.WriteEndElement(); } #endif + + private Dictionary GetCustomDefinitionsInSections() + { + var customDefinitions = new Dictionary(); + + foreach (var tuple in this.Sections.SelectMany(s => s.Tuples).Where(t => t.Definition.Type == TupleDefinitionType.MustBeFromAnExtension)) + { + if (!customDefinitions.ContainsKey(tuple.Definition.Name)) + { + customDefinitions.Add(tuple.Definition.Name, tuple.Definition); + } + } + + return customDefinitions; + } } } -- cgit v1.2.3-55-g6feb