aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/Binder.cs
blob: 2369b6000025684810ab978e83e5973786496953 (plain)
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
// 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.Core
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using WixToolset.Bind;
    using WixToolset.Core.Bind;
    using WixToolset.Data;
    using WixToolset.Data.Bind;
    using WixToolset.Data.Tuples;
    using WixToolset.Extensibility;
    using WixToolset.Extensibility.Services;

    /// <summary>
    /// Binder of the WiX toolset.
    /// </summary>
    public sealed class Binder
    {
        //private BinderCore core;
        //private List<IBinderExtension> extensions;
        //private List<IBinderFileManager> fileManagers;

        public Binder()
        {
            //this.DefaultCompressionLevel = CompressionLevel.High;

            //this.BindPaths = new List<BindPath>();
            //this.TargetBindPaths = new List<BindPath>();
            //this.UpdatedBindPaths = new List<BindPath>();

            //this.extensions = new List<IBinderExtension>();
            //this.fileManagers = new List<IBinderFileManager>();
            //this.inspectorExtensions = new List<InspectorExtension>();

            //this.Ices = new List<string>();
            //this.SuppressIces = new List<string>();
        }

        private IBindContext Context { get; set; }

        //private TableDefinitionCollection TableDefinitions { get; }

        //public IEnumerable<IBackendFactory> BackendFactories { get; set; }

        //public string ContentsFile { private get; set; }

        //public string OutputsFile { private get; set; }

        //public string BuiltOutputsFile { private get; set; }

        //public string WixprojectFile { private get; set; }

        /// <summary>
        /// Gets the list of bindpaths.
        /// </summary>
        //public List<BindPath> BindPaths { get; private set; }

        /// <summary>
        /// Gets the list of target bindpaths.
        /// </summary>
        //public List<BindPath> TargetBindPaths { get; private set; }

        /// <summary>
        /// Gets the list of updated bindpaths.
        /// </summary>
        //public List<BindPath> UpdatedBindPaths { get; private set; }

        /// <summary>
        /// Gets or sets the option to enable building binary delta patches.
        /// </summary>
        /// <value>The option to enable building binary delta patches.</value>
        public bool DeltaBinaryPatch { get; set; }

        /// <summary>
        /// Gets or sets the cabinet cache location.
        /// </summary>
        public string CabCachePath { get; set; }

        /// <summary>
        /// Gets or sets the number of threads to use for cabinet creation.
        /// </summary>
        /// <value>The number of threads to use for cabinet creation.</value>
        public int CabbingThreadCount { get; set; }

        /// <summary>
        /// Gets or sets the default compression level to use for cabinets
        /// that don't have their compression level explicitly set.
        /// </summary>
        //public CompressionLevel DefaultCompressionLevel { get; set; }

        /// <summary>
        /// Gets and sets the location to save the WixPdb.
        /// </summary>
        /// <value>The location in which to save the WixPdb. Null if the the WixPdb should not be output.</value>
        //public string PdbFile { get; set; }

        //public List<string> Ices { get; private set; }

        //public List<string> SuppressIces { get; private set; }

        /// <summary>
        /// Gets and sets the option to suppress resetting ACLs by the binder.
        /// </summary>
        /// <value>The option to suppress resetting ACLs by the binder.</value>
        public bool SuppressAclReset { get; set; }

        /// <summary>
        /// Gets and sets the option to suppress creating an image for MSI/MSM.
        /// </summary>
        /// <value>The option to suppress creating an image for MSI/MSM.</value>
        public bool SuppressLayout { get; set; }

        /// <summary>
        /// Gets and sets the option to suppress MSI/MSM validation.
        /// </summary>
        /// <value>The option to suppress MSI/MSM validation.</value>
        /// <remarks>This must be set before calling Bind.</remarks>
        public bool SuppressValidation { get; set; }

        /// <summary>
        /// Gets and sets the option to suppress adding _Validation table rows.
        /// </summary>
        public bool SuppressAddingValidationRows { get; set; }

        /// <summary>
        /// Gets or sets the localizer.
        /// </summary>
        /// <value>The localizer.</value>
        public Localizer Localizer { get; set; }

        /// <summary>
        /// Gets or sets the temporary path for the Binder.  If left null, the binder
        /// will use %TEMP% environment variable.
        /// </summary>
        /// <value>Path to temp files.</value>
        public string TempFilesLocation { get; set; }

        /// <summary>
        /// Gets or sets the Wix variable resolver.
        /// </summary>
        /// <value>The Wix variable resolver.</value>
        internal WixVariableResolver WixVariableResolver { get; set; }

        /// <summary>
        /// Add a binder extension.
        /// </summary>
        /// <param name="extension">New extension.</param>
        //public void AddExtension(IBinderExtension extension)
        //{
        //    this.extensions.Add(extension);
        //}

        /// <summary>
        /// Add a file manager extension.
        /// </summary>
        /// <param name="extension">New file manager.</param>
        //public void AddExtension(IBinderFileManager extension)
        //{
        //    this.fileManagers.Add(extension);
        //}

        public bool Bind(IBindContext context)
        {
            this.Context = context;

            //if (!String.IsNullOrEmpty(this.Context.FileManagerCore.CabCachePath))
            //{
            //    Directory.CreateDirectory(this.Context.FileManagerCore.CabCachePath);
            //}

            //this.core = new BinderCore();
            //this.core.FileManagerCore = this.Context.FileManagerCore;

            this.WriteBuildInfoTable(this.Context.IntermediateRepresentation, this.Context.OutputPath);

            // Prebind.
            //
            this.Context.Extensions = this.Context.ExtensionManager.Create<IBinderExtension>();

            foreach (IBinderExtension extension in this.Context.Extensions)
            {
                extension.PreBind(this.Context);
            }

            // Resolve.
            //
            var resolveResult = this.Resolve();

            this.Context.DelayedFields = resolveResult.DelayedFields;

            this.Context.ExpectedEmbeddedFiles = resolveResult.ExpectedEmbeddedFiles;

            // Backend.
            //
            var bindResult = this.BackendBind();

            if (bindResult != null)
            {
                // Postbind.
                //
                foreach (IBinderExtension extension in this.Context.Extensions)
                {
                    extension.PostBind(bindResult);
                }

                // Layout.
                //
                this.Layout(bindResult);
            }

            return this.Context.Messaging.EncounteredError;
        }

        private ResolveResult Resolve()
        {
            var buildingPatch = this.Context.IntermediateRepresentation.Sections.Any(s => s.Type == SectionType.Patch);

            var filesWithEmbeddedFiles = new ExtractEmbeddedFiles();

            IEnumerable<DelayedField> delayedFields;
            {
                var command = new ResolveFieldsCommand();
                command.Messaging = this.Context.Messaging;
                command.BuildingPatch = buildingPatch;
                command.BindVariableResolver = this.Context.WixVariableResolver;
                command.BindPaths = this.Context.BindPaths;
                command.Extensions = this.Context.Extensions;
                command.FilesWithEmbeddedFiles = filesWithEmbeddedFiles;
                command.IntermediateFolder = this.Context.IntermediateFolder;
                command.Intermediate = this.Context.IntermediateRepresentation;
                command.SupportDelayedResolution = true;
                command.Execute();

                delayedFields = command.DelayedFields;
            }

#if REVISIT_FOR_PATCHING
            if (this.Context.IntermediateRepresentation.SubStorages != null)
            {
                foreach (SubStorage transform in this.Context.IntermediateRepresentation.SubStorages)
                {
                    var command = new ResolveFieldsCommand();
                    command.BuildingPatch = buildingPatch;
                    command.BindVariableResolver = this.Context.WixVariableResolver;
                    command.BindPaths = this.Context.BindPaths;
                    command.Extensions = this.Context.Extensions;
                    command.FilesWithEmbeddedFiles = filesWithEmbeddedFiles;
                    command.IntermediateFolder = this.Context.IntermediateFolder;
                    command.Intermediate = this.Context.IntermediateRepresentation;
                    command.SupportDelayedResolution = false;
                    command.Execute();
                }
            }
#endif

            var expectedEmbeddedFiles = filesWithEmbeddedFiles.GetExpectedEmbeddedFiles();

            return new ResolveResult
            {
                ExpectedEmbeddedFiles = expectedEmbeddedFiles,
                DelayedFields = delayedFields,
            };
        }

        private BindResult BackendBind()
        {
            var backendFactories = this.Context.ExtensionManager.Create<IBackendFactory>();

            var entrySection = this.Context.IntermediateRepresentation.Sections[0];

            foreach (var factory in backendFactories)
            {
                if (factory.TryCreateBackend(entrySection.Type.ToString(), this.Context.OutputPath, null, out var backend))
                {
                    var result = backend.Bind(this.Context);
                    return result;
                }
            }

            // TODO: messaging that a backend could not be found to bind the output type?

            return null;
        }

        private void Layout(BindResult result)
        {
            try
            {
                this.LayoutMedia(result.FileTransfers);
            }
            finally
            {
                if (!String.IsNullOrEmpty(this.Context.ContentsFile) && result.ContentFilePaths != null)
                {
                    this.CreateContentsFile(this.Context.ContentsFile, result.ContentFilePaths);
                }

                if (!String.IsNullOrEmpty(this.Context.OutputsFile) && result.FileTransfers != null)
                {
                    this.CreateOutputsFile(this.Context.OutputsFile, result.FileTransfers, this.Context.OutputPdbPath);
                }

                if (!String.IsNullOrEmpty(this.Context.BuiltOutputsFile) && result.FileTransfers != null)
                {
                    this.CreateBuiltOutputsFile(this.Context.BuiltOutputsFile, result.FileTransfers, this.Context.OutputPdbPath);
                }
            }
        }

        /// <summary>
        /// Binds an output.
        /// </summary>
        /// <param name="output">The output to bind.</param>
        /// <param name="file">The Windows Installer file to create.</param>
        /// <remarks>The Binder.DeleteTempFiles method should be called after calling this method.</remarks>
        /// <returns>true if binding completed successfully; false otherwise</returns>
#if false
        public bool Bind(Output output, string file)
        {
            // Ensure the cabinet cache path exists if we are going to use it.
            if (!String.IsNullOrEmpty(this.CabCachePath))
            {
                Directory.CreateDirectory(this.CabCachePath);
            }

            //var fileManagerCore = new BinderFileManagerCore();
            //fileManagerCore.CabCachePath = this.CabCachePath;
            //fileManagerCore.Output = output;
            //fileManagerCore.TempFilesLocation = this.TempFilesLocation;
            //fileManagerCore.AddBindPaths(this.BindPaths, BindStage.Normal);
            //fileManagerCore.AddBindPaths(this.TargetBindPaths, BindStage.Target);
            //fileManagerCore.AddBindPaths(this.UpdatedBindPaths, BindStage.Updated);
            //foreach (IBinderFileManager fileManager in this.fileManagers)
            //{
            //    fileManager.Core = fileManagerCore;
            //}

            this.core = new BinderCore();
            this.core.FileManagerCore = fileManagerCore;

            this.WriteBuildInfoTable(output, file);

            // Initialize extensions.
            foreach (IBinderExtension extension in this.extensions)
            {
                extension.Core = this.core;

                extension.Initialize(output);
            }

            // Gather all the wix variables.
            //Table wixVariableTable = output.Tables["WixVariable"];
            //if (null != wixVariableTable)
            //{
            //    foreach (WixVariableRow wixVariableRow in wixVariableTable.Rows)
            //    {
            //        this.WixVariableResolver.AddVariable(wixVariableRow);
            //    }
            //}

            //BindContext context = new BindContext();
            //context.CabbingThreadCount = this.CabbingThreadCount;
            //context.DefaultCompressionLevel = this.DefaultCompressionLevel;
            //context.Extensions = this.extensions;
            //context.FileManagerCore = fileManagerCore;
            //context.FileManagers = this.fileManagers;
            //context.Ices = this.Ices;
            //context.IntermediateFolder = this.TempFilesLocation;
            //context.IntermediateRepresentation = output;
            //context.Localizer = this.Localizer;
            //context.OutputPath = file;
            //context.OutputPdbPath = this.PdbFile;
            //context.SuppressIces = this.SuppressIces;
            //context.SuppressValidation = this.SuppressValidation;
            //context.WixVariableResolver = this.WixVariableResolver;

            BindResult result = null;

            foreach (var factory in this.BackendFactories)
            {
                if (factory.TryCreateBackend(output.Type.ToString(), file, null, out var backend))
                {
                    result = backend.Bind(context);
                    break;
                }
            }

            if (result == null)
            {
                // TODO: messaging that a backend could not be found to bind the output type?

                return false;
            }

            // Layout media
            try
            {
                this.LayoutMedia(result.FileTransfers);
            }
            finally
            {
                if (!String.IsNullOrEmpty(this.ContentsFile) && result.ContentFilePaths != null)
                {
                    this.CreateContentsFile(this.ContentsFile, result.ContentFilePaths);
                }

                if (!String.IsNullOrEmpty(this.OutputsFile) && result.FileTransfers != null)
                {
                    this.CreateOutputsFile(this.OutputsFile, result.FileTransfers, this.PdbFile);
                }

                if (!String.IsNullOrEmpty(this.BuiltOutputsFile) && result.FileTransfers != null)
                {
                    this.CreateBuiltOutputsFile(this.BuiltOutputsFile, result.FileTransfers, this.PdbFile);
                }
            }

            this.core = null;

            return Messaging.Instance.EncounteredError;
        }
#endif

        /// <summary>
        /// Does any housekeeping after Bind.
        /// </summary>
        /// <param name="tidy">Whether or not any actual tidying should be done.</param>
        public void Cleanup(bool tidy)
        {
            if (tidy)
            {
                if (!this.DeleteTempFiles())
                {
                    this.Context.Messaging.Write(WarningMessages.FailedToDeleteTempDir(this.TempFilesLocation));
                }
            }
            else
            {
                this.Context.Messaging.Write(VerboseMessages.BinderTempDirLocatedAt(this.TempFilesLocation));
            }
        }

        /// <summary>
        /// Cleans up the temp files used by the Binder.
        /// </summary>
        /// <returns>True if all files were deleted, false otherwise.</returns>
        private bool DeleteTempFiles()
        {
            bool deleted = Common.DeleteTempFiles(this.TempFilesLocation, this.Context.Messaging);
            return deleted;
        }

        /// <summary>
        /// Populates the WixBuildInfo table in an output.
        /// </summary>
        /// <param name="output">The output.</param>
        /// <param name="databaseFile">The output file if OutputFile not set.</param>
        private void WriteBuildInfoTable(Intermediate output, string outputFile)
        {
            var entrySection = output.Sections.First(s => s.Type != SectionType.Fragment);

            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            FileVersionInfo fileVersion = FileVersionInfo.GetVersionInfo(executingAssembly.Location);

            var buildInfoRow = new WixBuildInfoTuple();
            buildInfoRow.WixVersion = fileVersion.FileVersion;
            buildInfoRow.WixOutputFile = outputFile;

            if (!String.IsNullOrEmpty(this.Context.WixprojectFile))
            {
                buildInfoRow.WixProjectFile = this.Context.WixprojectFile;
            }

            if (!String.IsNullOrEmpty(this.Context.OutputPdbPath))
            {
                buildInfoRow.WixPdbFile = this.Context.OutputPdbPath;
            }

            entrySection.Tuples.Add(buildInfoRow);
        }

#if DELETE_THIS_CODE
        /// <summary>
        /// Binds a bundle.
        /// </summary>
        /// <param name="bundle">The bundle to bind.</param>
        /// <param name="bundleFile">The bundle to create.</param>
        private void BindBundle(Output bundle, string bundleFile, out IEnumerable<FileTransfer> fileTransfers, out IEnumerable<string> contentPaths)
        {
            BindBundleCommand command = new BindBundleCommand();
            command.DefaultCompressionLevel = this.DefaultCompressionLevel;
            command.Extensions = this.extensions;
            command.FileManagerCore = this.fileManagerCore;
            command.FileManagers = this.fileManagers;
            command.Output = bundle;
            command.OutputPath = bundleFile;
            command.PdbFile = this.PdbFile;
            command.TableDefinitions = this.core.TableDefinitions;
            command.TempFilesLocation = this.TempFilesLocation;
            command.WixVariableResolver = this.WixVariableResolver;
            command.Execute();

            fileTransfers = command.FileTransfers;
            contentPaths = command.ContentFilePaths;
        }

        /// <summary>
        /// Binds a databse.
        /// </summary>
        /// <param name="output">The output to bind.</param>
        /// <param name="databaseFile">The database file to create.</param>
        private void BindDatabase(Output output, string databaseFile, out IEnumerable<FileTransfer> fileTransfers, out IEnumerable<string> contentPaths)
        {
            Validator validator = null;

            // tell the binder about the validator if validation isn't suppressed
            if (!this.SuppressValidation && (OutputType.Module == output.Type || OutputType.Product == output.Type))
            {
                validator = new Validator();
                validator.TempFilesLocation = Path.Combine(this.TempFilesLocation, "validate");

                // set the default cube file
                string lightDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string cubePath = (OutputType.Module == output.Type) ? Path.Combine(lightDirectory, "mergemod.cub") : Path.Combine(lightDirectory, "darice.cub");
                validator.AddCubeFile(cubePath);

                // by default, disable ICEs that have equivalent-or-better checks in WiX
                this.SuppressIces.Add("ICE08");
                this.SuppressIces.Add("ICE33");
                this.SuppressIces.Add("ICE47");
                this.SuppressIces.Add("ICE66");

                // set the ICEs
                validator.ICEs = this.Ices.ToArray();

                // set the suppressed ICEs
                validator.SuppressedICEs = this.SuppressIces.ToArray();
            }

            BindDatabaseCommand command = new BindDatabaseCommand();
            command.CabbingThreadCount = this.CabbingThreadCount;
            command.Codepage = this.Localizer == null ? -1 : this.Localizer.Codepage;
            command.DefaultCompressionLevel = this.DefaultCompressionLevel;
            command.Extensions = this.extensions;
            command.FileManagerCore = this.fileManagerCore;
            command.FileManagers = this.fileManagers;
            command.InspectorExtensions = this.inspectorExtensions;
            command.Localizer = this.Localizer;
            command.PdbFile = this.PdbFile;
            command.Output = output;
            command.OutputPath = databaseFile;
            command.SuppressAddingValidationRows = this.SuppressAddingValidationRows;
            command.SuppressLayout = this.SuppressLayout;
            command.TableDefinitions = this.core.TableDefinitions;
            command.TempFilesLocation = this.TempFilesLocation;
            command.Validator = validator;
            command.WixVariableResolver = this.WixVariableResolver;
            command.Execute();

            fileTransfers = command.FileTransfers;
            contentPaths = command.ContentFilePaths;
        }

        /// <summary>
        /// Binds a transform.
        /// </summary>
        /// <param name="transform">The transform to bind.</param>
        /// <param name="outputPath">The transform to create.</param>
        private void BindTransform(Output transform, string outputPath)
        {
            BindTransformCommand command = new BindTransformCommand();
            command.Extensions = this.extensions;
            command.FileManagers = this.fileManagers;
            command.TableDefinitions = this.core.TableDefinitions;
            command.TempFilesLocation = this.TempFilesLocation;
            command.Transform = transform;
            command.OutputPath = outputPath;
            command.Execute();
        }
#endif

        /// <summary>
        /// Final step in binding that transfers (moves/copies) all files generated into the appropriate
        /// location in the source image
        /// </summary>
        /// <param name="fileTransfers">List of files to transfer.</param>
        private void LayoutMedia(IEnumerable<FileTransfer> transfers)
        {
            if (null != transfers && transfers.Any())
            {
                this.Context.Messaging.Write(VerboseMessages.LayingOutMedia());

                var command = new TransferFilesCommand(this.Context.Messaging, this.Context.BindPaths, this.Context.Extensions, transfers, this.Context.SuppressAclReset);
                command.Execute();
            }
        }

        /// <summary>
        /// Get the source path of a directory.
        /// </summary>
        /// <param name="directories">All cached directories.</param>
        /// <param name="componentIdGenSeeds">Hash table of Component GUID generation seeds indexed by directory id.</param>
        /// <param name="directory">Directory identifier.</param>
        /// <param name="canonicalize">Canonicalize the path for standard directories.</param>
        /// <returns>Source path of a directory.</returns>
        public static string GetDirectoryPath(Dictionary<string, ResolvedDirectory> directories, Dictionary<string, string> componentIdGenSeeds, string directory, bool canonicalize)
        {
            if (!directories.ContainsKey(directory))
            {
                throw new WixException(ErrorMessages.ExpectedDirectory(directory));
            }

            ResolvedDirectory resolvedDirectory = (ResolvedDirectory)directories[directory];

            if (null == resolvedDirectory.Path)
            {
                if (null != componentIdGenSeeds && componentIdGenSeeds.ContainsKey(directory))
                {
                    resolvedDirectory.Path = (string)componentIdGenSeeds[directory];
                }
                else if (canonicalize && WindowsInstallerStandard.IsStandardDirectory(directory))
                {
                    // when canonicalization is on, standard directories are treated equally
                    resolvedDirectory.Path = directory;
                }
                else
                {
                    string name = resolvedDirectory.Name;

                    if (canonicalize && null != name)
                    {
                        name = name.ToLower(CultureInfo.InvariantCulture);
                    }

                    if (String.IsNullOrEmpty(resolvedDirectory.DirectoryParent))
                    {
                        resolvedDirectory.Path = name;
                    }
                    else
                    {
                        string parentPath = GetDirectoryPath(directories, componentIdGenSeeds, resolvedDirectory.DirectoryParent, canonicalize);

                        if (null != resolvedDirectory.Name)
                        {
                            resolvedDirectory.Path = Path.Combine(parentPath, name);
                        }
                        else
                        {
                            resolvedDirectory.Path = parentPath;
                        }
                    }
                }
            }

            return resolvedDirectory.Path;
        }

        /// <summary>
        /// Gets the source path of a file.
        /// </summary>
        /// <param name="directories">All cached directories in <see cref="ResolvedDirectory"/>.</param>
        /// <param name="directoryId">Parent directory identifier.</param>
        /// <param name="fileName">File name (in long|source format).</param>
        /// <param name="compressed">Specifies the package is compressed.</param>
        /// <param name="useLongName">Specifies the package uses long file names.</param>
        /// <returns>Source path of file relative to package directory.</returns>
        public static string GetFileSourcePath(Dictionary<string, ResolvedDirectory> directories, string directoryId, string fileName, bool compressed, bool useLongName)
        {
            string fileSourcePath = Common.GetName(fileName, true, useLongName);

            if (compressed)
            {
                // Use just the file name of the file since all uncompressed files must appear
                // in the root of the image in a compressed package.
            }
            else
            {
                // Get the relative path of where we want the file to be layed out as specified
                // in the Directory table.
                string directoryPath = Binder.GetDirectoryPath(directories, null, directoryId, false);
                fileSourcePath = Path.Combine(directoryPath, fileSourcePath);
            }

            // Strip off "SourceDir" if it's still on there.
            if (fileSourcePath.StartsWith("SourceDir\\", StringComparison.Ordinal))
            {
                fileSourcePath = fileSourcePath.Substring(10);
            }

            return fileSourcePath;
        }

        /// <summary>
        /// Writes the paths to the content files included in the package to a text file.
        /// </summary>
        /// <param name="path">Path to write file.</param>
        /// <param name="contentFilePaths">Collection of paths to content files that will be written to file.</param>
        private void CreateContentsFile(string path, IEnumerable<string> contentFilePaths)
        {
            string directory = Path.GetDirectoryName(path);
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            using (StreamWriter contents = new StreamWriter(path, false))
            {
                foreach (string contentPath in contentFilePaths)
                {
                    contents.WriteLine(contentPath);
                }
            }
        }

        /// <summary>
        /// Writes the paths to the content files included in the bundle to a text file.
        /// </summary>
        /// <param name="path">Path to write file.</param>
        /// <param name="payloads">Collection of payloads whose source will be written to file.</param>
        private void CreateContentsFile(string path, IEnumerable<WixBundlePayloadTuple> payloads)
        {
            string directory = Path.GetDirectoryName(path);
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            using (StreamWriter contents = new StreamWriter(path, false))
            {
                foreach (var payload in payloads)
                {
                    if (payload.ContentFile)
                    {
                        var fullPath = Path.GetFullPath(payload.SourceFile);
                        contents.WriteLine(fullPath);
                    }
                }
            }
        }

        /// <summary>
        /// Writes the paths to the output files to a text file.
        /// </summary>
        /// <param name="path">Path to write file.</param>
        /// <param name="fileTransfers">Collection of files that were transferred to the output directory.</param>
        /// <param name="pdbPath">Optional path to created .wixpdb.</param>
        private void CreateOutputsFile(string path, IEnumerable<FileTransfer> fileTransfers, string pdbPath)
        {
            string directory = Path.GetDirectoryName(path);
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            using (StreamWriter outputs = new StreamWriter(path, false))
            {
                foreach (FileTransfer fileTransfer in fileTransfers)
                {
                    // Don't list files where the source is the same as the destination since
                    // that might be the only place the file exists. The outputs file is often
                    // used to delete stuff and losing the original source would be bad.
                    if (!fileTransfer.Redundant)
                    {
                        outputs.WriteLine(fileTransfer.Destination);
                    }
                }

                if (!String.IsNullOrEmpty(pdbPath))
                {
                    outputs.WriteLine(Path.GetFullPath(pdbPath));
                }
            }
        }

        /// <summary>
        /// Writes the paths to the built output files to a text file.
        /// </summary>
        /// <param name="path">Path to write file.</param>
        /// <param name="fileTransfers">Collection of files that were transferred to the output directory.</param>
        /// <param name="pdbPath">Optional path to created .wixpdb.</param>
        private void CreateBuiltOutputsFile(string path, IEnumerable<FileTransfer> fileTransfers, string pdbPath)
        {
            string directory = Path.GetDirectoryName(path);
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            using (StreamWriter outputs = new StreamWriter(path, false))
            {
                foreach (FileTransfer fileTransfer in fileTransfers)
                {
                    // Only write the built file transfers. Also, skip redundant
                    // files for the same reason spelled out in this.CreateOutputsFile().
                    if (fileTransfer.Built && !fileTransfer.Redundant)
                    {
                        outputs.WriteLine(fileTransfer.Destination);
                    }
                }

                if (!String.IsNullOrEmpty(pdbPath))
                {
                    outputs.WriteLine(Path.GetFullPath(pdbPath));
                }
            }
        }
    }
}