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
|
// 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.Tools
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using WixToolset.Core;
using WixToolset.Data;
using WixToolset.Data.Bind;
using WixToolset.Extensibility;
using WixToolset.Extensibility.Services;
/// <summary>
/// The main entry point for light.
/// </summary>
public sealed class Light
{
LightCommandLine commandLine;
private IEnumerable<IExtensionData> extensionData;
//private IEnumerable<IBinderExtension> binderExtensions;
//private IEnumerable<IBinderFileManager> fileManagers;
/// <summary>
/// The main entry point for light.
/// </summary>
/// <param name="args">Commandline arguments for the application.</param>
/// <returns>Returns the application error code.</returns>
[MTAThread]
public static int Main(string[] args)
{
var serviceProvider = new WixToolsetServiceProvider();
var listener = new ConsoleMessageListener("WIX", "light.exe");
Light light = new Light();
return light.Run(serviceProvider, listener, args);
}
/// <summary>
/// Main running method for the application.
/// </summary>
/// <param name="args">Commandline arguments to the application.</param>
/// <returns>Returns the application error code.</returns>
public int Run(IServiceProvider serviceProvider, IMessageListener listener, string[] args)
{
var messaging = serviceProvider.GetService<IMessaging>();
messaging.SetListener(listener);
try
{
var unparsed = this.ParseCommandLineAndLoadExtensions(serviceProvider, messaging, args);
if (!messaging.EncounteredError)
{
if (this.commandLine.ShowLogo)
{
AppCommon.DisplayToolHeader();
}
if (this.commandLine.ShowHelp)
{
PrintHelp();
AppCommon.DisplayToolFooter();
}
else
{
foreach (string arg in unparsed)
{
messaging.Write(WarningMessages.UnsupportedCommandLineArgument(arg));
}
this.Bind(serviceProvider, messaging);
}
}
}
catch (WixException we)
{
messaging.Write(we.Error);
}
catch (Exception e)
{
messaging.Write(ErrorMessages.UnexpectedException(e.Message, e.GetType().ToString(), e.StackTrace));
if (e is NullReferenceException || e is SEHException)
{
throw;
}
}
return messaging.LastErrorNumber;
}
/// <summary>
/// Parse command line and load all the extensions.
/// </summary>
/// <param name="args">Command line arguments to be parsed.</param>
private IEnumerable<string> ParseCommandLineAndLoadExtensions(IServiceProvider serviceProvider, IMessaging messaging, string[] args)
{
this.commandLine = new LightCommandLine(messaging);
string[] unprocessed = this.commandLine.Parse(args);
if (messaging.EncounteredError)
{
return unprocessed;
}
// Load extensions.
var extensionManager = CreateExtensionManagerWithStandardBackends(serviceProvider);
foreach (string extension in this.commandLine.Extensions)
{
extensionManager.Load(extension);
}
// Extension data command line processing.
var context = serviceProvider.GetService<ICommandLineContext>();
context.Arguments = null;
context.ExtensionManager = extensionManager;
context.Messaging = messaging;
context.ParsedArguments = args;
var commandLineExtensions = extensionManager.Create<IExtensionCommandLine>();
foreach (var extension in commandLineExtensions)
{
extension.PreParse(context);
}
// Process unproccessed arguments.
List<string> actuallyUnprocessed = new List<string>();
foreach (var arg in unprocessed)
{
if (!this.TryParseCommandLineArgumentWithExtension(arg, commandLineExtensions))
{
actuallyUnprocessed.Add(arg);
}
}
return this.commandLine.ParsePostExtensions(actuallyUnprocessed.ToArray());
}
private void Bind(IServiceProvider serviceProvider, IMessaging messaging)
{
var output = this.LoadWixout(messaging);
if (messaging.EncounteredError)
{
return;
}
var intermediateFolder = this.commandLine.IntermediateFolder;
if (String.IsNullOrEmpty(intermediateFolder))
{
intermediateFolder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
}
var localizations = this.LoadLocalizationFiles(messaging, this.commandLine.LocalizationFiles);
if (messaging.EncounteredError)
{
return;
}
ResolveResult resolveResult;
{
var resolver = new Resolver(serviceProvider);
resolver.BindPaths = this.commandLine.BindPaths;
resolver.IntermediateFolder = intermediateFolder;
resolver.IntermediateRepresentation = output;
resolver.Localizations = localizations;
resolveResult = resolver.Execute();
}
if (messaging.EncounteredError)
{
return;
}
BindResult bindResult;
{
var binder = new Binder(serviceProvider);
binder.CabbingThreadCount = this.commandLine.CabbingThreadCount;
binder.CabCachePath = this.commandLine.CabCachePath;
binder.Codepage = resolveResult.Codepage;
binder.DefaultCompressionLevel = this.commandLine.DefaultCompressionLevel;
binder.DelayedFields = resolveResult.DelayedFields;
binder.ExpectedEmbeddedFiles = resolveResult.ExpectedEmbeddedFiles;
binder.Ices = this.commandLine.Ices;
binder.IntermediateFolder = intermediateFolder;
binder.IntermediateRepresentation = resolveResult.IntermediateRepresentation;
binder.OutputPath = this.commandLine.OutputFile;
binder.OutputPdbPath = Path.ChangeExtension(this.commandLine.OutputFile, ".wixpdb");
binder.SuppressIces = this.commandLine.SuppressIces;
binder.SuppressValidation = this.commandLine.SuppressValidation;
bindResult = binder.Execute();
}
if (messaging.EncounteredError)
{
return;
}
{
var layout = new Layout(serviceProvider);
layout.FileTransfers = bindResult.FileTransfers;
layout.ContentFilePaths = bindResult.ContentFilePaths;
layout.ContentsFile = this.commandLine.ContentsFile;
layout.OutputsFile = this.commandLine.OutputsFile;
layout.BuiltOutputsFile = this.commandLine.BuiltOutputsFile;
layout.SuppressAclReset = this.commandLine.SuppressAclReset;
layout.Execute();
}
}
private void Run(IMessaging messaging)
{
#if false
// Initialize the variable resolver from the command line.
WixVariableResolver wixVariableResolver = new WixVariableResolver();
foreach (var wixVar in this.commandLine.Variables)
{
wixVariableResolver.AddVariable(wixVar.Key, wixVar.Value);
}
// Initialize the linker from the command line.
Linker linker = new Linker();
linker.UnreferencedSymbolsFile = this.commandLine.UnreferencedSymbolsFile;
linker.ShowPedanticMessages = this.commandLine.ShowPedanticMessages;
linker.WixVariableResolver = wixVariableResolver;
foreach (IExtensionData data in this.extensionData)
{
linker.AddExtensionData(data);
}
// Initialize the binder from the command line.
WixToolset.Binder binder = new WixToolset.Binder();
binder.CabCachePath = this.commandLine.CabCachePath;
binder.ContentsFile = this.commandLine.ContentsFile;
binder.BuiltOutputsFile = this.commandLine.BuiltOutputsFile;
binder.OutputsFile = this.commandLine.OutputsFile;
binder.WixprojectFile = this.commandLine.WixprojectFile;
binder.BindPaths.AddRange(this.commandLine.BindPaths);
binder.CabbingThreadCount = this.commandLine.CabbingThreadCount;
if (this.commandLine.DefaultCompressionLevel.HasValue)
{
binder.DefaultCompressionLevel = this.commandLine.DefaultCompressionLevel.Value;
}
binder.Ices.AddRange(this.commandLine.Ices);
binder.SuppressIces.AddRange(this.commandLine.SuppressIces);
binder.SuppressAclReset = this.commandLine.SuppressAclReset;
binder.SuppressLayout = this.commandLine.SuppressLayout;
binder.SuppressValidation = this.commandLine.SuppressValidation;
binder.PdbFile = this.commandLine.SuppressWixPdb ? null : this.commandLine.PdbFile;
binder.TempFilesLocation = AppCommon.GetTempLocation();
binder.WixVariableResolver = wixVariableResolver;
foreach (IBinderExtension extension in this.binderExtensions)
{
binder.AddExtension(extension);
}
foreach (IBinderFileManager fileManager in this.fileManagers)
{
binder.AddExtension(fileManager);
}
// Initialize the localizer.
Localizer localizer = this.InitializeLocalization(linker.TableDefinitions);
if (messaging.EncounteredError)
{
return;
}
wixVariableResolver.Localizer = localizer;
linker.Localizer = localizer;
binder.Localizer = localizer;
// Loop through all the believed object files.
List<Section> sections = new List<Section>();
Output output = null;
foreach (string inputFile in this.commandLine.Files)
{
string inputFileFullPath = Path.GetFullPath(inputFile);
FileFormat format = FileStructure.GuessFileFormatFromExtension(Path.GetExtension(inputFileFullPath));
bool retry;
do
{
retry = false;
try
{
switch (format)
{
case FileFormat.Wixobj:
Intermediate intermediate = Intermediate.Load(inputFileFullPath, linker.TableDefinitions, this.commandLine.SuppressVersionCheck);
sections.AddRange(intermediate.Sections);
break;
case FileFormat.Wixlib:
Library library = Library.Load(inputFileFullPath, linker.TableDefinitions, this.commandLine.SuppressVersionCheck);
AddLibraryLocalizationsToLocalizer(library, this.commandLine.Cultures, localizer);
sections.AddRange(library.Sections);
break;
default:
output = Output.Load(inputFileFullPath, this.commandLine.SuppressVersionCheck);
break;
}
}
catch (WixUnexpectedFileFormatException e)
{
format = e.FileFormat;
retry = (FileFormat.Wixobj == format || FileFormat.Wixlib == format || FileFormat.Wixout == format); // .wixobj, .wixout and .wixout are supported by light.
if (!retry)
{
messaging.OnMessage(e.Error);
}
}
} while (retry);
}
// Stop processing if any errors were found loading object files.
if (messaging.EncounteredError)
{
return;
}
// and now for the fun part
if (null == output)
{
OutputType expectedOutputType = OutputType.Unknown;
if (!String.IsNullOrEmpty(this.commandLine.OutputFile))
{
expectedOutputType = Output.GetOutputType(Path.GetExtension(this.commandLine.OutputFile));
}
output = linker.Link(sections, expectedOutputType);
// If an error occurred during linking, stop processing.
if (null == output)
{
return;
}
}
else if (0 != sections.Count)
{
throw new InvalidOperationException(LightStrings.EXP_CannotLinkObjFilesWithOutpuFile);
}
bool tidy = true; // clean up after ourselves by default.
try
{
// only output the xml if its a patch build or user specfied to only output wixout
string outputFile = this.commandLine.OutputFile;
string outputExtension = Path.GetExtension(outputFile);
if (this.commandLine.OutputXml || OutputType.Patch == output.Type)
{
if (String.IsNullOrEmpty(outputExtension) || outputExtension.Equals(".wix", StringComparison.Ordinal))
{
outputExtension = (OutputType.Patch == output.Type) ? ".wixmsp" : ".wixout";
outputFile = Path.ChangeExtension(outputFile, outputExtension);
}
output.Save(outputFile);
}
else // finish creating the MSI/MSM
{
if (String.IsNullOrEmpty(outputExtension) || outputExtension.Equals(".wix", StringComparison.Ordinal))
{
outputExtension = Output.GetExtension(output.Type);
outputFile = Path.ChangeExtension(outputFile, outputExtension);
}
binder.Bind(output, outputFile);
}
}
catch (WixException we) // keep files around for debugging IDT issues.
{
if (we is WixInvalidIdtException)
{
tidy = false;
}
throw;
}
catch (Exception) // keep files around for debugging unexpected exceptions.
{
tidy = false;
throw;
}
finally
{
if (null != binder)
{
binder.Cleanup(tidy);
}
}
return;
#endif
}
#if false
private Localizer InitializeLocalization(TableDefinitionCollection tableDefinitions)
{
Localizer localizer = null;
// Instantiate the localizer and load any localization files.
if (!this.commandLine.SuppressLocalization || 0 < this.commandLine.LocalizationFiles.Count || null != this.commandLine.Cultures || !this.commandLine.OutputXml)
{
List<Localization> localizations = new List<Localization>();
// Load each localization file.
foreach (string localizationFile in this.commandLine.LocalizationFiles)
{
Localization localization = Localizer.ParseLocalizationFile(localizationFile, tableDefinitions);
if (null != localization)
{
localizations.Add(localization);
}
}
localizer = new Localizer();
if (null != this.commandLine.Cultures)
{
// Alocalizations in order specified in cultures.
foreach (string culture in this.commandLine.Cultures)
{
foreach (Localization localization in localizations)
{
if (culture.Equals(localization.Culture, StringComparison.OrdinalIgnoreCase))
{
localizer.AddLocalization(localization);
}
}
}
}
else // no cultures specified, so try neutral culture and if none of those add all loc files.
{
bool neutralFound = false;
foreach (Localization localization in localizations)
{
if (String.IsNullOrEmpty(localization.Culture))
{
// If a neutral wxl was provided use it.
localizer.AddLocalization(localization);
neutralFound = true;
}
}
if (!neutralFound)
{
// No cultures were specified and no neutral wxl are available, include all of the loc files.
foreach (Localization localization in localizations)
{
localizer.AddLocalization(localization);
}
}
}
// Load localizations provided by extensions with data.
foreach (IExtensionData data in this.extensionData)
{
Library library = data.GetLibrary(tableDefinitions);
if (null != library)
{
// Load the extension's default culture if it provides one and no cultures were specified.
string[] extensionCultures = this.commandLine.Cultures;
if (null == extensionCultures && null != data.DefaultCulture)
{
extensionCultures = new string[] { data.DefaultCulture };
}
AddLibraryLocalizationsToLocalizer(library, extensionCultures, localizer);
}
}
}
return localizer;
}
private void AddLibraryLocalizationsToLocalizer(Library library, string[] cultures, Localizer localizer)
{
foreach (Localization localization in library.GetLocalizations(cultures))
{
localizer.AddLocalization(localization);
}
}
#endif
private bool TryParseCommandLineArgumentWithExtension(string arg, IEnumerable<IExtensionCommandLine> extensions)
{
foreach (var extension in extensions)
{
// TODO: decide what to do with "IParseCommandLine" argument.
if (extension.TryParseArgument(null, arg))
{
return true;
}
}
return false;
}
private IEnumerable<Localization> LoadLocalizationFiles(IMessaging messaging, IEnumerable<string> locFiles)
{
foreach (var loc in locFiles)
{
var localization = Localizer.ParseLocalizationFile(messaging, loc);
yield return localization;
}
}
private Intermediate LoadWixout(IMessaging messaging)
{
var path = this.commandLine.Files.Single();
return Intermediate.Load(path);
}
private static IExtensionManager CreateExtensionManagerWithStandardBackends(IServiceProvider serviceProvider)
{
var extensionManager = serviceProvider.GetService<IExtensionManager>();
foreach (var type in new[] { typeof(WixToolset.Core.Burn.WixToolsetStandardBackend), typeof(WixToolset.Core.WindowsInstaller.WixToolsetStandardBackend) })
{
extensionManager.Add(type.Assembly);
}
return extensionManager;
}
private static void PrintHelp()
{
string lightArgs = LightStrings.CommandLineArguments;
Console.WriteLine(String.Format(LightStrings.HelpMessage, lightArgs));
}
private class ConsoleMessageListener : IMessageListener
{
public ConsoleMessageListener(string shortName, string longName)
{
this.ShortAppName = shortName;
this.LongAppName = longName;
PrepareConsoleForLocalization();
}
public string LongAppName { get; }
public string ShortAppName { get; }
public void Write(Message message)
{
var filename = message.SourceLineNumbers?.FileName ?? this.LongAppName;
var line = message.SourceLineNumbers?.LineNumber ?? -1;
var type = message.Level.ToString().ToLowerInvariant();
var output = message.Level >= MessageLevel.Warning ? Console.Out : Console.Error;
if (line > 0)
{
filename = String.Concat(filename, "(", line, ")");
}
output.WriteLine("{0} : {1} {2}{3:0000}: {4}", filename, type, this.ShortAppName, message.Id, message.ToString());
}
public void Write(string message)
{
Console.Out.WriteLine(message);
}
private static void PrepareConsoleForLocalization()
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture.GetConsoleFallbackUICulture();
if (Console.OutputEncoding.CodePage != Encoding.UTF8.CodePage &&
Console.OutputEncoding.CodePage != Thread.CurrentThread.CurrentUICulture.TextInfo.OEMCodePage &&
Console.OutputEncoding.CodePage != Thread.CurrentThread.CurrentUICulture.TextInfo.ANSICodePage)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
}
}
}
}
}
|