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
|
// 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.PowerShell
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml.Linq;
using WixToolset.Data;
using WixToolset.Data.Symbols;
using WixToolset.Extensibility;
/// <summary>
/// The compiler for the WiX Toolset PowerShell Extension.
/// </summary>
public sealed class PSCompiler : BaseCompilerExtension
{
private const string KeyFormat = @"SOFTWARE\Microsoft\PowerShell\{0}\PowerShellSnapIns\{1}";
private const string VarPrefix = "PSVersionMajor";
public override XNamespace Namespace => "http://wixtoolset.org/schemas/v4/wxs/powershell";
/// <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)
{
switch (parentElement.Name.LocalName)
{
case "File":
var fileId = context["FileId"];
var componentId = context["ComponentId"];
switch (element.Name.LocalName)
{
case "FormatsFile":
this.ParseExtensionsFile(intermediate, section, element, "Formats", fileId, componentId);
break;
case "SnapIn":
this.ParseSnapInElement(intermediate, section, element, fileId, componentId);
break;
case "TypesFile":
this.ParseExtensionsFile(intermediate, section, element, "Types", fileId, componentId);
break;
default:
this.ParseHelper.UnexpectedElement(parentElement, element);
break;
}
break;
default:
this.ParseHelper.UnexpectedElement(parentElement, element);
break;
}
}
/// <summary>
/// Parses a SnapIn element.
/// </summary>
/// <param name="node">Element to parse.</param>
/// <param name="fileId">Identifier for parent file.</param>
/// <param name="componentId">Identifier for parent component.</param>
private void ParseSnapInElement(Intermediate intermediate, IntermediateSection section, XElement node, string fileId, string componentId)
{
var sourceLineNumbers = this.ParseHelper.GetSourceLineNumbers(node);
string id = null;
string customSnapInType = null;
string description = null;
string descriptionIndirect = null;
var requiredPowerShellVersion = CompilerConstants.IllegalVersion;
string vendor = null;
string vendorIndirect = null;
string version = null;
foreach (var 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;
case "CustomSnapInType":
customSnapInType = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib);
break;
case "Description":
description = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib);
break;
case "DescriptionIndirect":
descriptionIndirect = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib);
break;
case "RequiredPowerShellVersion":
var ver = this.ParseHelper.GetAttributeVersionValue(sourceLineNumbers, attrib);
requiredPowerShellVersion = new Version(ver);
break;
case "Vendor":
vendor = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib);
break;
case "VendorIndirect":
vendorIndirect = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib);
break;
case "Version":
version = this.ParseHelper.GetAttributeVersionValue(sourceLineNumbers, attrib);
break;
default:
this.ParseHelper.UnexpectedAttribute(node, attrib);
break;
}
}
else
{
this.ParseHelper.ParseExtensionAttribute(this.Context.Extensions, intermediate, section, node, attrib);
}
}
if (null == id)
{
this.Messaging.Write(ErrorMessages.ExpectedAttribute(sourceLineNumbers, node.Name.LocalName, "Id"));
}
// Default to require PowerShell 1.0.
if (CompilerConstants.IllegalVersion == requiredPowerShellVersion)
{
requiredPowerShellVersion = new Version(1, 0);
}
// If the snap-in version isn't explicitly specified, get it
// from the assembly version at bind time.
if (null == version)
{
version = String.Format("!(bind.assemblyVersion.{0})", fileId);
}
foreach (var child in node.Elements())
{
if (this.Namespace == child.Name.Namespace)
{
switch (child.Name.LocalName)
{
case "FormatsFile":
this.ParseExtensionsFile(intermediate, section, child, "Formats", id, componentId);
break;
case "TypesFile":
this.ParseExtensionsFile(intermediate, section, child, "Types", id, componentId);
break;
default:
this.ParseHelper.UnexpectedElement(node, child);
break;
}
}
else
{
this.ParseHelper.ParseExtensionElement(this.Context.Extensions, intermediate, section, node, child);
}
}
// Get the major part of the required PowerShell version which is
// needed for the registry key, and put that into a WiX variable
// for use in Formats and Types files. PowerShell v2 still uses 1.
var major = (2 == requiredPowerShellVersion.Major) ? 1 : requiredPowerShellVersion.Major;
var variableId = new Identifier(AccessModifier.Global, String.Format(CultureInfo.InvariantCulture, "{0}_{1}", VarPrefix, id));
section.AddSymbol(new WixVariableSymbol(sourceLineNumbers, variableId)
{
Value = major.ToString(CultureInfo.InvariantCulture),
Overridable = false,
});
var registryRoot = RegistryRootType.LocalMachine; // HKLM
var registryKey = String.Format(CultureInfo.InvariantCulture, KeyFormat, major, id);
this.ParseHelper.CreateRegistrySymbol(section, sourceLineNumbers, registryRoot, registryKey, "ApplicationBase", String.Format(CultureInfo.InvariantCulture, "[${0}]", componentId), componentId);
// set the assembly name automatically when binding.
// processorArchitecture is not handled correctly by PowerShell v1.0
// so format the assembly name explicitly.
var assemblyName = String.Format(CultureInfo.InvariantCulture, "!(bind.assemblyName.{0}), Version=!(bind.assemblyVersion.{0}), Culture=!(bind.assemblyCulture.{0}), PublicKeyToken=!(bind.assemblyPublicKeyToken.{0})", fileId);
this.ParseHelper.CreateRegistrySymbol(section, sourceLineNumbers, registryRoot, registryKey, "AssemblyName", assemblyName, componentId);
if (null != customSnapInType)
{
this.ParseHelper.CreateRegistrySymbol(section, sourceLineNumbers, registryRoot, registryKey, "CustomPSSnapInType", customSnapInType, componentId);
}
if (null != description)
{
this.ParseHelper.CreateRegistrySymbol(section, sourceLineNumbers, registryRoot, registryKey, "Description", description, componentId);
}
if (null != descriptionIndirect)
{
this.ParseHelper.CreateRegistrySymbol(section, sourceLineNumbers, registryRoot, registryKey, "DescriptionIndirect", descriptionIndirect, componentId);
}
this.ParseHelper.CreateRegistrySymbol(section, sourceLineNumbers, registryRoot, registryKey, "ModuleName", String.Format(CultureInfo.InvariantCulture, "[#{0}]", fileId), componentId);
this.ParseHelper.CreateRegistrySymbol(section, sourceLineNumbers, registryRoot, registryKey, "PowerShellVersion", requiredPowerShellVersion.ToString(2), componentId);
if (null != vendor)
{
this.ParseHelper.CreateRegistrySymbol(section, sourceLineNumbers, registryRoot, registryKey, "Vendor", vendor, componentId);
}
if (null != vendorIndirect)
{
this.ParseHelper.CreateRegistrySymbol(section, sourceLineNumbers, registryRoot, registryKey, "VendorIndirect", vendorIndirect, componentId);
}
if (null != version)
{
this.ParseHelper.CreateRegistrySymbol(section, sourceLineNumbers, registryRoot, registryKey, "Version", version, componentId);
}
}
/// <summary>
/// Parses a FormatsFile and TypesFile element.
/// </summary>
/// <param name="node">Element to parse.</param>
/// <param name="valueName">Registry value name.</param>
/// <param name="id">Idendifier for parent file or snap-in.</param>
/// <param name="componentId">Identifier for parent component.</param>
private void ParseExtensionsFile(Intermediate intermediate, IntermediateSection section, XElement node, string valueName, string id, string componentId)
{
var sourceLineNumbers = this.ParseHelper.GetSourceLineNumbers(node);
string fileId = null;
string snapIn = null;
foreach (var attrib in node.Attributes())
{
if (String.IsNullOrEmpty(attrib.Name.NamespaceName) || this.Namespace == attrib.Name.Namespace)
{
switch (attrib.Name.LocalName)
{
case "FileId":
fileId = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib);
snapIn = id;
break;
case "SnapIn":
fileId = id;
snapIn = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib);
break;
default:
this.ParseHelper.UnexpectedAttribute(node, attrib);
break;
}
}
else
{
this.ParseHelper.ParseExtensionAttribute(this.Context.Extensions, intermediate, section, node, attrib);
}
}
if (null == fileId && null == snapIn)
{
this.Messaging.Write(PSErrors.NeitherIdSpecified(sourceLineNumbers, valueName));
}
this.ParseHelper.ParseForExtensionElements(this.Context.Extensions, intermediate, section, node);
var registryRoot = RegistryRootType.LocalMachine; // HKLM
var registryKey = String.Format(CultureInfo.InvariantCulture, KeyFormat, String.Format(CultureInfo.InvariantCulture, "!(wix.{0}_{1})", VarPrefix, snapIn), snapIn);
this.ParseHelper.CreateSimpleReference(section, sourceLineNumbers, SymbolDefinitions.File, fileId);
this.ParseHelper.CreateRegistrySymbol(section, sourceLineNumbers, registryRoot, registryKey, valueName, String.Format(CultureInfo.InvariantCulture, "[#{0}]", fileId), componentId, RegistryValueType.MultiString, RegistryValueActionType.Append);
}
}
}
|