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
|
// 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.VisualStudio
{
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using WixToolset.Data;
using WixToolset.Data.Symbols;
using WixToolset.Data.WindowsInstaller;
using WixToolset.Extensibility;
using WixToolset.Extensibility.Data;
/// <summary>
/// The compiler for the WiX Toolset Visual Studio Extension.
/// </summary>
public sealed class VSCompiler : BaseCompilerExtension
{
internal const int MsidbCustomActionTypeExe = 0x00000002; // Target = command line args
internal const int MsidbCustomActionTypeProperty = 0x00000030; // Source = full path to executable
internal const int MsidbCustomActionTypeContinue = 0x00000040; // ignore action return status; continue running
internal const int MsidbCustomActionTypeRollback = 0x00000100; // in conjunction with InScript: queue in Rollback script
internal const int MsidbCustomActionTypeInScript = 0x00000400; // queue for execution within script
internal const int MsidbCustomActionTypeNoImpersonate = 0x00000800; // queue for not impersonating
public override XNamespace Namespace => "http://wixtoolset.org/schemas/v4/wxs/vs";
public override void ParseElement(Intermediate intermediate, IntermediateSection section, XElement parentElement, XElement element, IDictionary<string, string> context)
{
switch (parentElement.Name.LocalName)
{
case "Component":
switch (element.Name.LocalName)
{
case "VsixPackage":
this.ParseVsixPackageElement(intermediate, section, element, context["ComponentId"], null);
break;
default:
this.ParseHelper.UnexpectedElement(parentElement, element);
break;
}
break;
case "File":
switch (element.Name.LocalName)
{
case "VsixPackage":
this.ParseVsixPackageElement(intermediate, section, element, context["ComponentId"], context["FileId"]);
break;
default:
this.ParseHelper.UnexpectedElement(parentElement, element);
break;
}
break;
case "Fragment":
case "Module":
case "Package":
switch (element.Name.LocalName)
{
case "FindVisualStudio":
this.ParseFindVisualStudioElement(intermediate, section, element);
break;
default:
this.ParseHelper.UnexpectedElement(parentElement, element);
break;
}
break;
default:
this.ParseHelper.UnexpectedElement(parentElement, element);
break;
}
}
private void ParseFindVisualStudioElement(Intermediate intermediate, IntermediateSection section, XElement element)
{
var sourceLineNumbers = this.ParseHelper.GetSourceLineNumbers(element);
this.ParseHelper.ParseForExtensionElements(this.Context.Extensions, intermediate, section, element);
this.ParseHelper.CreateCustomActionReference(sourceLineNumbers, section, "Wix4VSFindInstances", this.Context.Platform, CustomActionPlatforms.X86 | CustomActionPlatforms.X64 | CustomActionPlatforms.ARM64);
}
private void ParseVsixPackageElement(Intermediate intermediate, IntermediateSection section, XElement element, string componentId, string fileId)
{
var sourceLineNumbers = this.ParseHelper.GetSourceLineNumbers(element);
var propertyId = "VS_VSIX_INSTALLER_PATH";
string packageId = null;
var permanent = YesNoType.NotSet;
string target = null;
string targetVersion = null;
var vital = YesNoType.NotSet;
foreach (var attrib in element.Attributes())
{
if (String.IsNullOrEmpty(attrib.Name.NamespaceName) || this.Namespace == attrib.Name.Namespace)
{
switch (attrib.Name.LocalName)
{
case "File":
if (String.IsNullOrEmpty(fileId))
{
fileId = this.ParseHelper.GetAttributeIdentifierValue(sourceLineNumbers, attrib);
}
else
{
this.Messaging.Write(ErrorMessages.IllegalAttributeWhenNested(sourceLineNumbers, element.Name.LocalName, "File", "File"));
}
break;
case "PackageId":
packageId = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib);
break;
case "Permanent":
permanent = this.ParseHelper.GetAttributeYesNoValue(sourceLineNumbers, attrib);
break;
case "Target":
target = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib);
switch (target.ToLowerInvariant())
{
case "integrated":
case "integratedshell":
target = "IntegratedShell";
break;
case "professional":
target = "Pro";
break;
case "premium":
target = "Premium";
break;
case "ultimate":
target = "Ultimate";
break;
case "vbexpress":
target = "VBExpress";
break;
case "vcexpress":
target = "VCExpress";
break;
case "vcsexpress":
target = "VCSExpress";
break;
case "vwdexpress":
target = "VWDExpress";
break;
}
break;
case "TargetVersion":
targetVersion = this.ParseHelper.GetAttributeVersionValue(sourceLineNumbers, attrib);
break;
case "Vital":
vital = this.ParseHelper.GetAttributeYesNoValue(sourceLineNumbers, attrib);
break;
case "VsixInstallerPathProperty":
propertyId = this.ParseHelper.GetAttributeIdentifierValue(sourceLineNumbers, attrib);
break;
default:
this.ParseHelper.UnexpectedAttribute(element, attrib);
break;
}
}
else
{
this.ParseHelper.ParseExtensionAttribute(this.Context.Extensions, intermediate, section, element, attrib);
}
}
if (String.IsNullOrEmpty(fileId))
{
this.Messaging.Write(ErrorMessages.ExpectedAttribute(sourceLineNumbers, element.Name.LocalName, "File"));
}
if (String.IsNullOrEmpty(packageId))
{
this.Messaging.Write(ErrorMessages.ExpectedAttribute(sourceLineNumbers, element.Name.LocalName, "PackageId"));
}
if (!String.IsNullOrEmpty(target) && String.IsNullOrEmpty(targetVersion))
{
this.Messaging.Write(ErrorMessages.ExpectedAttribute(sourceLineNumbers, element.Name.LocalName, "TargetVersion", "Target"));
}
else if (String.IsNullOrEmpty(target) && !String.IsNullOrEmpty(targetVersion))
{
this.Messaging.Write(ErrorMessages.ExpectedAttribute(sourceLineNumbers, element.Name.LocalName, "Target", "TargetVersion"));
}
this.ParseHelper.ParseForExtensionElements(this.Context.Extensions, intermediate, section, element);
if (!this.Messaging.EncounteredError)
{
// Ensure there is a reference to the AppSearch Property that will find the VsixInstaller.exe.
this.ParseHelper.CreateSimpleReference(section, sourceLineNumbers, SymbolDefinitions.Property, propertyId);
// Ensure there is a reference to the package file (even if we are a child under it).
this.ParseHelper.CreateSimpleReference(section, sourceLineNumbers, SymbolDefinitions.File, fileId);
var cmdlinePrefix = "/q ";
if (!String.IsNullOrEmpty(target))
{
cmdlinePrefix = String.Format("{0} /skuName:{1} /skuVersion:{2}", cmdlinePrefix, target, targetVersion);
}
var installAfter = "WriteRegistryValues"; // by default, come after the registry key registration.
var installNamePerUser = this.ParseHelper.CreateIdentifier("viu", componentId, fileId, "per-user", target, targetVersion);
var installNamePerMachine = this.ParseHelper.CreateIdentifier("vim", componentId, fileId, "per-machine", target, targetVersion);
var installCmdLinePerUser = String.Format("{0} \"[#{1}]\"", cmdlinePrefix, fileId);
var installCmdLinePerMachine = String.Concat(installCmdLinePerUser, " /admin");
var installConditionPerUser = String.Format("NOT ALLUSERS AND ${0}=3", componentId); // only execute if the Component being installed.
var installConditionPerMachine = String.Format("ALLUSERS AND ${0}=3", componentId); // only execute if the Component being installed.
var installPerUserCA = new CustomActionSymbol(sourceLineNumbers, installNamePerUser)
{
ExecutionType = CustomActionExecutionType.Deferred,
Impersonate = true,
};
var installPerMachineCA = new CustomActionSymbol(sourceLineNumbers, installNamePerMachine)
{
ExecutionType = CustomActionExecutionType.Deferred,
Impersonate = false,
};
// If the package is not vital, mark the install action as continue.
if (vital == YesNoType.No)
{
installPerUserCA.IgnoreResult = true;
installPerMachineCA.IgnoreResult = true;
}
else // the package is vital so ensure there is a rollback action scheduled.
{
var rollbackNamePerUser = this.ParseHelper.CreateIdentifier("vru", componentId, fileId, "per-user", target, targetVersion);
var rollbackNamePerMachine = this.ParseHelper.CreateIdentifier("vrm", componentId, fileId, "per-machine", target, targetVersion);
var rollbackCmdLinePerUser = String.Concat(cmdlinePrefix, " /u:\"", packageId, "\"");
var rollbackCmdLinePerMachine = String.Concat(rollbackCmdLinePerUser, " /admin");
var rollbackConditionPerUser = String.Format("NOT ALLUSERS AND NOT Installed AND ${0}=2 AND ?{0}>2", componentId); // NOT Installed && Component being installed but not installed already.
var rollbackConditionPerMachine = String.Format("ALLUSERS AND NOT Installed AND ${0}=2 AND ?{0}>2", componentId); // NOT Installed && Component being installed but not installed already.
var rollbackPerUserCA = new CustomActionSymbol(sourceLineNumbers, rollbackNamePerUser)
{
ExecutionType = CustomActionExecutionType.Rollback,
IgnoreResult = true,
Impersonate = true,
};
var rollbackPerMachineCA = new CustomActionSymbol(sourceLineNumbers, rollbackNamePerMachine)
{
ExecutionType = CustomActionExecutionType.Rollback,
IgnoreResult = true,
Impersonate = false,
};
this.SchedulePropertyExeAction(section, sourceLineNumbers, rollbackNamePerUser, propertyId, rollbackCmdLinePerUser, rollbackPerUserCA, rollbackConditionPerUser, null, installAfter);
this.SchedulePropertyExeAction(section, sourceLineNumbers, rollbackNamePerMachine, propertyId, rollbackCmdLinePerMachine, rollbackPerMachineCA, rollbackConditionPerMachine, null, rollbackNamePerUser.Id);
installAfter = rollbackNamePerMachine.Id;
}
this.SchedulePropertyExeAction(section, sourceLineNumbers, installNamePerUser, propertyId, installCmdLinePerUser, installPerUserCA, installConditionPerUser, null, installAfter);
this.SchedulePropertyExeAction(section, sourceLineNumbers, installNamePerMachine, propertyId, installCmdLinePerMachine, installPerMachineCA, installConditionPerMachine, null, installNamePerUser.Id);
// If not permanent, schedule the uninstall custom action.
if (permanent != YesNoType.Yes)
{
var uninstallNamePerUser = this.ParseHelper.CreateIdentifier("vuu", componentId, fileId, "per-user", target ?? String.Empty, targetVersion ?? String.Empty);
var uninstallNamePerMachine = this.ParseHelper.CreateIdentifier("vum", componentId, fileId, "per-machine", target ?? String.Empty, targetVersion ?? String.Empty);
var uninstallCmdLinePerUser = String.Concat(cmdlinePrefix, " /u:\"", packageId, "\"");
var uninstallCmdLinePerMachine = String.Concat(uninstallCmdLinePerUser, " /admin");
var uninstallConditionPerUser = String.Format("NOT ALLUSERS AND ${0}=2 AND ?{0}>2", componentId); // Only execute if component is being uninstalled.
var uninstallConditionPerMachine = String.Format("ALLUSERS AND ${0}=2 AND ?{0}>2", componentId); // Only execute if component is being uninstalled.
var uninstallPerUserCA = new CustomActionSymbol(sourceLineNumbers, uninstallNamePerUser)
{
ExecutionType = CustomActionExecutionType.Deferred,
IgnoreResult = true,
Impersonate = true,
};
var uninstallPerMachineCA = new CustomActionSymbol(sourceLineNumbers, uninstallNamePerMachine)
{
ExecutionType = CustomActionExecutionType.Deferred,
IgnoreResult = true,
Impersonate = false,
};
this.SchedulePropertyExeAction(section, sourceLineNumbers, uninstallNamePerUser, propertyId, uninstallCmdLinePerUser, uninstallPerUserCA, uninstallConditionPerUser, "InstallFinalize", null);
this.SchedulePropertyExeAction(section, sourceLineNumbers, uninstallNamePerMachine, propertyId, uninstallCmdLinePerMachine, uninstallPerMachineCA, uninstallConditionPerMachine, "InstallFinalize", null);
}
}
}
private void SchedulePropertyExeAction(IntermediateSection section, SourceLineNumber sourceLineNumbers, Identifier name, string source, string cmdline, CustomActionSymbol caTemplate, string condition, string beforeAction, string afterAction)
{
const SequenceTable sequence = SequenceTable.InstallExecuteSequence;
caTemplate.SourceType = CustomActionSourceType.Property;
caTemplate.Source = source;
caTemplate.TargetType = CustomActionTargetType.Exe;
caTemplate.Target = cmdline;
section.AddSymbol(caTemplate);
section.AddSymbol(new WixActionSymbol(sourceLineNumbers, new Identifier(name.Access, sequence, name.Id))
{
SequenceTable = SequenceTable.InstallExecuteSequence,
Action = name.Id,
Condition = condition,
// no explicit sequence
Before = beforeAction,
After = afterAction,
Overridable = false,
});
if (null != beforeAction)
{
if (WindowsInstallerStandard.IsStandardAction(beforeAction))
{
this.ParseHelper.CreateSimpleReference(section, sourceLineNumbers, SymbolDefinitions.WixAction, sequence.ToString(), beforeAction);
}
else
{
this.ParseHelper.CreateSimpleReference(section, sourceLineNumbers, SymbolDefinitions.CustomAction, beforeAction);
}
}
if (null != afterAction)
{
if (WindowsInstallerStandard.IsStandardAction(afterAction))
{
this.ParseHelper.CreateSimpleReference(section, sourceLineNumbers, SymbolDefinitions.WixAction, sequence.ToString(), afterAction);
}
else
{
this.ParseHelper.CreateSimpleReference(section, sourceLineNumbers, SymbolDefinitions.CustomAction, afterAction);
}
}
}
}
}
|