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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
// 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.IO;
using System.Linq;
using System.Reflection;
using SimpleJson;
/// <summary>
/// Container class for an intermediate object.
/// </summary>
public sealed class Intermediate
{
private static readonly Version CurrentVersion = new Version("4.0.0.0");
private const string WixOutputStreamName = "wix-ir.json";
private readonly Dictionary<string, Localization> localizationsByCulture;
/// <summary>
/// Instantiate a new Intermediate.
/// </summary>
public Intermediate()
{
this.Id = Convert.ToBase64String(Guid.NewGuid().ToByteArray()).TrimEnd('=').Replace('+', '.').Replace('/', '_');
this.localizationsByCulture = new Dictionary<string, Localization>(StringComparer.OrdinalIgnoreCase);
this.Sections = new List<IntermediateSection>();
}
public Intermediate(string id, IEnumerable<IntermediateSection> sections, IDictionary<string, Localization> localizationsByCulture) : this(id, level: null, sections, localizationsByCulture)
{
}
public Intermediate(string id, string level, IEnumerable<IntermediateSection> sections, IDictionary<string, Localization> localizationsByCulture)
{
this.Id = id;
this.Level = level;
this.localizationsByCulture = (localizationsByCulture != null) ? new Dictionary<string, Localization>(localizationsByCulture, StringComparer.OrdinalIgnoreCase) : new Dictionary<string, Localization>(StringComparer.OrdinalIgnoreCase);
this.Sections = (sections != null) ? new List<IntermediateSection>(sections) : new List<IntermediateSection>();
}
/// <summary>
/// Get the id for the intermediate.
/// </summary>
public string Id { get; }
/// <summary>
/// Get the id for the intermediate.
/// </summary>
public string Level { get; private set; }
/// <summary>
/// Get the localizations contained in this intermediate.
/// </summary>
public IEnumerable<Localization> Localizations => this.localizationsByCulture.Values;
/// <summary>
/// Get the sections contained in this intermediate.
/// </summary>
public IList<IntermediateSection> Sections { get; }
/// <summary>
/// Loads an intermediate from a path on disk.
/// </summary>
/// <param name="path">Path to intermediate file saved on disk.</param>
/// <param name="suppressVersionCheck">Suppress checking for wix.dll version mismatches.</param>
/// <returns>Returns the loaded intermediate.</returns>
public static Intermediate Load(string path, bool suppressVersionCheck = false)
{
var creator = new SimpleTupleDefinitionCreator();
return Intermediate.Load(path, creator, suppressVersionCheck);
}
/// <summary>
/// Loads an intermediate from a stream.
/// </summary>
/// <param name="assembly">Assembly with intermediate embedded in resource stream.</param>
/// <param name="resourceName">Name of resource stream.</param>
/// <param name="suppressVersionCheck">Suppress checking for wix.dll version mismatches.</param>
/// <returns>Returns the loaded intermediate.</returns>
public static Intermediate Load(Assembly assembly, string resourceName, bool suppressVersionCheck = false)
{
var creator = new SimpleTupleDefinitionCreator();
return Intermediate.Load(assembly, resourceName, creator, suppressVersionCheck);
}
/// <summary>
/// Loads an intermediate from a stream.
/// </summary>
/// <param name="assembly">Assembly with intermediate embedded in resource stream.</param>
/// <param name="resourceName">Name of resource stream.</param>
/// <param name="creator">ITupleDefinitionCreator to use when reconstituting the intermediate.</param>
/// <param name="suppressVersionCheck">Suppress checking for wix.dll version mismatches.</param>
/// <returns>Returns the loaded intermediate.</returns>
public static Intermediate Load(Assembly assembly, string resourceName, ITupleDefinitionCreator creator, bool suppressVersionCheck = false)
{
using (var wixout = WixOutput.Read(assembly, resourceName))
{
return Intermediate.LoadIntermediate(wixout, creator, suppressVersionCheck);
}
}
/// <summary>
/// Loads an intermediate from a path on disk.
/// </summary>
/// <param name="path">Path to intermediate file saved on disk.</param>
/// <param name="creator">ITupleDefinitionCreator to use when reconstituting the intermediate.</param>
/// <param name="suppressVersionCheck">Suppress checking for wix.dll version mismatches.</param>
/// <returns>Returns the loaded intermediate.</returns>
public static Intermediate Load(string path, ITupleDefinitionCreator creator, bool suppressVersionCheck = false)
{
using (var wixout = WixOutput.Read(path))
{
return Intermediate.LoadIntermediate(wixout, creator, suppressVersionCheck);
}
}
/// <summary>
/// Loads an intermediate from a WixOutput object.
/// </summary>
/// <param name="wixOutput">WixOutput object.</param>
/// <param name="creator">ITupleDefinitionCreator to use when reconstituting the intermediate.</param>
/// <param name="suppressVersionCheck">Suppress checking for wix.dll version mismatches.</param>
/// <returns>Returns the loaded intermediate.</returns>
public static Intermediate Load(WixOutput wixOutput, bool suppressVersionCheck = false)
{
var creator = new SimpleTupleDefinitionCreator();
return Intermediate.LoadIntermediate(wixOutput, creator, suppressVersionCheck);
}
/// <summary>
/// Loads an intermediate from a WixOutput object.
/// </summary>
/// <param name="wixOutput">WixOutput object.</param>
/// <param name="creator">ITupleDefinitionCreator to use when reconstituting the intermediate.</param>
/// <param name="suppressVersionCheck">Suppress checking for wix.dll version mismatches.</param>
/// <returns>Returns the loaded intermediate.</returns>
public static Intermediate Load(WixOutput wixOutput, ITupleDefinitionCreator creator, bool suppressVersionCheck = false)
{
return Intermediate.LoadIntermediate(wixOutput, creator, suppressVersionCheck);
}
/// <summary>
/// Loads several intermediates from paths on disk using the same definitions.
/// </summary>
/// <param name="intermediateFiles">Paths to intermediate files saved on disk.</param>
/// <param name="suppressVersionCheck">Suppress checking for wix.dll version mismatches.</param>
/// <returns>Returns the loaded intermediates</returns>
public static IEnumerable<Intermediate> Load(IEnumerable<string> intermediateFiles)
{
var creator = new SimpleTupleDefinitionCreator();
return Intermediate.Load(intermediateFiles, creator);
}
/// <summary>
/// Loads several intermediates from paths on disk using the same definitions.
/// </summary>
/// <param name="intermediateFiles">Paths to intermediate files saved on disk.</param>
/// <param name="creator">ITupleDefinitionCreator to use when reconstituting the intermediates.</param>
/// <param name="suppressVersionCheck">Suppress checking for wix.dll version mismatches.</param>
/// <returns>Returns the loaded intermediates</returns>
public static IEnumerable<Intermediate> Load(IEnumerable<string> intermediateFiles, ITupleDefinitionCreator creator, bool suppressVersionCheck = false)
{
var jsons = new Queue<JsonWithPath>();
var intermediates = new List<Intermediate>();
foreach (var path in intermediateFiles)
{
using (var wixout = WixOutput.Read(path))
{
var data = wixout.GetData(WixOutputStreamName);
var json = Intermediate.LoadJson(data, wixout.Uri, suppressVersionCheck);
Intermediate.LoadDefinitions(json, creator);
jsons.Enqueue(new JsonWithPath { Json = json, Path = wixout.Uri });
}
}
while (jsons.Count > 0)
{
var jsonWithPath = jsons.Dequeue();
var intermediate = Intermediate.FinalizeLoad(jsonWithPath.Json, jsonWithPath.Path, creator);
intermediates.Add(intermediate);
}
return intermediates;
}
/// <summary>
/// Updates the intermediate level to the specified level.
/// </summary>
/// <param name="level">Intermediate level.</param>
public void UpdateLevel(string level)
{
this.Level = String.IsNullOrEmpty(this.Level) ? level : String.Concat(this.Level, ";", level);
}
/// <summary>
/// Returns whether a specifed intermediate level has been set for this intermediate.
/// </summary>
/// <param name="level">Intermediate level.</param>
/// <returns>True if the specifed intermediate level has been set for this intermediate.</returns>
public bool HasLevel(string level)
{
return this.Level?.Contains(level) == true;
}
/// <summary>
/// Saves an intermediate to a path on disk.
/// </summary>
/// <param name="path">Path to save intermediate file to disk.</param>
public void Save(string path)
{
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path)));
using (var wixout = WixOutput.Create(path))
{
this.Save(wixout);
}
}
/// <summary>
/// Saves an intermediate to a path on disk.
/// </summary>
/// <param name="path">Path to save intermediate file to disk.</param>
public void Save(WixOutput wixout)
{
this.SaveEmbedFiles(wixout);
this.SaveIR(wixout);
}
/// <summary>
/// Loads an intermediate from a path on disk.
/// </summary>
/// <param name="stream">Stream to intermediate file.</param>
/// <param name="baseUri">Path name of intermediate file.</param>
/// <param name="creator">ITupleDefinitionCreator to use when reconstituting the intermediate.</param>
/// <param name="suppressVersionCheck">Suppress checking for wix.dll version mismatches.</param>
/// <returns>Returns the loaded intermediate.</returns>
private static Intermediate LoadIntermediate(WixOutput wixout, ITupleDefinitionCreator creator, bool suppressVersionCheck = false)
{
var data = wixout.GetData(WixOutputStreamName);
var json = Intermediate.LoadJson(data, wixout.Uri, suppressVersionCheck);
Intermediate.LoadDefinitions(json, creator);
return Intermediate.FinalizeLoad(json, wixout.Uri, creator);
}
/// <summary>
/// Loads json form of intermedaite from stream.
/// </summary>
/// <param name="stream">Stream to intermediate file.</param>
/// <param name="baseUri">Path name of intermediate file.</param>
/// <param name="suppressVersionCheck">Suppress checking for wix.dll version mismatches.</param>
/// <returns>Returns the loaded json.</returns>
private static JsonObject LoadJson(string json, Uri baseUri, bool suppressVersionCheck)
{
var jsonObject = SimpleJson.DeserializeObject(json) as JsonObject;
if (!suppressVersionCheck)
{
var versionJson = jsonObject.GetValueOrDefault<string>("version");
if (!Version.TryParse(versionJson, out var version) || !Intermediate.CurrentVersion.Equals(version))
{
throw new WixException(ErrorMessages.VersionMismatch(SourceLineNumber.CreateFromUri(baseUri.AbsoluteUri), "intermediate", versionJson, Intermediate.CurrentVersion.ToString()));
}
}
return jsonObject;
}
/// <summary>
/// Loads custom definitions in intermediate json into the creator.
/// </summary>
/// <param name="json">Json version of intermediate.</param>
/// <param name="creator">ITupleDefinitionCreator to use when reconstituting the intermediate.</param>
private static void LoadDefinitions(JsonObject json, ITupleDefinitionCreator creator)
{
var definitionsJson = json.GetValueOrDefault<JsonArray>("definitions");
if (definitionsJson != null)
{
foreach (JsonObject definitionJson in definitionsJson)
{
var definition = IntermediateTupleDefinition.Deserialize(definitionJson);
creator.AddCustomTupleDefinition(definition);
}
}
}
/// <summary>
/// Loads the sections and localization for the intermediate.
/// </summary>
/// <param name="json">Json version of intermediate.</param>
/// <param name="baseUri">Path to the intermediate.</param>
/// <param name="creator">ITupleDefinitionCreator to use when reconstituting the intermediate.</param>
/// <returns>The finalized intermediate.</returns>
private static Intermediate FinalizeLoad(JsonObject json, Uri baseUri, ITupleDefinitionCreator creator)
{
var id = json.GetValueOrDefault<string>("id");
var level = json.GetValueOrDefault<string>("level");
var sections = new List<IntermediateSection>();
var sectionsJson = json.GetValueOrDefault<JsonArray>("sections");
foreach (JsonObject sectionJson in sectionsJson)
{
var section = IntermediateSection.Deserialize(creator, baseUri, sectionJson);
sections.Add(section);
}
var localizations = new Dictionary<string, Localization>(StringComparer.OrdinalIgnoreCase);
var localizationsJson = json.GetValueOrDefault<JsonArray>("localizations") ?? new JsonArray();
foreach (JsonObject localizationJson in localizationsJson)
{
var localization = Localization.Deserialize(localizationJson);
localizations.Add(localization.Culture, localization);
}
return new Intermediate(id, level, sections, localizations);
}
private void SaveEmbedFiles(WixOutput wixout)
{
var embeddedFields = this.Sections.SelectMany(s => s.Tuples)
.SelectMany(t => t.Fields)
.Where(f => f?.Type == IntermediateFieldType.Path)
.Select(f => f.AsPath())
.Where(f => f.Embed)
.ToList();
var savedEmbedFields = new Dictionary<string, IntermediateFieldPathValue>(StringComparer.OrdinalIgnoreCase);
var uniqueEntryNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var embeddedField in embeddedFields)
{
var key = String.Concat(embeddedField.BaseUri?.AbsoluteUri, "?", embeddedField.Path);
if (savedEmbedFields.TryGetValue(key, out var existing))
{
embeddedField.Path = existing.Path;
}
else
{
var entryName = CalculateUniqueEntryName(uniqueEntryNames, embeddedField.Path);
if (embeddedField.BaseUri == null)
{
wixout.ImportDataStream(entryName, embeddedField.Path);
}
else // open the container specified in baseUri and copy the correct stream out of it.
{
using (var otherWixout = WixOutput.Read(embeddedField.BaseUri))
using (var stream = otherWixout.GetDataStream(embeddedField.Path))
using (var target = wixout.CreateDataStream(entryName))
{
stream.CopyTo(target);
}
}
embeddedField.Path = entryName;
savedEmbedFields.Add(key, embeddedField);
}
}
}
private void SaveIR(WixOutput wixout)
{
using (var writer = new StreamWriter(wixout.CreateDataStream(WixOutputStreamName)))
{
var jsonObject = new JsonObject
{
{ "id", this.Id },
{ "level", this.Level },
{ "version", Intermediate.CurrentVersion.ToString() }
};
var sectionsJson = new JsonArray(this.Sections.Count);
foreach (var section in this.Sections)
{
var sectionJson = section.Serialize();
sectionsJson.Add(sectionJson);
}
jsonObject.Add("sections", sectionsJson);
var customDefinitions = this.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();
foreach (var localization in this.Localizations)
{
var localizationJson = localization.Serialize();
localizationsJson.Add(localizationJson);
}
jsonObject.Add("localizations", localizationsJson);
}
var json = SimpleJson.SerializeObject(jsonObject);
writer.Write(json);
}
}
private static string CalculateUniqueEntryName(ISet<string> entryNames, string path)
{
var filename = Path.GetFileName(path);
var entryName = "wix-ir/" + filename;
var i = 0;
while (!entryNames.Add(entryName))
{
entryName = $"wix-ir/{filename}-{++i}";
}
return entryName;
}
private Dictionary<string, IntermediateTupleDefinition> GetCustomDefinitionsInSections()
{
var customDefinitions = new Dictionary<string, IntermediateTupleDefinition>();
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;
}
private struct JsonWithPath
{
public JsonObject Json { get; set; }
public Uri Path { get; set; }
}
}
}
|