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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
|
// 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.Dependency
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Xml.Linq;
using WixToolset.Data;
using WixToolset.Extensibility;
using WixToolset.Extensibility.Data;
/// <summary>
/// The compiler for the WiX Toolset Dependency Extension.
/// </summary>
public sealed class DependencyCompiler : BaseCompilerExtension
{
/// <summary>
/// Package type when parsing the Provides element.
/// </summary>
private enum PackageType
{
None,
ExePackage,
MsiPackage,
MspPackage,
MsuPackage
}
public override XNamespace Namespace => "http://wixtoolset.org/schemas/v4/wxs/dependency";
/// <summary>
/// Processes an attribute for the Compiler.
/// </summary>
/// <param name="sourceLineNumbers">Source line number for the parent element.</param>
/// <param name="parentElement">Parent element of attribute.</param>
/// <param name="attribute">Attribute to process.</param>
public override void ParseAttribute(Intermediate intermediate, IntermediateSection section, XElement parentElement, XAttribute attribute, IDictionary<string, string> context)
{
SourceLineNumber sourceLineNumbers = this.ParseHelper.GetSourceLineNumbers(parentElement);
switch (parentElement.Name.LocalName)
{
case "Bundle":
switch (attribute.Name.LocalName)
{
case "ProviderKey":
this.ParseProviderKeyAttribute(section, sourceLineNumbers, parentElement, attribute);
break;
default:
this.ParseHelper.UnexpectedAttribute(parentElement, attribute);
break;
}
break;
default:
this.ParseHelper.UnexpectedAttribute(parentElement, attribute);
break;
}
}
/// <summary>
/// Processes an element for the Compiler.
/// </summary>
/// <param name="sourceLineNumbers">Source line number for the parent element.</param>
/// <param name="parentElement">Parent element of element to process.</param>
/// <param name="element">Element to process.</param>
/// <param name="contextValues">Extra information about the context in which this element is being parsed.</param>
public override void ParseElement(Intermediate intermediate, IntermediateSection section, XElement parentElement, XElement element, IDictionary<string, string> context)
{
PackageType packageType = PackageType.None;
switch (parentElement.Name.LocalName)
{
case "Bundle":
case "Fragment":
case "Module":
case "Product":
switch (element.Name.LocalName)
{
case "Requires":
this.ParseRequiresElement(intermediate, section, element, null, false);
break;
default:
this.ParseHelper.UnexpectedElement(parentElement, element);
break;
}
break;
case "ExePackage":
packageType = PackageType.ExePackage;
break;
case "MsiPackage":
packageType = PackageType.MsiPackage;
break;
case "MspPackage":
packageType = PackageType.MspPackage;
break;
case "MsuPackage":
packageType = PackageType.MsuPackage;
break;
default:
this.ParseHelper.UnexpectedElement(parentElement, element);
break;
}
if (PackageType.None != packageType)
{
string packageId = context["PackageId"];
switch (element.Name.LocalName)
{
case "Provides":
this.ParseProvidesElement(intermediate, section, element, packageType, packageId);
break;
default:
this.ParseHelper.UnexpectedElement(parentElement, element);
break;
}
}
}
/// <summary>
/// Processes a child element of a Component for the Compiler.
/// </summary>
/// <param name="parentElement">Parent element of element to process.</param>
/// <param name="element">Element to process.</param>
/// <param name="context">Extra information about the context in which this element is being parsed.</param>
/// <returns>The component key path type if set.</returns>
public override ComponentKeyPath ParsePossibleKeyPathElement(Intermediate intermediate, IntermediateSection section, XElement parentElement, XElement element, IDictionary<string, string> context)
{
SourceLineNumber sourceLineNumbers = this.ParseHelper.GetSourceLineNumbers(parentElement);
ComponentKeyPath keyPath = null;
switch (parentElement.Name.LocalName)
{
case "Component":
string componentId = context["ComponentId"];
// 64-bit components may cause issues downlevel.
bool win64 = false;
Boolean.TryParse(context["Win64"], out win64);
switch (element.Name.LocalName)
{
case "Provides":
if (win64)
{
this.Messaging.Write(DependencyWarnings.Win64Component(sourceLineNumbers, componentId));
}
keyPath = this.ParseProvidesElement(intermediate, section, element, PackageType.None, componentId);
break;
default:
this.ParseHelper.UnexpectedElement(parentElement, element);
break;
}
break;
default:
this.ParseHelper.UnexpectedElement(parentElement, element);
break;
}
return keyPath;
}
/// <summary>
/// Processes the ProviderKey bundle attribute.
/// </summary>
/// <param name="sourceLineNumbers">Source line number for the parent element.</param>
/// <param name="parentElement">Parent element of attribute.</param>
/// <param name="attribute">The XML attribute for the ProviderKey attribute.</param>
private void ParseProviderKeyAttribute(IntermediateSection section, SourceLineNumber sourceLineNumbers, XElement parentElement, XAttribute attribute)
{
Identifier id = null;
string providerKey = null;
int illegalChar = -1;
switch (attribute.Name.LocalName)
{
case "ProviderKey":
providerKey = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attribute);
break;
default:
this.ParseHelper.UnexpectedAttribute(parentElement, attribute);
break;
}
// Make sure the key does not contain any illegal characters or values.
if (String.IsNullOrEmpty(providerKey))
{
this.Messaging.Write(ErrorMessages.IllegalEmptyAttributeValue(sourceLineNumbers, parentElement.Name.LocalName, attribute.Name.LocalName));
}
else if (0 <= (illegalChar = providerKey.IndexOfAny(DependencyCommon.InvalidCharacters)))
{
StringBuilder sb = new StringBuilder(DependencyCommon.InvalidCharacters.Length * 2);
Array.ForEach<char>(DependencyCommon.InvalidCharacters, c => sb.Append(c).Append(" "));
this.Messaging.Write(DependencyErrors.IllegalCharactersInProvider(sourceLineNumbers, "ProviderKey", providerKey[illegalChar], sb.ToString()));
}
else if ("ALL" == providerKey)
{
this.Messaging.Write(DependencyErrors.ReservedValue(sourceLineNumbers, parentElement.Name.LocalName, "ProviderKey", providerKey));
}
// Generate the primary key for the row.
id = this.ParseHelper.CreateIdentifier("dep", attribute.Name.LocalName, providerKey);
if (!this.Messaging.EncounteredError)
{
// Create the provider row for the bundle. The Component_ field is required
// in the table definition but unused for bundles, so just set it to the valid ID.
var row = this.ParseHelper.CreateRow(section, sourceLineNumbers, "WixDependencyProvider", id);
row.Set(1, id.Id);
row.Set(2, providerKey);
row.Set(5, DependencyCommon.ProvidesAttributesBundle);
}
}
/// <summary>
/// Processes the Provides element.
/// </summary>
/// <param name="node">The XML node for the Provides element.</param>
/// <param name="packageType">The type of the package being chained into a bundle, or "None" if building an MSI package.</param>
/// <param name="keyPath">Explicit key path.</param>
/// <param name="parentId">The identifier of the parent component or package.</param>
/// <returns>The type of key path if set.</returns>
private ComponentKeyPath ParseProvidesElement(Intermediate intermediate, IntermediateSection section, XElement node, PackageType packageType, string parentId)
{
SourceLineNumber sourceLineNumbers = this.ParseHelper.GetSourceLineNumbers(node);
ComponentKeyPath keyPath = null;
Identifier id = null;
string key = null;
string version = null;
string displayName = null;
int attributes = 0;
int illegalChar = -1;
foreach (XAttribute attrib in node.Attributes())
{
if (String.IsNullOrEmpty(attrib.Name.NamespaceName) || this.Namespace == attrib.Name.Namespace)
{
switch (attrib.Name.LocalName)
{
case "Id":
id = this.ParseHelper.GetAttributeIdentifier(sourceLineNumbers, attrib);
break;
case "Key":
key = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib);
break;
case "Version":
version = this.ParseHelper.GetAttributeVersionValue(sourceLineNumbers, attrib);
break;
case "DisplayName":
displayName = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib);
break;
default:
this.ParseHelper.UnexpectedAttribute(node, attrib);
break;
}
}
else
{
this.ParseHelper.ParseExtensionAttribute(this.Context.Extensions, intermediate, section, node, attrib);
}
}
// Make sure the key is valid. The key will default to the ProductCode for MSI packages
// and the package code for MSP packages in the binder if not specified.
if (!String.IsNullOrEmpty(key))
{
// Make sure the key does not contain any illegal characters or values.
if (0 <= (illegalChar = key.IndexOfAny(DependencyCommon.InvalidCharacters)))
{
StringBuilder sb = new StringBuilder(DependencyCommon.InvalidCharacters.Length * 2);
Array.ForEach<char>(DependencyCommon.InvalidCharacters, c => sb.Append(c).Append(" "));
this.Messaging.Write(DependencyErrors.IllegalCharactersInProvider(sourceLineNumbers, "Key", key[illegalChar], sb.ToString()));
}
else if ("ALL" == key)
{
this.Messaging.Write(DependencyErrors.ReservedValue(sourceLineNumbers, node.Name.LocalName, "Key", key));
}
}
else if (PackageType.ExePackage == packageType || PackageType.MsuPackage == packageType)
{
// Must specify the provider key when authored for a package.
this.Messaging.Write(ErrorMessages.ExpectedAttribute(sourceLineNumbers, node.Name.LocalName, "Key"));
}
else if (PackageType.None == packageType)
{
// Make sure the ProductCode is authored and set the key.
this.ParseHelper.CreateSimpleReference(section, sourceLineNumbers, "Property", "ProductCode");
key = "!(bind.property.ProductCode)";
}
// The Version attribute should not be authored in or for an MSI package.
if (!String.IsNullOrEmpty(version))
{
switch (packageType)
{
case PackageType.None:
this.Messaging.Write(DependencyWarnings.DiscouragedVersionAttribute(sourceLineNumbers));
break;
case PackageType.MsiPackage:
this.Messaging.Write(DependencyWarnings.DiscouragedVersionAttribute(sourceLineNumbers, parentId));
break;
}
}
else if (PackageType.MspPackage == packageType || PackageType.MsuPackage == packageType)
{
// Must specify the Version when authored for packages that do not contain a version.
this.Messaging.Write(ErrorMessages.ExpectedAttribute(sourceLineNumbers, node.Name.LocalName, "Version"));
}
// Need the element ID for child element processing, so generate now if not authored.
if (null == id)
{
id = this.ParseHelper.CreateIdentifier("dep", node.Name.LocalName, parentId, key);
}
foreach (XElement child in node.Elements())
{
if (this.Namespace == child.Name.Namespace)
{
switch (child.Name.LocalName)
{
case "Requires":
this.ParseRequiresElement(intermediate, section, child, id.Id, PackageType.None == packageType);
break;
case "RequiresRef":
this.ParseRequiresRefElement(intermediate, section, child, id.Id, PackageType.None == packageType);
break;
default:
this.ParseHelper.UnexpectedElement(node, child);
break;
}
}
else
{
this.ParseHelper.ParseExtensionElement(this.Context.Extensions, intermediate, section, node, child);
}
}
if (!this.Messaging.EncounteredError)
{
// Create the row in the provider table.
var row = this.ParseHelper.CreateRow(section, sourceLineNumbers, "WixDependencyProvider", id);
row.Set(1, parentId);
row.Set(2, key);
if (!String.IsNullOrEmpty(version))
{
row.Set(3, version);
}
if (!String.IsNullOrEmpty(displayName))
{
row.Set(4, displayName);
}
if (0 != attributes)
{
row.Set(5, attributes);
}
if (PackageType.None == packageType)
{
// Reference the Check custom action to check for dependencies on the current provider.
if (Platform.ARM == this.Context.Platform)
{
// Ensure the ARM version of the CA is referenced.
this.ParseHelper.CreateSimpleReference(section, sourceLineNumbers, "CustomAction", "WixDependencyCheck_ARM");
}
else
{
// All other supported platforms use x86.
this.ParseHelper.CreateSimpleReference(section, sourceLineNumbers, "CustomAction", "WixDependencyCheck");
}
// Generate registry rows for the provider using binder properties.
string keyProvides = String.Concat(DependencyCommon.RegistryRoot, key);
row = this.ParseHelper.CreateRow(section, sourceLineNumbers, "Registry", this.ParseHelper.CreateIdentifier("reg", id.Id, "(Default)"));
row.Set(1, -1);
row.Set(2, keyProvides);
row.Set(4, "[ProductCode]");
row.Set(5, parentId);
// Use the Version registry value and use that as a potential key path.
Identifier idVersion = this.ParseHelper.CreateIdentifier("reg", id.Id, "Version");
keyPath = new ComponentKeyPath() { Id = idVersion.Id, Explicit = false, Type = ComponentKeyPathType.Registry };
row = this.ParseHelper.CreateRow(section, sourceLineNumbers, "Registry", idVersion);
row.Set(1, -1);
row.Set(2, keyProvides);
row.Set(3, "Version");
row.Set(4, !String.IsNullOrEmpty(version) ? version : "[ProductVersion]");
row.Set(5, parentId);
row = this.ParseHelper.CreateRow(section, sourceLineNumbers, "Registry", this.ParseHelper.CreateIdentifier("reg", id.Id, "DisplayName"));
row.Set(1, -1);
row.Set(2, keyProvides);
row.Set(3, "DisplayName");
row.Set(4, !String.IsNullOrEmpty(displayName) ? displayName : "[ProductName]");
row.Set(5, parentId);
if (0 != attributes)
{
row = this.ParseHelper.CreateRow(section, sourceLineNumbers, "Registry", this.ParseHelper.CreateIdentifier("reg", id.Id, "Attributes"));
row.Set(1, -1);
row.Set(2, keyProvides);
row.Set(3, "Attributes");
row.Set(4, String.Concat("#", attributes.ToString(CultureInfo.InvariantCulture.NumberFormat)));
row.Set(5, parentId);
}
}
}
return keyPath;
}
/// <summary>
/// Processes the Requires element.
/// </summary>
/// <param name="node">The XML node for the Requires element.</param>
/// <param name="providerId">The parent provider identifier.</param>
/// <param name="requiresAction">Whether the Requires custom action should be referenced.</param>
private void ParseRequiresElement(Intermediate intermediate, IntermediateSection section, XElement node, string providerId, bool requiresAction)
{
SourceLineNumber sourceLineNumbers = this.ParseHelper.GetSourceLineNumbers(node);
Identifier id = null;
string providerKey = null;
string minVersion = null;
string maxVersion = null;
int attributes = 0;
int illegalChar = -1;
foreach (XAttribute attrib in node.Attributes())
{
if (String.IsNullOrEmpty(attrib.Name.NamespaceName) || this.Namespace == attrib.Name.Namespace)
{
switch (attrib.Name.LocalName)
{
case "Id":
id = this.ParseHelper.GetAttributeIdentifier(sourceLineNumbers, attrib);
break;
case "ProviderKey":
providerKey = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib);
break;
case "Minimum":
minVersion = this.ParseHelper.GetAttributeVersionValue(sourceLineNumbers, attrib);
break;
case "Maximum":
maxVersion = this.ParseHelper.GetAttributeVersionValue(sourceLineNumbers, attrib);
break;
case "IncludeMinimum":
if (YesNoType.Yes == this.ParseHelper.GetAttributeYesNoValue(sourceLineNumbers, attrib))
{
attributes |= DependencyCommon.RequiresAttributesMinVersionInclusive;
}
break;
case "IncludeMaximum":
if (YesNoType.Yes == this.ParseHelper.GetAttributeYesNoValue(sourceLineNumbers, attrib))
{
attributes |= DependencyCommon.RequiresAttributesMaxVersionInclusive;
}
break;
default:
this.ParseHelper.UnexpectedAttribute(node, attrib);
break;
}
}
else
{
this.ParseHelper.ParseExtensionAttribute(this.Context.Extensions, intermediate, section, node, attrib);
}
}
this.ParseHelper.ParseForExtensionElements(this.Context.Extensions, intermediate, section, node);
if (null == id)
{
// Generate an ID only if this element is authored under a Provides element; otherwise, a RequiresRef
// element will be necessary and the Id attribute will be required.
if (!String.IsNullOrEmpty(providerId))
{
id = this.ParseHelper.CreateIdentifier("dep", node.Name.LocalName, providerKey);
}
else
{
this.Messaging.Write(ErrorMessages.ExpectedAttributeWhenElementNotUnderElement(sourceLineNumbers, node.Name.LocalName, "Id", "Provides"));
id = Identifier.Invalid;
}
}
if (String.IsNullOrEmpty(providerKey))
{
this.Messaging.Write(ErrorMessages.ExpectedAttribute(sourceLineNumbers, node.Name.LocalName, "ProviderKey"));
}
// Make sure the key does not contain any illegal characters.
else if (0 <= (illegalChar = providerKey.IndexOfAny(DependencyCommon.InvalidCharacters)))
{
StringBuilder sb = new StringBuilder(DependencyCommon.InvalidCharacters.Length * 2);
Array.ForEach<char>(DependencyCommon.InvalidCharacters, c => sb.Append(c).Append(" "));
this.Messaging.Write(DependencyErrors.IllegalCharactersInProvider(sourceLineNumbers, "ProviderKey", providerKey[illegalChar], sb.ToString()));
}
if (!this.Messaging.EncounteredError)
{
// Reference the Require custom action if required.
if (requiresAction)
{
if (Platform.ARM == this.Context.Platform)
{
// Ensure the ARM version of the CA is referenced.
this.ParseHelper.CreateSimpleReference(section, sourceLineNumbers, "CustomAction", "WixDependencyRequire_ARM");
}
else
{
// All other supported platforms use x86.
this.ParseHelper.CreateSimpleReference(section, sourceLineNumbers, "CustomAction", "WixDependencyRequire");
}
}
var row = this.ParseHelper.CreateRow(section, sourceLineNumbers, "WixDependency", id);
row.Set(1, providerKey);
row.Set(2, minVersion);
row.Set(3, maxVersion);
if (0 != attributes)
{
row.Set(4, attributes);
}
// Create the relationship between this WixDependency row and the WixDependencyProvider row.
if (!String.IsNullOrEmpty(providerId))
{
// Create the relationship between the WixDependency row and the parent WixDependencyProvider row.
row = this.ParseHelper.CreateRow(section, sourceLineNumbers, "WixDependencyRef");
row.Set(0, providerId);
row.Set(1, id.Id);
}
}
}
/// <summary>
/// Processes the RequiresRef element.
/// </summary>
/// <param name="node">The XML node for the RequiresRef element.</param>
/// <param name="providerId">The parent provider identifier.</param>
/// <param name="requiresAction">Whether the Requires custom action should be referenced.</param>
private void ParseRequiresRefElement(Intermediate intermediate, IntermediateSection section, XElement node, string providerId, bool requiresAction)
{
SourceLineNumber sourceLineNumbers = this.ParseHelper.GetSourceLineNumbers(node);
string id = null;
foreach (XAttribute attrib in node.Attributes())
{
if (String.IsNullOrEmpty(attrib.Name.NamespaceName) || this.Namespace == attrib.Name.Namespace)
{
switch (attrib.Name.LocalName)
{
case "Id":
id = this.ParseHelper.GetAttributeIdentifierValue(sourceLineNumbers, attrib);
break;
default:
this.ParseHelper.UnexpectedAttribute(node, attrib);
break;
}
}
else
{
this.ParseHelper.ParseExtensionAttribute(this.Context.Extensions, intermediate, section, node, attrib);
}
}
this.ParseHelper.ParseForExtensionElements(this.Context.Extensions, intermediate, section, node);
if (String.IsNullOrEmpty(id))
{
this.Messaging.Write(ErrorMessages.ExpectedAttribute(sourceLineNumbers, node.Name.LocalName, "Id"));
}
if (!this.Messaging.EncounteredError)
{
// Reference the Require custom action if required.
if (requiresAction)
{
if (Platform.ARM == this.Context.Platform)
{
// Ensure the ARM version of the CA is referenced.
this.ParseHelper.CreateSimpleReference(section, sourceLineNumbers, "CustomAction", "WixDependencyRequire_ARM");
}
else
{
// All other supported platforms use x86.
this.ParseHelper.CreateSimpleReference(section, sourceLineNumbers, "CustomAction", "WixDependencyRequire");
}
}
// Create a link dependency on the row that contains information we'll need during bind.
this.ParseHelper.CreateSimpleReference(section, sourceLineNumbers, "WixDependency", id);
// Create the relationship between the WixDependency row and the parent WixDependencyProvider row.
var row = this.ParseHelper.CreateRow(section, sourceLineNumbers, "WixDependencyRef");
row.Set(0, providerId);
row.Set(1, id);
}
}
}
}
|