aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/Intermediate.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Data/Intermediate.cs')
-rw-r--r--src/WixToolset.Data/Intermediate.cs170
1 files changed, 127 insertions, 43 deletions
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
4{ 4{
5 using System; 5 using System;
6 using System.Collections.Generic; 6 using System.Collections.Generic;
7 using System.Linq;
7 using System.IO; 8 using System.IO;
8 using SimpleJson; 9 using SimpleJson;
9 10
@@ -96,8 +97,23 @@ namespace WixToolset.Data
96 /// <returns>Returns the loaded intermediate.</returns> 97 /// <returns>Returns the loaded intermediate.</returns>
97 public static Intermediate Load(string path, bool suppressVersionCheck = false) 98 public static Intermediate Load(string path, bool suppressVersionCheck = false)
98 { 99 {
100 using (var stream = File.OpenRead(path))
101 {
102 var creator = new SimpleTupleDefinitionCreator();
103 return Intermediate.Load(stream, path, creator, suppressVersionCheck);
104 }
105 }
106
107 /// <summary>
108 /// Loads an intermediate from a stream.
109 /// </summary>
110 /// <param name="stream">Stream to intermediate file.</param>
111 /// <param name="suppressVersionCheck">Suppress checking for wix.dll version mismatches.</param>
112 /// <returns>Returns the loaded intermediate.</returns>
113 public static Intermediate Load(Stream stream, bool suppressVersionCheck = false)
114 {
99 var creator = new SimpleTupleDefinitionCreator(); 115 var creator = new SimpleTupleDefinitionCreator();
100 return Intermediate.Load(path, creator, suppressVersionCheck); 116 return Intermediate.Load(stream, creator, suppressVersionCheck);
101 } 117 }
102 118
103 /// <summary> 119 /// <summary>
@@ -109,51 +125,22 @@ namespace WixToolset.Data
109 /// <returns>Returns the loaded intermediate.</returns> 125 /// <returns>Returns the loaded intermediate.</returns>
110 public static Intermediate Load(string path, ITupleDefinitionCreator creator, bool suppressVersionCheck = false) 126 public static Intermediate Load(string path, ITupleDefinitionCreator creator, bool suppressVersionCheck = false)
111 { 127 {
112 JsonObject jsonObject; 128 using (var stream = File.OpenRead(path))
113
114 using (FileStream stream = File.OpenRead(path))
115 using (FileStructure fs = FileStructure.Read(stream))
116 {
117 if (FileFormat.WixIR != fs.FileFormat)
118 {
119 throw new WixUnexpectedFileFormatException(path, FileFormat.WixIR, fs.FileFormat);
120 }
121
122 var json = fs.GetData();
123 jsonObject = SimpleJson.DeserializeObject(json) as JsonObject;
124 }
125
126 if (!suppressVersionCheck)
127 { 129 {
128 var versionJson = jsonObject.GetValueOrDefault<string>("version"); 130 return Intermediate.Load(stream, path, creator, suppressVersionCheck);
129
130 if (!Version.TryParse(versionJson, out var version) || !Intermediate.CurrentVersion.Equals(version))
131 {
132 throw new WixException(WixDataErrors.VersionMismatch(SourceLineNumber.CreateFromUri(path), "intermediate", versionJson, Intermediate.CurrentVersion.ToString()));
133 }
134 } 131 }
132 }
135 133
136 var id = jsonObject.GetValueOrDefault<string>("id"); 134 /// <summary>
137 135 /// Loads an intermediate from a path on disk.
138 var sections = new List<IntermediateSection>(); 136 /// </summary>
139 137 /// <param name="stream">Stream to intermediate file.</param>
140 var sectionsJson = jsonObject.GetValueOrDefault<JsonArray>("sections"); 138 /// <param name="creator">ITupleDefinitionCreator to use when reconstituting the intermediate.</param>
141 foreach (JsonObject sectionJson in sectionsJson) 139 /// <param name="suppressVersionCheck">Suppress checking for wix.dll version mismatches.</param>
142 { 140 /// <returns>Returns the loaded intermediate.</returns>
143 var section = IntermediateSection.Deserialize(creator, sectionJson); 141 public static Intermediate Load(Stream stream, ITupleDefinitionCreator creator, bool suppressVersionCheck = false)
144 sections.Add(section); 142 {
145 } 143 return Load(stream, "<unknown>", creator, suppressVersionCheck);
146
147 var localizations = new Dictionary<string, Localization>(StringComparer.OrdinalIgnoreCase);
148
149 //var localizationsJson = jsonObject.GetValueOrDefault<JsonArray>("localizations") ?? new JsonArray();
150 //foreach (JsonObject localizationJson in localizationsJson)
151 //{
152 // var localization = Localization.Deserialize(localizationJson);
153 // localizations.Add(localization.Culture, localization);
154 //}
155
156 return new Intermediate(id, sections, localizations, null);
157 } 144 }
158 145
159 /// <summary> 146 /// <summary>
@@ -183,6 +170,21 @@ namespace WixToolset.Data
183 170
184 jsonObject.Add("sections", sectionsJson); 171 jsonObject.Add("sections", sectionsJson);
185 172
173 var customDefinitions = GetCustomDefinitionsInSections();
174
175 if (customDefinitions.Count > 0)
176 {
177 var customDefinitionsJson = new JsonArray(customDefinitions.Count);
178
179 foreach (var kvp in customDefinitions.OrderBy(d => d.Key))
180 {
181 var customDefinitionJson = kvp.Value.Serialize();
182 customDefinitionsJson.Add(customDefinitionJson);
183 }
184
185 jsonObject.Add("definitions", customDefinitionsJson);
186 }
187
186 //if (this.Localizations.Any()) 188 //if (this.Localizations.Any())
187 //{ 189 //{
188 // var localizationsJson = new JsonArray(); 190 // var localizationsJson = new JsonArray();
@@ -200,6 +202,73 @@ namespace WixToolset.Data
200 } 202 }
201 } 203 }
202 204
205 /// <summary>
206 /// Loads an intermediate from a path on disk.
207 /// </summary>
208 /// <param name="stream">Stream to intermediate file.</param>
209 /// <param name="path">Path name of intermediate file.</param>
210 /// <param name="creator">ITupleDefinitionCreator to use when reconstituting the intermediate.</param>
211 /// <param name="suppressVersionCheck">Suppress checking for wix.dll version mismatches.</param>
212 /// <returns>Returns the loaded intermediate.</returns>
213 internal static Intermediate Load(Stream stream, string path, ITupleDefinitionCreator creator, bool suppressVersionCheck = false)
214 {
215 JsonObject jsonObject;
216
217 using (var fs = FileStructure.Read(stream))
218 {
219 if (FileFormat.WixIR != fs.FileFormat)
220 {
221 throw new WixUnexpectedFileFormatException(path, FileFormat.WixIR, fs.FileFormat);
222 }
223
224 var json = fs.GetData();
225 jsonObject = SimpleJson.DeserializeObject(json) as JsonObject;
226 }
227
228 if (!suppressVersionCheck)
229 {
230 var versionJson = jsonObject.GetValueOrDefault<string>("version");
231
232 if (!Version.TryParse(versionJson, out var version) || !Intermediate.CurrentVersion.Equals(version))
233 {
234 throw new WixException(WixDataErrors.VersionMismatch(SourceLineNumber.CreateFromUri(path), "intermediate", versionJson, Intermediate.CurrentVersion.ToString()));
235 }
236 }
237
238 var definitionsJson = jsonObject.GetValueOrDefault<JsonArray>("definitions");
239
240 if (definitionsJson != null)
241 {
242 foreach (JsonObject definitionJson in definitionsJson)
243 {
244 var definition = IntermediateTupleDefinition.Deserialize(definitionJson);
245 creator.AddCustomTupleDefinition(definition);
246 }
247 }
248
249 var id = jsonObject.GetValueOrDefault<string>("id");
250
251 var sections = new List<IntermediateSection>();
252
253 var sectionsJson = jsonObject.GetValueOrDefault<JsonArray>("sections");
254 foreach (JsonObject sectionJson in sectionsJson)
255 {
256 var section = IntermediateSection.Deserialize(creator, sectionJson);
257 sections.Add(section);
258 }
259
260 var localizations = new Dictionary<string, Localization>(StringComparer.OrdinalIgnoreCase);
261
262 //var localizationsJson = jsonObject.GetValueOrDefault<JsonArray>("localizations") ?? new JsonArray();
263 //foreach (JsonObject localizationJson in localizationsJson)
264 //{
265 // var localization = Localization.Deserialize(localizationJson);
266 // localizations.Add(localization.Culture, localization);
267 //}
268
269 return new Intermediate(id, sections, localizations, null);
270 }
271
203#if false 272#if false
204 /// <summary> 273 /// <summary>
205 /// Loads an intermediate from a path on disk. 274 /// Loads an intermediate from a path on disk.
@@ -344,5 +413,20 @@ namespace WixToolset.Data
344 writer.WriteEndElement(); 413 writer.WriteEndElement();
345 } 414 }
346#endif 415#endif
416
417 private Dictionary<string, IntermediateTupleDefinition> GetCustomDefinitionsInSections()
418 {
419 var customDefinitions = new Dictionary<string, IntermediateTupleDefinition>();
420
421 foreach (var tuple in this.Sections.SelectMany(s => s.Tuples).Where(t => t.Definition.Type == TupleDefinitionType.MustBeFromAnExtension))
422 {
423 if (!customDefinitions.ContainsKey(tuple.Definition.Name))
424 {
425 customDefinitions.Add(tuple.Definition.Name, tuple.Definition);
426 }
427 }
428
429 return customDefinitions;
430 }
347 } 431 }
348} 432}