/
Project.cs
1062 lines (939 loc) · 45.5 KB
/
Project.cs
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region Licence...
/*
The MIT License (MIT)
Copyright (c) 2014 Oleg Shilo
Permission is hereby granted,
free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion Licence...
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using WixSharp.CommonTasks;
namespace WixSharp
{
// Wix/Msi bug/limitation: every component that is to be placed in the user profile has to have Registry key
// Wix# places dummy key into every component to handle the problem
// Wix# auto-generates components contain RemoveFolder elements for all subfolders in the path chain.
// All auto-generates components are automatically inserted in all features
/// <summary>
/// Represents Wix# project. This class defines the WiX/MSI entities and their relationships.
/// <para>
/// <see cref="Project"/> instance can be compiled into complete MSI or WiX source file with one of the <see cref="Compiler"/> "Build" methods.
/// </para>
/// <para>
/// Use <see cref="Project"/> non-default constructor or C# initializers to specify required installation components.
/// </para>
/// </summary>
/// <example>
/// <code>
/// var project = new Project("MyProduct",
/// new Dir(@"%ProgramFiles%\My Company\My Product",
/// new File(@"Files\Bin\MyApp.exe"),
/// new Dir(@"Docs\Manual",
/// new File(@"Files\Docs\Manual.txt"))));
///
/// project.GUID = new Guid("6f330b47-2577-43ad-9095-1861ba25889b");
/// project.BuildMsi();
/// </code>
/// </example>
public class Project : WixProject
{
internal new string ComponentId(string seed)
{
// Component id must be globally unique. Otherwise other products can
// accidentally trigger MSI ref-counting by installing more than one product
// with the same component id.
//
// The problem is caused by the mind bending MSi concept that a identity does
// not logically belong to the product but to the target system. Thus if two
// different products happens too have the component with the same id MSI will
// treat the components as the same component.
// Using GUID seems to be a good technique to overcome this limitation but the
// the vast majority of the samples (e.g. WiX) use human readable ids, which are
// of course not unique.
//
// The excellent reading on the topic can be found here:
// http://geekswithblogs.net/akraus1/archive/2011/06/17/145898.aspx
// Ideally we would want to keep readability and uniqueness. Thus the solution
// is to merge "human readable" id with the project guid.
if (Compiler.AutoGeneration.ForceComponentIdUniqueness)
return $"{seed}.{this.UpgradeCode.ToString().Replace("-", "")}";
else
return seed;
}
/// <summary>
/// Initializes a new instance of the <see cref="Project"/> class.
/// </summary>
public Project()
{
if (!Compiler.AutoGeneration.LegacyDefaultIdAlgorithm)
this.CustomIdAlgorithm = this.HashedTargetPathIdAlgorithm;
}
/// <summary>
/// Initializes a new instance of the <see cref="Project"/> class.
/// </summary>
/// <param name="name">The name of the project. Typically it is the name of the product to be installed.</param>
/// <param name="items">The project installable items (e.g. directories, files, registry keys, Custom Actions).</param>
public Project(string name, params WixObject[] items)
{
if (!Compiler.AutoGeneration.LegacyDefaultIdAlgorithm)
this.CustomIdAlgorithm = this.HashedTargetPathIdAlgorithm;
Name = name;
OutFileName = name;
var dirs = new List<Dir>();
var actions = new List<Action>();
var regs = new List<RegValue>();
var props = new List<Property>();
var bins = new List<Binary>();
var genericItems = new List<IGenericEntity>();
if (items.OfType<Media>().Any())
this.Media.Clear();
foreach (WixObject obj in items)
{
var rawItems = new List<WixObject>();
if (obj is WixItems)
rawItems.AddRange((obj as WixItems).Items);
else
rawItems.Add(obj);
foreach (WixObject item in rawItems)
{
if (item is LaunchCondition)
LaunchConditions.Add(item as LaunchCondition);
else if (item is Dir)
dirs.Add(item as Dir);
else if (item is Action)
actions.Add(item as Action);
else if (item is RegValue)
regs.Add(item as RegValue);
else if (item is RegKey regkey)
regs.AddRange(regkey.GetValues());
else if (item is RegFile)
{
var file = item as RegFile;
var values = Tasks.ImportRegFile(file.Path);
if (file.ActualFeatures.Any())
values.ForEach(x =>
{
x.Feature = file.Feature;
x.Features = file.Features;
});
regs.AddRange(values);
}
else if (item is Property || item is PropertyRef)
props.Add(item as Property);
else if (item is Binary)
bins.Add(item as Binary);
else if (item is WixGuid)
GUID = (item as WixGuid).Value;
else if (item is Media)
Media.Add(item as Media);
else if (item is IGenericEntity)
genericItems.Add(item as IGenericEntity);
else
throw new Exception("Unexpected object type is among Project constructor arguments: " + item.GetType().Name);
}
}
Dirs = dirs.ToArray();
Actions = actions.ToArray();
RegValues = regs.ToArray();
Properties = props.ToArray();
Binaries = bins.ToArray();
GenericItems = genericItems.ToArray();
}
/// <summary>
/// The product full name or description.
/// </summary>
public string Description = "";
/// <summary>
/// Parameters of digitally sign of this project
/// </summary>
public DigitalSignature DigitalSignature;
internal virtual void Preprocess()
{
var managedActions = this.Actions.OfType<ManagedAction>()
.Select(x => new { Action = x, Asm = x.ActionAssembly })
.GroupBy(x => x.Asm)
.ToDictionary(x => x.Key);
foreach (var uniqueAsm in managedActions.Keys)
{
var actions = managedActions[uniqueAsm].Select(x => x.Action).ToArray();
var refAsms = actions.SelectMany(a => a.RefAssemblies).Distinct().ToArray();
actions.ForEach(a => a.RefAssemblies = refAsms);
}
if (WixSharp.Compiler.AutoGeneration.Map64InstallDirs && this.Platform.HasValue && this.Platform.Value == WixSharp.Platform.x64)
{
foreach (var dir in this.AllDirs)
{
dir.Name = dir.Name.Map64Dirs();
foreach (Shortcut s in dir.Shortcuts)
s.Location = s.Location.Map64Dirs();
}
foreach (Shortcut s in this.AllFiles.SelectMany(f => f.Shortcuts))
{
s.Location = s.Location.Map64Dirs();
}
foreach (var action in this.Actions.OfType<PathFileAction>())
{
action.AppPath = action.AppPath.Map64Dirs();
}
}
foreach (File f in this.AllFiles.Where(f => f.AddCloseAction == true))
{
this.AddGenericItem(new CloseApplication(f.Name.PathGetFileName(), true, false));
}
}
/// <summary>
/// Generic <see cref="T:WixSharp.WixEntity"/> container for defining WiX <c>Package</c> element attributes.
/// <para>These attributes are the properties about the package to be placed in the Summary Information Stream. These are visible from COM through the IStream interface, and these properties can be seen on the package in Explorer. </para>
///<example>The following is an example of defining the <c>Package</c> attributes.
///<code>
/// var project =
/// new Project("My Product",
/// new Dir(@"%ProgramFiles%\My Company\My Product",
///
/// ...
///
/// project.Package.AttributesDefinition = @"AdminImage=Yes;
/// Comments=Release candidate;
/// Description=Fantastic product...";
///
/// Compiler.BuildMsi(project);
/// </code>
/// </example>
/// </summary>
public Package Package = new Package();
/// <summary>
/// The target platform type.
/// </summary>
public Platform? Platform;
/// <summary>
/// Collection of Media generic <see cref="T:WixSharp.WixEntity"/> containers for defining WiX <c>Media</c> elements
/// attributes. Project is always initialized with a sinle Media item. Though if you add multiples media items via
/// <see cref="T:WixSharp.Project"/> constructor it remeve the initial Media item befeore adding any new items.
/// <para>These attributes describe a disk that makes up the source media for the installation.</para>
///<example>The following is an example of defining the <c>Package</c> attributes.
///<code>
/// var project =
/// new Project("My Product",
/// new Dir(@"%ProgramFiles%\My Company\My Product",
///
/// ...
///
/// project.Media.First().Id = 2;
/// project.Media.First().CompressionLevel = CompressionLevel.mszip;
///
/// Compiler.BuildMsi(project);
/// </code>
/// </example>
/// </summary>
public List<Media> Media = new List<Media>(new[] { new Media() });
/// <summary>
/// The REINSTALLMODE property is a string that contains letters specifying the type of reinstall to perform.
/// Options are case-insensitive and order-independent. This property should normally always be used in
/// conjunction with the REINSTALL property.
/// <para>Note, REINSTALLMODE property will be created only in the automatically produced WiX definition file
/// only if <see cref="WixSharp.Project.MajorUpgrade"/> is set.</para>
/// <para>Read more: https://docs.microsoft.com/en-us/windows/desktop/msi/reinstallmode </para>
/// </summary>
public string ReinstallMode = "omus";
/// <summary>
/// Relative path to RTF file with the custom licence agreement to be displayed in the Licence dialog.
/// If this value is not specified the default WiX licence agreement will be used.
/// </summary>
public string LicenceFile = "";
/// <summary>
/// The Encoding to be used for MSI UI dialogs. If not specified the
/// <c>System.Text.Encoding.UTF8</c> will be used.
/// </summary>
public Encoding Encoding = Encoding.UTF8;
/// <summary>
/// Type of the MSI User Interface. This value is assigned to the <c>UIRef</c> WiX element during the compilation.
/// If not specified <see cref="WUI.WixUI_Minimal"/> will used.
/// </summary>
public WUI UI = WUI.WixUI_Minimal;
/// <summary>
/// The Binary (assembly) implementing WiX embedded UI
/// </summary>
public Binary EmbeddedUI = null;
/// <summary>
/// The custom UI definition. Use CustomUIBuilder to generate the WiX UI definition or compose
/// <see cref="WixSharp.Controls.CustomUI"/> manually.
/// </summary>
public Controls.CustomUI CustomUI = null;
/// <summary>
/// Simplifies authoring for major upgrades, including support for preventing downgrades.
/// </summary>
public MajorUpgrade MajorUpgrade = null;
/// <summary>
/// This is the value of the <c>UpgradeCode</c> attribute of the Wix <c>Product</c> element.
/// <para>Both WiX and MSI consider this element as optional even it is the only available identifier
/// for defining relationship between different versions of the same product. Wix# in contrary enforces
/// that value to allow any future updates of the product being installed.
/// </para>
/// <para>
/// If user doesn't specify this value Wix# engine will use <see cref="Project.GUID"/> as <c>UpgradeCode</c>.
/// </para>
/// </summary>
public Guid? UpgradeCode;
private Guid? guid;
/// <summary>
/// This value uniquely identifies the software product being installed.
/// <para>
/// All setup build scripts for different versions of the same product should have the same <see cref="GUID"/>.
/// If user doesn't specify this value Wix# engine will generate new random GUID for it.
/// </para>
/// <remarks>This value should not be confused with MSI <c>ProductId</c>, which is in fact
/// not an identifier of the product but rather an identifier of the product particular version.
/// MSI uses <c>UpgradeCode</c> as a common identification of the product regardless of it's version.
/// <para>In a way <see cref="GUID"/> is an alias for <see cref="UpgradeCode"/>.</para>
/// </remarks>
/// </summary>
public Guid? GUID
{
get { return guid; }
set
{
guid = value;
ResetWixGuidStartValue();
}
}
internal void ResetWixGuidStartValue()
{
WixGuid.ConsistentGenerationStartValue = this.ProductId ?? this.GUID ?? Guid.NewGuid();
}
internal void ResetAutoIdGeneration(bool supressWarning)
{
ResetWixGuidStartValue();
WixEntity.ResetIdGenerator(supressWarning);
}
/// <summary>
/// Set of values in 'Add/Remove Programs' of Control Panel.
/// </summary>
public ProductInfo ControlPanelInfo = new ProductInfo();
/// <summary>
/// Provides fine control over rebooting at the end of installation.
/// <para>If set it creates <c>ScheduleReboot</c> element in the <c>InstallExecuteSequence</c> or <c>InstallUISequence</c>.</para>
/// </summary>
/// <example>
/// <code>
/// var project = new Project("MyProduct",
/// new Dir("%ProgramFiles%",
/// ...
/// project.ScheduleReboot = new ScheduleReboot { InstallSequence = RebootInstallSequence.Both };
/// </code>
/// </example>
public ScheduleReboot ScheduleReboot;
/// <summary>
/// Provides fine control over rebooting at the end of installation.
/// <para>If set it creates <c>ForceReboot</c> element in the <c>InstallExecuteSequence</c>.</para>
/// </summary>
/// <example>
/// <code>
/// var project = new Project("MyProduct",
/// new Dir("%ProgramFiles%",
/// ...
/// project.ForceReboot = new ForceReboot();
/// </code>
/// </example>
public ForceReboot ForceReboot;
/// <summary>
/// Provides fine control over rebooting at the end of installation.
/// <para>If set it creates MSI <c>REBOOT</c> property with the user specified value <see cref="RebootSupressing"/>.</para>
/// </summary>
/// <example>
/// <code>
/// var project = new Project("MyProduct",
/// new Dir("%ProgramFiles%",
/// ...
/// project.RebootSupressing = RebootSupressing.ReallySuppress;
/// </code>
/// </example>
public RebootSupressing? RebootSupressing;
/// <summary>
/// Use this attribute if you need to specify the installation scope of this package: per-machine or per-user.
/// </summary>
public InstallScope? InstallScope;
/// <summary>
/// Use this attribute to specify the privileges required to install the package on Windows Vista and above.
/// </summary>
public InstallPrivileges? InstallPrivileges;
/// <summary>
/// Version of the product to be installed.
/// </summary>
public Version Version = new Version("1.0.0.0");
/// <summary>
/// Defines Major Upgrade behavior. By default it is <c>null</c> thus upgrade is not supported.
/// <para>If you need to implement Major Upgrade define this member to appropriate
/// <see cref="MajorUpgradeStrategy"/> instance.</para>
/// <para><c>Note</c>: <see cref="MajorUpgradeStrategy"/> yields WiX UpgradeVersion element, which is arguably the most comprehensive
/// upgrade definition. However in the later versions of WiX a simplified upgrade definition has been introduced. It relies
/// on MajorUpgrade WiX element. For most of the upgrade scenarios you will find that MajorUpgrade allows to achieve the same result with
/// much less effort. Wix# supports MajorUpgrade element via <see cref="WixSharp.Project.MajorUpgrade"/> member.</para>
/// </summary>
///<example>The following is an example of building product MSI with auto uninstalling any older version of the product
///and preventing downgrading.
///<code>
/// var project = new Project("My Product",
/// ...
///
/// project.MajorUpgradeStrategy = new MajorUpgradeStrategy
/// {
/// UpgradeVersions = VersionRange.OlderThanThis,
/// PreventDowngradingVersions = VersionRange.NewerThanThis,
/// NewerProductInstalledErrorMessage = "Newer version already installed",
/// };
/// Compiler.BuildMsi(project);
/// </code>
/// or the same scenario but using predefined <c>MajorUpgradeStrategy.Default</c> strategy.
///<code>
/// project.MajorUpgradeStrategy = MajorUpgradeStrategy.Default;
/// </code>
/// You can also specify custom range of versions:
///<code>
/// project.MajorUpgradeStrategy = new MajorUpgradeStrategy
/// {
/// UpgradeVersions = new VersionRange
/// {
/// Minimum = "2.0.5.0", IncludeMaximum = true,
/// Maximum = "%this%", IncludeMaximum = false
/// },
/// PreventDowngradingVersions = new VersionRange
/// {
/// Minimum = "%this%", IncludeMinimum = false
/// },
/// NewerProductInstalledErrorMessage = "Newer version already installed",
/// };
/// </code>
/// Note that %this% will be replaced by Wix# compiler with <c>project.Version.ToString()</c> during the MSI building.
/// However you can use explicit values (e.g. 1.0.0) if you prefer.
/// </example>
public MajorUpgradeStrategy MajorUpgradeStrategy;
/// <summary>
/// Generates all missing product Guids (e.g. <see cref="UpgradeCode"/> and <see cref="ProductId"/>).
/// <para>Wix# compiler call this method just before building the MSI. However you can call it any time
/// if you want to preview auto-generated Guids.</para>
/// </summary>
public void GenerateProductGuids()
{
if (!GUID.HasValue)
GUID = Guid.NewGuid();
if (!UpgradeCode.HasValue)
UpgradeCode = GUID;
if (!ProductId.HasValue)
ProductId = CalculateProductId(guid.Value, Version);
}
/// <summary>
/// Calculates the product id.
/// </summary>
/// <param name="productGuid">The product GUID.</param>
/// <param name="version">The version.</param>
/// <returns></returns>
public static Guid CalculateProductId(Guid productGuid, Version version)
{
return WixGuid.HashGuidByInteger(productGuid, version.GetHashCode() + 1);
}
/// <summary>
/// This is the value of the <c>Id</c> attribute of the Wix <c>Product</c> element.
/// This value is unique for any given version of a product being installed.
/// <para></para>
/// If user doesn't specify this value Wix# engine will derive it from
/// project <see cref="Project.GUID"/> and the product <see cref="Project.Version"/>.
/// </summary>
public Guid? ProductId;
/// <summary>
/// Collection of <see cref="Dir"/>s to be installed.
/// </summary>
public Dir[] Dirs = new Dir[0];
/// <summary>
/// Collection of <see cref="Actions"/>s to be performed during the installation.
/// </summary>
public Action[] Actions = new Action[0];
/// <summary>
/// Collection of <see cref="RegValue"/>s to be set during the installation.
/// </summary>
public RegValue[] RegValues = new RegValue[0];
/// <summary>
/// Collection of <see cref="UrlReservation"/> to be installed.
/// </summary>
public UrlReservation[] UrlReservations = new UrlReservation[0];
/// <summary>
/// Collection of the user defined <see cref="IGenericEntity"/> items.
/// </summary>
public IGenericEntity[] GenericItems = new IGenericEntity[0];
/// <summary>
/// Collection of WiX/MSI <see cref="Property"/> objects to be created during the installed.
/// </summary>
public Property[] Properties = new Property[0];
/// <summary>
/// Indicates whether compiler should emit consistent package Id (package code). Set <c>EmitConsistentPackageId</c> to 'false' (default value) if
/// you want the WiX compilers automatically generate a new package code for each new .msi file built. Or set it to 'true' if you want Wix# to auto generate a
/// unique consistent package code for a given combination of the product code, product version and product upgrade code.
/// <para>
/// WiX package code generation policy discourages the use of this attribute as it is a primary MSI identifier
/// used to distinguish packages in ARP. Thus WiX tools always auto-generate the code for each build. This in turn makes it impossible to
/// rebuild a truly identical MSIs from the same WiX code even with the same set of product code, version and upgrade code.
/// </para><para>
/// This very artificial limitation has severe practical impact. For example if a specific MSI file is lost it cannot be recreated even if
/// the original source code that was used to built the lost MSI is available.
/// </para><para>
/// From another hand Wix# encourages using a singe GUID (Project.GUID) as a primary identifier of the product. Thus all other MSI identifiers
/// can be derived by the compiler from the unique combination of this GUID and the product version. This also included generation of the package Id
/// attribute controlled by the EmitConsistentPackageId.
/// </para><para>
/// Wix# does not changes the WiX default package code generation it just gives the opportunity to control it when required.
/// </para>
/// </summary>
public bool EmitConsistentPackageId = false;
internal bool SuppressSettingPackageLanguages = false;
/// <summary>
/// Collection of WiX/MSI <see cref="Binary"/> objects to be embedded into MSI database.
/// Normally you doe not need to deal with this property as <see cref="Compiler"/> will populate
/// it automatically.
/// </summary>
public Binary[] Binaries = new Binary[0];
/// <summary>
/// Collection of paths to the assemblies referenced by <see cref="ManagedAction"/>s.
/// </summary>
public List<string> DefaultRefAssemblies = new List<string>();
/// <summary>
/// Collection of the <see cref="T:WixSharp.LaunchCondition"/>s associated with the setup.
/// </summary>
public List<LaunchCondition> LaunchConditions = new List<LaunchCondition>();
/// <summary>
/// Path to the file containing the image (e.g. bmp) setup dialogs banner. If not specified default image will be used.
/// </summary>
public string BannerImage = "";
/// <summary>
/// Path to the file containing the image (e.g. bmp) setup dialogs background. If not specified default image will be used.
/// <remarks>
/// <para>If the image is to be used in the default ManagedUI dialogs it will be left-docked at runtime and will effectively
/// play the role of a left-aligned dialog banner. Thus if it is too wide it can push away (to right) the all other UI elements.
/// The optimal size of the image for ManagedUI is 494x312.</para>
/// </remarks>
/// </summary>
public string BackgroundImage = "";
/// <summary>
/// Performs validation of aspect ratio for <see cref="Project.BackgroundImage"/>. Validation assists with avoiding the situations
/// when ManagedUI dialog has UI elements 'pushed away' from the view by oversizes <see cref="Project.BackgroundImage"/>.
/// </summary>
public bool ValidateBackgroundImage = true;
private Feature _defaultFeature = new Feature("Complete");
/// <summary>
/// Gets or Sets the default Feature for the project.
/// All elements without an explicitly assigned Feature will be placed under the DefaultFeature.
/// If DefaultFeature is not set, then DefaultFeature will default to a Feature with name 'Complete'.
/// </summary>
public Feature DefaultFeature
{
get { return _defaultFeature; }
set
{
if (value == null) throw new ArgumentNullException(nameof(value), "DefaultFeature cannot be null");
_defaultFeature = value;
}
}
/// <summary>
/// The wild card deduplication algorithm to be used during wild card resolution (<c>ResolveWildCards</c>).
/// <para>The default implementation does nothing but you can assgn a custom routine that
/// can be used to do post-resolving deduplication of the <see cref="Dir"/> items.</para>
/// <para>
/// The following sample demonstrates how to remove files with the same file name:
/// </para>
/// <code>
/// project.WildCardDedup = dir =>
/// {
/// if (dir.Files.Any())
/// dir.Files = dir.Files.DistinctBy(x => x.Name.PathGetFileName()).ToArray();
/// };
///
/// // or built-in routine
///
/// project.WildCardDedup = Project.UniqueFileNameDedup;
/// ...
/// Compiler.BuildMsi(project);
/// </code>
/// <para>Note, the need for <c>project.WildCardDedup</c> may araise only for very specific
/// deployment scenarios. Some of them are discussed in this thread: https://github.com/oleg-shilo/wixsharp/issues/270
/// </para>
/// </summary>
public Action<Dir> WildCardDedup = dir =>
{
// Issue #270: Deduplication of files added with wildcards
// sample dedup
};
/// <summary>
/// The unique file name deduplication algorithm to be used as <see cref="Project.WildCardDedup"/>.
/// It implements removing the <see cref="Dir.Files"/> items with the same file name.
/// <para>The actual algorithm implementation is very simple:
/// <code>
/// public static Action<Dir> UniqueFileNameDedup = dir =>
/// {
/// if (dir.Files.Any())
/// dir.Files = dir.Files.DistinctBy(x => Path.GetFileName(x.Name)).ToArray();
/// }
/// </code>
/// </para>
/// </summary>
public static Action<Dir> UniqueFileNameDedup = dir =>
{
// Issue #270: Deduplication of files added with wildcards sample dedup
if (dir.Files.Any())
dir.Files = dir.Files.DistinctBy(x => x.Name.PathGetFileName()).ToArray();
};
/// <summary>
/// Resolves all wild card specifications if any.
/// <para>
/// This method is called by <see cref="Compiler" /> during the compilation. However it might be convenient
/// to call it before the compilation if any files matching the wild card mask need to be handled in special
/// way (e.g. shortcuts created). See <c>WildCard Files</c> example.
/// </para><remarks><see cref="ResolveWildCards" /> should be called only after <see cref="T:WixSharp.WixProject.SourceBaseDir" /> is set.
/// Otherwise wild card paths may not be resolved correctly.</remarks>
/// </summary>
/// <param name="ignoreEmptyDirectories">if set to <c>true</c> empty directories are ignored and not added to the project.</param>
/// <returns></returns>
public Project ResolveWildCards(bool ignoreEmptyDirectories = false)
{
int iterator = 0;
var dirList = new List<Dir>();
var fileList = new List<File>();
dirList.AddRange(Dirs);
while (iterator < dirList.Count)
{
foreach (Files dirItems in dirList[iterator].FileCollections)
{
foreach (WixEntity item in dirItems.GetAllItems(SourceBaseDir, dirList[iterator]))
{
if (item is DirFiles)
{
dirList[iterator].AddDirFileCollection(item as DirFiles);
}
else if (item is Dir discoveredDir && !dirList[iterator].Dirs.Contains(item))
{
WildCardDedup?.Invoke(discoveredDir);
dirList[iterator].AddDir(discoveredDir);
}
}
}
foreach (Dir dir in dirList[iterator].Dirs)
dirList.Add(dir);
foreach (DirFiles coll in dirList[iterator].DirFileCollections)
dirList[iterator].Files = dirList[iterator].Files.Combine(coll.GetFiles(SourceBaseDir));
//clear resolved collections
dirList[iterator].FileCollections = new Files[0];
dirList[iterator].DirFileCollections = new DirFiles[0];
iterator++;
}
if (ignoreEmptyDirectories)
{
IEnumerable<Dir> getEmptyDirs() => AllDirs.Where(d => !d.Files.Any() && !d.Dirs.Any());
IEnumerable<Dir> emptyDirs;
while ((emptyDirs = getEmptyDirs()).Any())
{
emptyDirs.ForEach(emptyDir => AllDirs.ForEach(d =>
{
if (d.Dirs.Contains(emptyDir))
d.Dirs = d.Dirs.Where(x => x != emptyDir).ToArray();
}));
}
}
return this;
}
/// <summary>
/// Returns all <see cref="File"/>s of the <see cref="Project"/> matching the <paramref name="match"/> pattern.
/// </summary>
/// <param name="match">The match pattern.</param>
/// <returns>Matching <see cref="File"/>s.</returns>
public File[] FindFile(Func<File, bool> match)
{
return AllFiles.Where(match).ToArray();
}
/// <summary>
/// Finds the first <see cref="File"/> in the <see cref="Project"/> matching the <paramref name="fileName"/>.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <returns>Matching <see cref="File"/>.</returns>
public File FindFirstFile(string fileName)
{
return FindFile(f => f.Name.PathGetFileName().SameAs(fileName, ignoreCase: true)).FirstOrDefault();
}
/// <summary>
/// Flattened "view" of all <see cref="File"/>s of the <see cref="Project"/>.
/// </summary>
public File[] AllFiles
{
get
{
int iterator = 0;
var dirList = new List<Dir>();
var fileList = new List<File>();
dirList.AddRange(Dirs);
while (iterator < dirList.Count)
{
foreach (Dir dir in dirList[iterator].Dirs)
dirList.Add(dir);
fileList.AddRange(dirList[iterator].Files);
iterator++;
}
return fileList.ToArray();
}
}
/// <summary>
/// Flattened "view" of all <see cref="Dir"/>s of the <see cref="Project"/>.
/// </summary>
public Dir[] AllDirs
{
get
{
int iterator = 0;
var dirList = new List<Dir>();
dirList.AddRange(Dirs);
while (iterator < dirList.Count)
{
dirList.AddRange(dirList[iterator].Dirs);
iterator++;
}
return dirList.ToArray();
}
}
/// <summary>
/// Calculates the target path of the specified file.
/// </summary>
/// <param name="file">The file.</param>
/// <returns></returns>
public string GetTargetPathOf(File file)
{
var dir = this.AllDirs.FirstOrDefault(d => d.Files.Contains(file));
var path = new List<string>();
path.Add(file.Name);
while (dir != null)
{
path.Insert(0, dir.Name);
dir = this.AllDirs.FirstOrDefault(d => d.Dirs.Contains(dir));
}
return path.JoinBy("\\");
}
/// <summary>
/// For future use
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
private string HashedIdAlgorithm(WixEntity entity)
{
if (entity is File file)
{
var target_path = this.GetTargetPathOf(file);
var hash = target_path.GetHashCode32();
// WiX does not allow '-' char in ID. So need to use `Math.Abs`
return $"{target_path.PathGetFileName()}_{(uint)hash}";
}
return null; // next two lines produce the same result
// return WixEntity.DefaultIdAlgorithm(entity);
// return WixEntity.IncrementalIdFor(entity);
}
/// <summary>
/// Finds <see cref="T:WixSharp.Dir"/> corresponding to the specified path.
/// </summary>
/// <example>
/// <code>
/// new Project("MyProduct",
/// new Dir("%ProgramFiles%",
/// new Dir("My Company",
/// new Dir("My Product",
/// ...
/// </code>
/// In the sample above the call <c>FindDir(@"%ProgramFiles%\My Company\My Product")</c> returns the last declared <see cref="T:WixSharp.Dir"/>.
/// </example>
/// <param name="path">The path string.</param>
/// <returns><see cref="T:WixSharp.Dir"/> instance if the search was successful, otherwise return <c>null</c></returns>
public Dir FindDir(string path)
{
int iterator = 0;
var dirList = new List<Dir>();
int tokenIndex = 0;
string[] pathTokens = path.Split("\\/".ToCharArray());
dirList.AddRange(Dirs);
while (iterator < dirList.Count)
{
string dirName = dirList[iterator].Name.Expand().ToLower();
string currentSubDir = pathTokens[tokenIndex].Expand().ToLower();
if (dirName == currentSubDir)
{
if (tokenIndex == pathTokens.Length - 1)
return dirList[iterator];
dirList.AddRange(dirList[iterator].Dirs);
tokenIndex++;
}
iterator++;
}
return null;
}
/// <summary>
/// The installer version
/// </summary>
public int InstallerVersion = 200;
private string codepage = "";
/// <summary>
/// Installation UI Code Page. If not specified
/// ANSICodePage of the <see cref="T:WixSharp.WixProject.Language"/> will be used.
/// </summary>
public string Codepage
{
get
{
if (!codepage.IsEmpty())
return codepage;
else
return Encoding.GetEncoding(this.DefaultLanguage.TextInfo.ANSICodePage).WebName;
}
set
{
codepage = value;
}
}
/// <summary>
/// List of culture names (see <see cref="CultureInfo"/>) based on the specified <see cref="T:WixSharp.WixProject.Language"/>
/// </summary>
public string Culture
{
get
{
return string.Join(",", Language.Split(',', ';')
.Select(x => new CultureInfo(x.Trim()).Name)
.ToArray());
}
}
internal bool IsLocalized
{
get { return (Language.ToLower() != "en-us" && Language.ToLower() != "en") || !LocalizationFile.IsEmpty(); }
}
/// <summary>
/// Path to the Localization file.
/// </summary>
public string LocalizationFile = "";
/// <summary>
/// Name (path) of the directory which was assigned <see cref="T:WixSharp.Compiler.AutoGeneration.InstallDirDefaultId"/> ID as part of XML auto-generation (see <see cref="T:WixSharp.AutoGenerationOptions"/>).
/// </summary>
public string AutoAssignedInstallDirPath = "";
internal string ActualInstallDirId = "";
internal Dir GetLogicalInstallDir()
{
Dir firstDirWithItems = Dirs.First();
while (firstDirWithItems.Shortcuts.Count() == 0 &&
firstDirWithItems.Dirs.Count() == 1 &&
firstDirWithItems.Files.None())
{
firstDirWithItems = firstDirWithItems.Dirs.First();
}
return firstDirWithItems;
}
/// <summary>
/// Builds the MSI file from the specified <see cref="Project"/> instance.
/// </summary>
/// <param name="path">The path to the MSI file to be build.</param>
/// <returns>Path to the built MSI file.</returns>
public string BuildMsi(string path = null)
{
if (path == null)
return Compiler.BuildMsi(this);
else
return Compiler.BuildMsi(this, path);
}
/// <summary>
/// Builds the WiX source file and generates batch file capable of building
/// MSI with WiX toolset.
/// </summary>
/// <param name="path">The path to the batch file to be build.</param>
/// <returns>Path to the batch file.</returns>
public string BuildMsiCmd(string path = null)
{
if (path == null)
return Compiler.BuildMsiCmd(this);
else
return Compiler.BuildMsiCmd(this, path);
}
/// <summary>
/// Builds the WiX source file (*.wxs) from the specified <see cref="Project"/> instance.
/// </summary>
/// <param name="path">The path to the WXS file to be build.</param>
/// <param name="type">The type (<see cref="Compiler.OutputType"/>) of the setup file to be defined in the source file (MSI vs. MSM).</param>
/// <returns>Path to the built WXS file.</returns>
public string BuildWxs(Compiler.OutputType type = Compiler.OutputType.MSI, string path = null)
{
if (path == null)
return Compiler.BuildWxs(this, type);
else
return Compiler.BuildWxs(this, path, type);